Search in sources :

Example 96 with IOException

use of java.io.IOException in project camel by apache.

the class JcloudsPayloadConverter method toPayload.

@Converter
public static Payload toPayload(final InputStream is, Exchange exchange) throws IOException {
    InputStreamPayload payload = new InputStreamPayload(is);
    // only set the contentlength if possible
    if (is.markSupported()) {
        long contentLength = ByteStreams.length(new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                return is;
            }
        });
        is.reset();
        payload.getContentMetadata().setContentLength(contentLength);
    }
    return payload;
}
Also used : InputStream(java.io.InputStream) InputStreamPayload(org.jclouds.io.payloads.InputStreamPayload) IOException(java.io.IOException) FallbackConverter(org.apache.camel.FallbackConverter) Converter(org.apache.camel.Converter) TypeConverter(org.apache.camel.TypeConverter)

Example 97 with IOException

use of java.io.IOException in project camel by apache.

the class OSGiCacheManager method getClassLoader.

private ClassLoader getClassLoader(String providerName) throws Exception {
    if (providerName == null || !getConfiguration().isLookupProviders()) {
        return null;
    }
    final BundleContext bc = FrameworkUtil.getBundle(JCacheHelper.class).getBundleContext();
    final ClassLoader bcl = bc.getBundle().adapt(BundleWiring.class).getClassLoader();
    final ClassLoader acl = getConfiguration().getApplicationContextClassLoader();
    for (final Bundle bundle : bc.getBundles()) {
        URL spi = bundle.getResource("META-INF/services/javax.cache.spi.CachingProvider");
        if (spi != null) {
            try (BufferedReader in = new BufferedReader(new InputStreamReader(spi.openStream()))) {
                if (ObjectHelper.equal(providerName, in.readLine())) {
                    return new ClassLoader(bcl) {

                        @Override
                        protected Class<?> findClass(String name) throws ClassNotFoundException {
                            try {
                                return acl.loadClass(name);
                            } catch (ClassNotFoundException e) {
                                return bundle.loadClass(name);
                            }
                        }

                        @Override
                        protected URL findResource(String name) {
                            URL resource = acl.getResource(name);
                            if (resource == null) {
                                resource = bundle.getResource(name);
                            }
                            return resource;
                        }

                        @Override
                        protected Enumeration findResources(String name) throws IOException {
                            try {
                                return acl.getResources(name);
                            } catch (IOException e) {
                                return bundle.getResources(name);
                            }
                        }
                    };
                }
            }
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BufferedReader(java.io.BufferedReader) JCacheHelper(org.apache.camel.component.jcache.JCacheHelper) IOException(java.io.IOException) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext)

Example 98 with IOException

use of java.io.IOException in project camel by apache.

the class JettyContentExchange9 method send.

public void send(HttpClient client) throws IOException {
    org.eclipse.jetty.client.api.Request.Listener listener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            onRequestComplete();
        }

        @Override
        public void onFailure(Request request, Throwable failure) {
            onConnectionFailed(failure);
        }
    };
    InputStreamResponseListener responseListener = new InputStreamResponseListener() {

        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            byte[] buffer = new byte[content.limit()];
            content.get(buffer);
            try {
                osb.write(buffer);
                callback.succeeded();
            } catch (IOException e) {
                callback.failed(e);
            }
        }

        @Override
        public void onComplete(Result result) {
            if (result.isFailed()) {
                doTaskCompleted(result.getFailure());
            } else {
                try {
                    Object content = osb.build();
                    if (content instanceof byte[]) {
                        onResponseComplete(result, (byte[]) content);
                    } else {
                        StreamCache cos = (StreamCache) content;
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        cos.writeTo(baos);
                        onResponseComplete(result, baos.toByteArray());
                    }
                } catch (IOException e) {
                    doTaskCompleted(e);
                }
            }
        }
    };
    request.followRedirects(supportRedirect).listener(listener).send(responseListener);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Request(org.eclipse.jetty.client.api.Request) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StreamCache(org.apache.camel.StreamCache) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 99 with IOException

use of java.io.IOException 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 100 with IOException

use of java.io.IOException 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)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916