Search in sources :

Example 1 with RuntimeExchangeException

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

the class DefaultExchangeHolderTest method testFileNotSupported.

public void testFileNotSupported() throws Exception {
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody(new File("src/test/resources/log4j2.properties"));
    try {
        DefaultExchangeHolder.marshal(exchange);
        fail("Should have thrown exception");
    } catch (RuntimeExchangeException e) {
    // expected
    }
}
Also used : Exchange(org.apache.camel.Exchange) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) File(java.io.File)

Example 2 with RuntimeExchangeException

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

the class HttpHelper method createMethod.

/**
     * Creates the HttpMethod to use to call the remote server, often either its GET or POST.
     *
     * @param exchange  the exchange
     * @return the created method
     * @throws URISyntaxException
     */
public static HttpMethods createMethod(Exchange exchange, HttpCommonEndpoint 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) {
        // in case the URI string contains unsafe characters
        uriString = UnsafeUriCharactersEncoder.encodeHttpURI(uriString);
        URI uri = new URI(uriString);
        queryString = uri.getQuery();
    }
    if (queryString == null) {
        queryString = endpoint.getHttpUri().getRawQuery();
    }
    HttpMethods answer;
    if (endpoint.getHttpMethod() != null) {
        // endpoint configured take precedence
        answer = endpoint.getHttpMethod();
    } else {
        // compute what method to use either GET or POST (header take precedence)
        HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, HttpMethods.class);
        if (m != null) {
            // always use what end-user provides in a header
            answer = m;
        } else if (queryString != null) {
            // if a query string is provided then use GET
            answer = HttpMethods.GET;
        } else {
            // fallback to POST if we have payload, otherwise GET
            answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
        }
    }
    return answer;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 3 with RuntimeExchangeException

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

the class HttpMethodHelper method createMethod.

/**
     * Creates the HttpMethod to use to call the remote server, often either its GET or POST.
     *
     * @param exchange the exchange
     * @return the created method
     * @throws URISyntaxException 
     */
public static HttpMethods createMethod(Exchange exchange, HttpEndpoint 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) {
        // in case the URI string contains unsafe characters
        uriString = UnsafeUriCharactersEncoder.encodeHttpURI(uriString);
        URI uri = new URI(uriString);
        queryString = uri.getQuery();
    }
    if (queryString == null) {
        queryString = endpoint.getHttpUri().getRawQuery();
    }
    // compute what method to use either GET or POST
    HttpMethods answer;
    if (endpoint.getHttpMethod() != null) {
        // endpoint configured take precedence
        answer = HttpMethods.valueOf(endpoint.getHttpMethod().name());
    } else {
        // compute what method to use either GET or POST (header take precedence)
        HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, HttpMethods.class);
        if (m != null) {
            // always use what end-user provides in a header
            answer = m;
        } else if (queryString != null) {
            // if a query string is provided then use GET
            answer = HttpMethods.GET;
        } else {
            // fallback to POST if we have payload, otherwise GET
            answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
        }
    }
    return answer;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) HttpMethods(org.apache.camel.component.http4.HttpMethods) URI(java.net.URI) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URISyntaxException(java.net.URISyntaxException)

Example 4 with RuntimeExchangeException

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

the class DefaultJdbcPrepareStatementStrategy method createPopulateIterator.

