camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r571471 - in /activemq/camel/trunk/components/camel-jetty/src: main/java/org/apache/camel/component/jetty/ test/java/org/apache/camel/component/jetty/
Date Fri, 31 Aug 2007 14:06:27 GMT
Author: chirino
Date: Fri Aug 31 07:06:26 2007
New Revision: 571471

URL: http://svn.apache.org/viewvc?rev=571471&view=rev
Log:
adding missing files breaking the build.


Added:
    activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
  (with props)
    activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
  (with props)
    activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGetTest.java
  (with props)

Added: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java?rev=571471&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
(added)
+++ activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
Fri Aug 31 07:06:26 2007
@@ -0,0 +1,109 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.component.http.CamelServlet;
+import org.apache.camel.component.http.HttpConsumer;
+import org.apache.camel.component.http.HttpExchange;
+
+import org.mortbay.util.ajax.Continuation;
+import org.mortbay.util.ajax.ContinuationSupport;
+
+/**
+ * @version $Revision$
+ */
+public class CamelContinuationServlet extends CamelServlet {
+
+    // private static final String EXCHANGE_ATTRIBUTE =
+    // CamelContinuationServlet.class.getName()+".EXCHANGE_ATTRIBUTE";
+
+    @Override
+    protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
+        try {
+
+            // Is there a consumer registered for the request.
+            HttpConsumer consumer = resolve(request);
+            if (consumer == null) {
+                response.sendError(HttpServletResponse.SC_NOT_FOUND);
+                return;
+            }
+
+            // HttpExchange exchange =
+            // (HttpExchange)request.getAttribute(EXCHANGE_ATTRIBUTE);
+            // if( exchange == null ) {
+            // exchange = new HttpExchange(consumer.getEndpoint(), request,
+            // response);
+            // }
+            // Continuation continuation =
+            // ContinuationSupport.getContinuation(request, exchange);
+
+            final Continuation continuation = ContinuationSupport.getContinuation(request,
null);
+            if (continuation.isNew()) {
+
+                // Have the camel process the HTTP exchange.
+                final HttpExchange exchange = new HttpExchange(consumer.getEndpoint(), request,
response);
+                boolean sync = consumer.getAsyncProcessor().process(exchange, new AsyncCallback()
{
+                    public void done(boolean sync) {
+                        if (sync) {
+                            return;
+                        }
+                        continuation.setObject(exchange);
+                        continuation.resume();
+                    }
+                });
+
+                if (!sync) {
+                    
+                    // Wait for the exchange to get processed.
+                    // This might block until it completes or it might return via an exception
and
+                    // then this method is re-invoked once the the exchange has finished
processing
+                    continuation.suspend(0); 
+                    
+                }
+                
+                // HC: The getBinding() is interesting because it illustrates the
+                // impedance miss-match between
+                // HTTP's stream oriented protocol, and Camels more message oriented
+                // protocol exchanges.
+
+                // now lets output to the response
+                consumer.getBinding().writeResponse(exchange);
+                return;
+            }
+            
+            if (continuation.isResumed() ) {
+                HttpExchange exchange = (HttpExchange)continuation.getObject();
+                // now lets output to the response
+                consumer.getBinding().writeResponse(exchange);
+                return;
+            }
+
+
+        } catch (Exception e) {
+            throw new ServletException(e);
+        }
+    }
+
+
+}

