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