servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r956109 - /servicemix/components/engines/servicemix-pdfcomposer/trunk/src/main/java/org/apache/servicemix/pdfcomposer/PdfComposerEndpoint.java
Date Fri, 18 Jun 2010 19:51:41 GMT
Author: jbonofre
Date: Fri Jun 18 19:51:40 2010
New Revision: 956109

URL: http://svn.apache.org/viewvc?rev=956109&view=rev
Log:
Manage InOnly MEP with outputDir.

Modified:
    servicemix/components/engines/servicemix-pdfcomposer/trunk/src/main/java/org/apache/servicemix/pdfcomposer/PdfComposerEndpoint.java

Modified: servicemix/components/engines/servicemix-pdfcomposer/trunk/src/main/java/org/apache/servicemix/pdfcomposer/PdfComposerEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-pdfcomposer/trunk/src/main/java/org/apache/servicemix/pdfcomposer/PdfComposerEndpoint.java?rev=956109&r1=956108&r2=956109&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-pdfcomposer/trunk/src/main/java/org/apache/servicemix/pdfcomposer/PdfComposerEndpoint.java
(original)
+++ servicemix/components/engines/servicemix-pdfcomposer/trunk/src/main/java/org/apache/servicemix/pdfcomposer/PdfComposerEndpoint.java
Fri Jun 18 19:51:40 2010
@@ -16,11 +16,14 @@
  */
 package org.apache.servicemix.pdfcomposer;
 
+import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
 
 import javax.jbi.management.DeploymentException;
 import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
 import javax.jbi.messaging.InOut;
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
@@ -54,7 +57,8 @@ public class PdfComposerEndpoint extends
     public final static String DEFAULT_WSDL = "servicemix-pdfcomposer.wsdl"; // the default
abstract WSDL
     
     private String template; // the template can be static (define in the endpoint descriptor)
or provided in the "in" message
-    private String templatesBasedir = "."; // the location where to looking for template
files
+    private String templatesDir = "."; // the location where to looking for template files
+    private String outputDir = null; // in case of InOnly exchange, write file to the output
directory
     private Resource wsdl; // the abstract WSDL describing the endpoint behavior
     private PdfComposerMarshalerSupport marshaler = new JaxbPdfComposerMarshaler();
     
@@ -74,20 +78,38 @@ public class PdfComposerEndpoint extends
         this.template = template;
     }
     
-    public String getTemplatesBasedir() {
-        return this.templatesBasedir;
+    public String getTemplatesDir() {
+        return this.templatesDir;
     }
     
     /**
      * <p>
-     * This attribute specifies the basedir where to looking for template files.
+     * This attribute specifies the directory where to looking for template files.
      * </p>
      * <i>&nbsp;&nbsp;&nbsp;The default value is <code>.</code>.
      * 
-     * @param templatesBasedir the basedir path.
+     * @param templatesDir the basedir path.
      */
-    public void setTemplatesBasedir(String templatesBasedir) {
-        this.templatesBasedir = templatesBasedir;
+    public void setTemplatesDir(String templatesDir) {
+        this.templatesDir = templatesDir;
+    }
+    
+    public String getOutputDir() {
+        return this.outputDir;
+    }
+    
+    /**
+     * <p>
+     * This attribute specifies the output directory where the PDF composer generates resulting
PDF files.
+     * It's used when the incoming message exchange is in-only, in the case of in-out, the
resulting PDF
+     * is send in out message.
+     * </p>
+     * <i>&nbsp;&nbsp;&nbsp;The default value is <code>null</code>.
+     * 
+     * @param outputDir the PDF output directory.
+     */
+    public void setOutputDir(String outputDir) {
+        this.outputDir = outputDir;
     }
     
     public Resource getWsdl() {
@@ -193,9 +215,9 @@ public class PdfComposerEndpoint extends
             return;
         }
         
-        // check if we have an InOut MEP
-        if (!(exchange instanceof InOut)) {
-            throw new IllegalStateException("PdfComposer only supports InOut MEP.");
+        // check if we have an InOnly MEP and outputDirectory attribute define
+        if (exchange instanceof InOnly && outputDir == null) {
+            throw new IllegalStateException("InOnly message exchange received but the outputDir
attribute is not defined. The outputDir attribute is mandatory in case of InOnly MEP.");
         }
         
         // check if we have an in message 
@@ -214,21 +236,26 @@ public class PdfComposerEndpoint extends
             templateToUse = this.template;
         }
         if (templateToUse == null) {
-            throw new IllegalArgumentException("No template provided in the in message or
in the endpoint descriptor.");
+            throw new IllegalArgumentException("No template found in the \"in\" message or
in the endpoint descriptor.");
         }
-        if (templatesBasedir != null) {
-            templateToUse = templatesBasedir + "/" + templateToUse;
+        if (templatesDir != null) {
+            templateToUse = templatesDir + "/" + templateToUse;
         }
         
         // load PDF template
         PdfReader templateReader = new PdfReader(templateToUse);
         
-        // create "out" message
-        NormalizedMessage out = exchange.createMessage();
-        
-        // create a stamper to populate the target document using pipe
+        // create a stamper to populate the target document
+        PdfStamper stamper;
         PipedInputStream stream = new PipedInputStream();
-        PdfStamper stamper = new PdfStamper(templateReader, new PipedOutputStream(stream));
+        if (exchange instanceof InOut) {
+            // when we have an InOut, we use a pipe
+            stamper = new PdfStamper(templateReader, new PipedOutputStream(stream));
+        } else {
+            // when we have an InOnly, we directly write a file in the output directory
+            FileOutputStream fileStream = new FileOutputStream(outputDir + "/test.pdf");
+            stamper = new PdfStamper(templateReader, fileStream);
+        }
         
         // get the acrofields
         AcroFields fields = stamper.getAcroFields();
@@ -241,12 +268,20 @@ public class PdfComposerEndpoint extends
         stamper.setFormFlattening(true);
         stamper.close();
         
-        // set the "out" message content with the piped stream
-        out.setContent(new StreamSource(stream));
-        
-        // set the "out" message of the exchange
-        exchange.setMessage(out, "out");
+        if (exchange instanceof InOut) {
+            // create "out" message
+            NormalizedMessage out = exchange.createMessage();
+            // set the "out" message content with the piped stream
+            out.setContent(new StreamSource(stream));
         
+            // set the "out" message of the exchange
+            exchange.setMessage(out, "out");
+        } else {
+            // the exchange is InOnly, Robust InOnly, In Optional Out
+            // set the exchange as DONE
+            exchange.setStatus(ExchangeStatus.DONE);
+        }
+
         // send back the exchange
         send(exchange);
     }



Mime
View raw message