Search in sources :

Example 11 with RuntimeExchangeException

use of org.apache.camel.RuntimeExchangeException in project camel by apache.

the class XmppPubSubProducer method process.

public void process(Exchange exchange) throws Exception {
    try {
        if (connection == null) {
            connection = endpoint.createConnection();
        }
        // make sure we are connected
        if (!connection.isConnected()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Reconnecting to: " + XmppEndpoint.getConnectionMessage(connection));
            }
            connection.connect();
        }
    } catch (XMPPException e) {
        throw new RuntimeExchangeException("Cannot connect to XMPP Server: " + ((connection != null) ? XmppEndpoint.getConnectionMessage(connection) : endpoint.getHost()), exchange, e);
    }
    try {
        Object body = exchange.getIn().getBody(Object.class);
        if (body instanceof PubSub) {
            PubSub pubsubpacket = (PubSub) body;
            endpoint.getBinding().populateXmppPacket(pubsubpacket, exchange);
            exchange.getIn().setHeader(XmppConstants.DOC_HEADER, pubsubpacket);
            connection.sendPacket(pubsubpacket);
        } else {
            throw new Exception("Message does not contain a pubsub packet");
        }
    } catch (XMPPException xmppe) {
        throw new RuntimeExchangeException("Cannot send XMPP pubsub: from " + endpoint.getUser() + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, xmppe);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot send XMPP pubsub: from " + endpoint.getUser() + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, e);
    }
}
Also used : PubSub(org.jivesoftware.smackx.pubsub.packet.PubSub) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 12 with RuntimeExchangeException

use of org.apache.camel.RuntimeExchangeException in project camel by apache.

the class NettyHttpHelper method createURL.

/**
     * Creates the URL to invoke.
     *
     * @param exchange the exchange
     * @param endpoint the endpoint
     * @return the URL to invoke
     */
public static String createURL(Exchange exchange, NettyHttpEndpoint endpoint) throws URISyntaxException {
    // rest producer may provide an override url to be used which we should discard if using (hence the remove)
    String uri = (String) exchange.getIn().removeHeader(Exchange.REST_HTTP_URI);
    if (uri == null) {
        uri = endpoint.getEndpointUri();
    }
    // resolve placeholders in uri
    try {
        uri = exchange.getContext().resolvePropertyPlaceholders(uri);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uri, exchange, e);
    }
    // append HTTP_PATH to HTTP_URI if it is provided in the header
    String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
    // NOW the HTTP_PATH is just related path, we don't need to trim it
    if (path != null) {
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        if (path.length() > 0) {
            // inject the dynamic path before the query params, if there are any
            int idx = uri.indexOf("?");
            // if there are no query params
            if (idx == -1) {
                // make sure that there is exactly one "/" between HTTP_URI and HTTP_PATH
                uri = uri.endsWith("/") ? uri : uri + "/";
                uri = uri.concat(path);
            } else {
                // there are query params, so inject the relative path in the right place
                String base = uri.substring(0, idx);
                base = base.endsWith("/") ? base : base + "/";
                base = base.concat(path);
                uri = base.concat(uri.substring(idx));
            }
        }
    }
    // ensure uri is encoded to be valid
    uri = UnsafeUriCharactersEncoder.encodeHttpURI(uri);
    return uri;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 13 with RuntimeExchangeException

use of org.apache.camel.RuntimeExchangeException in project camel by apache.

the class HttpHelper method createURL.

/**
     * Creates the URL to invoke.
     *
     * @param exchange the exchange
     * @param endpoint the endpoint
     * @return the URL to invoke
     */
public static String createURL(Exchange exchange, HttpCommonEndpoint endpoint) {
    // rest producer may provide an override url to be used which we should discard if using (hence the remove)
    String uri = (String) exchange.getIn().removeHeader(Exchange.REST_HTTP_URI);
    if (uri == null && !(endpoint.isBridgeEndpoint())) {
        uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
    }
    if (uri == null) {
        uri = endpoint.getHttpUri().toASCIIString();
    }
    // resolve placeholders in uri
    try {
        uri = exchange.getContext().resolvePropertyPlaceholders(uri);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uri, exchange, e);
    }
    // append HTTP_PATH to HTTP_URI if it is provided in the header
    String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
    // NOW the HTTP_PATH is just related path, we don't need to trim it
    if (path != null) {
        if (path.length() > 1 && path.startsWith("/")) {
            path = path.substring(1);
        }
        if (path.length() > 0) {
            // inject the dynamic path before the query params, if there are any
            int idx = uri.indexOf("?");
            // if there are no query params
            if (idx == -1) {
                // make sure that there is exactly one "/" between HTTP_URI and HTTP_PATH
                uri = uri.endsWith("/") || path.startsWith("/") ? uri : uri + "/";
                uri = uri.concat(path);
            } else {
                // there are query params, so inject the relative path in the right place
                String base = uri.substring(0, idx);
                base = base.endsWith("/") ? base : base + "/";
                base = base.concat(path);
                uri = base.concat(uri.substring(idx));
            }
        }
    }
    // ensure uri is encoded to be valid
    uri = UnsafeUriCharactersEncoder.encodeHttpURI(uri);
    return uri;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 14 with RuntimeExchangeException

