Search in sources :

Example 21 with InputStreamResponseListener

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);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) Response(org.eclipse.jetty.client.api.Response) Type(java.lang.reflect.Type) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 22 with InputStreamResponseListener

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;
}
Also used : Response(org.eclipse.jetty.client.api.Response) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) InputStream(java.io.InputStream) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 23 with InputStreamResponseListener

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);
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Request(org.eclipse.jetty.client.api.Request) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) IOException(java.io.IOException)

Example 24 with InputStreamResponseListener

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;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) RequestInfo(io.airlift.http.client.jetty.HttpClientLogger.RequestInfo) ByteBuffer(java.nio.ByteBuffer) GeneralSecurityException(java.security.GeneralSecurityException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Response(org.eclipse.jetty.client.api.Response) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 25 with InputStreamResponseListener

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);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) Response(org.eclipse.jetty.client.api.Response) Type(java.lang.reflect.Type) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)26 IOException (java.io.IOException)24 Response (org.eclipse.jetty.client.api.Response)24 InputStream (java.io.InputStream)20 Test (org.junit.Test)17 ServletException (javax.servlet.ServletException)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)16 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)16 InterruptedIOException (java.io.InterruptedIOException)15 Request (org.eclipse.jetty.server.Request)13 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)13 ByteArrayInputStream (java.io.ByteArrayInputStream)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExecutionException (java.util.concurrent.ExecutionException)8 TimeoutException (java.util.concurrent.TimeoutException)7 Result (org.eclipse.jetty.client.api.Result)7 ServletOutputStream (javax.servlet.ServletOutputStream)6 Request (org.eclipse.jetty.client.api.Request)6 ByteBuffer (java.nio.ByteBuffer)5