use of org.apache.commons.io.input.TeeInputStream in project application by collectionspace.
the class NullResolver method serveExternalContent.
/**
* serve content with tweaking of mime types if important
*
* @param sub
* @param sc
* @param is
* @param path
* @return
* @throws UIException
* @throws IOException
*/
private boolean serveExternalContent(CompositeWebUIRequestPart sub, ServletContext sc, InputStream is, String path) throws UIException, IOException {
if (is == null)
// Not for us
return false;
byte[] bytebody;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
IOUtils.copy(is, byteOut);
new TeeInputStream(is, byteOut);
bytebody = byteOut.toByteArray();
String mimetype = sc.getMimeType(path);
if (mimetype == null && path.endsWith(".appcache")) {
mimetype = "text/cache-manifest";
}
sub.sendUnknown(bytebody, mimetype, null);
if (bytebody == null)
// Not for us
return false;
return true;
}
use of org.apache.commons.io.input.TeeInputStream in project tutorials by eugenp.
the class CommonsIOUnitTest method whenUsingTeeInputOutputStream_thenWriteto2OutputStreams.
@SuppressWarnings("resource")
@Test
public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams() throws IOException {
final String str = "Hello World.";
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
FilterOutputStream teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
new TeeInputStream(inputStream, teeOutputStream, true).read(new byte[str.length()]);
Assert.assertEquals(str, String.valueOf(outputStream1));
Assert.assertEquals(str, String.valueOf(outputStream2));
}
use of org.apache.commons.io.input.TeeInputStream in project application by collectionspace.
the class ServicesConnection method doRequest.
// XXX eugh! error case control-flow nightmare
private void doRequest(Returned out, RequestMethod method_type, String uri, RequestDataSource src, CSPRequestCredentials creds, CSPRequestCache cache) throws ConnectionException {
InputStream body_data = null;
if (src != null) {
body_data = src.getStream();
}
try {
HttpMethod method = createMethod(method_type, uri, body_data);
if (body_data != null) {
method.setRequestHeader("Content-Type", src.getMIMEType());
// XXX Not sure if or when this ever actually writes to stderr?
body_data = new TeeInputStream(body_data, System.err);
}
boolean releaseConnection = true;
try {
HttpClient client = makeClient(creds, cache);
String requestContext = null;
if (perflog.isDebugEnabled()) {
// TODO add more context, e.g. session id?
requestContext = "HttpClient@" + Integer.toHexString(client.hashCode());
requestContext += "/CSPRequestCache@" + Integer.toHexString(cache.hashCode()) + ",";
// String queryString = method.getQueryString();
perflog.debug(System.currentTimeMillis() + ",\"" + Thread.currentThread().getName() + "\",app,svc," + requestContext + method.getName() + " " + method.getURI());
}
int response = client.executeMethod(method);
if (perflog.isDebugEnabled()) {
perflog.debug(System.currentTimeMillis() + ",\"" + Thread.currentThread().getName() + "\",svc,app," + requestContext + "HttpClient.executeMethod done");
}
releaseConnection = out.setResponse(method, response);
} catch (ConnectionException ce) {
throw new ConnectionException(ce.getMessage(), ce.getStatus(), base_url + "/" + uri, ce);
} catch (ExistException ee) {
throw new ConnectionException(ee.getMessage(), ee.getStatus(), base_url + "/" + uri, ee);
} catch (Exception e) {
throw new ConnectionException(e.getMessage(), 0, base_url + "/" + uri, e);
} finally {
if (releaseConnection == true) {
method.releaseConnection();
// Only release the connection if we know the associated response stream has been
// read and processed. The response streams are instances of AutoCloseInputStream, which will automatically
// close after being read. Also, the AutoCloseInputStream response streams will close their corresponding HttpMethod instance connection
// once their data has been completely consumed.
}
if (log.isTraceEnabled()) {
log.trace("HTTP connection pool size: " + manager.getConnectionsInPool());
}
if (log.isWarnEnabled()) {
if (manager.getConnectionsInPool() >= MAX_SERVICES_CONNECTIONS) {
log.warn("reached max services connection limit of " + MAX_SERVICES_CONNECTIONS);
// Delete closed connections from the pool, so that the
// warning will cease
// once a connection becomes available.
manager.deleteClosedConnections();
}
}
}
} finally {
closeStream(body_data);
}
}
use of org.apache.commons.io.input.TeeInputStream in project application by collectionspace.
the class ReturnedDocument method setResponse.
@Override
public boolean setResponse(HttpMethod method, int status) throws IOException, DocumentException {
// it's ok to release the parent connection since we consume the entire response stream here
boolean result = true;
this.status = status;
InputStream stream = method.getResponseBodyAsStream();
SAXReader reader = new SAXReader();
if (status >= 400) {
log.debug("Got error : " + IOUtils.toString(stream));
}
// TODO errorhandling
Document out = null;
Header content_type = method.getResponseHeader("Content-Type");
if (content_type != null && "application/xml".equals(content_type.getValue())) {
if (log.isDebugEnabled()) {
ByteArrayOutputStream dump = new ByteArrayOutputStream();
// TODO CSPACE-2552 add ,"UTF-8" to reader.read()?
out = reader.read(new TeeInputStream(stream, dump));
log.debug(dump.toString("UTF-8"));
} else {
// TODO CSPACE-2552 add ,"UTF-8" to reader.read()?
out = reader.read(stream);
}
}
stream.close();
doc = out;
return result;
}
use of org.apache.commons.io.input.TeeInputStream in project mycore by MyCoRe-Org.
the class MCRDefaultConfigurationLoader method getConfigInputStream.
private InputStream getConfigInputStream() throws IOException {
MCRConfigurationInputStream configurationInputStream = MCRConfigurationInputStream.getMyCoRePropertiesInstance();
File configFile = MCRConfigurationDir.getConfigFile("mycore.active.properties");
if (configFile != null) {
FileOutputStream fout = new FileOutputStream(configFile);
return new TeeInputStream(configurationInputStream, fout, true);
}
return configurationInputStream;
}
Aggregations