Repository: camel
Updated Branches:
  refs/heads/master c09207f59 -> 610adf16f


CAMEL-8752 camel-fop - Allow to set output format easier


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/610adf16
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/610adf16
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/610adf16

Branch: refs/heads/master
Commit: 610adf16fd60dc5779ff7482624a95b28056eab1
Parents: c09207f
Author: Claus Ibsen <davscl...@apache.org>
Authored: Fri May 8 08:25:30 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri May 8 08:25:30 2015 +0200

----------------------------------------------------------------------
 .../camel/component/fop/FopComponent.java       |  9 ++-
 .../apache/camel/component/fop/FopEndpoint.java | 18 +++---
 .../camel/component/fop/FopOutputType.java      | 67 ++++++++++++++++++++
 .../apache/camel/component/fop/FopProducer.java | 32 ++++------
 .../component/fop/utils/OutputFormatEnum.java   | 41 ------------
 .../camel/component/fop/FopComponentTest.java   | 35 ++--------
 .../camel/component/fop/FopOutputTypeTest.java  | 32 ++++++++++
 .../fop/utils/OutputFormatEnumTest.java         | 33 ----------
 8 files changed, 131 insertions(+), 136 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopComponent.java
 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopComponent.java
index 774dd47..f853faa 100644
--- 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopComponent.java
+++ 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopComponent.java
@@ -30,9 +30,12 @@ public class FopComponent extends UriEndpointComponent {
         super(FopEndpoint.class);
     }
 
