use of org.apache.camel.http.common.HttpConsumer in project camel by apache.
the class JettyHttpEndpoint method createConsumer.
@Override
public Consumer createConsumer(Processor processor) throws Exception {
HttpConsumer answer = new HttpConsumer(this, processor);
configureConsumer(answer);
return answer;
}
use of org.apache.camel.http.common.HttpConsumer in project camel by apache.
the class CamelContinuationServlet method doService.
@Override
protected void doService(final HttpServletRequest request, final 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) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// figure out if continuation is enabled and what timeout to use
boolean useContinuation = false;
Long continuationTimeout = null;
HttpCommonEndpoint endpoint = consumer.getEndpoint();
if (endpoint instanceof JettyHttpEndpoint) {
JettyHttpEndpoint jettyEndpoint = (JettyHttpEndpoint) endpoint;
Boolean epUseContinuation = jettyEndpoint.getUseContinuation();
Long epContinuationTimeout = jettyEndpoint.getContinuationTimeout();
if (epUseContinuation != null) {
useContinuation = epUseContinuation;
} else {
useContinuation = jettyEndpoint.getComponent().isUseContinuation();
}
if (epContinuationTimeout != null) {
continuationTimeout = epContinuationTimeout;
} else {
continuationTimeout = jettyEndpoint.getComponent().getContinuationTimeout();
}
}
if (useContinuation) {
log.trace("Start request with continuation timeout of {}", continuationTimeout != null ? continuationTimeout : "jetty default");
} else {
log.trace("Usage of continuation is disabled, either by component or endpoint configuration, fallback to normal servlet processing instead");
super.doService(request, response);
return;
}
if (consumer.getEndpoint().getHttpMethodRestrict() != null) {
Iterator<?> it = ObjectHelper.createIterable(consumer.getEndpoint().getHttpMethodRestrict()).iterator();
boolean match = false;
while (it.hasNext()) {
String method = it.next().toString();
if (method.equalsIgnoreCase(request.getMethod())) {
match = true;
break;
}
}
if (!match) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
}
if ("TRACE".equals(request.getMethod()) && !consumer.isTraceEnabled()) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
// we do not support java serialized objects unless explicit enabled
String contentType = request.getContentType();
if (HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType) && !consumer.getEndpoint().getComponent().isAllowJavaSerializedObject()) {
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
return;
}
final Exchange result = (Exchange) request.getAttribute(EXCHANGE_ATTRIBUTE_NAME);
if (result == null) {
// no asynchronous result so leverage continuation
final Continuation continuation = ContinuationSupport.getContinuation(request);
if (continuation.isInitial() && continuationTimeout != null) {
// set timeout on initial
continuation.setTimeout(continuationTimeout);
}
// are we suspended and a request is dispatched initially?
if (consumer.isSuspended() && continuation.isInitial()) {
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return;
}
if (continuation.isExpired()) {
String id = (String) continuation.getAttribute(EXCHANGE_ATTRIBUTE_ID);
// remember this id as expired
expiredExchanges.put(id, id);
log.warn("Continuation expired of exchangeId: {}", id);
consumer.getBinding().doWriteExceptionResponse(new TimeoutException(), response);
return;
}
// a new request so create an exchange
final Exchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
if (consumer.getEndpoint().isBridgeEndpoint()) {
exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
exchange.setProperty(Exchange.SKIP_WWW_FORM_URLENCODED, Boolean.TRUE);
}
if (consumer.getEndpoint().isDisableStreamCache()) {
exchange.setProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.TRUE);
}
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);
updateHttpPath(exchange, contextPath);
if (log.isTraceEnabled()) {
log.trace("Suspending continuation of exchangeId: {}", exchange.getExchangeId());
}
continuation.setAttribute(EXCHANGE_ATTRIBUTE_ID, exchange.getExchangeId());
// we want to handle the UoW
try {
consumer.createUoW(exchange);
} catch (Exception e) {
log.error("Error processing request", e);
throw new ServletException(e);
}
// must suspend before we process the exchange
continuation.suspend();
ClassLoader oldTccl = overrideTccl(exchange);
if (log.isTraceEnabled()) {
log.trace("Processing request for exchangeId: {}", exchange.getExchangeId());
}
// use the asynchronous API to process the exchange
consumer.getAsyncProcessor().process(exchange, new AsyncCallback() {
public void done(boolean doneSync) {
// check if the exchange id is already expired
boolean expired = expiredExchanges.remove(exchange.getExchangeId()) != null;
if (!expired) {
if (log.isTraceEnabled()) {
log.trace("Resuming continuation of exchangeId: {}", exchange.getExchangeId());
}
// resume processing after both, sync and async callbacks
continuation.setAttribute(EXCHANGE_ATTRIBUTE_NAME, exchange);
continuation.resume();
} else {
log.warn("Cannot resume expired continuation of exchangeId: {}", exchange.getExchangeId());
}
}
});
if (oldTccl != null) {
restoreTccl(exchange, oldTccl);
}
// method again when its resumed
return;
}
try {
// now lets output to the response
if (log.isTraceEnabled()) {
log.trace("Resumed continuation and writing response for exchangeId: {}", result.getExchangeId());
}
Integer bs = consumer.getEndpoint().getResponseBufferSize();
if (bs != null) {
log.trace("Using response buffer size: {}", bs);
response.setBufferSize(bs);
}
consumer.getBinding().writeResponse(result, response);
} catch (IOException e) {
log.error("Error processing request", e);
throw e;
} catch (Exception e) {
log.error("Error processing request", e);
throw new ServletException(e);
} finally {
consumer.doneUoW(result);
}
}
use of org.apache.camel.http.common.HttpConsumer in project camel by apache.
the class JettySuspendResumeTest method testJettySuspendResume.
@Test
public void testJettySuspendResume() throws Exception {
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);
}
use of org.apache.camel.http.common.HttpConsumer in project camel by apache.
the class JettySuspendTest method testJettySuspend.
@Test
public void testJettySuspend() throws Exception {
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());
}
}
use of org.apache.camel.http.common.HttpConsumer in project ddf by codice.
the class HttpProxyCamelHttpTransportServletTest method testResolve.
@Test
public void testResolve() throws Exception {
HttpProxyCamelHttpTransportServlet servlet = new HttpProxyCamelHttpTransportServlet(mockContext);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn("/example0/something/something");
HttpConsumer consumer = servlet.resolve(request);
try {
when(request.getPathInfo()).thenReturn("/example0");
consumer = servlet.resolve(request);
} catch (Exception e) {
fail("Failed to resolve request. " + e.getMessage());
}
}
Aggregations