engines/servicemix-pdf/src/main/java/org/apache/servicemix/pdfcomposer/PdfComposerEndpoint.java [211:287]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    protected void handleProviderExchange(MessageExchange exchange) throws Exception {
        // fault message
        if (exchange.getFault() != null) {
            done(exchange);
            return;
        }
        
        // 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 
        if (exchange.getMessage("in") == null) {
            throw new MessagingException("No \"in\" message.");
        }
        
        // get the in message
        NormalizedMessage in = exchange.getMessage("in");
        // unmarshal the PDF Composer request in the "in" message
        PdfComposerRequest request = marshaler.unmarshal(in);
        
        // if the template is not contained in the request, fall back to the default one
        String templateToUse = request.getTemplate();
        if (templateToUse == null) {
            templateToUse = this.template;
        }
        if (templateToUse == null) {
            throw new IllegalArgumentException("No template found in the \"in\" message or in the endpoint descriptor.");
        }
        if (templatesDir != null) {
            templateToUse = templatesDir + "/" + templateToUse;
        }
        
        // load PDF template
        PdfReader templateReader = new PdfReader(templateToUse);
        
        // create a stamper to populate the target document
        PdfStamper stamper;
        PipedInputStream stream = new PipedInputStream();
        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();
        // replace the field
        for (PdfComposerDataField field:request.getData()) {
            fields.setField(field.getName(), field.getValue());
        }
        
        // close the stamper
        stamper.setFormFlattening(true);
        stamper.close();
        
        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);
    }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



engines/servicemix-pdf/src/main/java/org/apache/servicemix/pdf/PdfComposerEndpoint.java [210:286]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    protected void handleProviderExchange(MessageExchange exchange) throws Exception {
        // fault message
        if (exchange.getFault() != null) {
            done(exchange);
            return;
        }
        
        // 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 
        if (exchange.getMessage("in") == null) {
            throw new MessagingException("No \"in\" message.");
        }
        
        // get the in message
        NormalizedMessage in = exchange.getMessage("in");
        // unmarshal the PDF Composer request in the "in" message
        PdfComposerRequest request = marshaler.unmarshal(in);
        
        // if the template is not contained in the request, fall back to the default one
        String templateToUse = request.getTemplate();
        if (templateToUse == null) {
            templateToUse = this.template;
        }
        if (templateToUse == null) {
            throw new IllegalArgumentException("No template found in the \"in\" message or in the endpoint descriptor.");
        }
        if (templatesDir != null) {
            templateToUse = templatesDir + "/" + templateToUse;
        }
        
        // load PDF template
        PdfReader templateReader = new PdfReader(templateToUse);
        
        // create a stamper to populate the target document
        PdfStamper stamper;
        PipedInputStream stream = new PipedInputStream();
        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();
        // replace the field
        for (PdfComposerDataField field:request.getData()) {
            fields.setField(field.getName(), field.getValue());
        }
        
        // close the stamper
        stamper.setFormFlattening(true);
        stamper.close();
        
        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);
    }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



