Last active
December 21, 2015 14:09
-
-
Save geissebn/6317468 to your computer and use it in GitHub Desktop.
demo code for the jadice knowledge base article at https://levigo.de/info/display/JKB/Erstellung+eines+Inhaltsverzeichnisses+bei+der+E-Mail-Verarbeitung
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2014, levigo holding gmbh. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of levigo holding gmbh nor the names of its contributors | |
may be used to endorse or promote products derived from this software | |
without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
import java.io.ByteArrayOutputStream; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.URI; | |
import java.util.Set; | |
import java.util.concurrent.CountDownLatch; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
import com.levigo.jadice.server.Job; | |
import com.levigo.jadice.server.JobListenerAdapter; | |
import com.levigo.jadice.server.Node; | |
import com.levigo.jadice.server.Job.State; | |
import com.levigo.jadice.server.client.jms.JMSJobFactory; | |
import com.levigo.jadice.server.javamail.MessageRFC822Node; | |
import com.levigo.jadice.server.mail.AttachmentDirectory; | |
import com.levigo.jadice.server.mail.MailBodyCreatorNode; | |
import com.levigo.jadice.server.nodes.ScriptNode; | |
import com.levigo.jadice.server.nodes.StreamInputNode; | |
import com.levigo.jadice.server.nodes.StreamOutputNode; | |
import com.levigo.jadice.server.pdfmerge.PDFMergeNode; | |
import com.levigo.jadice.server.shared.types.Stream; | |
import com.levigo.jadice.server.util.TraceListener; | |
import com.levigo.jadice.server.util.Util; | |
public class AttachmentDirectoryDemo { | |
/** | |
* Thread-safe receiver of a {@link AttachmentDirectory} | |
*/ | |
private static class AttachmentDirectoryReceiver extends JobListenerAdapter { | |
private final CountDownLatch latch = new CountDownLatch(1); | |
private final ScriptNode outerScriptNode; | |
private AttachmentDirectory attachmentDirectory; | |
public AttachmentDirectoryReceiver(ScriptNode scriptNode) { | |
outerScriptNode = scriptNode; | |
} | |
@Override | |
public void stateChanged(Job job, State oldState, State newState) { | |
// Count down the latch if job fails. AttechmentDirectory will never be received | |
if (newState == State.FAILED || newState == State.ABORTED) { | |
latch.countDown(); | |
} | |
} | |
@Override | |
public void subPipelineCreated(Job job, Node parent, Set<? extends Node> createdNodes) { | |
// Only care about the ScriptNode created here, | |
// not be those ones created for mails attached within the mail | |
if (!parent.getUUID().equals(outerScriptNode.getUUID())) { | |
return; | |
} | |
for (Node node : createdNodes) { | |
if (node instanceof MailBodyCreatorNode) { | |
this.attachmentDirectory = ((MailBodyCreatorNode) node).getAttachmentDirectory(); | |
latch.countDown(); | |
return; | |
} | |
} | |
} | |
public AttachmentDirectory getAttachmentDirectory() throws InterruptedException { | |
latch.await(); | |
return attachmentDirectory; | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
// | |
final InputStream mail = new FileInputStream("C:/temp/demo.eml"); | |
final OutputStream result = new ByteArrayOutputStream(); // resulting pdf not relevant for this example | |
final AttachmentDirectory attachmentDirectory = runMailConversion(mail, result); | |
prettyPrint(attachmentDirectory, 0); | |
} | |
private static AttachmentDirectory runMailConversion(InputStream mail, OutputStream target) throws Exception { | |
// e-mail conversion as described in jadice server developer manual | |
final Job job = new JMSJobFactory(new ActiveMQConnectionFactory("tcp://jadice-server:61616"), "JS.REQUEST").createJob(); | |
final StreamInputNode inputNode = new StreamInputNode(); | |
final ScriptNode scriptNode = new ScriptNode(); | |
scriptNode.setScript(new URI("resource:email-conversion/EmailConversion.groovy")); | |
final PDFMergeNode pdfMergeNode = new PDFMergeNode(); | |
final StreamOutputNode outputNode = new StreamOutputNode(); | |
// Create and attach AttachmentDirectoryReceiver as JobListener | |
final AttachmentDirectoryReceiver attachmentDirectoryReceiver = new AttachmentDirectoryReceiver(scriptNode); | |
job.addJobListener(attachmentDirectoryReceiver); | |
// Workflow configuration and submit workflow | |
job.attach(inputNode | |
.appendSuccessor(new MessageRFC822Node()) | |
.appendSuccessor(scriptNode) | |
.appendSuccessor(pdfMergeNode) | |
.appendSuccessor(outputNode)); | |
job.addJobListener(new TraceListener()); | |
job.submit(); | |
// Submit mail stream | |
inputNode.addStream(mail); | |
inputNode.complete(); | |
for (Stream s : outputNode.getStreamBundle()) { | |
Util.copyAndClose(s.getInputStream(), target); | |
} | |
// get the attachment directory | |
return attachmentDirectoryReceiver.getAttachmentDirectory(); | |
} | |
/** | |
* Simple demo of how to use the attachment directory and process it recursively | |
* | |
* @param dir the current {@link AttachmentDirectory} | |
* @param level the level of recursion | |
*/ | |
private static void prettyPrint(AttachmentDirectory dir, int level) { | |
for (AttachmentDirectory subDir : dir.getSubDirectories()) { | |
final String string = String.format("%"+(level+1)+"s %s (%s)", "", subDir.getFilename(), subDir.getType()); | |
System.out.println(string); | |
prettyPrint(subDir, level + 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment