use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class DefaultJettyHttpBinding method extractResponseBody.
protected Object extractResponseBody(Exchange exchange, JettyContentExchange httpExchange) throws IOException {
Map<String, String> headers = getSimpleMap(httpExchange.getResponseHeaders());
String contentType = headers.get(Exchange.CONTENT_TYPE);
// if content type is serialized java object, then de-serialize it to a Java object
if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
// only deserialize java if allowed
if (isAllowJavaSerializedObject() || isTransferException()) {
try {
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, httpExchange.getResponseContentBytes());
return HttpHelper.deserializeJavaObjectFromStream(is, exchange.getContext());
} catch (Exception e) {
throw new RuntimeCamelException("Cannot deserialize body to Java object", e);
}
} else {
// empty body
return null;
}
} else {
// just grab the raw content body
return httpExchange.getBody();
}
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class JettyHttpComponent method enableMultipartFilter.
private void enableMultipartFilter(HttpCommonEndpoint endpoint, Server server, String connectorKey) throws Exception {
ServletContextHandler context = server.getChildHandlerByClass(ServletContextHandler.class);
CamelContext camelContext = this.getCamelContext();
FilterHolder filterHolder = new FilterHolder();
filterHolder.setInitParameter("deleteFiles", "true");
if (ObjectHelper.isNotEmpty(camelContext.getProperty(TMP_DIR))) {
File file = new File(camelContext.getProperty(TMP_DIR));
if (!file.isDirectory()) {
throw new RuntimeCamelException("The temp file directory of camel-jetty is not exists, please recheck it with directory name :" + camelContext.getProperties().get(TMP_DIR));
}
context.setAttribute("javax.servlet.context.tempdir", file);
}
// if a filter ref was provided, use it.
Filter filter = ((JettyHttpEndpoint) endpoint).getMultipartFilter();
if (filter == null) {
// if no filter ref was provided, use the default filter
filter = new MultiPartFilter();
}
filterHolder.setFilter(new CamelFilterWrapper(filter));
String pathSpec = endpoint.getPath();
if (pathSpec == null || "".equals(pathSpec)) {
pathSpec = "/";
}
if (endpoint.isMatchOnUriPrefix()) {
pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*";
}
addFilter(context, filterHolder, pathSpec);
LOG.debug("using multipart filter implementation " + filter.getClass().getName() + " for path " + pathSpec);
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class JettyHttpComponent method createServer.
protected Server createServer() {
Server s = null;
ThreadPool tp = threadPool;
QueuedThreadPool qtp = null;
// configure thread pool if min/max given
if (minThreads != null || maxThreads != null) {
if (getThreadPool() != null) {
throw new IllegalArgumentException("You cannot configure both minThreads/maxThreads and a custom threadPool on JettyHttpComponent: " + this);
}
qtp = new QueuedThreadPool();
if (minThreads != null) {
qtp.setMinThreads(minThreads.intValue());
}
if (maxThreads != null) {
qtp.setMaxThreads(maxThreads.intValue());
}
tp = qtp;
}
if (tp != null) {
try {
if (!Server.getVersion().startsWith("8")) {
s = Server.class.getConstructor(ThreadPool.class).newInstance(tp);
} else {
s = new Server();
if (isEnableJmx()) {
enableJmx(s);
}
Server.class.getMethod("setThreadPool", ThreadPool.class).invoke(s, tp);
}
} catch (Exception e) {
//ignore
}
}
if (s == null) {
s = new Server();
}
if (qtp != null) {
// let the thread names indicate they are from the server
qtp.setName("CamelJettyServer(" + ObjectHelper.getIdentityHashCode(s) + ")");
try {
qtp.start();
} catch (Exception e) {
throw new RuntimeCamelException("Error starting JettyServer thread pool: " + qtp, e);
}
}
ContextHandlerCollection collection = new ContextHandlerCollection();
s.setHandler(collection);
// setup the error handler if it set to Jetty component
if (getErrorHandler() != null) {
s.addBean(getErrorHandler());
} else if (!Server.getVersion().startsWith("8")) {
//need an error handler that won't leak information about the exception
//back to the client.
ErrorHandler eh = new ErrorHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
String msg = HttpStatus.getMessage(response.getStatus());
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
if (response instanceof Response) {
//need to use the deprecated method to support compiling with Jetty 8
((Response) response).setStatus(response.getStatus(), msg);
}
super.handle(target, baseRequest, request, response);
}
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
super.writeErrorPage(request, writer, code, message, false);
}
};
s.addBean(eh, false);
}
return s;
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class JettyHttpComponent method createConnector.
protected Connector createConnector(Server server, JettyHttpEndpoint endpoint) {
// now we just use the SelectChannelConnector as the default connector
SslContextFactory sslcf = null;
// Note that this was set on the endpoint when it was constructed. It was
// either explicitly set at the component or on the endpoint, but either way,
// the value is already set. We therefore do not need to look at the component
// level SSLContextParameters again in this method.
SSLContextParameters endpointSslContextParameters = endpoint.getSslContextParameters();
if (endpointSslContextParameters != null) {
try {
sslcf = createSslContextFactory(endpointSslContextParameters);
} catch (Exception e) {
throw new RuntimeCamelException(e);
}
} else if ("https".equals(endpoint.getProtocol())) {
sslcf = new SslContextFactory();
String keystoreProperty = System.getProperty(JETTY_SSL_KEYSTORE);
if (keystoreProperty != null) {
sslcf.setKeyStorePath(keystoreProperty);
} else if (sslKeystore != null) {
sslcf.setKeyStorePath(sslKeystore);
}
String keystorePassword = System.getProperty(JETTY_SSL_KEYPASSWORD);
if (keystorePassword != null) {
sslcf.setKeyManagerPassword(keystorePassword);
} else if (sslKeyPassword != null) {
sslcf.setKeyManagerPassword(sslKeyPassword);
}
String password = System.getProperty(JETTY_SSL_PASSWORD);
if (password != null) {
sslcf.setKeyStorePassword(password);
} else if (sslPassword != null) {
sslcf.setKeyStorePassword(sslPassword);
}
}
return createConnectorJettyInternal(server, endpoint, sslcf);
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class HttpBridgeRouteTest method testHttpClient.
@Test
public void testHttpClient() throws Exception {
String response = template.requestBodyAndHeader("http://localhost:" + port2 + "/test/hello", new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml", String.class);
assertEquals("Get a wrong response", "/", response);
response = template.requestBody("http://localhost:" + port1 + "/hello/world", "hello", String.class);
assertEquals("Get a wrong response", "/hello/world", response);
try {
template.requestBody("http://localhost:" + port2 + "/hello/world", "hello", String.class);
fail("Expect exception here!");
} catch (Exception ex) {
assertTrue("We should get a RuntimeCamelException", ex instanceof RuntimeCamelException);
}
}
Aggregations