Search in sources :

Example 91 with HttpContext

use of org.apache.http.protocol.HttpContext 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 92 with HttpContext

use of org.apache.http.protocol.HttpContext 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 93 with HttpContext

use of org.apache.http.protocol.HttpContext in project spark by perwendel.

the class SparkTestUtil method setFollowRedirectStrategy.

public void setFollowRedirectStrategy(Integer... codes) {
    final List<Integer> redirectCodes = Arrays.asList(codes);
    DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
            boolean isRedirect = false;
            try {
                isRedirect = super.isRedirected(request, response, context);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!isRedirect) {
                int responseCode = response.getStatusLine().getStatusCode();
                if (redirectCodes.contains(responseCode)) {
                    return true;
                }
            }
            return isRedirect;
        }
    };
    this.httpClient = httpClientBuilder().setRedirectStrategy(redirectStrategy).build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpContext(org.apache.http.protocol.HttpContext) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 94 with HttpContext

use of org.apache.http.protocol.HttpContext in project oap by oaplatform.

the class Server method accepted.

@SneakyThrows
public void accepted(final Socket socket) {
    try {
        final DefaultBHttpServerConnection connection = connectionFactory.createConnection(socket);
        final String connectionId = connection.toString();
        executor.submit(() -> {
            try {
                connections.put(connectionId, connection);
                log.debug("connection accepted: {}", connection);
                final HttpContext httpContext = createHttpContext(socket);
                Thread.currentThread().setName(connection.toString());
                log.debug("start handling {}", connection);
                while (!Thread.interrupted() && connection.isOpen()) httpService.handleRequest(connection, httpContext);
            } catch (SocketException e) {
                if (socketClosed(e))
                    log.debug("Socket closed: {}", connection);
                else if (connectionReset(e))
                    log.warn("Connection reset: {}", connection);
                else
                    log.error(e.getMessage(), e);
            } catch (ConnectionClosedException e) {
                log.debug("connection closed: {}", connection);
            } catch (Throwable e) {
                log.error(e.getMessage(), e);
            } finally {
                connections.remove(connectionId);
                Closeables.close(connection);
            }
        });
    } catch (final IOException e) {
        log.warn(e.getMessage());
        connections.values().forEach(Closeables::close);
        connections.clear();
        throw e;
    }
}
Also used : SocketException(java.net.SocketException) DefaultBHttpServerConnection(org.apache.http.impl.DefaultBHttpServerConnection) HttpContext(org.apache.http.protocol.HttpContext) ConnectionClosedException(org.apache.http.ConnectionClosedException) IOException(java.io.IOException) SneakyThrows(lombok.SneakyThrows)

Example 95 with HttpContext

use of org.apache.http.protocol.HttpContext in project oap by oaplatform.

the class Server method createHttpContext.

// TODO Fix resolution of local through headers instead of socket inet address
private static HttpContext createHttpContext(final Socket socket) {
    final HttpContext httpContext = HttpCoreContext.create();
    final Protocol protocol;
    if (!Inet.isLocalAddress(socket.getInetAddress()))
        protocol = Protocol.LOCAL;
    else
        protocol = SSLSocket.class.isInstance(socket) ? Protocol.HTTPS : Protocol.HTTP;
    httpContext.setAttribute("protocol", protocol);
    return httpContext;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) HttpContext(org.apache.http.protocol.HttpContext)

Aggregations

HttpContext (org.apache.http.protocol.HttpContext)169 HttpResponse (org.apache.http.HttpResponse)77 IOException (java.io.IOException)72 HttpRequest (org.apache.http.HttpRequest)55 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)47 HttpException (org.apache.http.HttpException)30 Test (org.junit.Test)29 HttpGet (org.apache.http.client.methods.HttpGet)27 HttpHost (org.apache.http.HttpHost)26 HttpPost (org.apache.http.client.methods.HttpPost)26 ArrayList (java.util.ArrayList)22 Header (org.apache.http.Header)21 StringEntity (org.apache.http.entity.StringEntity)21 NameValuePair (org.apache.http.NameValuePair)18 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)18 ProtocolException (org.apache.http.ProtocolException)17 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)17 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)17 URI (java.net.URI)16 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)16