use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class SimulatorTest method assertRespondsWith.
protected void assertRespondsWith(final String value, String containedText) throws InvalidPayloadException {
Exchange response = template.request("direct:a", new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
in.setBody("answer");
in.setHeader("cheese", value);
}
});
assertNotNull("Should receive a response!", response);
String text = response.getOut().getMandatoryBody(String.class);
assertStringContains(text, containedText);
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class DefaultHttpBinding method doWriteDirectResponse.
protected void doWriteDirectResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
// if content type is serialized Java object, then serialize and write it to the response
String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class);
if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
if (allowJavaSerializedObject || isTransferException()) {
try {
Object object = message.getMandatoryBody(Serializable.class);
HttpHelper.writeObjectToServletResponse(response, object);
// object is written so return
return;
} catch (InvalidPayloadException e) {
throw new IOException(e);
}
} else {
throw new RuntimeCamelException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed");
}
}
// prefer streaming
InputStream is = null;
if (checkChunked(message, exchange)) {
is = message.getBody(InputStream.class);
} else {
// try to use input stream first, so we can copy directly
if (!isText(contentType)) {
is = exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, message.getBody());
}
}
if (is != null) {
ServletOutputStream os = response.getOutputStream();
if (!checkChunked(message, exchange)) {
CachedOutputStream stream = new CachedOutputStream(exchange);
try {
// copy directly from input stream to the cached output stream to get the content length
int len = copyStream(is, stream, response.getBufferSize());
// we need to setup the length if message is not chucked
response.setContentLength(len);
OutputStream current = stream.getCurrentStream();
if (current instanceof ByteArrayOutputStream) {
if (LOG.isDebugEnabled()) {
LOG.debug("Streaming (direct) response in non-chunked mode with content-length {}");
}
ByteArrayOutputStream bos = (ByteArrayOutputStream) current;
bos.writeTo(os);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Streaming response in non-chunked mode with content-length {} and buffer size: {}", len, len);
}
copyStream(stream.getInputStream(), os, len);
}
} finally {
IOHelper.close(is, os);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Streaming response in chunked mode with buffer size {}", response.getBufferSize());
}
copyStream(is, os, response.getBufferSize());
}
} else {
// not convertable as a stream so fallback as a String
String data = message.getBody(String.class);
if (data != null) {
// set content length and encoding before we write data
String charset = IOHelper.getCharsetName(exchange, true);
final int dataByteLength = data.getBytes(charset).length;
response.setCharacterEncoding(charset);
response.setContentLength(dataByteLength);
if (LOG.isDebugEnabled()) {
LOG.debug("Writing response in non-chunked mode as plain text with content-length {} and buffer size: {}", dataByteLength, response.getBufferSize());
}
try {
response.getWriter().print(data);
} finally {
response.getWriter().flush();
}
}
}
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class TokenXMLExpressionIterator method doEvaluate.
/**
* Strategy to evaluate the exchange
*
* @param exchange the exchange
* @param closeStream whether to close the stream before returning from this method.
* @return the evaluated value
*/
protected Object doEvaluate(Exchange exchange, boolean closeStream) {
InputStream in = null;
try {
in = exchange.getIn().getMandatoryBody(InputStream.class);
// we may read from a file, and want to support custom charset defined on the exchange
String charset = IOHelper.getCharsetName(exchange);
return createIterator(exchange, in, charset);
} catch (InvalidPayloadException e) {
exchange.setException(e);
// must close input stream
IOHelper.close(in);
return null;
} finally {
if (closeStream) {
IOHelper.close(in);
}
}
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class BeanProxyNoBindingTest method testBeanProxyFailureInvalidReturnType.
public void testBeanProxyFailureInvalidReturnType() throws Exception {
Endpoint endpoint = context.getEndpoint("direct:start");
OrderService service = ProxyHelper.createProxy(endpoint, false, OrderService.class);
try {
service.invalidReturnType("<order type=\"beer\">Carlsberg</order>");
fail("Should have thrown exception");
} catch (Exception e) {
// expected
InvalidPayloadException cause = assertIsInstanceOf(InvalidPayloadException.class, e.getCause());
assertEquals(Integer.class, cause.getType());
}
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class VelocitySetHeaderTest method assertRespondsWith.
protected void assertRespondsWith(final String value, String expectedBody) throws InvalidPayloadException, InterruptedException {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived("fruit", value);
mock.expectedBodiesReceived(expectedBody);
template.request("direct:start", new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
in.setBody(value);
}
});
mock.assertIsSatisfied();
}
Aggregations