Repository: camel
Updated Branches:
  refs/heads/master e1b6592a1 -> e42434dac


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/e42434da
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e42434da
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e42434da

Branch: refs/heads/master
Commit: e42434dac80951af1a07b3214c12e4657036bb38
Parents: e1b6592
Author: Andrea Cosentino <anco...@gmail.com>
Authored: Thu May 7 15:31:44 2015 +0200
Committer: Andrea Cosentino <anco...@gmail.com>
Committed: Thu May 7 21:33:55 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/component/fop/FopProducer.java | 23 +++++++++--
 .../component/fop/utils/OutputFormatEnum.java   | 41 ++++++++++++++++++++
 .../camel/component/fop/FopComponentTest.java   | 26 ++++++++++++-
 .../camel/component/fop/FopEndpointTest.java    | 14 +++----
 .../fop/utils/OutputFormatEnumTest.java         | 33 ++++++++++++++++
 5 files changed, 124 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e42434da/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 d41b6d1..a494c3e 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,7 +18,9 @@ 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;
@@ -29,6 +31,7 @@ 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;
@@ -67,12 +70,14 @@ public class FopProducer extends DefaultProducer {
     }
 
     private String getOutputFormat(Exchange exchange) {
-        String outputFormat = exchange.getIn()
-                .getHeader(FopConstants.CAMEL_FOP_OUTPUT_FORMAT, 
this.outputFormat, String.class);
-        if (outputFormat == null) {
+        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 outputFormat = 
OutputFormatEnum.valueOf(headerOutputFormat).getFormatExtended();
         return outputFormat;
     }
 
@@ -107,4 +112,14 @@ public class FopProducer extends DefaultProducer {
             IntrospectionSupport.setProperties(userAgent, parameters);
         }
     }
+    
+    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/e42434da/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
new file mode 100644
index 0000000..67e59b4
--- /dev/null
+++ 
b/components/camel-fop/src/main/java/org/apache/camel/component/fop/utils/OutputFormatEnum.java
@@ -0,0 +1,41 @@
+/**
+ * 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/e42434da/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 d371027..14ba095 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,6 +18,7 @@ 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;
@@ -36,6 +37,9 @@ public class FopComponentTest extends CamelTestSupport {
 
     @Produce(uri = "direct:start")
     protected ProducerTemplate template;
+    
+    @Produce(uri = "direct:error")
+    protected ProducerTemplate templateError;
 
     @Override
     @Before
@@ -62,6 +66,18 @@ public class FopComponentTest extends CamelTestSupport {
         Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
         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() {
@@ -70,11 +86,17 @@ public class FopComponentTest extends CamelTestSupport {
                 from("direct:start")
                         .to("xslt:xslt/template.xsl")
                         .setHeader("foo", constant("bar"))
-                        .to("fop:application/pdf")
+                        .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");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/e42434da/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java
 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java
index ed9a6e4..884300b 100644
--- 
a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java
+++ 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java
@@ -32,7 +32,7 @@ public class FopEndpointTest extends CamelTestSupport {
 
     @Test
     public void generatePdfFromXslfoWithSpecificText() throws Exception {
-        Endpoint endpoint = context().getEndpoint("fop:application/pdf");
+        Endpoint endpoint = context().getEndpoint("fop:pdf");
         Producer producer = endpoint.createProducer();
         Exchange exchange = new DefaultExchange(context);
         exchange.getIn().setBody(FopHelper.decorateTextWithXSLFO("Test 
Content"));
@@ -46,7 +46,7 @@ public class FopEndpointTest extends CamelTestSupport {
     @Test
     public void specifyCustomUserConfigurationFile() throws Exception {
         FopEndpoint customConfiguredEndpoint = context()
-                
.getEndpoint("fop:application/pdf?userConfigURL=file:src/test/data/conf/testcfg.xml",
+                
.getEndpoint("fop:pdf?userConfigURL=file:src/test/data/conf/testcfg.xml",
                         FopEndpoint.class);
         float customSourceResolution = 
customConfiguredEndpoint.getFopFactory().getSourceResolution();
         assertEquals(96.0, customSourceResolution, 0.1);
@@ -55,7 +55,7 @@ public class FopEndpointTest extends CamelTestSupport {
     @Test
     public void specifyCustomUserConfigurationFileClasspath() throws Exception 
{
         FopEndpoint customConfiguredEndpoint = context()
-                
.getEndpoint("fop:application/pdf?userConfigURL=myconf/testcfg.xml",
+                .getEndpoint("fop:pdf?userConfigURL=myconf/testcfg.xml",
                         FopEndpoint.class);
         float customSourceResolution = 
customConfiguredEndpoint.getFopFactory().getSourceResolution();
         assertEquals(96.0, customSourceResolution, 0.1);
@@ -63,7 +63,7 @@ public class FopEndpointTest extends CamelTestSupport {
 
     @Test
     public void setPDFRenderingMetadataPerDocument() throws Exception {
-        Endpoint endpoint = context().getEndpoint("fop:application/pdf");
+        Endpoint endpoint = context().getEndpoint("fop:pdf");
         Producer producer = endpoint.createProducer();
         Exchange exchange = new DefaultExchange(context);
         exchange.getIn().setHeader("CamelFop.Render.Creator", "Test User");
@@ -77,7 +77,7 @@ public class FopEndpointTest extends CamelTestSupport {
 
     @Test
     public void encryptPdfWithUserPassword() throws Exception {
-        Endpoint endpoint = context().getEndpoint("fop:application/pdf");
+        Endpoint endpoint = context().getEndpoint("fop:pdf");
         Producer producer = endpoint.createProducer();
         Exchange exchange = new DefaultExchange(context);
         exchange.getIn().setHeader("CamelFop.Encrypt.userPassword", "secret");
@@ -90,11 +90,11 @@ public class FopEndpointTest extends CamelTestSupport {
 
     @Test
     public void overridePdfOutputFormatToPlainText() throws Exception {
-        String defaultOutputFormat = "application/pdf";
+        String defaultOutputFormat = "pdf";
         Endpoint endpoint = context().getEndpoint("fop:" + 
defaultOutputFormat);
         Producer producer = endpoint.createProducer();
         Exchange exchange = new DefaultExchange(context);
-        exchange.getIn().setHeader(FopConstants.CAMEL_FOP_OUTPUT_FORMAT, 
org.apache.xmlgraphics.util.MimeConstants.MIME_PLAIN_TEXT);
+        exchange.getIn().setHeader(FopConstants.CAMEL_FOP_OUTPUT_FORMAT, 
"txt");
         exchange.getIn().setBody(FopHelper.decorateTextWithXSLFO("Test 
Content"));
 
         producer.process(exchange);

http://git-wip-us.apache.org/repos/asf/camel/blob/e42434da/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
new file mode 100644
index 0000000..8a2b3ca
--- /dev/null
+++ 
b/components/camel-fop/src/test/java/org/apache/camel/component/fop/utils/OutputFormatEnumTest.java
@@ -0,0 +1,33 @@
+/**
+ * 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