Author: davsclaus
Date: Tue Nov 10 12:40:31 2009
New Revision: 834452
URL: http://svn.apache.org/viewvc?rev=834452&view=rev
Log:
CAMEL-2135: jetty http producer in blocking mode now waits and populates
response as the regular http producer does. Removing the Future Body stuff as
it complicates matter and the new toAsync should help avoid the need for Future.
Removed:
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerHeaderBasedCBRTestTest.java
Modified:
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyFutureGetBody.java
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpOperationFailedException.java
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGoogleTest.java
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSimulate404ErrorTest.java
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSlowResponseTest.java
Modified:
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java?rev=834452&r1=834451&r2=834452&view=diff
==============================================================================
---
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
(original)
+++
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
Tue Nov 10 12:40:31 2009
@@ -40,7 +40,7 @@
private static final transient Log LOG =
LogFactory.getLog(JettyContentExchange.class);
- private final Map<String, String> headers = new LinkedHashMap<String,
String>();
+ private final Map<String, Object> headers = new LinkedHashMap<String,
Object>();
private CountDownLatch headersComplete = new CountDownLatch(1);
private CountDownLatch bodyComplete = new CountDownLatch(1);
private volatile boolean failed;
@@ -83,10 +83,6 @@
}
if (callback != null && exchange != null) {
- if (LOG.isTraceEnabled()) {
- LOG.trace("Adding Exchange to completed task: " + exchange);
- }
-
// signal we are complete
callback.onDataReceived(exchange);
}
@@ -106,7 +102,7 @@
return bodyComplete.getCount() == 0;
}
- public Map<String, String> getHeaders() {
+ public Map<String, Object> getHeaders() {
return headers;
}
Modified:
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyFutureGetBody.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyFutureGetBody.java?rev=834452&r1=834451&r2=834452&view=diff
==============================================================================
---
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyFutureGetBody.java
(original)
+++
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyFutureGetBody.java
Tue Nov 10 12:40:31 2009
@@ -31,6 +31,7 @@
*
* @version $Revision$
*/
+...@deprecated
public class JettyFutureGetBody implements Future<String>, Serializable {
private final Exchange exchange;
@@ -88,7 +89,7 @@
private String doGetBody() throws ExecutionException {
try {
if (httpExchange.isFailed() && throwExceptionOnFailure) {
- throw new
JettyHttpOperationFailedException(httpExchange.getUrl(),
httpExchange.getResponseStatus(), httpExchange.getBody());
+ throw new
JettyHttpOperationFailedException(httpExchange.getUrl(),
httpExchange.getResponseStatus(), httpExchange.getHeaders(),
httpExchange.getBody());
} else {
return httpExchange.getBody();
}
Modified:
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpOperationFailedException.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpOperationFailedException.java?rev=834452&r1=834451&r2=834452&view=diff
==============================================================================
---
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpOperationFailedException.java
(original)
+++
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpOperationFailedException.java
Tue Nov 10 12:40:31 2009
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.jetty;
+import java.util.Map;
+
import org.apache.camel.CamelException;
/**
@@ -25,12 +27,20 @@
private final String url;
private final int statusCode;
+ private final String redirectLocation;
+ private final Map<String, Object> responseHeaders;
private final String responseBody;
- public JettyHttpOperationFailedException(String url, int statusCode,
String responseBody) {
- super("HTTP operation failed invoking " + url + " with statusCode: " +
statusCode);
- this.statusCode = statusCode;
+ public JettyHttpOperationFailedException(String url, int statusCode,
Map<String, Object> headers, String responseBody) {
+ this(url, statusCode, null, headers, responseBody);
+ }
+
+ public JettyHttpOperationFailedException(String url, int statusCode,
String location, Map<String, Object> headers, String responseBody) {
+ super("HTTP operation failed invoking " + url + " with statusCode: " +
statusCode + (location != null ? ", redirectLocation: " + location : ""));
this.url = url;
+ this.statusCode = statusCode;
+ this.redirectLocation = location;
+ this.responseHeaders = headers;
this.responseBody = responseBody;
}
@@ -49,4 +59,12 @@
public String getResponseBody() {
return responseBody;
}
+
+ public String getRedirectLocation() {
+ return redirectLocation;
+ }
+
+ public Map<String, Object> getResponseHeaders() {
+ return responseHeaders;
+ }
}
Modified:
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java?rev=834452&r1=834451&r2=834452&view=diff
==============================================================================
---
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
(original)
+++
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
Tue Nov 10 12:40:31 2009
@@ -24,9 +24,11 @@
import org.apache.camel.AsyncProcessor;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
+import org.apache.camel.Message;
import org.apache.camel.component.http.HttpMethods;
import org.apache.camel.component.http.helper.HttpProducerHelper;
import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.URISupport;
import org.apache.commons.logging.Log;
@@ -59,6 +61,7 @@
JettyContentExchange httpExchange = createHttpExchange(exchange);
sendSynchronous(exchange, client, httpExchange);
}
+
public void process(Exchange exchange, AsyncCallback callback) throws
Exception {
HttpClient client = getEndpoint().getClient();
@@ -66,18 +69,9 @@
sendAsynchronous(exchange, client, httpExchange, callback);
}
- protected void sendSynchronous(Exchange exchange, HttpClient client,
JettyContentExchange httpExchange) throws IOException {
- // set the body with the message holder
- exchange.setOut(new JettyHttpMessage(exchange, httpExchange,
getEndpoint().isThrowExceptionOnFailure()));
-
- doSendExchange(client, httpExchange);
- }
-
protected void sendAsynchronous(final Exchange exchange, final HttpClient
client, final JettyContentExchange httpExchange,
final AsyncCallback callback) throws
IOException {
- // TODO: Use something that marks it as async routed
- exchange.setProperty("CamelSendAsync", Boolean.TRUE);
httpExchange.setCallback(callback);
httpExchange.setExchange(exchange);
@@ -87,11 +81,80 @@
doSendExchange(client, httpExchange);
}
- protected void doSendExchange(HttpClient client, JettyContentExchange
httpExchange) throws IOException {
+ protected void sendSynchronous(Exchange exchange, HttpClient client,
JettyContentExchange httpExchange) throws Exception {
+ doSendExchange(client, httpExchange);
+
+ // we send synchronous so wait for it to be done
+ httpExchange.waitForDone();
+ // and then process the response
+ processResponse(exchange, httpExchange);
+ }
+
+ protected void processResponse(Exchange exchange, JettyContentExchange
httpExchange) throws IOException, JettyHttpOperationFailedException {
+ int responseCode = httpExchange.getResponseStatus();
+
if (LOG.isDebugEnabled()) {
- LOG.debug("Sending HTTP request to: " + httpExchange.getUrl());
+ LOG.debug("HTTP responseCode: " + responseCode);
+ }
+
+ Message in = exchange.getIn();
+ HeaderFilterStrategy strategy =
getEndpoint().getHeaderFilterStrategy();
+ if (!getEndpoint().isThrowExceptionOnFailure()) {
+ // if we do not use failed exception then populate response for
all response codes
+ populateResponse(exchange, httpExchange, exchange.getIn(),
strategy, responseCode);
+ } else {
+ if (responseCode >= 100 && responseCode < 300) {
+ // only populate response for OK response
+ populateResponse(exchange, httpExchange, in, strategy,
responseCode);
+ } else {
+ // operation failed so populate exception to throw
+ throw populateHttpOperationFailedException(exchange,
httpExchange, responseCode);
+ }
}
- client.send(httpExchange);
+ }
+
+ protected void populateResponse(Exchange exchange, JettyContentExchange
httpExchange,
+ Message in, HeaderFilterStrategy strategy,
int responseCode) throws IOException {
+ Message answer = exchange.getOut();
+
+ answer.setHeaders(in.getHeaders());
+ answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
+ answer.setBody(httpExchange.getBody());
+
+ // propagate HTTP response headers
+ for (Map.Entry<String, Object> entry :
httpExchange.getHeaders().entrySet()) {
+ String name = entry.getKey();
+ Object value = entry.getValue();
+ if (name.toLowerCase().equals("content-type")) {
+ name = Exchange.CONTENT_TYPE;
+ }
+ if (strategy != null &&
!strategy.applyFilterToExternalHeaders(name, value, exchange)) {
+ answer.setHeader(name, value);
+ }
+ }
+ }
+
+ protected JettyHttpOperationFailedException
populateHttpOperationFailedException(Exchange exchange, JettyContentExchange
httpExchange,
+
int responseCode) throws IOException {
+ JettyHttpOperationFailedException exception;
+ String uri = httpExchange.getUrl();
+ Map<String, Object> headers = httpExchange.getHeaders();
+ String body = httpExchange.getBody();
+
+ if (responseCode >= 300 && responseCode < 400) {
+ String locationHeader =
httpExchange.getResponseFields().getStringField("location");
+ if (locationHeader != null) {
+ exception = new JettyHttpOperationFailedException(uri,
responseCode, locationHeader, headers, body);
+ } else {
+ // no redirect location
+ exception = new JettyHttpOperationFailedException(uri,
responseCode, headers, body);
+ }
+ } else {
+ // internal server error (error code 500)
+ exception = new JettyHttpOperationFailedException(uri,
responseCode, headers, body);
+ }
+
+ return exception;
}
protected JettyContentExchange createHttpExchange(Exchange exchange)
throws Exception {
@@ -105,6 +168,18 @@
doSetQueryParameters(exchange, httpExchange);
+ // and copy headers from IN message
+ Message in = exchange.getIn();
+ HeaderFilterStrategy strategy =
getEndpoint().getHeaderFilterStrategy();
+
+ // propagate headers as HTTP headers
+ for (String headerName : in.getHeaders().keySet()) {
+ String headerValue = in.getHeader(headerName, String.class);
+ if (strategy != null &&
!strategy.applyFilterToCamelHeaders(headerName, headerValue, exchange)) {
+ httpExchange.addRequestHeader(headerName, headerValue);
+ }
+ }
+
return httpExchange;
}
@@ -127,6 +202,13 @@
}
}
+ protected static void doSendExchange(HttpClient client,
JettyContentExchange httpExchange) throws IOException {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Sending HTTP request to: " + httpExchange.getUrl());
+ }
+ client.send(httpExchange);
+ }
+
@Override
protected void doStart() throws Exception {
super.doStart();
Modified:
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGoogleTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGoogleTest.java?rev=834452&r1=834451&r2=834452&view=diff
==============================================================================
---
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGoogleTest.java
(original)
+++
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGoogleTest.java
Tue Nov 10 12:40:31 2009
@@ -16,8 +16,6 @@
*/
package org.apache.camel.component.jetty.jettyproducer;
-import java.util.concurrent.Future;
-
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
@@ -33,14 +31,6 @@
assertNotNull(reply);
}
- @Test
- public void testGoogleFrontPageFutureTask() throws Exception {
- Object body = null;
- Future<String> reply = (Future<String>)
template.requestBody("direct:start", body);
- assertNotNull(reply);
- assertNotNull(reply.get());
- }
-
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
Modified:
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSimulate404ErrorTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSimulate404ErrorTest.java?rev=834452&r1=834451&r2=834452&view=diff
==============================================================================
---
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSimulate404ErrorTest.java
(original)
+++
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSimulate404ErrorTest.java
Tue Nov 10 12:40:31 2009
@@ -16,9 +16,6 @@
*/
package org.apache.camel.component.jetty.jettyproducer;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
@@ -35,20 +32,14 @@
@Test
public void test404() throws Exception {
- Exchange exchange = template.request(url, null);
- assertNotNull(exchange);
-
- Future<String> future = exchange.getOut().getBody(Future.class);
- assertNotNull(future);
- assertEquals(false, future.isDone());
-
try {
- future.get();
- } catch (ExecutionException e) {
+ template.request(url, null);
+ } catch (Exception e) {
JettyHttpOperationFailedException cause =
assertIsInstanceOf(JettyHttpOperationFailedException.class, e.getCause());
assertEquals(404, cause.getStatusCode());
assertEquals("http//0.0.0.0:9123/bar", cause.getUrl());
assertEquals("Page not found", cause.getResponseBody());
+ assertNotNull(cause.getResponseHeaders());
}
}
Modified:
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSlowResponseTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSlowResponseTest.java?rev=834452&r1=834451&r2=834452&view=diff
==============================================================================
---
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSlowResponseTest.java
(original)
+++
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSlowResponseTest.java
Tue Nov 10 12:40:31 2009
@@ -16,7 +16,6 @@
*/
package org.apache.camel.component.jetty.jettyproducer;
-import java.util.concurrent.Future;
import javax.servlet.http.HttpServletResponse;
import org.apache.camel.Exchange;
@@ -37,14 +36,10 @@
Exchange exchange = template.request(url, null);
assertNotNull(exchange);
- Future<String> future = exchange.getOut().getBody(Future.class);
- assertNotNull(future);
- assertEquals(false, future.isDone());
-
- String reply = future.get();
+ String reply = exchange.getOut().getBody(String.class);
assertEquals("Bye World", reply);
- assertEquals(3, exchange.getOut().getHeaders().size());
+ assertEquals(4, exchange.getOut().getHeaders().size());
}
@Override
@@ -62,7 +57,7 @@
res.getWriter().write("");
res.flushBuffer();
- Thread.sleep(2000);
+ Thread.sleep(1000);
res.getWriter().write("Bye World");
res.flushBuffer();