-    protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters)
-        throws Exception {
-        FopEndpoint endpoint = new FopEndpoint(uri, this, remaining);
+    protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+        FopOutputType type = FopOutputType.asFooOutputType(remaining);
+        if (type == null) {
+            type = 
getCamelContext().getTypeConverter().mandatoryConvertTo(FopOutputType.class, 
remaining);
+        }
+        FopEndpoint endpoint = new FopEndpoint(uri, this, type);
         setProperties(endpoint, parameters);
         return endpoint;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java
 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java
index 1ea5823..8826f02 100644
--- 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java
+++ 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java
@@ -34,23 +34,23 @@ import org.apache.fop.apps.FopFactory;
 /**
  * Represents a Fop endpoint.
  */
-@UriEndpoint(scheme = "fop", title = "FOP", syntax = "fop:outputFormat", 
producerOnly = true, label = "transformation")
+@UriEndpoint(scheme = "fop", title = "FOP", syntax = "fop:outputType", 
producerOnly = true, label = "transformation")
 public class FopEndpoint extends DefaultEndpoint {
 
     @UriPath @Metadata(required = "true")
-    private String outputFormat;
+    private FopOutputType outputType;
     @UriParam
     private String userConfigURL;
     @UriParam
     private FopFactory fopFactory;
 
-    public FopEndpoint(String uri, FopComponent component, String 
outputFormat) {
+    public FopEndpoint(String uri, FopComponent component, FopOutputType 
outputType) {
         super(uri, component);
-        this.outputFormat = outputFormat;
+        this.outputType = outputType;
     }
 
     public Producer createProducer() throws Exception {
-        return new FopProducer(this, fopFactory, outputFormat);
+        return new FopProducer(this, fopFactory, 
outputType.getFormatExtended());
     }
 
     public Consumer createConsumer(Processor processor) throws Exception {
@@ -61,15 +61,15 @@ public class FopEndpoint extends DefaultEndpoint {
         return true;
     }
 
-    public String getOutputFormat() {
-        return outputFormat;
+    public FopOutputType getOutputType() {
+        return outputType;
     }
 
     /**
      * The primary output format is PDF but other output formats are also 
supported.
      */
-    public void setOutputFormat(String outputFormat) {
-        this.outputFormat = outputFormat;
+    public void setOutputType(FopOutputType outputType) {
+        this.outputType = outputType;
     }
 
     public String getUserConfigURL() {

http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopOutputType.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopOutputType.java
 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopOutputType.java
new file mode 100644
index 0000000..6041f28
--- /dev/null
+++ 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopOutputType.java
@@ -0,0 +1,67 @@
+/**
+ * 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.fop;
+
+public enum FopOutputType {
+
+    pdf("application/pdf"),
+    ps("application/postscript"),
+    pcl("application/x-pcl"),
+    png("image/png"),
+    jpeg("image/jpeg"),
+    svg("image/svg+xml"),
+    xml("application/X-fop-areatree"),
+    mif("application/mif"),
+    rtf("application/rtf"),
+    txt("text/plain");
+    
+    private final String outputFormatExtended;
+
+    FopOutputType(String outputFormatExtended) {
+        this.outputFormatExtended = outputFormatExtended;
+    }
+    
+    public String getFormatExtended() {
+        return outputFormatExtended;
+    }
+
+    public static FopOutputType asFooOutputType(String outputFormatExtended) {
+        if ("application/pdf".equalsIgnoreCase(outputFormatExtended)) {
+            return pdf;
+        } else if 
("application/postscript".equalsIgnoreCase(outputFormatExtended)) {
+            return ps;
+        } else if ("application/x-pcl".equalsIgnoreCase(outputFormatExtended)) 
{
+            return pcl;
+        } else if ("image/png".equalsIgnoreCase(outputFormatExtended)) {
+            return png;
+        } else if ("image/jpeg".equalsIgnoreCase(outputFormatExtended)) {
+            return jpeg;
+        } else if ("image/svg+xml".equalsIgnoreCase(outputFormatExtended)) {
+            return svg;
+        } else if 
("application/X-fop-areatree".equalsIgnoreCase(outputFormatExtended)) {
+            return xml;
+        } else if ("application/mif".equalsIgnoreCase(outputFormatExtended)) {
+            return mif;
+        } else if ("application/rtf".equalsIgnoreCase(outputFormatExtended)) {
+            return rtf;
+        } else if ("text/plain".equalsIgnoreCase(outputFormatExtended)) {
+            return txt;
+        }
+
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java
 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java
index a494c3e..a6da1aa 100644
--- 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java
+++ 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java
@@ -18,9 +18,7 @@ package org.apache.camel.component.fop;
 
 import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
-import java.util.Arrays;
 import java.util.Map;
-
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.Transformer;
@@ -30,8 +28,6 @@ import javax.xml.transform.sax.SAXResult;
 import javax.xml.transform.stream.StreamSource;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.RuntimeExchangeException;
-import org.apache.camel.component.fop.utils.OutputFormatEnum;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.fop.apps.FOPException;
@@ -70,15 +66,18 @@ public class FopProducer extends DefaultProducer {
     }
 
     private String getOutputFormat(Exchange exchange) {
-        String headerOutputFormat = 
exchange.getIn().getHeader(FopConstants.CAMEL_FOP_OUTPUT_FORMAT, 
this.outputFormat, String.class);
-        if (headerOutputFormat == null) {
-            throw new RuntimeExchangeException("Missing output format", 
exchange);
-        }
-        if (!isOutputFormatDefined(headerOutputFormat)) {
-            throw new RuntimeExchangeException("The output format is not 
valid", exchange);
+        String headerOutputFormat = 
exchange.getIn().getHeader(FopConstants.CAMEL_FOP_OUTPUT_FORMAT, String.class);
+        if (headerOutputFormat != null) {
+            // it may be a short hand
+            FopOutputType type = 
exchange.getContext().getTypeConverter().tryConvertTo(FopOutputType.class, 
exchange, headerOutputFormat);
+            if (type != null) {
+                return type.getFormatExtended();
+            } else {
+                return headerOutputFormat;
+            }
+        } else {
+            return outputFormat;
         }
-        String outputFormat = 
OutputFormatEnum.valueOf(headerOutputFormat).getFormatExtended();
-        return outputFormat;
     }
 
     private OutputStream transform(FOUserAgent userAgent, String outputFormat, 
Source src)
@@ -113,13 +112,4 @@ public class FopProducer extends DefaultProducer {
         }
     }
     
-    private static boolean isOutputFormatDefined(String test) {
-
-        for (OutputFormatEnum c : OutputFormatEnum.values()) {
-            if (c.name().equals(test)) {
-                return true;
-            }
-        }
-        return false;
-    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/main/java/org/apache/camel/component/fop/utils/OutputFormatEnum.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/utils/OutputFormatEnum.java
 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/utils/OutputFormatEnum.java
deleted file mode 100644
index 67e59b4..0000000
--- 
a/components/camel-fop/src/main/java/org/apache/camel/component/fop/utils/OutputFormatEnum.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * 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.fop.utils;
-
-public enum OutputFormatEnum {
-
-    pdf("application/pdf"),
-    ps("application/postscript"),
-    pcl("application/x-pcl"),
-    png("image/png"),
-    jpeg("image/jpeg"),
-    svg("image/svg+xml"),
-    xml("application/X-fop-areatree"),
-    mif("application/mif"),
-    rtf("application/rtf"),
-    txt("text/plain");
-    
-    private final String outputFormatExtended;
-    
-    OutputFormatEnum(String outputFormatExtended) {
-        this.outputFormatExtended = outputFormatExtended;
-    }
-    
-    public String getFormatExtended() {
-        return outputFormatExtended;
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopComponentTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopComponentTest.java
 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopComponentTest.java
index 14ba095..7111ac3 100644
--- 
a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopComponentTest.java
+++ 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopComponentTest.java
@@ -18,7 +18,6 @@ package org.apache.camel.component.fop;
 
 import java.io.FileInputStream;
 
-import org.apache.camel.CamelExecutionException;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Exchange;
 import org.apache.camel.Produce;
@@ -38,9 +37,6 @@ public class FopComponentTest extends CamelTestSupport {
     @Produce(uri = "direct:start")
     protected ProducerTemplate template;
     
-    @Produce(uri = "direct:error")
-    protected ProducerTemplate templateError;
-
     @Override
     @Before
     public void setUp() throws Exception {
@@ -67,36 +63,17 @@ public class FopComponentTest extends CamelTestSupport {
         assertEquals("Header value is lost!", "bar", 
exchange.getIn().getHeader("foo"));
     }
     
-    @Test
-    public void testEnumError() throws Exception {
-        resultEndpoint.expectedMessageCount(1);
-        FileInputStream inputStream = new 
FileInputStream("src/test/data/xml/data.xml");
-
-        try { 
-            templateError.sendBody(inputStream);
-        } catch (CamelExecutionException e) { 
-            assertTrue(e.getCause().getMessage().contains("The output format 
is not valid on the exchange"));
-        }
-    }
-
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
                 from("direct:start")
-                        .to("xslt:xslt/template.xsl")
-                        .setHeader("foo", constant("bar"))
-                        .to("fop:pdf")
-                        .setHeader(Exchange.FILE_NAME, constant("result.pdf"))
-                        .to("file:target/data")
-                        .to("mock:result");
-                from("direct:error")
-                        .to("xslt:xslt/template.xsl")
-                        .setHeader("foo", constant("bar"))
-                         .to("fop:test")
-                         .setHeader(Exchange.FILE_NAME, constant("result.pdf"))
-                         .to("file:target/data")
-                         .to("mock:result");
+                    .to("xslt:xslt/template.xsl")
+                    .setHeader("foo", constant("bar"))
+                    .to("fop:pdf")
+                    .setHeader(Exchange.FILE_NAME, constant("result.pdf"))
+                    .to("file:target/data")
+                    .to("mock:result");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopOutputTypeTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopOutputTypeTest.java
 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopOutputTypeTest.java
new file mode 100644
index 0000000..889fbf3
--- /dev/null
+++ 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopOutputTypeTest.java
@@ -0,0 +1,32 @@
+/**
+ * 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.fop;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class FopOutputTypeTest {
+
+    @Test
+    public void testOutputFormatEnum() {
+        String pdfExtended = FopOutputType.valueOf("pdf").getFormatExtended();
+        assertEquals("application/pdf", pdfExtended);
+        String pngExtended = FopOutputType.valueOf("png").getFormatExtended();
+        assertEquals("image/png", pngExtended);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/610adf16/components/camel-fop/src/test/java/org/apache/camel/component/fop/utils/OutputFormatEnumTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/test/java/org/apache/camel/component/fop/utils/OutputFormatEnumTest.java
 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/utils/OutputFormatEnumTest.java
deleted file mode 100644
index 8a2b3ca..0000000
--- 
a/components/camel-fop/src/test/java/org/apache/camel/component/fop/utils/OutputFormatEnumTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * 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.fop.utils;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-
-public final class OutputFormatEnumTest {
-
-    @Test
-    public void testOutputFormatEnum() {
-        String pdfExtended = 
OutputFormatEnum.valueOf("pdf").getFormatExtended();
-        assertEquals("application/pdf", pdfExtended);
-        String pngExtended = 
OutputFormatEnum.valueOf("png").getFormatExtended();
-        assertEquals("image/png", pngExtended);
-    }
-}

Reply via email to