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;
}
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;
}
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;
}
Aggregations