Search in sources :

Example 21 with MultiPayloadAdaptrisMessage

use of com.adaptris.core.MultiPayloadAdaptrisMessage in project interlok by adaptris.

the class MultiPayloadXmlTransformServiceTest method testObtainUrlWithEmptyMetadataValueInMessage.

@Test
public void testObtainUrlWithEmptyMetadataValueInMessage() throws Exception {
    MultiPayloadXmlTransformService service = new MultiPayloadXmlTransformService();
    MultiPayloadAdaptrisMessage msg = (MultiPayloadAdaptrisMessage) new MultiPayloadMessageFactory().newMessage();
    msg.addMetadata("key", "");
    try {
        service.setUrl(URL);
        service.setMetadataKey("key");
        LifecycleHelper.init(service);
        assertEquals(URL, service.obtainUrlToUse(msg));
    } finally {
        LifecycleHelper.close(service);
    }
}
Also used : MultiPayloadMessageFactory(com.adaptris.core.MultiPayloadMessageFactory) MultiPayloadAdaptrisMessage(com.adaptris.core.MultiPayloadAdaptrisMessage) Test(org.junit.Test)

Example 22 with MultiPayloadAdaptrisMessage

use of com.adaptris.core.MultiPayloadAdaptrisMessage in project interlok by adaptris.

the class ForEach method doService.

/**
 * {@inheritDoc}.
 */
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    ThreadPoolExecutor executor = null;
    try {
        log.info("Starting for-each");
        if (!(msg instanceof MultiPayloadAdaptrisMessage)) {
            log.warn("Message [{}] is not a multi-payload message!", msg.getUniqueId());
            iterate(msg);
        } else {
            MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) msg;
            int threads = threadCount;
            if (threads == 0) {
                // use as many threads as necessary
                threads = message.getPayloadCount();
            }
            log.trace("Using {} thread{}", threads, threads > 1 ? "s" : "");
            executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads);
            for (String id : message.getPayloadIDs()) {
                try {
                    message.switchPayload(id);
                    AdaptrisMessage each = DefaultMessageFactory.getDefaultInstance().newMessage(message, null);
                    each.setPayload(message.getPayload());
                    executor.execute(() -> iterate(each));
                } catch (CloneNotSupportedException e) {
                    log.error("Could not clone message [{}]", id, e);
                }
            }
        }
    } finally {
        if (executor != null) {
            executor.shutdown();
            try {
                while (!executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {
                /* wait for all threads to complete */
                }
            } catch (InterruptedException e) {
                log.warn("Interrupted while waiting for tasks to finish!", e);
            }
        }
        log.info("Finished for-each");
    }
}
Also used : MultiPayloadAdaptrisMessage(com.adaptris.core.MultiPayloadAdaptrisMessage) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MultiPayloadAdaptrisMessage(com.adaptris.core.MultiPayloadAdaptrisMessage) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 23 with MultiPayloadAdaptrisMessage

use of com.adaptris.core.MultiPayloadAdaptrisMessage in project interlok by adaptris.

the class MultiPayloadStreamInputParameterTest method testExtract.

@Test
public void testExtract() throws Exception {
    MultiPayloadStreamInputParameter parameter = new MultiPayloadStreamInputParameter();
    parameter.setPayloadId(ID);
    MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) new MultiPayloadMessageFactory().newMessage(ID, PAYLOAD);
    try (InputStream stream = parameter.extract(message)) {
        for (byte b : PAYLOAD) {
            assertEquals(b, stream.read());
        }
    }
    try (InputStream stream = parameter.extract(ID, message)) {
        for (byte b : PAYLOAD) {
            assertEquals(b, stream.read());
        }
    }
    try (InputStream stream = parameter.extract(null, message)) {
        for (byte b : PAYLOAD) {
            assertEquals(b, stream.read());
        }
    }
}
Also used : MultiPayloadMessageFactory(com.adaptris.core.MultiPayloadMessageFactory) InputStream(java.io.InputStream) MultiPayloadAdaptrisMessage(com.adaptris.core.MultiPayloadAdaptrisMessage) Test(org.junit.Test)

Example 24 with MultiPayloadAdaptrisMessage

use of com.adaptris.core.MultiPayloadAdaptrisMessage in project interlok by adaptris.

the class MultiPayloadStreamOutputParameterTest method testInsertNullStream.

@Test
public void testInsertNullStream() throws Exception {
    MultiPayloadMessageFactory messageFactory = new MultiPayloadMessageFactory();
    MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage();
    try {
        InputStreamWithEncoding stream = new InputStreamWithEncoding(null, ENCODING);
        MultiPayloadStreamOutputParameter parameter = new MultiPayloadStreamOutputParameter();
        parameter.insert(stream, ID, message);
    } catch (Exception e) {
    // expected
    }
}
Also used : MultiPayloadMessageFactory(com.adaptris.core.MultiPayloadMessageFactory) MultiPayloadAdaptrisMessage(com.adaptris.core.MultiPayloadAdaptrisMessage) Test(org.junit.Test)

Example 25 with MultiPayloadAdaptrisMessage

use of com.adaptris.core.MultiPayloadAdaptrisMessage in project interlok by adaptris.

the class MultiPayloadStreamOutputParameterTest method testInsert.

@Test
public void testInsert() throws Exception {
    MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) new MultiPayloadMessageFactory().newMessage();
    try (InputStream in = new ByteArrayInputStream(PAYLOAD.getBytes())) {
        InputStreamWithEncoding stream = new InputStreamWithEncoding(in, ENCODING);
        MultiPayloadStreamOutputParameter parameter = new MultiPayloadStreamOutputParameter();
        parameter.insert(stream, ID, message);
    }
    assertEquals(PAYLOAD, message.getContent(ID));
}
Also used : MultiPayloadMessageFactory(com.adaptris.core.MultiPayloadMessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MultiPayloadAdaptrisMessage(com.adaptris.core.MultiPayloadAdaptrisMessage) Test(org.junit.Test)

Aggregations

MultiPayloadAdaptrisMessage (com.adaptris.core.MultiPayloadAdaptrisMessage)48 Test (org.junit.Test)44 MultiPayloadMessageFactory (com.adaptris.core.MultiPayloadMessageFactory)24 ServiceException (com.adaptris.core.ServiceException)5 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)3 StxTransformerFactory (com.adaptris.util.text.xml.StxTransformerFactory)3 XsltTransformerFactory (com.adaptris.util.text.xml.XsltTransformerFactory)3 TransformerException (javax.xml.transform.TransformerException)3 KeyValuePair (com.adaptris.util.KeyValuePair)2 XPath (com.adaptris.util.text.xml.XPath)2 UncheckedXPathException (net.sf.saxon.trans.UncheckedXPathException)2 Document (org.w3c.dom.Document)2 CoreException (com.adaptris.core.CoreException)1 KeyValuePairList (com.adaptris.util.KeyValuePairList)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1