CAMEL-7354: Rest DSL. Integrate with camel-jetty. Work in progress.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/60fd9e97 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/60fd9e97 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/60fd9e97 Branch: refs/heads/master Commit: 60fd9e978f876c48721378664a1135fe66532ac9 Parents: f1cf139 Author: Claus Ibsen <[email protected]> Authored: Sat Jul 26 10:41:02 2014 +0200 Committer: Claus Ibsen <[email protected]> Committed: Mon Jul 28 10:50:48 2014 +0200 ---------------------------------------------------------------------- .../websocket/CamelWebSocketServlet.java | 2 +- .../camel/component/http/CamelServlet.java | 41 +++---- .../HttpServletResolveConsumerStrategy.java | 46 ++++++++ .../http/ServletResolveConsumerStrategy.java | 37 +++++++ .../jetty/CamelContinuationServlet.java | 2 +- .../component/jetty/CamelFilterWrapper.java | 1 - .../component/jetty/JettyHttpComponent.java | 106 +++++++++++++++++-- .../camel/component/jetty/JettyRestFilter.java | 44 ++++++++ .../component/jetty/rest/RestJettyGetTest.java | 58 ++++++++++ 9 files changed, 305 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/CamelWebSocketServlet.java ---------------------------------------------------------------------- diff --git a/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/CamelWebSocketServlet.java b/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/CamelWebSocketServlet.java index a8e3414..50174b4 100644 --- a/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/CamelWebSocketServlet.java +++ b/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/CamelWebSocketServlet.java @@ -42,7 +42,7 @@ public class CamelWebSocketServlet extends CamelHttpTransportServlet { log.trace("Service: {}", request); // Is there a consumer registered for the request. - HttpConsumer consumer = resolve(request); + HttpConsumer consumer = getServletResolveConsumerStrategy().resolve(request, getConsumers()); if (consumer == null) { log.debug("No consumer to service request {}", request); response.sendError(HttpServletResponse.SC_NOT_FOUND); http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java b/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java index cbfded3..926d25d 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java @@ -17,9 +17,10 @@ package org.apache.camel.component.http; import java.io.IOException; +import java.util.Collections; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; - import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -46,7 +47,8 @@ public class CamelServlet extends HttpServlet { */ private String servletName; - private ConcurrentMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>(); + private ServletResolveConsumerStrategy servletResolveConsumerStrategy = new HttpServletResolveConsumerStrategy(); + private final ConcurrentMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>(); @Override public void init(ServletConfig config) throws ServletException { @@ -153,22 +155,12 @@ public class CamelServlet extends HttpServlet { } } + /** + * @deprecated use {@link ServletResolveConsumerStrategy#resolve(javax.servlet.http.HttpServletRequest, java.util.Map)} + */ + @Deprecated protected HttpConsumer resolve(HttpServletRequest request) { - String path = request.getPathInfo(); - if (path == null) { - return null; - } - HttpConsumer answer = consumers.get(path); - - if (answer == null) { - for (String key : consumers.keySet()) { - if (consumers.get(key).getEndpoint().isMatchOnUriPrefix() && path.startsWith(key)) { - answer = consumers.get(key); - break; - } - } - } - return answer; + return getServletResolveConsumerStrategy().resolve(request, getConsumers()); } public void connect(HttpConsumer consumer) { @@ -188,9 +180,22 @@ public class CamelServlet extends HttpServlet { public void setServletName(String servletName) { this.servletName = servletName; } - + + public ServletResolveConsumerStrategy getServletResolveConsumerStrategy() { + return servletResolveConsumerStrategy; + } + + public void setServletResolveConsumerStrategy(ServletResolveConsumerStrategy servletResolveConsumerStrategy) { + this.servletResolveConsumerStrategy = servletResolveConsumerStrategy; + } + + public Map<String, HttpConsumer> getConsumers() { + return Collections.unmodifiableMap(consumers); + } + /** * Override the Thread Context ClassLoader if need be. + * * @param exchange * @return old classloader if overridden; otherwise returns null */ http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-http/src/main/java/org/apache/camel/component/http/HttpServletResolveConsumerStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpServletResolveConsumerStrategy.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpServletResolveConsumerStrategy.java new file mode 100644 index 0000000..dee20c3 --- /dev/null +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpServletResolveConsumerStrategy.java @@ -0,0 +1,46 @@ +/** + * 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 java.util.Map; +import javax.servlet.http.HttpServletRequest; + +/** + * A default implementation of {@link org.apache.camel.component.http.ServletResolveConsumerStrategy}. + */ +public class HttpServletResolveConsumerStrategy implements ServletResolveConsumerStrategy { + + @Override + public HttpConsumer resolve(HttpServletRequest request, Map<String, HttpConsumer> consumers) { + String path = request.getPathInfo(); + if (path == null) { + return null; + } + HttpConsumer answer = consumers.get(path); + + if (answer == null) { + for (String key : consumers.keySet()) { + if (consumers.get(key).getEndpoint().isMatchOnUriPrefix() && path.startsWith(key)) { + answer = consumers.get(key); + break; + } + } + } + return answer; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-http/src/main/java/org/apache/camel/component/http/ServletResolveConsumerStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/ServletResolveConsumerStrategy.java b/components/camel-http/src/main/java/org/apache/camel/component/http/ServletResolveConsumerStrategy.java new file mode 100644 index 0000000..68f27fa --- /dev/null +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/ServletResolveConsumerStrategy.java @@ -0,0 +1,37 @@ +/** + * 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 java.util.Map; +import javax.servlet.http.HttpServletRequest; + +/** + * Strategy to resolve which consumer to service an incoming {@link javax.servlet.http.HttpServletRequest}. + */ +public interface ServletResolveConsumerStrategy { + + /** + * Resolve the consumer to use. + * + * @param request the http request + * @param consumers the map of registered consumers + * @return the consumer to service the request, or <tt>null</tt> if no match, + * which sends back a {@link javax.servlet.http.HttpServletResponse#SC_NOT_FOUND} to the client. + */ + HttpConsumer resolve(HttpServletRequest request, Map<String, HttpConsumer> consumers); + +} http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java index 7e48d2e..3ab15f2 100644 --- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java +++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java @@ -57,7 +57,7 @@ public class CamelContinuationServlet extends CamelServlet { log.trace("Service: {}", request); // is there a consumer registered for the request. - HttpConsumer consumer = resolve(request); + HttpConsumer consumer = getServletResolveConsumerStrategy().resolve(request, getConsumers()); if (consumer == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java index 102bafb..f247e5f 100644 --- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java +++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java @@ -30,7 +30,6 @@ import javax.servlet.ServletResponse; * The camel filter wrapper that processes only initially dispatched requests. * Re-dispatched requests are ignored. */ - public class CamelFilterWrapper implements Filter { private Filter wrapped; http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java index 68dd70b..77bb504 100644 --- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java +++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java @@ -21,14 +21,18 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URI; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import javax.management.MBeanServer; import javax.servlet.Filter; import org.apache.camel.CamelContext; +import org.apache.camel.Consumer; import org.apache.camel.Endpoint; +import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.http.CamelServlet; import org.apache.camel.component.http.HttpBinding; @@ -39,6 +43,9 @@ import org.apache.camel.component.http.UrlRewrite; import org.apache.camel.spi.HeaderFilterStrategy; import org.apache.camel.spi.ManagementAgent; import org.apache.camel.spi.ManagementStrategy; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.spi.RestConsumerFactory; +import org.apache.camel.util.FileUtil; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; @@ -72,7 +79,7 @@ import org.slf4j.LoggerFactory; * * @version */ -public class JettyHttpComponent extends HttpComponent { +public class JettyHttpComponent extends HttpComponent implements RestConsumerFactory { public static final String TMP_DIR = "CamelJettyTempDir"; protected static final HashMap<String, ConnectorRef> CONNECTORS = new HashMap<String, ConnectorRef>(); @@ -370,18 +377,25 @@ public class JettyHttpComponent extends HttpComponent { ServletContextHandler context = server.getChildHandlerByClass(ServletContextHandler.class); List<Filter> filters = endpoint.getFilters(); for (Filter filter : filters) { - FilterHolder filterHolder = new FilterHolder(); - filterHolder.setFilter(new CamelFilterWrapper(filter)); - String pathSpec = endpoint.getPath(); - if (pathSpec == null || "".equals(pathSpec)) { - pathSpec = "/"; - } - if (endpoint.isMatchOnUriPrefix()) { - pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*"; + if (filter instanceof JettyRestFilter) { + // special for rest filter + FilterHolder filterHolder = new FilterHolder(); + filterHolder.setFilter(new CamelFilterWrapper(filter)); +// context.getServletHandler().addFilter(filterHolder); + addFilter(context, filterHolder, "*"); + } else { + FilterHolder filterHolder = new FilterHolder(); + filterHolder.setFilter(new CamelFilterWrapper(filter)); + String pathSpec = endpoint.getPath(); + if (pathSpec == null || "".equals(pathSpec)) { + pathSpec = "/"; + } + if (endpoint.isMatchOnUriPrefix()) { + pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*"; + } + addFilter(context, filterHolder, pathSpec); } - addFilter(context, filterHolder, pathSpec); } - } private void addFilter(ServletContextHandler context, FilterHolder filterHolder, String pathSpec) { @@ -931,6 +945,76 @@ public class JettyHttpComponent extends HttpComponent { // Implementation methods // ------------------------------------------------------------------------- + + + @Override + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + String consumes, String produces, Map<String, Object> parameters) throws Exception { + + path = FileUtil.stripLeadingSeparator(path); + + String scheme = "http"; + String host = "0.0.0.0"; + int port = 0; + + // if no explicit port/host configured, then use port from rest configuration + RestConfiguration config = getCamelContext().getRestConfiguration(); + if (config != null && (config.getComponent() == null || config.getComponent().equals("jetty"))) { + if (config.getScheme() != null) { + scheme = config.getScheme(); + } + if (config.getHost() != null) { + host = config.getHost(); + } + int num = config.getPort(); + if (num > 0) { + port = num; + } + } + + Map<String, Object> map = new HashMap<String, Object>(); + // build query string, and append any endpoint configuration properties + if (config != null && (config.getComponent() == null || config.getComponent().equals("jetty"))) { + // setup endpoint options + if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) { + map.putAll(config.getEndpointProperties()); + } + } + + String query = URISupport.createQueryString(map); + + String url = "jetty:%s://%s:%s/%s?httpMethodRestrict=%s"; + if (!query.isEmpty()) { + url = url + "?" + query; + } + + // must use upper case for restrict + String restrict = verb.toUpperCase(Locale.US); + + // get the endpoint + url = String.format(url, scheme, host, port, path, restrict); + JettyHttpEndpoint endpoint = camelContext.getEndpoint(url, JettyHttpEndpoint.class); + setProperties(endpoint, parameters); + + // add our filter + List<Filter> list = endpoint.getFilters(); + if (list == null) { + list = new ArrayList<Filter>(); + } + list.add(0, new JettyRestFilter()); + endpoint.setFilters(list); + // disable this filter as we want to use ours + endpoint.setEnableMultipartFilter(false); + + // configure consumer properties + Consumer consumer = endpoint.createConsumer(processor); + if (config != null && config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) { + setProperties(consumer, config.getConsumerProperties()); + } + + return consumer; + } + protected CamelServlet createServletForConnector(Server server, Connector connector, List<Handler> handlers, JettyHttpEndpoint endpoint) throws Exception { ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS); http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyRestFilter.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyRestFilter.java b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyRestFilter.java new file mode 100644 index 0000000..2203c82 --- /dev/null +++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyRestFilter.java @@ -0,0 +1,44 @@ +/** + * 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.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +public class JettyRestFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + // noop + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + System.out.println("Doing my filter here"); + filterChain.doFilter(servletRequest, servletResponse); + } + + @Override + public void destroy() { + // noop + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/60fd9e97/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java new file mode 100644 index 0000000..438bb86 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java @@ -0,0 +1,58 @@ +/** + * 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.rest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore +public class RestJettyGetTest extends BaseJettyTest { + + @Test + public void testJettyProducerGet() throws Exception { + String out = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class); + assertEquals("123;Donald Duck", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use restlet on localhost with the given port + restConfiguration().component("jetty").host("localhost").port(getPort()); + + // use the rest DSL to define the rest services + rest("/users/") + .get("{id}/basic") + .route() + .to("mock:input") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + String id = exchange.getIn().getHeader("id", String.class); + exchange.getOut().setBody(id + ";Donald Duck"); + } + }); + } + }; + } + +}