use of org.apache.camel.RuntimeExchangeException in project camel by apache.

the class CMISProducerTest method failCreatingFolderAtNonExistingPath.

@Test
public void failCreatingFolderAtNonExistingPath() throws Exception {
    String existingFolderStructure = "/No/Path/Here";
    Exchange exchange = createExchangeWithInBody(null);
    exchange.getIn().getHeaders().put(PropertyIds.NAME, "folder1");
    exchange.getIn().getHeaders().put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
    exchange.getIn().getHeaders().put(CamelCMISConstants.CMIS_FOLDER_PATH, existingFolderStructure);
    template.send(exchange);
    assertTrue(exchange.getException() instanceof RuntimeExchangeException);
}
Also used : Exchange(org.apache.camel.Exchange) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) Test(org.junit.Test)

Example 15 with RuntimeExchangeException

use of org.apache.camel.RuntimeExchangeException in project camel by apache.

the class AhcHelper method doCreateURL.

private static String doCreateURL(Exchange exchange, AhcEndpoint endpoint) {
    String uri = null;
    if (!(endpoint.isBridgeEndpoint())) {
        uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
    }
    if (uri == null) {
        uri = endpoint.getHttpUri().toASCIIString();
    }
    // resolve placeholders in uri
    try {
        uri = exchange.getContext().resolvePropertyPlaceholders(uri);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uri, exchange, e);
    }
    // append HTTP_PATH to HTTP_URI if it is provided in the header
    String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
    if (path != null) {
        if (path.startsWith("/")) {
            URI baseURI;
            String baseURIString = exchange.getIn().getHeader(Exchange.HTTP_BASE_URI, String.class);
            try {
                if (baseURIString == null) {
                    if (exchange.getFromEndpoint() != null) {
                        baseURIString = exchange.getFromEndpoint().getEndpointUri();
                    } else {
                        // will set a default one for it
                        baseURIString = "/";
                    }
                }
                baseURI = new URI(baseURIString);
                String basePath = baseURI.getPath();
                if (path.startsWith(basePath)) {
                    path = path.substring(basePath.length());
                    if (path.startsWith("/")) {
                        path = path.substring(1);
                    }
                } else {
                    throw new RuntimeExchangeException("Cannot analyze the Exchange.HTTP_PATH header, due to: cannot find the right HTTP_BASE_URI", exchange);
                }
            } catch (Throwable t) {
                throw new RuntimeExchangeException("Cannot analyze the Exchange.HTTP_PATH header, due to: " + t.getMessage(), exchange, t);
            }
        }
        if (path.length() > 0) {
            // HTTP_PATH
            if (!uri.endsWith("/")) {
                uri = uri + "/";
            }
            uri = uri.concat(path);
        }
    }
    // ensure uri is encoded to be valid
    uri = UnsafeUriCharactersEncoder.encodeHttpURI(uri);
    return uri;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

RuntimeExchangeException (org.apache.camel.RuntimeExchangeException)16 URISyntaxException (java.net.URISyntaxException)8 IOException (java.io.IOException)7 URI (java.net.URI)4 Exchange (org.apache.camel.Exchange)3 XMPPException (org.jivesoftware.smack.XMPPException)3 HttpString (io.undertow.util.HttpString)2 ProtocolException (java.net.ProtocolException)2 SmackException (org.jivesoftware.smack.SmackException)2 Message (org.jivesoftware.smack.packet.Message)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Destination (javax.jms.Destination)1 Message (javax.jms.Message)1 Session (javax.jms.Session)1 HttpMethods (org.apache.camel.component.http4.HttpMethods)1 UseMessageIdAsCorrelationIdMessageSentCallback (org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback)1