Search in sources :

Example 6 with ProduceException

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

the class SimpleHttpProducer method readResponse.

private void readResponse(HttpSession httpReply, AdaptrisMessage reply) throws IOException, CoreException {
    int responseCode = httpReply.getResponseLine().getResponseCode();
    if (!ignoreServerResponseCode()) {
        if (responseCode < 200 || responseCode > 299) {
            throw new ProduceException("Failed to send payload, got " + responseCode);
        }
    }
    if (getEncoder() != null) {
        copy(getEncoder().readMessage(httpReply), reply);
    } else {
        OutputStream out = reply.getOutputStream();
        InputStream in = httpReply.getResponseMessage().getInputStream();
        StreamUtil.copyStream(in, out);
        reply.addMetadata(new MetadataElement(CoreConstants.HTTP_PRODUCER_RESPONSE_CODE, String.valueOf(responseCode)));
        out.close();
        in.close();
    }
    return;
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) MetadataElement(com.adaptris.core.MetadataElement) ProduceException(com.adaptris.core.ProduceException)

Example 7 with ProduceException

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

the class FtpProducer method doProduce.

@Override
public void doProduce(AdaptrisMessage msg, String endpoint) throws ProduceException {
    FileTransferConnection conn = retrieveConnection(FileTransferConnection.class);
    FileTransferClient client = null;
    FileNameCreator creator = filenameCreatorToUse();
    try {
        client = conn.connect(endpoint);
        String dirRoot = conn.getDirectoryRoot(endpoint);
        String fileName = creator.createName(msg);
        String destFilename = dirRoot + destDirectory + SLASH + fileName;
        String buildFilename = dirRoot + buildDirectory + SLASH + fileName;
        if (conn.additionalDebug()) {
            log.trace("buildFilename=[{}], destFilename=[{}]", buildFilename, destFilename);
        } else {
            log.debug("destFilename=[{}]", destFilename);
        }
        msg.addMetadata(CoreConstants.PRODUCED_NAME_KEY, fileName);
        if (getEncoder() != null) {
            byte[] bytesToWrite = encode(msg);
            client.put(bytesToWrite, buildFilename);
        } else {
            try (InputStream in = msg.getInputStream()) {
                client.put(in, buildFilename);
            }
        }
        client.rename(buildFilename, destFilename);
    } catch (Exception e) {
        throw new ProduceException(e);
    } finally {
        conn.disconnect(client);
    }
}
Also used : FileNameCreator(com.adaptris.core.FileNameCreator) FileTransferClient(com.adaptris.filetransfer.FileTransferClient) InputStream(java.io.InputStream) ProduceException(com.adaptris.core.ProduceException) CoreException(com.adaptris.core.CoreException) ProduceException(com.adaptris.core.ProduceException)

Example 8 with ProduceException

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

the class StandardHttpProducer method doRequest.

private AdaptrisMessage doRequest(AdaptrisMessage msg, String endpointUrl, long timeout, AdaptrisMessage reply) throws ProduceException {
    try {
        URL url = new URL(endpointUrl);
        authenticator.setup(url.toString(), msg, null);
        HttpURLConnection http = configure(configureTimeouts((HttpURLConnection) url.openConnection(), timeout), msg);
        if (authenticator instanceof HttpURLConnectionAuthenticator) {
            ((HttpURLConnectionAuthenticator) authenticator).configureConnection(http);
        }
        writeData(getMethod(msg), msg, http);
        handleResponse(http, reply);
    } catch (Exception e) {
        throw ExceptionHelper.wrapProduceException(e);
    } finally {
        authenticator.close();
    }
    return reply;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) ProduceException(com.adaptris.core.ProduceException) InterlokException(com.adaptris.interlok.InterlokException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException)

Example 9 with ProduceException

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

the class StandardHttpProducer method fail.

private void fail(int responseCode, InputStreamWithEncoding data) throws ProduceException {
    if (log.isTraceEnabled()) {
        try {
            try (OutputStream slf4j = new Slf4jLoggingOutputStream(log, Slf4jLoggingOutputStream.LogLevel.TRACE);
                InputStream in = new BufferedInputStream(data.inputStream);
                PrintStream out = data.encoding == null ? new PrintStream(slf4j) : new PrintStream(slf4j, false, data.encoding)) {
                out.println("Error Data from remote server :");
                IOUtils.copy(in, out);
            }
        } catch (IOException e) {
            log.trace("No Error Data available");
        }
    }
    throw new ProduceException("Failed to send payload, got " + responseCode);
}
Also used : PrintStream(java.io.PrintStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Slf4jLoggingOutputStream(com.adaptris.util.stream.Slf4jLoggingOutputStream) IOException(java.io.IOException) Slf4jLoggingOutputStream(com.adaptris.util.stream.Slf4jLoggingOutputStream) ProduceException(com.adaptris.core.ProduceException)

Example 10 with ProduceException

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

the class FsMessageProducerTest method testProduceWithNoCreateDir.

@Test
public void testProduceWithNoCreateDir() throws Exception {
    String subdir = new GuidGenerator().safeUUID();
    FsProducer fs = createProducer(subdir);
    fs.setCreateDirs(false);
    File parentDir = FsHelper.createFileReference(FsHelper.createUrlFromString(PROPERTIES.getProperty(BASE_KEY), true));
    try {
        start(fs);
        fs.produce(new DefaultMessageFactory().newMessage(TEXT));
        fail();
    } catch (ProduceException expected) {
    } finally {
        FileUtils.deleteQuietly(new File(parentDir, subdir));
        stop(fs);
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) GuidGenerator(com.adaptris.util.GuidGenerator) File(java.io.File) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Aggregations

ProduceException (com.adaptris.core.ProduceException)29 Test (org.junit.Test)17 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)13 CoreException (com.adaptris.core.CoreException)9 MockMessageProducer (com.adaptris.core.stubs.MockMessageProducer)7 Channel (com.adaptris.core.Channel)6 StandaloneProducer (com.adaptris.core.StandaloneProducer)6 IOException (java.io.IOException)6 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)5 File (java.io.File)5 GuidGenerator (com.adaptris.util.GuidGenerator)4 JMSException (javax.jms.JMSException)4 PtpProducer (com.adaptris.core.jms.PtpProducer)3 MockChannel (com.adaptris.core.stubs.MockChannel)3 InputStream (java.io.InputStream)3 Destination (javax.jms.Destination)3 FileNameCreator (com.adaptris.core.FileNameCreator)2 MetadataElement (com.adaptris.core.MetadataElement)2 MetadataFileNameCreator (com.adaptris.core.MetadataFileNameCreator)2 ServiceException (com.adaptris.core.ServiceException)2