Search in sources :

Example 86 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class GroovyExpression method instantiateScript.

@SuppressWarnings("unchecked")
private Script instantiateScript(Exchange exchange) {
    // Get the script from the cache, or create a new instance
    GroovyLanguage language = (GroovyLanguage) exchange.getContext().resolveLanguage("groovy");
    Class<Script> scriptClass = language.getScriptFromCache(text);
    if (scriptClass == null) {
        GroovyShell shell;
        Set<GroovyShellFactory> shellFactories = exchange.getContext().getRegistry().findByType(GroovyShellFactory.class);
        if (shellFactories.size() > 1) {
            throw new IllegalStateException("Too many GroovyShellFactory instances found: " + shellFactories.size());
        } else if (shellFactories.size() == 1) {
            shell = shellFactories.iterator().next().createGroovyShell(exchange);
        } else {
            ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
            shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
        }
        scriptClass = shell.getClassLoader().parseClass(text);
        language.addScriptToCache(text, scriptClass);
    }
    // New instance of the script
    try {
        return scriptClass.newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeCamelException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeCamelException(e);
    }
}
Also used : Script(groovy.lang.Script) RuntimeCamelException(org.apache.camel.RuntimeCamelException) GroovyShell(groovy.lang.GroovyShell)

Example 87 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class HdfsOutputStream method createOutputStream.

public static HdfsOutputStream createOutputStream(String hdfsPath, HdfsConfiguration configuration) throws IOException {
    HdfsOutputStream ret = new HdfsOutputStream();
    ret.fileType = configuration.getFileType();
    ret.actualPath = hdfsPath;
    ret.info = new HdfsInfo(ret.actualPath);
    ret.suffixedPath = ret.actualPath + '.' + configuration.getOpenedSuffix();
    if (configuration.isWantAppend() || configuration.isAppend()) {
        if (!ret.info.getFileSystem().exists(new Path(ret.actualPath))) {
            configuration.setAppend(false);
        } else {
            configuration.setAppend(true);
            ret.info = new HdfsInfo(ret.suffixedPath);
            ret.info.getFileSystem().rename(new Path(ret.actualPath), new Path(ret.suffixedPath));
        }
    } else {
        if (ret.info.getFileSystem().exists(new Path(ret.actualPath))) {
            //only check if not directory
            if (!ret.info.getFileSystem().isDirectory(new Path(ret.actualPath))) {
                if (configuration.isOverwrite()) {
                    ret.info.getFileSystem().delete(new Path(ret.actualPath), true);
                } else {
                    throw new RuntimeCamelException("The file already exists");
                }
            }
        }
    }
    ret.out = ret.fileType.createOutputStream(ret.suffixedPath, configuration);
    ret.opened = true;
    return ret;
}
Also used : Path(org.apache.hadoop.fs.Path) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 88 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException 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();
            }
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Endpoint(org.apache.camel.Endpoint) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Example 89 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class CamelServlet method doServiceAsync.

/**
     * This is used to handle request asynchronously
     * @param context the {@link AsyncContext}
     */
protected void doServiceAsync(AsyncContext context) {
    final HttpServletRequest request = (HttpServletRequest) context.getRequest();
    final HttpServletResponse response = (HttpServletResponse) context.getResponse();
    try {
        doService(request, response);
    } catch (Exception e) {
        //An error shouldn't occur as we should handle most of error in doService
        log.error("Error processing request", e);
        try {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (Exception e1) {
            log.debug("Cannot send reply to client!", e1);
        }
        //Need to wrap it in RuntimeException as it occurs in a Runnable
        throw new RuntimeCamelException(e);
    } finally {
        context.complete();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 90 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class DefaultInjector method newInstance.

@Override
public <T> T newInstance(Class<T> type) {
    T answer = delegate.newInstance(type);
    if (answer != null) {
        try {
            postProcessor.postProcessBeforeInitialization(answer, answer.getClass().getName());
            postProcessor.postProcessAfterInitialization(answer, answer.getClass().getName());
        } catch (Exception e) {
            throw new RuntimeCamelException("Error during post processing of bean " + answer, e);
        }
    }
    return answer;
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

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