Search in sources :

Example 11 with ResponseHandler

use of org.apache.http.client.ResponseHandler in project vcell by virtualcell.

the class VCellApiClient method sendRpcMessage.

public Serializable sendRpcMessage(RpcDestination rpcDestination, VCellApiRpcRequest rpcRequest, boolean returnRequired, int timeoutMS, String[] specialProperties, Object[] specialValues) throws ClientProtocolException, IOException {
    HttpPost httppost = new HttpPost("https://" + httpHost.getHostName() + ":" + httpHost.getPort() + "/rpc");
    VCellApiRpcBody vcellapiRpcBody = new VCellApiRpcBody(rpcDestination, rpcRequest, returnRequired, timeoutMS, specialProperties, specialValues);
    byte[] compressedSerializedRpcBody = null;
    try {
        compressedSerializedRpcBody = toCompressedSerialized(vcellapiRpcBody);
    } catch (IOException e2) {
        e2.printStackTrace();
        throw new RuntimeException("vcellapi rpc failure serializing request body, method=" + rpcRequest.methodName + ": " + e2.getMessage(), e2);
    }
    ByteArrayEntity input = new ByteArrayEntity(compressedSerializedRpcBody);
    input.setContentType(ContentType.APPLICATION_OCTET_STREAM.getMimeType());
    httppost.setEntity(input);
    httppost.addHeader("username", rpcRequest.username);
    httppost.addHeader("destination", rpcRequest.rpcDestination.name());
    httppost.addHeader("method", rpcRequest.methodName);
    httppost.addHeader("returnRequired", Boolean.toString(returnRequired));
    httppost.addHeader("timeoutMS", Integer.toString(timeoutMS));
    httppost.addHeader("compressed", "zip");
    httppost.addHeader("class", VCellApiRpcBody.class.getCanonicalName());
    if (specialProperties != null) {
        httppost.addHeader("specialProperties", Arrays.asList(specialProperties).toString());
    }
    if (lg.isLoggable(Level.INFO)) {
        lg.info("Executing request to submit rpc call " + httppost.getRequestLine());
    }
    ResponseHandler<Serializable> handler = new ResponseHandler<Serializable>() {

        public Serializable handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
            int status = response.getStatusLine().getStatusCode();
            if (lg.isLoggable(Level.INFO)) {
                lg.info("in rpc response handler, status=" + status);
            }
            if (status == 200) {
                HttpEntity entity = response.getEntity();
                try {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    entity.writeTo(byteArrayOutputStream);
                    byte[] returnValueBytes = byteArrayOutputStream.toByteArray();
                    Serializable returnValue = fromCompressedSerialized(returnValueBytes);
                    if (returnRequired) {
                        if (returnValue instanceof Exception) {
                            Exception e = (Exception) returnValue;
                            e.printStackTrace();
                            lg.severe("vcellapi rpc failure, method=" + rpcRequest.methodName + ": " + e.getMessage());
                            throw new ClientProtocolException("vcellapi rpc failure, method=" + rpcRequest.methodName + ": " + e.getMessage(), e);
                        } else {
                            if (lg.isLoggable(Level.INFO)) {
                                lg.info("returning normally from rpc response handler (" + toStringTruncated(returnValue) + ")");
                            }
                            return returnValue;
                        }
                    } else {
                        if (lg.isLoggable(Level.INFO)) {
                            lg.info("returning null from rpc response handler (returnRequired==false)");
                        }
                        return null;
                    }
                } catch (ClassNotFoundException | IllegalStateException e1) {
                    e1.printStackTrace();
                    lg.severe("vcellapi rpc failure deserializing return value, method=" + rpcRequest.methodName + ": " + e1.getMessage());
                    throw new RuntimeException("vcellapi rpc failure deserializing return value, method=" + rpcRequest.methodName + ": " + e1.getMessage(), e1);
                }
            } else {
                HttpEntity entity = response.getEntity();
                String message = null;
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()))) {
                    message = reader.lines().collect(Collectors.joining());
                }
                lg.severe("RPC method " + rpcDestination + ":" + rpcRequest.methodName + "() failed: response status: " + status + "\nreason: " + message);
                throw new ClientProtocolException("RPC method " + rpcDestination + ":" + rpcRequest.methodName + "() failed: response status: " + status + "\nreason: " + message);
            }
        }
    };
    Serializable returnedValue = httpclient.execute(httppost, handler, httpClientContext);
    if (lg.isLoggable(Level.INFO)) {
        lg.info("returned from vcellapi rpc method=" + rpcDestination + ":" + rpcRequest.methodName + "()");
    }
    return returnedValue;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Serializable(java.io.Serializable) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProtocolException(org.apache.http.ProtocolException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BufferedReader(java.io.BufferedReader)

Example 12 with ResponseHandler

use of org.apache.http.client.ResponseHandler in project vcell by virtualcell.

the class VCellApiClient method submitOptimization.

public String submitOptimization(String optProblemJson) throws IOException, URISyntaxException {
    HttpPost httppost = new HttpPost("https://" + httpHost.getHostName() + ":" + httpHost.getPort() + "/optimization");
    StringEntity input = new StringEntity(optProblemJson);
    input.setContentType("application/json");
    httppost.setEntity(input);
    if (lg.isLoggable(Level.INFO)) {
        lg.info("Executing request to submit optProblem " + httppost.getRequestLine());
    }
    ResponseHandler<String> handler = new ResponseHandler<String>() {

        public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
            int status = response.getStatusLine().getStatusCode();
            if (status == 202) {
                HttpEntity entity = response.getEntity();
                if (lg.isLoggable(Level.INFO)) {
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()))) {
                        lg.info("optimizationId = " + reader.readLine());
                    }
                }
                final Header locationHeader = response.getFirstHeader("location");
                if (locationHeader == null) {
                    // got a redirect response, but no location header
                    throw new ClientProtocolException("Received redirect response " + response.getStatusLine() + " but no location header");
                }
                final String location = locationHeader.getValue();
                URI uri = createLocationURI(location);
                return uri.toString();
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        }
    };
    String responseUri = httpclient.execute(httppost, handler, httpClientContext);
    if (lg.isLoggable(Level.INFO)) {
        lg.info("returned: " + toStringTruncated(responseUri));
    }
    String optimizationId = responseUri.substring(responseUri.lastIndexOf('/') + 1);
    return optimizationId;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) BufferedReader(java.io.BufferedReader)

Example 13 with ResponseHandler

use of org.apache.http.client.ResponseHandler 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

HttpEntity (org.apache.http.HttpEntity)13 ResponseHandler (org.apache.http.client.ResponseHandler)13 HttpResponse (org.apache.http.HttpResponse)10 HttpPost (org.apache.http.client.methods.HttpPost)8 StringEntity (org.apache.http.entity.StringEntity)7 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 HttpGet (org.apache.http.client.methods.HttpGet)4 IOException (java.io.IOException)3 URI (java.net.URI)3 SSLContext (javax.net.ssl.SSLContext)3 Header (org.apache.http.Header)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Serializable (java.io.Serializable)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 HttpHost (org.apache.http.HttpHost)2 HttpRequest (org.apache.http.HttpRequest)2 HttpClient (org.apache.http.client.HttpClient)2