use of java.io.OutputStream in project camel by apache.
the class IOConverterTest method testToOutputStreamFile.
public void testToOutputStreamFile() throws Exception {
template.sendBodyAndHeader("file://target/test", "Hello World", Exchange.FILE_NAME, "hello.txt");
File file = new File("target/test/hello.txt");
OutputStream os = IOConverter.toOutputStream(file);
assertIsInstanceOf(BufferedOutputStream.class, os);
os.close();
}
use of java.io.OutputStream in project camel by apache.
the class MessageHelperTest method testResetStreamCache.
/*
* Tests the {@link MessageHelper#resetStreamCache(Message)} method
*/
public void testResetStreamCache() throws Exception {
// should not throw exceptions when Message or message body is null
MessageHelper.resetStreamCache(null);
MessageHelper.resetStreamCache(message);
// handle StreamCache
final ValueHolder<Boolean> reset = new ValueHolder<Boolean>(Boolean.FALSE);
message.setBody(new StreamCache() {
@SuppressWarnings("deprecation")
public void reset() {
reset.set(Boolean.TRUE);
}
public void writeTo(OutputStream os) throws IOException {
// noop
}
public StreamCache copy(Exchange exchange) throws IOException {
return null;
}
public boolean inMemory() {
return true;
}
@Override
public long length() {
return 0;
}
});
MessageHelper.resetStreamCache(message);
assertTrue("Should have reset the stream cache", reset.get());
}
use of java.io.OutputStream 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 java.io.OutputStream in project camel by apache.
the class SessionReflectionHandler method handle.
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
HttpSession session = request.getSession();
OutputStream os = response.getOutputStream();
baseRequest.setHandled(true);
if (session.getAttribute("foo") == null) {
session.setAttribute("foo", "bar");
os.write("New ".getBytes());
} else {
os.write("Old ".getBytes());
}
IOHelper.copyAndCloseInput(request.getInputStream(), os);
response.setStatus(HttpServletResponse.SC_OK);
}
use of java.io.OutputStream in project camel by apache.
the class Utility method setSecurityPolicy.
public static synchronized void setSecurityPolicy(String policyResourceName, String tmpFileName) throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(policyResourceName);
if (in == null) {
throw new IOException("Unable to find the resource policy.all on classpath");
}
File outfile = new File(tmpFileName);
OutputStream out = new FileOutputStream(outfile);
byte[] tmp = new byte[8192];
int len = 0;
while (true) {
len = in.read(tmp);
if (len <= 0) {
break;
}
out.write(tmp, 0, len);
}
out.close();
in.close();
System.setProperty("java.security.policy", outfile.getAbsolutePath());
}
Aggregations