Search in sources :

Example 6 with RuntimeExchangeException

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

the class UndertowHelper 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, UndertowEndpoint 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) {
        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.startsWith("/")) {
            path = path.substring(1);
        }
        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) HttpString(io.undertow.util.HttpString) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URISyntaxException(java.net.URISyntaxException)

Example 7 with RuntimeExchangeException

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

the class UndertowHelper method createMethod.

/**
     * Creates the HttpMethod to use to call the remote server, often either its GET or POST.
     */
public static HttpString createMethod(Exchange exchange, UndertowEndpoint endpoint, boolean hasPayload) throws URISyntaxException {
    // is a query string provided in the endpoint URI or in a header (header
    // overrules endpoint)
    String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
    // We need also check the HTTP_URI header query part
    String uriString = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
    // resolve placeholders in uriString
    try {
        uriString = exchange.getContext().resolvePropertyPlaceholders(uriString);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uriString, exchange, e);
    }
    if (uriString != null) {
        URI uri = new URI(uriString);
        queryString = uri.getQuery();
    }
    if (queryString == null) {
        queryString = endpoint.getHttpURI().getRawQuery();
    }
    // compute what method to use either GET or POST
    HttpString answer;
    String m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, String.class);
    if (m != null) {
        // always use what end-user provides in a header
        answer = new HttpString(m);
    } else if (queryString != null) {
        // if a query string is provided then use GET
        answer = Methods.GET;
    } else {
        // fallback to POST if we have payload, otherwise GET
        answer = hasPayload ? Methods.POST : Methods.GET;
    }
    return answer;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) HttpString(io.undertow.util.HttpString) URI(java.net.URI) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URISyntaxException(java.net.URISyntaxException) HttpString(io.undertow.util.HttpString)

Example 8 with RuntimeExchangeException

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

the class XmppPrivateChatProducer method process.

public void process(Exchange exchange) {
    // make sure we are connected
    try {
        if (connection == null) {
            connection = endpoint.createConnection();
        }
        if (!connection.isConnected()) {
            this.reconnect();
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not connect to XMPP server.", e);
    }
    String participant = endpoint.getParticipant();
    String thread = endpoint.getChatId();
    if (participant == null) {
        participant = getParticipant();
    } else {
        thread = "Chat:" + participant + ":" + endpoint.getUser();
    }
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    Chat chat = getOrCreateChat(chatManager, participant, thread);
    Message message = null;
    try {
        message = new Message();
        message.setTo(participant);
        message.setThread(thread);
        message.setType(Message.Type.normal);
        endpoint.getBinding().populateXmppMessage(message, exchange);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sending XMPP message to {} from {} : {}", new Object[] { participant, endpoint.getUser(), message.getBody() });
        }
        chat.sendMessage(message);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Could not send XMPP message to " + participant + " from " + endpoint.getUser() + " : " + message + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, e);
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) Chat(org.jivesoftware.smack.Chat) ChatManager(org.jivesoftware.smack.ChatManager) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 9 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 {
    String 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 10 with RuntimeExchangeException

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

the class XmppGroupChatProducer method process.

public void process(Exchange exchange) {
    if (connection == null) {
        try {
            connection = endpoint.createConnection();
        } catch (Exception e) {
            throw new RuntimeExchangeException("Could not connect to XMPP server.", exchange, e);
        }
    }
    if (chat == null) {
        try {
            initializeChat();
        } catch (Exception e) {
            throw new RuntimeExchangeException("Could not initialize XMPP chat.", exchange, e);
        }
    }
    Message message = chat.createMessage();
    message.setTo(room);
    message.setFrom(endpoint.getUser());
    endpoint.getBinding().populateXmppMessage(message, exchange);
    try {
        // make sure we are connected
        if (!connection.isConnected()) {
            this.reconnect();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sending XMPP message: {}", message.getBody());
        }
        chat.sendMessage(message);
        // must invoke nextMessage to consume the response from the server
        // otherwise the client local queue will fill up (CAMEL-1467)
        chat.pollMessage();
    } catch (Exception e) {
        throw new RuntimeExchangeException("Could not send XMPP message: " + message, exchange, e);
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException)

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