Search in sources :

Example 21 with HttpException

use of org.apache.http.HttpException in project camel by apache.

the class HttpProducerTwoHeadersWithSameKeyTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor()).setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory()).setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext()).registerHandler("/myapp", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            Header[] from = request.getHeaders("from");
            assertEquals("me", from[0].getValue());
            Header[] to = request.getHeaders("to");
            assertEquals("[foo, bar]", to[0].getValue());
            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    }).registerHandler("/myapp", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            Header[] from = request.getHeaders("from");
            assertEquals("me", from[0].getValue());
            Header[] to = request.getHeaders("to");
            assertEquals("[foo, bar]", to[0].getValue());
            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    }).create();
    localServer.start();
    super.setUp();
}
Also used : HttpRequest(org.apache.http.HttpRequest) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) HttpRequestHandler(org.apache.http.protocol.HttpRequestHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Before(org.junit.Before)

Example 22 with HttpException

use of org.apache.http.HttpException in project camel by apache.

the class HttpProducerTwoParametersWithSameKeyTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor()).setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory()).setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext()).registerHandler("/myapp", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            String uri = request.getRequestLine().getUri();
            assertEquals("/myapp?from=me&to=foo&to=bar", uri);
            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    }).create();
    localServer.start();
    super.setUp();
}
Also used : HttpRequest(org.apache.http.HttpRequest) StringEntity(org.apache.http.entity.StringEntity) HttpRequestHandler(org.apache.http.protocol.HttpRequestHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Before(org.junit.Before)

Example 23 with HttpException

use of org.apache.http.HttpException in project camel by apache.

the class HttpProducerContentTypeTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor()).setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory()).setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext()).registerHandler("/content", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            String contentType = request.getFirstHeader(Exchange.CONTENT_TYPE).getValue();
            assertEquals(CONTENT_TYPE, contentType);
            response.setEntity(new StringEntity(contentType, "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    }).create();
    localServer.start();
    super.setUp();
}
Also used : HttpRequest(org.apache.http.HttpRequest) StringEntity(org.apache.http.entity.StringEntity) HttpRequestHandler(org.apache.http.protocol.HttpRequestHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Before(org.junit.Before)

Example 24 with HttpException

use of org.apache.http.HttpException in project camel by apache.

the class HttpNoCamelHeaderTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor()).setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory()).setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext()).registerHandler("/hello", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            response.setStatusCode(HttpStatus.SC_OK);
            Object header = request.getFirstHeader(Exchange.FILE_NAME);
            assertNull("There should be no Camel header", header);
            for (Header h : request.getAllHeaders()) {
                if (h.getName().startsWith("Camel") || h.getName().startsWith("org.apache.camel")) {
                    assertNull("There should be no Camel header", h);
                }
            }
            // set ar regular and Camel header
            response.setHeader("MyApp", "dude");
            response.setHeader(Exchange.TO_ENDPOINT, "foo");
        }
    }).create();
    localServer.start();
    super.setUp();
}
Also used : HttpRequest(org.apache.http.HttpRequest) Header(org.apache.http.Header) HttpRequestHandler(org.apache.http.protocol.HttpRequestHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Before(org.junit.Before)

Example 25 with HttpException

use of org.apache.http.HttpException in project android_frameworks_base by ParanoidAndroid.

the class Connection method processRequests.

/**
     * Process requests in queue
     * pipelines requests
     */
void processRequests(Request firstRequest) {
    Request req = null;
    boolean empty;
    int error = EventHandler.OK;
    Exception exception = null;
    LinkedList<Request> pipe = new LinkedList<Request>();
    int minPipe = MIN_PIPE, maxPipe = MAX_PIPE;
    int state = SEND;
    while (state != DONE) {
        if (HttpLog.LOGV)
            HttpLog.v(states[state] + " pipe " + pipe.size());
        /* If a request was cancelled, give other cancel requests
               some time to go through so we don't uselessly restart
               connections */
        if (mActive == STATE_CANCEL_REQUESTED) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException x) {
            /* ignore */
            }
            mActive = STATE_NORMAL;
        }
        switch(state) {
            case SEND:
                {
                    if (pipe.size() == maxPipe) {
                        state = READ;
                        break;
                    }
                    /* get a request */
                    if (firstRequest == null) {
                        req = mRequestFeeder.getRequest(mHost);
                    } else {
                        req = firstRequest;
                        firstRequest = null;
                    }
                    if (req == null) {
                        state = DRAIN;
                        break;
                    }
                    req.setConnection(this);
                    /* Don't work on cancelled requests. */
                    if (req.mCancelled) {
                        if (HttpLog.LOGV)
                            HttpLog.v("processRequests(): skipping cancelled request " + req);
                        req.complete();
                        break;
                    }
                    if (mHttpClientConnection == null || !mHttpClientConnection.isOpen()) {
                        if (!openHttpConnection(req)) {
                            state = DONE;
                            break;
                        }
                    }
                    /* we have a connection, let the event handler
                     * know of any associated certificate,
                     * potentially none.
                     */
                    req.mEventHandler.certificate(mCertificate);
                    try {
                        /* FIXME: don't increment failure count if old
                           connection?  There should not be a penalty for
                           attempting to reuse an old connection */
                        req.sendRequest(mHttpClientConnection);
                    } catch (HttpException e) {
                        exception = e;
                        error = EventHandler.ERROR;
                    } catch (IOException e) {
                        exception = e;
                        error = EventHandler.ERROR_IO;
                    } catch (IllegalStateException e) {
                        exception = e;
                        error = EventHandler.ERROR_IO;
                    }
                    if (exception != null) {
                        if (httpFailure(req, error, exception) && !req.mCancelled) {
                            /* retry request if not permanent failure
                               or cancelled */
                            pipe.addLast(req);
                        }
                        exception = null;
                        state = clearPipe(pipe) ? DONE : SEND;
                        minPipe = maxPipe = 1;
                        break;
                    }
                    pipe.addLast(req);
                    if (!mCanPersist)
                        state = READ;
                    break;
                }
            case DRAIN:
            case READ:
                {
                    empty = !mRequestFeeder.haveRequest(mHost);
                    int pipeSize = pipe.size();
                    if (state != DRAIN && pipeSize < minPipe && !empty && mCanPersist) {
                        state = SEND;
                        break;
                    } else if (pipeSize == 0) {
                        /* Done if no other work to do */
                        state = empty ? DONE : SEND;
                        break;
                    }
                    req = (Request) pipe.removeFirst();
                    if (HttpLog.LOGV)
                        HttpLog.v("processRequests() reading " + req);
                    try {
                        req.readResponse(mHttpClientConnection);
                    } catch (ParseException e) {
                        exception = e;
                        error = EventHandler.ERROR_IO;
                    } catch (IOException e) {
                        exception = e;
                        error = EventHandler.ERROR_IO;
                    } catch (IllegalStateException e) {
                        exception = e;
                        error = EventHandler.ERROR_IO;
                    }
                    if (exception != null) {
                        if (httpFailure(req, error, exception) && !req.mCancelled) {
                            /* retry request if not permanent failure
                               or cancelled */
                            req.reset();
                            pipe.addFirst(req);
                        }
                        exception = null;
                        mCanPersist = false;
                    }
                    if (!mCanPersist) {
                        if (HttpLog.LOGV)
                            HttpLog.v("processRequests(): no persist, closing " + mHost);
                        closeConnection();
                        mHttpContext.removeAttribute(HTTP_CONNECTION);
                        clearPipe(pipe);
                        minPipe = maxPipe = 1;
                        state = SEND;
                    }
                    break;
                }
        }
    }
}
Also used : HttpException(org.apache.http.HttpException) IOException(java.io.IOException) ParseException(org.apache.http.ParseException) ParseException(org.apache.http.ParseException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) HttpException(org.apache.http.HttpException) LinkedList(java.util.LinkedList)

Aggregations

HttpException (org.apache.http.HttpException)37 IOException (java.io.IOException)22 HttpRequest (org.apache.http.HttpRequest)22 HttpResponse (org.apache.http.HttpResponse)21 HttpContext (org.apache.http.protocol.HttpContext)13 HttpEntity (org.apache.http.HttpEntity)12 HttpHost (org.apache.http.HttpHost)12 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)11 Header (org.apache.http.Header)10 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)8 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)8 HttpRequestHandler (org.apache.http.protocol.HttpRequestHandler)7 ParseException (org.apache.http.ParseException)6 ProtocolVersion (org.apache.http.ProtocolVersion)6 CredentialsProvider (org.apache.http.client.CredentialsProvider)6 StringEntity (org.apache.http.entity.StringEntity)6 AuthenticationException (org.apache.http.auth.AuthenticationException)5 Before (org.junit.Before)5 InterruptedIOException (java.io.InterruptedIOException)4 Socket (java.net.Socket)4