Propchange: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java?rev=571471&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
(added)
+++ activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
Fri Aug 31 07:06:26 2007
@@ -0,0 +1,148 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.concurrent.CountDownLatch;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.AsyncProcessor;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Producer;
+import org.apache.camel.component.http.HttpEndpoint;
+import org.apache.camel.component.http.HttpExchange;
+import org.apache.camel.impl.DefaultProducer;
+
+import org.mortbay.io.Buffer;
+import org.mortbay.jetty.HttpFields;
+import org.mortbay.jetty.HttpMethods;
+import org.mortbay.jetty.HttpURI;
+import org.mortbay.jetty.HttpFields.Field;
+import org.mortbay.jetty.client.HttpClient;
+import org.mortbay.jetty.client.HttpExchange.ContentExchange;
+
+public class JettyHttpProducer extends DefaultProducer<HttpExchange> implements Producer<HttpExchange>,
AsyncProcessor {
+
+    private final class CamelContentExchange extends ContentExchange {
+
+        private final AsyncCallback callback;
+        private final Exchange exchange;
+        private HttpFields responseFields;
+
+        private CamelContentExchange(Exchange exchange, AsyncCallback callback) {
+            this.exchange = exchange;
+            this.callback = callback;
+            responseFields = new HttpFields();
+        }
+
+        protected void onResponseComplete() throws IOException {
+            super.onRequestComplete();
+            try {
+                Message out = exchange.getOut(true);
+                out.setBody(getResponseContent());
+                for (Iterator i = responseFields.getFields(); i.hasNext();) {
+                    Field field = (Field)i.next();
+                    out.setHeader(field.getName(), field.getValue());
+                }
+            } catch (Throwable e) {
+                exchange.setException(e);
+            }
+            callback.done(false);
+        }
+
+        public HttpFields getResponseFields() {
+            return responseFields;
+        }
+
+        protected void onResponsetHeader(Buffer name, Buffer value) throws IOException {
+            responseFields.add(name, value);
+        }
+
+    }
+
+    private HttpClient httpClient;
+    private String address;
+
+    public JettyHttpProducer(HttpEndpoint endpoint) {
+        super(endpoint);
+        httpClient = ((JettyHttpComponent)endpoint.getComponent()).getHttpClient();
+
+        address = endpoint.getHttpUri().toString();
+
+        // A workaround where the Jetty client does not like to see
+        // urls like http://google.com but does like http://google.com/
+        HttpURI uri = new HttpURI(address);
+        if (uri.getCompletePath() == null) {
+            address += "/";
+        }
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        final CountDownLatch latch = new CountDownLatch(1);
+        process(exchange, new AsyncCallback() {
+            public void done(boolean sync) {
+                latch.countDown();
+            }
+        });
+        latch.await();
+        exchange.throwException();
+    }
+
+    public boolean process(final Exchange exchange, final AsyncCallback callback) {
+
+        ContentExchange jettyExchange = new CamelContentExchange(exchange, callback);
+
+        jettyExchange.setURL(address);
+
+        // If a in body can be converted to an InputStream or a Buffer
+        // then do a POST otherwise, do a GET
+        Message in = exchange.getIn();
+        InputStream is = in.getBody(InputStream.class);
+        if (is != null) {
+            jettyExchange.setMethod(HttpMethods.POST);
+            jettyExchange.setRequestContentSource(is);
+        } else {
+            Buffer buffer = in.getBody(Buffer.class);
+            if (buffer != null) {
+                jettyExchange.setMethod(HttpMethods.POST);
+                jettyExchange.setRequestContent(buffer);
+            } else {
+                jettyExchange.setMethod(HttpMethods.GET);
+            }
+        }
+
+        for (String name : in.getHeaders().keySet()) {            
+            String value = in.getHeader(name, String.class);
+            if( "Content-Type".equals(name) ) {
+                jettyExchange.setRequestContentType(value);
+            } else {
+                jettyExchange.addRequestHeader(name, value);
+            }
+        }
+        try {
+            httpClient.send(jettyExchange);
+        } catch (IOException e) {
+            exchange.setException(e);
+            return true;
+        }
+        return false;
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGetTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGetTest.java?rev=571471&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGetTest.java
(added)
+++ activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGetTest.java
Fri Aug 31 07:06:26 2007
@@ -0,0 +1,68 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class HttpGetTest extends ContextTestSupport {
+    protected String expectedText = "<html";
+
+    public void testHttpGet() throws Exception {
+        MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", MockEndpoint.class);
+        mockEndpoint.expectedMessageCount(1);
+
+        template.sendBody("direct:start", null);
+
+        mockEndpoint.assertIsSatisfied();
+        List<Exchange> list = mockEndpoint.getReceivedExchanges();
+        Exchange exchange = list.get(0);
+        assertNotNull("exchange", exchange);
+
+        Message in = exchange.getIn();
+        assertNotNull("in", in);
+
+        Map<String, Object> headers = in.getHeaders();
+
+        log.debug("Headers: " + headers);
+        assertTrue("Should be more than one header but was: " + headers, headers.size() >
0);
+
+        String body = in.getBody(String.class);
+
+        log.debug("Body: " + body);
+        assertNotNull("Should have a body!", body);
+        assertTrue("body should contain: " + expectedText, body.contains(expectedText));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").to("jetty:http://www.google.com").to("mock:results");
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGetTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGetTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message