use of org.apache.camel.CamelExchangeException in project camel by apache.
the class DefaultAhcBinding method populateCookieHeaders.
private void populateCookieHeaders(RequestBuilder builder, AhcEndpoint endpoint, Exchange exchange, URI uri) throws CamelExchangeException {
if (endpoint.getCookieHandler() != null) {
try {
Map<String, List<String>> cookieHeaders = endpoint.getCookieHandler().loadCookies(exchange, uri);
for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
String key = entry.getKey();
if (entry.getValue().size() > 0) {
// use the default toString of a ArrayList to create in the form [xxx, yyy]
// if multi valued, for a single value, then just output the value as is
String s = entry.getValue().size() > 1 ? entry.getValue().toString() : entry.getValue().get(0);
builder.addHeader(key, s);
}
}
} catch (IOException e) {
throw new CamelExchangeException("Error loading cookies", exchange, e);
}
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class HttpProducer method createRequestEntity.
/**
* Creates a holder object for the data to send to the remote server.
*
* @param exchange the exchange with the IN message with data to send
* @return the data holder
* @throws CamelExchangeException is thrown if error creating RequestEntity
*/
protected RequestEntity createRequestEntity(Exchange exchange) throws CamelExchangeException {
Message in = exchange.getIn();
if (in.getBody() == null) {
return null;
}
RequestEntity answer = in.getBody(RequestEntity.class);
if (answer == null) {
try {
Object data = in.getBody();
if (data != null) {
String contentType = ExchangeHelper.getContentType(exchange);
if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
if (!getEndpoint().getComponent().isAllowJavaSerializedObject()) {
throw new CamelExchangeException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange);
}
// serialized java object
Serializable obj = in.getMandatoryBody(Serializable.class);
// write object to output stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HttpHelper.writeObjectToStream(bos, obj);
answer = new ByteArrayRequestEntity(bos.toByteArray(), HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
IOHelper.close(bos);
} else if (data instanceof File || data instanceof GenericFile) {
// file based (could potentially also be a FTP file etc)
File file = in.getBody(File.class);
if (file != null) {
answer = new FileRequestEntity(file, contentType);
}
} else if (data instanceof String) {
// be a bit careful with String as any type can most likely be converted to String
// so we only do an instanceof check and accept String if the body is really a String
// do not fallback to use the default charset as it can influence the request
// (for example application/x-www-form-urlencoded forms being sent)
String charset = IOHelper.getCharsetName(exchange, false);
answer = new StringRequestEntity((String) data, contentType, charset);
}
// fallback as input stream
if (answer == null) {
// force the body as an input stream since this is the fallback
InputStream is = in.getMandatoryBody(InputStream.class);
answer = new InputStreamRequestEntity(is, contentType);
}
}
} catch (UnsupportedEncodingException e) {
throw new CamelExchangeException("Error creating RequestEntity from message body", exchange, e);
} catch (IOException e) {
throw new CamelExchangeException("Error serializing message body", exchange, e);
}
}
return answer;
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class HystrixProcessorCommand method run.
@Override
protected Message run() throws Exception {
LOG.debug("Running processor: {} with exchange: {}", processor, exchange);
// prepare a copy of exchange so downstream processors don't cause side-effects if they mutate the exchange
// in case Hystrix timeout processing and continue with the fallback etc
Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false, false);
try {
// process the processor until its fully done
// (we do not hav any hystrix callback to leverage so we need to complete all work in this run method)
processor.process(copy);
} catch (Exception e) {
copy.setException(e);
}
// and therefore we need this thread to not do anymore if fallback is already in process
if (fallbackInUse.get()) {
LOG.debug("Exiting run command as fallback is already in use processing exchange: {}", exchange);
return null;
}
// remember any hystrix execution exception which for example can be triggered by a hystrix timeout
Throwable cause = getExecutionException();
synchronized (lock) {
// and therefore we need this thread to not do anymore if fallback is already in process
if (fallbackInUse.get()) {
LOG.debug("Exiting run command as fallback is already in use processing exchange: {}", exchange);
return null;
}
// and copy the result
ExchangeHelper.copyResults(exchange, copy);
// is fallback enabled
Boolean fallbackEnabled = getProperties().fallbackEnabled().get();
// because hystrix may have caused this command to fail due timeout or something else
if (cause != null) {
exchange.setException(new CamelExchangeException("Hystrix execution exception occurred while processing Exchange", exchange, cause));
}
// and Camel will detect the exception anyway
if (fallback != null || fallbackCommand != null) {
if (fallbackEnabled == null || fallbackEnabled && exchange.getException() != null) {
// throwing exception will cause hystrix to execute fallback
throw exchange.getException();
}
}
LOG.debug("Running processor: {} with exchange: {} done", processor, exchange);
return exchange.hasOut() ? exchange.getOut() : exchange.getIn();
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class KafkaProducer method createRecorder.
@SuppressWarnings("unchecked")
protected Iterator<ProducerRecord> createRecorder(Exchange exchange) throws CamelException {
String topic = endpoint.getConfiguration().getTopic();
if (!endpoint.isBridgeEndpoint()) {
topic = exchange.getIn().getHeader(KafkaConstants.TOPIC, topic, String.class);
}
if (topic == null) {
throw new CamelExchangeException("No topic key set", exchange);
}
// endpoint take precedence over header configuration
final Integer partitionKey = endpoint.getConfiguration().getPartitionKey() != null ? endpoint.getConfiguration().getPartitionKey() : exchange.getIn().getHeader(KafkaConstants.PARTITION_KEY, Integer.class);
final boolean hasPartitionKey = partitionKey != null;
// endpoint take precedence over header configuration
Object key = endpoint.getConfiguration().getKey() != null ? endpoint.getConfiguration().getKey() : exchange.getIn().getHeader(KafkaConstants.KEY);
final Object messageKey = key != null ? tryConvertToSerializedType(exchange, key, endpoint.getConfiguration().getKeySerializerClass()) : null;
final boolean hasMessageKey = messageKey != null;
Object msg = exchange.getIn().getBody();
// is the message body a list or something that contains multiple values
Iterator<Object> iterator = null;
if (msg instanceof Iterable) {
iterator = ((Iterable<Object>) msg).iterator();
} else if (msg instanceof Iterator) {
iterator = (Iterator<Object>) msg;
}
if (iterator != null) {
final Iterator<Object> msgList = iterator;
final String msgTopic = topic;
return new Iterator<ProducerRecord>() {
@Override
public boolean hasNext() {
return msgList.hasNext();
}
@Override
public ProducerRecord next() {
// must convert each entry of the iterator into the value according to the serializer
Object next = msgList.next();
Object value = tryConvertToSerializedType(exchange, next, endpoint.getConfiguration().getSerializerClass());
if (hasPartitionKey && hasMessageKey) {
return new ProducerRecord(msgTopic, partitionKey, key, value);
} else if (hasMessageKey) {
return new ProducerRecord(msgTopic, key, value);
} else {
return new ProducerRecord(msgTopic, value);
}
}
@Override
public void remove() {
msgList.remove();
}
};
}
// must convert each entry of the iterator into the value according to the serializer
Object value = tryConvertToSerializedType(exchange, msg, endpoint.getConfiguration().getSerializerClass());
ProducerRecord record;
if (hasPartitionKey && hasMessageKey) {
record = new ProducerRecord(topic, partitionKey, key, value);
} else if (hasMessageKey) {
record = new ProducerRecord(topic, key, value);
} else {
record = new ProducerRecord(topic, value);
}
return Collections.singletonList(record).iterator();
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class MinaHelper method writeBody.
/**
* Asynchronously writes the given body to MINA session. Will wait at most for
* 10 seconds until the body has been written.
*
* @param session the MINA session
* @param body the body to write (send)
* @param exchange the exchange
* @throws CamelExchangeException is thrown if the body could not be written for some reasons
* (eg remote connection is closed etc.)
*/
public static void writeBody(IoSession session, Object body, Exchange exchange) throws CamelExchangeException {
// the write operation is asynchronous. Use WriteFuture to wait until the session has been written
WriteFuture future = session.write(body);
// must use a timeout (we use 10s) as in some very high performance scenarios a write can cause
// thread hanging forever
LOG.trace("Waiting for write to complete for body: {} using session: {}", body, session);
future.join(10000L);
if (!future.isWritten()) {
throw new CamelExchangeException("Cannot write body: " + body + " using session: " + session, exchange);
}
}
Aggregations