Search in sources :

Example 26 with InputStreamResponseListener

use of org.eclipse.jetty.client.util.InputStreamResponseListener in project smarthome by eclipse.

the class BlockingProxyServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    URI uri = service.uriFromRequest(request);
    if (uri == null) {
        service.sendError(request, response);
    } else {
        Request httpRequest = httpClient.newRequest(uri);
        service.maybeAppendAuthHeader(uri, httpRequest);
        InputStreamResponseListener listener = new InputStreamResponseListener();
        // do the client request
        try {
            httpRequest.send(listener);
            // wait for the response headers to arrive or the timeout to expire
            Response httpResponse = listener.get(TIMEOUT, TimeUnit.MILLISECONDS);
            // get response headers
            HttpFields headers = httpResponse.getHeaders();
            Iterator<HttpField> iterator = headers.iterator();
            // copy all headers
            while (iterator.hasNext()) {
                HttpField header = iterator.next();
                response.setHeader(header.getName(), header.getValue());
            }
        } catch (Exception e) {
            if (e instanceof TimeoutException) {
                logger.warn("Proxy servlet failed to stream content due to a timeout");
                response.sendError(HttpServletResponse.SC_GATEWAY_TIMEOUT);
            } else {
                logger.warn("Proxy servlet failed to stream content: {}", e.getMessage());
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
            }
            return;
        }
        // now copy/stream the body content
        try (InputStream responseContent = listener.getInputStream()) {
            IOUtils.copy(responseContent, response.getOutputStream());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) HttpField(org.eclipse.jetty.http.HttpField) InputStream(java.io.InputStream) HttpFields(org.eclipse.jetty.http.HttpFields) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) URI(java.net.URI) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) 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