Search in sources :

Example 6 with HttpConsumer

use of org.apache.camel.http.common.HttpConsumer in project camel by apache.

the class JettyHttpProducerSuspendResumeTest method testJettySuspendResume.

@Test
public void testJettySuspendResume() throws Exception {
    // these tests does not run well on Windows
    if (isPlatform("windows")) {
        return;
    }
    context.getShutdownStrategy().setTimeout(50);
    String reply = template.requestBody(serverUri, "World", String.class);
    assertEquals("Bye World", reply);
    // now suspend jetty
    HttpConsumer consumer = (HttpConsumer) context.getRoute("route1").getConsumer();
    assertNotNull(consumer);
    // suspend
    consumer.suspend();
    try {
        template.requestBody(serverUri, "Moon", String.class);
        fail("Should throw exception");
    } catch (Exception e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(503, cause.getStatusCode());
    }
    // resume
    consumer.resume();
    // and send request which should be processed
    reply = template.requestBody(serverUri, "Moon", String.class);
    assertEquals("Bye Moon", reply);
}
Also used : HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 7 with HttpConsumer

use of org.apache.camel.http.common.HttpConsumer in project camel by apache.

the class JettyHttpProducerSuspendTest method testJettySuspend.

@Test
public void testJettySuspend() throws Exception {
    // these tests does not run well on Windows
    if (isPlatform("windows")) {
        return;
    }
    context.getShutdownStrategy().setTimeout(50);
    String reply = template.requestBody(serverUri, "World", String.class);
    assertEquals("Bye World", reply);
    // now suspend jetty
    HttpConsumer consumer = (HttpConsumer) context.getRoute("route1").getConsumer();
    assertNotNull(consumer);
    // suspend
    consumer.suspend();
    try {
        template.requestBody(serverUri, "Moon", String.class);
        fail("Should throw exception");
    } catch (Exception e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(503, cause.getStatusCode());
    }
}
Also used : HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 8 with HttpConsumer

use of org.apache.camel.http.common.HttpConsumer in project camel by apache.

the class CamelWebSocketServlet method doService.

