Search in sources :

Example 6 with TeeInputStream

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;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream)

Example 7 with TeeInputStream

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));
}
Also used : TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilterOutputStream(java.io.FilterOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) Test(org.junit.Test)

Example 8 with TeeInputStream

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);
    }
}
Also used : TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) TeeInputStream(org.apache.commons.io.input.TeeInputStream) ExistException(org.collectionspace.csp.api.persistence.ExistException) HttpMethod(org.apache.commons.httpclient.HttpMethod) IOException(java.io.IOException) ExistException(org.collectionspace.csp.api.persistence.ExistException)

Example 9 with TeeInputStream

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;
}
Also used : Header(org.apache.commons.httpclient.Header) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) TeeInputStream(org.apache.commons.io.input.TeeInputStream)

Example 10 with TeeInputStream

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) File(java.io.File)

Aggregations

TeeInputStream (org.apache.commons.io.input.TeeInputStream)14 InputStream (java.io.InputStream)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 StringWriter (java.io.StringWriter)2 URI (java.net.URI)2 Header (org.apache.commons.httpclient.Header)2 TeeOutputStream (org.apache.commons.io.output.TeeOutputStream)2 ConfigRoot (org.collectionspace.chain.csp.config.ConfigRoot)2 Spec (org.collectionspace.chain.csp.schema.Spec)2 UIMapping (org.collectionspace.chain.csp.webui.external.UIMapping)2 WebUI (org.collectionspace.chain.csp.webui.main.WebUI)2 UI (org.collectionspace.csp.api.ui.UI)2 JSONObject (org.json.JSONObject)2 FilterOutputStream (java.io.FilterOutputStream)1 OutputStream (java.io.OutputStream)1