Search in sources :

Example 41 with BytesContentProvider

use of org.eclipse.jetty.client.util.BytesContentProvider 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)

Aggregations

BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)41 IOException (java.io.IOException)32 HttpServletRequest (javax.servlet.http.HttpServletRequest)32 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)30 Test (org.junit.Test)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)28 ServletException (javax.servlet.ServletException)27 Request (org.eclipse.jetty.client.api.Request)18 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 Response (org.eclipse.jetty.client.api.Response)12 Request (org.eclipse.jetty.server.Request)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Result (org.eclipse.jetty.client.api.Result)11 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)10 InputStream (java.io.InputStream)9 ByteBuffer (java.nio.ByteBuffer)9 HttpServlet (javax.servlet.http.HttpServlet)9 InterruptedIOException (java.io.InterruptedIOException)8 Random (java.util.Random)8