@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.trace("Service: {}", request);
    // Is there a consumer registered for the request.
    HttpConsumer consumer = getServletResolveConsumerStrategy().resolve(request, getConsumers());
    if (consumer == null) {
        log.debug("No consumer to service request {}", request);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    // are we suspended?
    if (consumer.isSuspended()) {
        log.debug("Consumer suspended, cannot service request {}", request);
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return;
    }
    if (consumer.getEndpoint().getHttpMethodRestrict() != null && !consumer.getEndpoint().getHttpMethodRestrict().equals(request.getMethod())) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    if ("TRACE".equals(request.getMethod()) && !consumer.isTraceEnabled()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    if (!(consumer instanceof WebsocketConsumer)) {
        log.debug("Consumer not supporting websocket {}", request);
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return;
    }
    log.debug("Dispatching to Websocket Consumer at {}", consumer.getPath());
    ((WebsocketConsumer) consumer).service(request, response, enableEventsResending);
}
Also used : HttpConsumer(org.apache.camel.http.common.HttpConsumer)

Example 9 with HttpConsumer

use of org.apache.camel.http.common.HttpConsumer in project ddf by codice.

the class HttpProxyCamelHttpTransportServlet method resolve.

@Override
protected HttpConsumer resolve(HttpServletRequest request) {
    String path = request.getPathInfo();
    log.trace("Request path is: {}", path);
    String endpointName = getEndpointNameFromPath(path);
    log.trace("Looking up consumer for endpoint: {}", endpointName);
    HttpConsumer answer = consumers.get(endpointName);
    if (answer == null) {
        log.debug("Consumer Keys: {}", Arrays.toString(consumers.keySet().toArray()));
        for (Entry<String, HttpConsumer> entry : consumers.entrySet()) {
            if (entry.getValue().getEndpoint().isMatchOnUriPrefix() && path.startsWith(entry.getKey())) {
                answer = consumers.get(entry.getKey());
                break;
            }
        }
    }
    return answer;
}
Also used : HttpConsumer(org.apache.camel.http.common.HttpConsumer)

Example 10 with HttpConsumer

use of org.apache.camel.http.common.HttpConsumer in project ddf by codice.

the class HttpProxyCamelHttpTransportServlet method doService.

@Override
protected void doService(HttpServletRequest oldRequest, HttpServletResponse response) throws ServletException, IOException {
    //Wrap request and clean the query String
    HttpProxyWrappedCleanRequest request = new HttpProxyWrappedCleanRequest(oldRequest);
    log.trace("Service: {}", request);
    // Is there a consumer registered for the request.
    HttpConsumer consumer = resolve(request);
    if (consumer == null) {
        String path = request.getPathInfo();
        log.trace("Service Request Path = {}", path);
        String endpointName = getEndpointNameFromPath(path);
        log.trace("Endpoint Name = {}", endpointName);
        Route route = camelContext.getRoute(endpointName);
        try {
            if (route != null) {
                connect((HttpConsumer) route.getConsumer());
            }
        } catch (Exception e) {
            log.debug("Exception while creating consumer", e);
        }
        consumer = resolve(request);
    }
    if (consumer == null) {
        log.debug("No consumer to service request {}", request);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    // are we suspended?
    if (consumer.getEndpoint().isSuspended()) {
        log.debug("Consumer suspended, cannot service request {}", request);
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return;
    }
    if (consumer.getEndpoint().getHttpMethodRestrict() != null && !consumer.getEndpoint().getHttpMethodRestrict().equals(request.getMethod())) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    if ("TRACE".equals(request.getMethod()) && !consumer.getEndpoint().isTraceEnabled()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    // create exchange and set data on it
    Exchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
    if (consumer.getEndpoint().isBridgeEndpoint()) {
        exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
    }
    if (consumer.getEndpoint().isDisableStreamCache()) {
        exchange.setProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.TRUE);
    }
    // we override the classloader before building the HttpMessage just in case the binding
    // does some class resolution
    ClassLoader oldTccl = overrideTccl(exchange);
    HttpHelper.setCharsetFromContentType(request.getContentType(), exchange);
    exchange.setIn(new HttpMessage(exchange, request, response));
    // set context path as header
    String contextPath = consumer.getEndpoint().getPath();
    exchange.getIn().setHeader("CamelServletContextPath", contextPath);
    String httpPath = (String) exchange.getIn().getHeader(Exchange.HTTP_PATH);
    // here we just remove the CamelServletContextPath part from the HTTP_PATH
    if (contextPath != null && httpPath.startsWith(contextPath)) {
        exchange.getIn().setHeader(Exchange.HTTP_PATH, httpPath.substring(contextPath.length()));
    }
    // we want to handle the UoW
    try {
        consumer.createUoW(exchange);
    } catch (Exception e) {
        log.debug("Error processing request", e);
        throw new ServletException(e);
    }
    try {
        if (log.isTraceEnabled()) {
            log.trace("Processing request for exchangeId: {}", exchange.getExchangeId());
        }
        // process the exchange
        consumer.getProcessor().process(exchange);
    } catch (Exception e) {
        exchange.setException(e);
    }
    try {
        // now lets output to the response
        if (log.isTraceEnabled()) {
            log.trace("Writing response for exchangeId: {}", exchange.getExchangeId());
        }
        Integer bs = consumer.getEndpoint().getResponseBufferSize();
        if (bs != null) {
            log.trace("Using response buffer size: {}", bs);
            response.setBufferSize(bs);
        }
        consumer.getBinding().writeResponse(exchange, response);
    } catch (IOException e) {
        log.debug("Error processing request", e);
        throw e;
    } catch (Exception e) {
        log.debug("Error processing request", e);
        throw new ServletException(e);
    } finally {
        consumer.doneUoW(exchange);
        restoreTccl(exchange, oldTccl);
    }
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpMessage(org.apache.camel.http.common.HttpMessage) Route(org.apache.camel.Route) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

HttpConsumer (org.apache.camel.http.common.HttpConsumer)10 Test (org.junit.Test)5 HttpOperationFailedException (org.apache.camel.http.common.HttpOperationFailedException)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 Exchange (org.apache.camel.Exchange)2 BaseJettyTest (org.apache.camel.component.jetty.BaseJettyTest)2 HttpMessage (org.apache.camel.http.common.HttpMessage)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 TimeoutException (java.util.concurrent.TimeoutException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AsyncCallback (org.apache.camel.AsyncCallback)1 Route (org.apache.camel.Route)1 HttpCommonEndpoint (org.apache.camel.http.common.HttpCommonEndpoint)1 Continuation (org.eclipse.jetty.continuation.Continuation)1