Search in sources :

Example 11 with TeeInputStream

use of org.apache.commons.io.input.TeeInputStream in project opencast by opencast.

the class WorkspaceImpl method put.

@Override
public URI put(String mediaPackageID, String mediaPackageElementID, String fileName, InputStream in) throws IOException {
    String safeFileName = PathSupport.toSafeName(fileName);
    final URI uri = wfr.getURI(mediaPackageID, mediaPackageElementID, fileName);
    notNull(in, "in");
    // Determine the target location in the workspace
    File workspaceFile = null;
    FileOutputStream out = null;
    synchronized (lock) {
        workspaceFile = toWorkspaceFile(uri);
        FileUtils.touch(workspaceFile);
    }
    // Try hard linking first and fall back to tee-ing to both the working file repository and the workspace
    if (linkingEnabled) {
        // The WFR stores an md5 hash along with the file, so we need to use the API and not try to write (link) the file
        // there ourselves
        wfr.put(mediaPackageID, mediaPackageElementID, fileName, in);
        File workingFileRepoDirectory = workingFileRepositoryFile(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mediaPackageID, mediaPackageElementID);
        File workingFileRepoCopy = new File(workingFileRepoDirectory, safeFileName);
        FileSupport.link(workingFileRepoCopy, workspaceFile, true);
    } else {
        InputStream tee = null;
        try {
            out = new FileOutputStream(workspaceFile);
            tee = new TeeInputStream(in, out, true);
            wfr.put(mediaPackageID, mediaPackageElementID, fileName, tee);
        } finally {
            IOUtils.closeQuietly(tee);
            IOUtils.closeQuietly(out);
        }
    }
    // wait until the file appears on the WFR node
    waitForResource(uri, HttpServletResponse.SC_OK, "File %s does not appear in WFR");
    return uri;
}
Also used : TeeInputStream(org.apache.commons.io.input.TeeInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) URI(java.net.URI) File(java.io.File)

Example 12 with TeeInputStream

use of org.apache.commons.io.input.TeeInputStream in project opencast by opencast.

the class WorkspaceImpl method putInCollection.

@Override
public URI putInCollection(String collectionId, String fileName, InputStream in) throws IOException {
    String safeFileName = PathSupport.toSafeName(fileName);
    URI uri = wfr.getCollectionURI(collectionId, fileName);
    // Determine the target location in the workspace
    InputStream tee = null;
    File tempFile = null;
    FileOutputStream out = null;
    try {
        synchronized (lock) {
            tempFile = toWorkspaceFile(uri);
            FileUtils.touch(tempFile);
            out = new FileOutputStream(tempFile);
        }
        // Try hard linking first and fall back to tee-ing to both the working file repository and the workspace
        if (linkingEnabled) {
            tee = in;
            wfr.putInCollection(collectionId, fileName, tee);
            FileUtils.forceMkdir(tempFile.getParentFile());
            File workingFileRepoDirectory = workingFileRepositoryFile(WorkingFileRepository.COLLECTION_PATH_PREFIX, collectionId);
            File workingFileRepoCopy = new File(workingFileRepoDirectory, safeFileName);
            FileSupport.link(workingFileRepoCopy, tempFile, true);
        } else {
            tee = new TeeInputStream(in, out, true);
            wfr.putInCollection(collectionId, fileName, tee);
        }
    } catch (IOException e) {
        FileUtils.deleteQuietly(tempFile);
        throw e;
    } finally {
        IoSupport.closeQuietly(tee);
        IoSupport.closeQuietly(out);
    }
    waitForResource(uri, HttpServletResponse.SC_OK, "File %s does not appear in WFR");
    return uri;
}
Also used : TeeInputStream(org.apache.commons.io.input.TeeInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) TeeInputStream(org.apache.commons.io.input.TeeInputStream) URI(java.net.URI) File(java.io.File)

Example 13 with TeeInputStream

use of org.apache.commons.io.input.TeeInputStream in project application by collectionspace.

the class ReturnedMultipartDocument method setResponse.

@Override
public boolean setResponse(HttpMethod method, int status) throws Exception {
    // it's ok to release the parent connection since we consume the entire response stream here
    boolean result = true;
    setStatus(status);
    InputStream stream = method.getResponseBodyAsStream();
    SAXReader reader = new SAXReader();
    if (isErrorStatus()) {
        String streamMessage = IOUtils.toString(stream);
        String Credentials401Error = "Incorrect of missing credentials. This request requires HTTP authentication.";
        String msg = String.format("%s request failed with status code %s: %s: %s", method.getName(), status, status == 401 ? Credentials401Error : streamMessage, method.getURI());
        log.warn(msg);
    }
    // TODO errorhandling
    Document doc = 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();
            doc = reader.read(new TeeInputStream(stream, dump));
            log.debug(dump.toString("UTF-8"));
        } else {
            doc = reader.read(stream, "UTF-8");
        }
        // split up document
        Element root = doc.getRootElement();
        // iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext(); ) {
            Element element = (Element) i.next();
            addDocument(element.getName(), DocumentHelper.parseText(element.asXML()));
        }
    }
    stream.close();
    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) Element(org.dom4j.Element) Iterator(java.util.Iterator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) TeeInputStream(org.apache.commons.io.input.TeeInputStream)

Example 14 with TeeInputStream

use of org.apache.commons.io.input.TeeInputStream in project application by collectionspace.

the class WebUIRequest method initRequest.

private void initRequest(UIUmbrella umbrella, HttpServletRequest request, HttpServletResponse response, List<String> p) throws IOException, UIException {
    this.request = request;
    this.response = response;
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();
        // Parse the request
        FileItemIterator iter;
        try {
            iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                // InputStream stream = item.openStream();
                if (item.isFormField()) {
                // System.out.println("Form field " + name + " with value "
                // + Streams.asString(stream) + " detected.");
                } else {
                    // System.out.println("File field " + name + " with file name "
                    // + item.getName() + " detected.");
                    // Process the input stream
                    contentHeaders = item.getHeaders();
                    uploadName = item.getName();
                    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                    if (item != null) {
                        InputStream stream = item.openStream();
                        IOUtils.copy(stream, byteOut);
                        new TeeInputStream(stream, byteOut);
                    }
                    bytebody = byteOut.toByteArray();
                }
            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        body = IOUtils.toString(request.getInputStream(), "UTF-8");
    }
    this.ppath = p.toArray(new String[0]);
    if (!(umbrella instanceof WebUIUmbrella))
        throw new UIException("Bad umbrella");
    this.umbrella = (WebUIUmbrella) umbrella;
    session = calculateSessionId();
}
Also used : FileItemStream(org.apache.commons.fileupload.FileItemStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) UIException(org.collectionspace.csp.api.ui.UIException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

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