Repository: camel Updated Branches: refs/heads/master bf47f40da -> 55813f73f
CAMEL-9633: camel-serlvet has option to turn on multipart-form binding to attachments. You may need to do app server specific configuration to enable it on the app server to make it work, and hence its default disabled in camel-servlet. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/55813f73 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/55813f73 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/55813f73 Branch: refs/heads/master Commit: 55813f73f4a245e938ee24f5b4e69f108183a4fe Parents: bf0f638 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue May 3 11:09:26 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue May 3 11:09:44 2016 +0200 ---------------------------------------------------------------------- .../servlet/AttachmentHttpBinding.java | 82 ++++++++++++++++++++ .../component/servlet/ServletComponent.java | 15 ++++ .../component/servlet/ServletEndpoint.java | 38 +++++++++ .../servlet/ServletAsyncArquillianTest.java | 5 -- 4 files changed, 135 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/55813f73/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java new file mode 100644 index 0000000..084e1a3 --- /dev/null +++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java @@ -0,0 +1,82 @@ +/** + * 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.servlet; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collection; +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.Part; + +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.http.common.DefaultHttpBinding; +import org.apache.camel.http.common.HttpMessage; + +/** + * To handle attachments with Servlet. + * <p/> + * This implementation is needed to deal with attachments when using Servlet. + */ +final class AttachmentHttpBinding extends DefaultHttpBinding { + + AttachmentHttpBinding() { + } + + @Override + protected void populateAttachments(HttpServletRequest request, HttpMessage message) { + try { + Collection<Part> parts = request.getParts(); + for (Part part : parts) { + DataSource ds = new PartDataSource(part); + message.addAttachment(part.getName(), new DataHandler(ds)); + } + } catch (Exception e) { + throw new RuntimeCamelException("Cannot populate attachments", e); + } + } + + final class PartDataSource implements DataSource { + private final Part part; + + PartDataSource(Part part) { + this.part = part; + } + + @Override + public OutputStream getOutputStream() throws IOException { + return null; + } + + @Override + public String getName() { + return part.getName(); + } + + @Override + public InputStream getInputStream() throws IOException { + return part.getInputStream(); + } + + @Override + public String getContentType() { + return part.getContentType(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/55813f73/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java index 161c275..2332154 100644 --- a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java +++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java @@ -24,6 +24,7 @@ import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.Consumer; import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.http.common.HttpBinding; import org.apache.camel.http.common.HttpCommonComponent; @@ -41,6 +42,7 @@ public class ServletComponent extends HttpCommonComponent implements RestConsume private String servletName = "CamelServlet"; private HttpRegistry httpRegistry; + private boolean attachmentMultipartBinding; public ServletComponent() { super(ServletEndpoint.class); @@ -178,6 +180,19 @@ public class ServletComponent extends HttpCommonComponent implements RestConsume this.httpRegistry = httpRegistry; } + public boolean isAttachmentMultipartBinding() { + return attachmentMultipartBinding; + } + + /** + * Whether to automatic bind multipart/form-data as attachments on the Camel {@link Exchange}. + * <p/> + * This is turn off by default as this may require servet specific configuration to enable this when using Servlet's. + */ + public void setAttachmentMultipartBinding(boolean attachmentMultipartBinding) { + this.attachmentMultipartBinding = attachmentMultipartBinding; + } + @Override public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { http://git-wip-us.apache.org/repos/asf/camel/blob/55813f73/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java index 5beefc3..384a4ec 100644 --- a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java +++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java @@ -22,6 +22,8 @@ import java.net.URISyntaxException; import org.apache.camel.Consumer; import org.apache.camel.Processor; import org.apache.camel.Producer; +import org.apache.camel.http.common.DefaultHttpBinding; +import org.apache.camel.http.common.HttpBinding; import org.apache.camel.http.common.HttpCommonEndpoint; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriEndpoint; @@ -35,6 +37,8 @@ import org.apache.camel.spi.UriPath; syntax = "servlet:contextPath", consumerOnly = true, consumerClass = ServletConsumer.class, label = "http") public class ServletEndpoint extends HttpCommonEndpoint { + private HttpBinding binding; + @UriPath(label = "consumer") @Metadata(required = "true") private String contextPath; @@ -49,6 +53,40 @@ public class ServletEndpoint extends HttpCommonEndpoint { this.contextPath = httpUri.getPath(); } + @Override + public ServletComponent getComponent() { + return (ServletComponent) super.getComponent(); + } + + @Override + public HttpBinding getHttpBinding() { + // make sure we include servlet variant of the http binding + if (this.binding == null) { + // is attachment binding enabled? + if (getComponent().isAttachmentMultipartBinding()) { + this.binding = new AttachmentHttpBinding(); + } else { + this.binding = new DefaultHttpBinding(); + } + this.binding.setTransferException(isTransferException()); + if (getComponent() != null) { + this.binding.setAllowJavaSerializedObject(getComponent().isAllowJavaSerializedObject()); + } + this.binding.setHeaderFilterStrategy(getHeaderFilterStrategy()); + this.binding.setEagerCheckContentAvailable(isEagerCheckContentAvailable()); + this.binding.setMapHttpMessageBody(isMapHttpMessageBody()); + this.binding.setMapHttpMessageHeaders(isMapHttpMessageHeaders()); + this.binding.setMapHttpMessageFormUrlEncodedBody(isMapHttpMessageFormUrlEncodedBody()); + } + return this.binding; + } + + @Override + public void setHttpBinding(HttpBinding binding) { + super.setHttpBinding(binding); + this.binding = binding; + } + public String getContextPath() { return contextPath; } http://git-wip-us.apache.org/repos/asf/camel/blob/55813f73/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletAsyncArquillianTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletAsyncArquillianTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletAsyncArquillianTest.java index 8d50058..7c1a877 100644 --- a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletAsyncArquillianTest.java +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletAsyncArquillianTest.java @@ -32,11 +32,6 @@ import org.junit.runner.RunWith; import static com.jayway.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.equalTo; - -/** - * @author arnaud.deprez - * @since 18/04/16 - */ @RunWith(Arquillian.class) public class ServletAsyncArquillianTest {