use of org.vcell.api.common.UserInfo in project vcell by virtualcell.
the class VCellApiClient method insertUserInfo.
public UserInfo insertUserInfo(UserInfo newUserInfo) throws ClientProtocolException, IOException {
HttpPost httppost = new HttpPost("https://" + httpHost.getHostName() + ":" + httpHost.getPort() + "/newuser");
Gson gson = new Gson();
String newUserInfoJSON = gson.toJson(newUserInfo);
StringEntity input = new StringEntity(newUserInfoJSON);
input.setContentType(ContentType.APPLICATION_JSON.getMimeType());
httppost.setEntity(input);
if (lg.isLoggable(Level.INFO)) {
lg.info("Executing request to submit new user " + httppost.getRequestLine());
}
ResponseHandler<UserInfo> handler = new ResponseHandler<UserInfo>() {
public UserInfo handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_CREATED) {
HttpEntity entity = response.getEntity();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()))) {
String json = reader.lines().collect(Collectors.joining());
UserInfo userInfo = gson.fromJson(json, UserInfo.class);
return userInfo;
}
} else {
HttpEntity entity = response.getEntity();
String message = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()))) {
message = reader.lines().collect(Collectors.joining());
}
throw new ClientProtocolException("Unexpected response status: " + status + "\nreason: " + message);
}
}
};
UserInfo insertedUserInfo = httpclient.execute(httppost, handler, httpClientContext);
if (lg.isLoggable(Level.INFO)) {
lg.info("returned userinfo: " + insertedUserInfo);
}
return insertedUserInfo;
}
Aggregations