use of org.eclipse.jetty.client.util.InputStreamResponseListener in project zeppelin by apache.
the class ZeppelinhubRestApiHandler method getInstances.
/**
* Fetch zeppelin instances for a given user.
* @param ticket
* @return
* @throws IOException
*/
public List<Instance> getInstances(String ticket) throws IOException {
InputStreamResponseListener listener = new InputStreamResponseListener();
Response response;
String url = zepelinhubUrl + "instances";
String data;
Request request = client.newRequest(url).header(USER_SESSION_HEADER, ticket);
request.send(listener);
try {
response = listener.get(30, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
LOG.error("Cannot perform request to ZeppelinHub", e);
throw new IOException("Cannot perform GET request to ZeppelinHub", e);
}
int code = response.getStatus();
if (code == 200) {
try (InputStream responseContent = listener.getInputStream()) {
data = IOUtils.toString(responseContent, "UTF-8");
}
} else {
LOG.error("ZeppelinHub GET {} returned with status {} ", url, code);
throw new IOException("Cannot perform GET request to ZeppelinHub");
}
Type listType = new TypeToken<ArrayList<Instance>>() {
}.getType();
return new Gson().fromJson(data, listType);
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project zeppelin by apache.
the class ZeppelinhubRestApiHandler method sendToZeppelinHub.
private String sendToZeppelinHub(final Request request) throws IOException {
InputStreamResponseListener listener = new InputStreamResponseListener();
Response response;
String data;
request.send(listener);
try {
response = listener.get(30, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
String method = request.getMethod();
LOG.error("Cannot perform {} request to ZeppelinHub", method, e);
throw new IOException("Cannot perform " + method + " request to ZeppelinHub", e);
}
int code = response.getStatus();
if (code == 200) {
try (InputStream responseContent = listener.getInputStream()) {
data = IOUtils.toString(responseContent, "UTF-8");
}
} else {
String method = response.getRequest().getMethod();
String url = response.getRequest().getURI().toString();
LOG.error("ZeppelinHub {} {} returned with status {} ", method, url, code);
throw new IOException("Cannot perform " + method + " request to ZeppelinHub");
}
return data;
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project cayenne by apache.
the class JettyHttpROPConnector method sendMessage.
@Override
public InputStream sendMessage(byte[] message) throws IOException {
try {
Request request = httpClient.newRequest(url).method(HttpMethod.POST).header(HttpHeader.CONTENT_TYPE, "application/octet-stream").header(HttpHeader.ACCEPT_ENCODING, "gzip").content(new BytesContentProvider(message));
addSessionCookie(request);
InputStreamResponseListener listener = new InputStreamResponseListener();
request.send(listener);
/**
* Waits for the given timeout for the response to be available, then returns it.
* The wait ends as soon as all the HTTP headers have been received, without waiting for the content.
*/
Response response = listener.get(readTimeout, TimeUnit.SECONDS);
if (response.getStatus() >= 300) {
throw new IOException("Did not receive successful HTTP response: status code = " + response.getStatus() + ", status message = [" + response.getReason() + "]");
}
return listener.getInputStream();
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new IOException("Exception while sending message", e);
}
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project airlift by airlift.
the class JettyHttpClient method execute.
@Override
public <T, E extends Exception> T execute(Request request, ResponseHandler<T, E> responseHandler) throws E {
long requestStart = System.nanoTime();
// apply filters
request = applyRequestFilters(request);
// create jetty request and response listener
JettyRequestListener requestListener = new JettyRequestListener(request.getUri());
HttpRequest jettyRequest = buildJettyRequest(request, requestListener);
InputStreamResponseListener listener = new InputStreamResponseListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
// ignore empty blocks
if (content.remaining() == 0) {
return;
}
super.onContent(response, content);
}
};
long requestTimestamp = System.currentTimeMillis();
RequestInfo requestInfo = RequestInfo.from(jettyRequest, requestTimestamp);
if (logEnabled) {
addLoggingListener(jettyRequest, requestTimestamp);
}
// fire the request
jettyRequest.send(listener);
// wait for response to begin
Response response;
try {
response = listener.get(httpClient.getIdleTimeout(), MILLISECONDS);
} catch (InterruptedException e) {
stats.recordRequestFailed();
requestLogger.log(requestInfo, ResponseInfo.failed(Optional.empty(), Optional.of(e)));
jettyRequest.abort(e);
Thread.currentThread().interrupt();
return responseHandler.handleException(request, e);
} catch (TimeoutException e) {
stats.recordRequestFailed();
requestLogger.log(requestInfo, ResponseInfo.failed(Optional.empty(), Optional.of(e)));
jettyRequest.abort(e);
return responseHandler.handleException(request, e);
} catch (ExecutionException e) {
stats.recordRequestFailed();
requestLogger.log(requestInfo, ResponseInfo.failed(Optional.empty(), Optional.of(e)));
Throwable cause = e.getCause();
if (cause instanceof Exception) {
return responseHandler.handleException(request, (Exception) cause);
} else if ((cause instanceof NoClassDefFoundError) && cause.getMessage().endsWith("ALPNClientConnection")) {
return responseHandler.handleException(request, new RuntimeException("HTTPS cannot be used when HTTP/2 is enabled", cause));
} else {
return responseHandler.handleException(request, new RuntimeException(cause));
}
}
// process response
long responseStart = System.nanoTime();
JettyResponse jettyResponse = null;
T value;
try {
jettyResponse = new JettyResponse(response, listener.getInputStream());
value = responseHandler.handle(request, jettyResponse);
} finally {
if (jettyResponse != null) {
try {
jettyResponse.getInputStream().close();
} catch (IOException ignored) {
// ignore errors closing the stream
}
}
if (recordRequestComplete) {
recordRequestComplete(stats, request, requestStart, jettyResponse, responseStart);
}
}
return value;
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project SSM by Intel-bigdata.
the class ZeppelinhubRestApiHandler method getInstances.
/**
* Fetch zeppelin instances for a given user.
* @param ticket
* @return
* @throws IOException
*/
public List<Instance> getInstances(String ticket) throws IOException {
InputStreamResponseListener listener = new InputStreamResponseListener();
Response response;
String url = zepelinhubUrl + "instances";
String data;
Request request = client.newRequest(url).header(USER_SESSION_HEADER, ticket);
request.send(listener);
try {
response = listener.get(30, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
LOG.error("Cannot perform request to ZeppelinHub", e);
throw new IOException("Cannot perform GET request to ZeppelinHub", e);
}
int code = response.getStatus();
if (code == 200) {
try (InputStream responseContent = listener.getInputStream()) {
data = IOUtils.toString(responseContent, "UTF-8");
}
} else {
LOG.error("ZeppelinHub GET {} returned with status {} ", url, code);
throw new IOException("Cannot perform GET request to ZeppelinHub");
}
Type listType = new TypeToken<ArrayList<Instance>>() {
}.getType();
return new Gson().fromJson(data, listType);
}
Aggregations