@Override
public Iterator<?> createPopulateIterator(final String query, final String preparedQuery, final int expectedParams, final Exchange exchange, final Object value) throws SQLException {
    Map<?, ?> map = null;
    if (exchange.getIn().hasHeaders()) {
        if (exchange.getIn().getHeader(JdbcConstants.JDBC_PARAMETERS) != null) {
            // header JDBC_PARAMETERS takes precedence over regular headers
            map = exchange.getIn().getHeader(JdbcConstants.JDBC_PARAMETERS, Map.class);
        } else {
            map = exchange.getIn().getHeaders();
        }
    }
    final Map<?, ?> headerMap = map;
    if (hasNamedParameters(query)) {
        // create an iterator that returns the value in the named order
        try {
            return new Iterator<Object>() {

                private NamedQueryParser parser = new NamedQueryParser(query);

                private Object next;

                private boolean done;

                private boolean preFetched;

                @Override
                public boolean hasNext() {
                    if (!done && !preFetched) {
                        next();
                        preFetched = true;
                    }
                    return !done;
                }

                @Override
                public Object next() {
                    if (!preFetched) {
                        String key = parser.next();
                        if (key == null) {
                            done = true;
                            return null;
                        }
                        // the key is expected to exist, if not report so end user can see this
                        boolean contains = headerMap != null && headerMap.containsKey(key);
                        if (!contains) {
                            throw new RuntimeExchangeException("Cannot find key [" + key + "] in message body or headers to use when setting named parameter in query [" + query + "]", exchange);
                        }
                        next = headerMap.get(key);
                    }
                    preFetched = false;
                    return next;
                }

                @Override
                public void remove() {
                // noop
                }
            };
        } catch (Exception e) {
            throw new SQLException("Error iterating parameters for the query: " + query, e);
        }
    } else {
        // just use a regular iterator
        return exchange.getContext().getTypeConverter().convertTo(Iterator.class, headerMap != null ? headerMap.values() : null);
    }
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) SQLException(java.sql.SQLException) Iterator(java.util.Iterator) Map(java.util.Map) SQLException(java.sql.SQLException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 5 with RuntimeExchangeException

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

the class JmsProducer method processInOut.

protected boolean processInOut(final Exchange exchange, final AsyncCallback callback) {
    final org.apache.camel.Message in = exchange.getIn();
    String destinationName = in.getHeader(JmsConstants.JMS_DESTINATION_NAME, String.class);
    // remove the header so it wont be propagated
    in.removeHeader(JmsConstants.JMS_DESTINATION_NAME);
    if (destinationName == null) {
        destinationName = endpoint.getDestinationName();
    }
    Destination destination = in.getHeader(JmsConstants.JMS_DESTINATION, Destination.class);
    // remove the header so it wont be propagated
    in.removeHeader(JmsConstants.JMS_DESTINATION);
    if (destination == null) {
        destination = endpoint.getDestination();
    }
    if (destination != null) {
        // prefer to use destination over destination name
        destinationName = null;
    }
    initReplyManager();
    // the request timeout can be overruled by a header otherwise the endpoint configured value is used
    final long timeout = exchange.getIn().getHeader(JmsConstants.JMS_REQUEST_TIMEOUT, endpoint.getRequestTimeout(), long.class);
    final JmsConfiguration configuration = endpoint.getConfiguration();
    // when using message id as correlation id, we need at first to use a provisional correlation id
    // which we then update to the real JMSMessageID when the message has been sent
    // this is done with the help of the MessageSentCallback
    final boolean msgIdAsCorrId = configuration.isUseMessageIDAsCorrelationID();
    final String provisionalCorrelationId = msgIdAsCorrId ? getUuidGenerator().generateUuid() : null;
    MessageSentCallback messageSentCallback = null;
    if (msgIdAsCorrId) {
        messageSentCallback = new UseMessageIdAsCorrelationIdMessageSentCallback(replyManager, provisionalCorrelationId, timeout);
    }
    final String correlationProperty = configuration.getCorrelationProperty();
    final String correlationPropertyToUse = ofNullable(correlationProperty).orElse("JMSCorrelationID");
    final String originalCorrelationId = in.getHeader(correlationPropertyToUse, String.class);
    boolean generateFreshCorrId = (ObjectHelper.isEmpty(originalCorrelationId) && !msgIdAsCorrId) || (originalCorrelationId != null && originalCorrelationId.startsWith(GENERATED_CORRELATION_ID_PREFIX));
    if (generateFreshCorrId) {
        // we append the 'Camel-' prefix to know it was generated by us
        in.setHeader(correlationPropertyToUse, GENERATED_CORRELATION_ID_PREFIX + getUuidGenerator().generateUuid());
    }
    MessageCreator messageCreator = new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            Message answer = endpoint.getBinding().makeJmsMessage(exchange, in, session, null);
            Destination replyTo = null;
            String replyToOverride = configuration.getReplyToOverride();
            if (replyToOverride != null) {
                replyTo = resolveOrCreateDestination(replyToOverride, session);
            } else {
                // get the reply to destination to be used from the reply manager
                replyTo = replyManager.getReplyTo();
            }
            if (replyTo == null) {
                throw new RuntimeExchangeException("Failed to resolve replyTo destination", exchange);
            }
            JmsMessageHelper.setJMSReplyTo(answer, replyTo);
            replyManager.setReplyToSelectorHeader(in, answer);
            String correlationId = determineCorrelationId(answer, provisionalCorrelationId);
            replyManager.registerReply(replyManager, exchange, callback, originalCorrelationId, correlationId, timeout);
            if (correlationProperty != null) {
                replyManager.setCorrelationProperty(correlationProperty);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Using {}: {}, JMSReplyTo destination: {}, with request timeout: {} ms.", new Object[] { correlationPropertyToUse, correlationId, replyTo, timeout });
            }
            LOG.trace("Created javax.jms.Message: {}", answer);
            return answer;
        }
    };
    doSend(true, destinationName, destination, messageCreator, messageSentCallback);
    // continue routing asynchronously (reply will be processed async when its received)
    return false;
}
Also used : Destination(javax.jms.Destination) Message(javax.jms.Message) UseMessageIdAsCorrelationIdMessageSentCallback(org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback) UseMessageIdAsCorrelationIdMessageSentCallback(org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback) MessageCreator(org.springframework.jms.core.MessageCreator) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) Session(javax.jms.Session)

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