camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r571436 - in /activemq/camel/trunk/components/camel-http/src: main/java/org/apache/camel/component/http/ test/java/org/apache/camel/component/http/
Date Fri, 31 Aug 2007 11:43:03 GMT
Author: jstrachan
Date: Fri Aug 31 04:43:03 2007
New Revision: 571436

URL: http://svn.apache.org/viewvc?rev=571436&view=rev
Log:
added HTTP polling capability for CAMEL-132

Added:
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
  (with props)
    activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPollingGetTest.java
      - copied, changed from r571046, activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java
Modified:
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java?rev=571436&r1=571435&r2=571436&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
(original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
Fri Aug 31 04:43:03 2007
@@ -53,14 +53,7 @@
 
     @Override
     protected Endpoint<HttpExchange> createEndpoint(String uri, String remaining, Map
parameters) throws Exception {
-        return new HttpEndpoint(uri, this, new URI(uri)) {
-            
-            // TODO: we should implement this using a polling http client.
-            @Override
-            public Consumer<HttpExchange> createConsumer(Processor processor) throws
Exception {
-                throw new RuntimeCamelException("Not implemented.  You can only produce to
a http endpoint.");
-            }
-        };
+        return new HttpEndpoint(uri, this, new URI(uri));
     }
 
 }

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=571436&r1=571435&r2=571436&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
(original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
Fri Aug 31 04:43:03 2007
@@ -16,24 +16,21 @@
  */
 package org.apache.camel.component.http;
 
-import java.net.URI;
-import java.net.URISyntaxException;
+import org.apache.camel.PollingConsumer;
+import org.apache.camel.impl.DefaultPollingEndpoint;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
-import org.apache.camel.Consumer;
-import org.apache.camel.Processor;
-import org.apache.camel.Producer;
-import org.apache.camel.impl.DefaultEndpoint;
+import java.net.URI;
+import java.net.URISyntaxException;
 
 /**
  * Represents a <a href="http://activemq.apache.org/camel/http.html">HTTP
  * endpoint</a>
- * 
+ *
  * @version $Revision$
  */
-public class HttpEndpoint extends DefaultEndpoint<HttpExchange> {
+public class HttpEndpoint extends DefaultPollingEndpoint<HttpExchange> {
 
     private HttpBinding binding;
     private HttpComponent component;
@@ -45,12 +42,13 @@
         this.httpUri = httpURI;
     }
 
-    public Producer<HttpExchange> createProducer() throws Exception {
+    public HttpProducer createProducer() throws Exception {
         return new HttpProducer(this);
     }
 
-    public Consumer<HttpExchange> createConsumer(Processor processor) throws Exception
{
-        return new HttpConsumer(this, processor);
+    @Override
+    public PollingConsumer<HttpExchange> createPollingConsumer() throws Exception {
+        return new HttpPollingConsumer(this);
     }
 
     public HttpExchange createExchange() {

Added: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java?rev=571436&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
(added)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
Fri Aug 31 04:43:03 2007
@@ -0,0 +1,101 @@
+/**
+ *
+ * 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.http;
+
+import org.apache.camel.Message;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.impl.PollingConsumerSupport;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+
+import java.io.IOException;
+
+/**
+ * A polling HTTP consumer which by default performs a GET
+ *
+ * @version $Revision: 1.1 $
+ */
+public class HttpPollingConsumer extends PollingConsumerSupport<HttpExchange> {
+    private final HttpEndpoint endpoint;
+    private HttpClient httpClient = new HttpClient();
+
+    public HttpPollingConsumer(HttpEndpoint endpoint) {
+        super(endpoint);
+        this.endpoint = endpoint;
+    }
+
+    public HttpExchange receive() {
+        return receiveNoWait();
+    }
+
+    public HttpExchange receive(long timeout) {
+        return receiveNoWait();
+    }
+
+    public HttpExchange receiveNoWait() {
+        try {
+            HttpExchange exchange = endpoint.createExchange();
+            HttpMethod method = createMethod();
+            int responseCode = httpClient.executeMethod(method);
+
+            // lets store the result in the output message.
+            byte[] responseBody = method.getResponseBody();
+            Message message = exchange.getIn();
+            message.setBody(responseBody);
+
+            // lets set the headers
+            Header[] headers = method.getResponseHeaders();
+            for (Header header : headers) {
+                String name = header.getName();
+                String value = header.getValue();
+                message.setHeader(name, value);
+            }
+
+            message.setHeader("http.responseCode", responseCode);
+            return exchange;
+        }
+        catch (IOException e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public HttpClient getHttpClient() {
+        return httpClient;
+    }
+
+    public void setHttpClient(HttpClient httpClient) {
+        this.httpClient = httpClient;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+    protected HttpMethod createMethod() {
+        String uri = endpoint.getEndpointUri();
+        return new GetMethod(uri);
+    }
+
+    protected void doStart() throws Exception {
+    }
+
+    protected void doStop() throws Exception {
+    }
+}

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

Copied: activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPollingGetTest.java
(from r571046, activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPollingGetTest.java?p2=activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPollingGetTest.java&p1=activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java&r1=571046&r2=571436&rev=571436&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java
(original)
+++ activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPollingGetTest.java
Fri Aug 31 04:43:03 2007
@@ -28,12 +28,12 @@
 /**
  * @version $Revision: 520220 $
  */
-public class HttpGetTest extends ContextTestSupport {
+public class HttpPollingGetTest extends ContextTestSupport {
     protected String expectedText = "<html";
 
     public void testHttpGet() throws Exception {
         MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", MockEndpoint.class);
-        mockEndpoint.expectedMessageCount(1);
+        mockEndpoint.expectedMinimumMessageCount(1);
 
         template.sendBody("direct:start", null);
 
@@ -61,7 +61,7 @@
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() {
-                from("direct:start").to("http://www.google.com").to("mock:results");
+                from("http://www.google.com").to("mock:results");
             }
         };
     }



Mime
View raw message