Search in sources :

Example 16 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTP2 method onData.

@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null) {
        callback.failed(new IOException("terminated"));
        return;
    }
    // We must copy the data since we do not know when the
    // application will consume the bytes and the parsing
    // will continue as soon as this method returns, eventually
    // leading to reusing the underlying buffer for more reads.
    ByteBufferPool byteBufferPool = getHttpDestination().getHttpClient().getByteBufferPool();
    ByteBuffer original = frame.getData();
    int length = original.remaining();
    final ByteBuffer copy = byteBufferPool.acquire(length, original.isDirect());
    BufferUtil.clearToFill(copy);
    copy.put(original);
    BufferUtil.flipToFlush(copy, 0);
    contentNotifier.offer(new DataInfo(exchange, copy, callback, frame.isEndStream()));
    contentNotifier.iterate();
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpExchange(org.eclipse.jetty.client.HttpExchange) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 17 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTP2 method onPush.

@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return null;
    HttpRequest request = exchange.getRequest();
    MetaData.Request metaData = (MetaData.Request) frame.getMetaData();
    HttpRequest pushRequest = (HttpRequest) getHttpDestination().getHttpClient().newRequest(metaData.getURIString());
    BiFunction<Request, Request, Response.CompleteListener> pushListener = request.getPushListener();
    if (pushListener != null) {
        Response.CompleteListener listener = pushListener.apply(request, pushRequest);
        if (listener != null) {
            HttpChannelOverHTTP2 pushChannel = getHttpChannel().getHttpConnection().newHttpChannel(true);
            List<Response.ResponseListener> listeners = Collections.singletonList(listener);
            HttpExchange pushExchange = new HttpExchange(getHttpDestination(), pushRequest, listeners);
            pushChannel.associate(pushExchange);
            pushChannel.setStream(stream);
            // TODO: idle timeout ?
            pushExchange.requestComplete(null);
            pushExchange.terminateRequest();
            return pushChannel.getStreamListener();
        }
    }
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
    return null;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpExchange(org.eclipse.jetty.client.HttpExchange) Request(org.eclipse.jetty.client.api.Request) HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame)

Example 18 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project openhab1-addons by openhab.

the class FritzahaWebInterface method asyncPost.

/**
     * Sends an HTTP POST request using the asynchronous client
     * 
     * @param Path
     *            Path of the requested resource
     * @param Args
     *            Arguments for the request
     * @param Callback
     *            Callback to handle the response with
     */
public HttpExchange asyncPost(String path, String args, FritzahaCallback callback) {
    if (!isAuthenticated()) {
        authenticate();
    }
    HttpExchange postExchange = new FritzahaContentExchange(callback);
    postExchange.setMethod("POST");
    postExchange.setURL(getURL(path));
    try {
        postExchange.setRequestContent(new ByteArrayBuffer(addSID(args).getBytes("UTF-8")));
    } catch (UnsupportedEncodingException e1) {
        logger.error("An encoding error occurred in the POST arguments");
        return null;
    }
    postExchange.setRequestContentType("application/x-www-form-urlencoded;charset=utf-8");
    try {
        asyncclient.send(postExchange);
    } catch (IOException e) {
        logger.error("An I/O error occurred while sending the POST request to " + getURL(path));
        return null;
    }
    return postExchange;
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 19 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTP method parsedHeader.

@Override
public void parsedHeader(HttpField field) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return;
    responseHeader(exchange, field);
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange)

Example 20 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTP method earlyEOF.

@Override
public void earlyEOF() {
    HttpExchange exchange = getHttpExchange();
    HttpConnectionOverHTTP connection = getHttpConnection();
    if (exchange == null)
        connection.close();
    else
        failAndClose(new EOFException(String.valueOf(connection)));
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) EOFException(java.io.EOFException)

Aggregations

HttpExchange (org.eclipse.jetty.client.HttpExchange)24 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)7 Response (org.eclipse.jetty.client.api.Response)6 Test (org.junit.Test)6 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)3 HttpResponse (org.eclipse.jetty.client.HttpResponse)3 HttpFields (org.eclipse.jetty.http.HttpFields)3 EOFException (java.io.EOFException)2 HttpRequest (org.eclipse.jetty.client.HttpRequest)2 HttpResponseException (org.eclipse.jetty.client.HttpResponseException)2 MetaData (org.eclipse.jetty.http.MetaData)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ByteBuffer (java.nio.ByteBuffer)1 TimeoutException (java.util.concurrent.TimeoutException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1