Search in sources :

Example 66 with RuntimeCamelException

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();
    }
}
Also used : InputStream(java.io.InputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException)

Example 67 with RuntimeCamelException

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);
}
Also used : CamelContext(org.apache.camel.CamelContext) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MultiPartFilter(org.eclipse.jetty.servlets.MultiPartFilter) Filter(javax.servlet.Filter) MultiPartFilter(org.eclipse.jetty.servlets.MultiPartFilter) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File)

Example 68 with RuntimeCamelException

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;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) MBeanServer(javax.management.MBeanServer) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) GeneralSecurityException(java.security.GeneralSecurityException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Endpoint(org.apache.camel.Endpoint) HttpCommonEndpoint(org.apache.camel.http.common.HttpCommonEndpoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Writer(java.io.Writer)

Example 69 with RuntimeCamelException

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);
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) RuntimeCamelException(org.apache.camel.RuntimeCamelException) URISyntaxException(java.net.URISyntaxException) GeneralSecurityException(java.security.GeneralSecurityException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters)

Example 70 with RuntimeCamelException

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Test(org.junit.Test)

Aggregations

RuntimeCamelException (org.apache.camel.RuntimeCamelException)196 HashMap (java.util.HashMap)52 CamelContextAware (org.apache.camel.CamelContextAware)45 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)45 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)45 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)45 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)45 Bean (org.springframework.context.annotation.Bean)45 IOException (java.io.IOException)36 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)32 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)11 Exchange (org.apache.camel.Exchange)11 InputStream (java.io.InputStream)9 GeneralSecurityException (java.security.GeneralSecurityException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 TimeoutException (java.util.concurrent.TimeoutException)7 QName (javax.xml.namespace.QName)7 Message (org.apache.camel.Message)7 Method (java.lang.reflect.Method)6