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);
}
}
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;
}
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();
}
}
}
}
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();
}
}
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;
}
Aggregations