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;
}
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;
}
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);
}
}
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;
}
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);
}
}
Aggregations