Search in sources :

Example 1 with UserInfo

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) UserInfo(org.vcell.api.common.UserInfo) ClientProtocolException(org.apache.http.client.ClientProtocolException) StringEntity(org.apache.http.entity.StringEntity) BufferedReader(java.io.BufferedReader)

Aggregations

Gson (com.google.gson.Gson)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 ResponseHandler (org.apache.http.client.ResponseHandler)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 UserInfo (org.vcell.api.common.UserInfo)1