Example usage for org.apache.commons.io.input TeeInputStream TeeInputStream

List of usage examples for org.apache.commons.io.input TeeInputStream TeeInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input TeeInputStream TeeInputStream.

Prototype

public TeeInputStream(InputStream input, OutputStream branch) 

Source Link

Document

Creates a TeeInputStream that proxies the given InputStream and copies all read bytes to the given OutputStream .

Usage

From source file:edu.caltech.ipac.firefly.server.servlets.AnyFileUpload.java

protected void processRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {

    String dest = req.getParameter(DEST_PARAM);
    String preload = req.getParameter(PRELOAD_PARAM);
    String overrideCacheKey = req.getParameter(CACHE_KEY);
    String fileType = req.getParameter(FILE_TYPE);

    if (!ServletFileUpload.isMultipartContent(req)) {
        sendReturnMsg(res, 400, "Is not a Multipart request. Request rejected.", "");
    }//from w  ww  .ja  v a2s.  c o  m
    StopWatch.getInstance().start("Upload File");

    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iter = upload.getItemIterator(req);
    while (iter.hasNext()) {
        FileItemStream item = iter.next();

        if (!item.isFormField()) {
            String fileName = item.getName();
            InputStream inStream = new BufferedInputStream(item.openStream(),
                    IpacTableUtil.FILE_IO_BUFFER_SIZE);
            String ext = resolveExt(fileName);
            FileType fType = resolveType(fileType, ext, item.getContentType());
            File destDir = resolveDestDir(dest, fType);
            boolean doPreload = resolvePreload(preload, fType);

            File uf = File.createTempFile("upload_", ext, destDir);
            String rPathInfo = ServerContext.replaceWithPrefix(uf);

            UploadFileInfo fi = new UploadFileInfo(rPathInfo, uf, fileName, item.getContentType());
            String fileCacheKey = overrideCacheKey != null ? overrideCacheKey : rPathInfo;
            UserCache.getInstance().put(new StringKey(fileCacheKey), fi);

            if (doPreload && fType == FileType.FITS) {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(uf),
                        IpacTableUtil.FILE_IO_BUFFER_SIZE);
                TeeInputStream tee = new TeeInputStream(inStream, bos);
                try {
                    final Fits fits = new Fits(tee);
                    FitsRead[] frAry = FitsRead.createFitsReadArray(fits);
                    FitsCacher.addFitsReadToCache(uf, frAry);
                } finally {
                    FileUtil.silentClose(bos);
                    FileUtil.silentClose(tee);
                }
            } else {
                FileUtil.writeToFile(inStream, uf);
            }
            sendReturnMsg(res, 200, null, fileCacheKey);
            Counters.getInstance().increment(Counters.Category.Upload, fi.getContentType());
            return;
        }
    }
    StopWatch.getInstance().printLog("Upload File");
}

From source file:com.cedarsoft.couchdb.io.ActionResponseSerializer.java

@Nonnull
public ActionResponse deserialize(@Nonnull ClientResponse response) throws VersionException {
    if (!MediaType.APPLICATION_JSON_TYPE.equals(response.getType())) {
        throw new IllegalStateException("Invalid media type: " + response.getType());
    }//from   w w  w. j a  va  2  s . c  o m

    InputStream entityInputStream = response.getEntityInputStream();

    try {
        //Wrap the input stream
        try (MaxLengthByteArrayOutputStream teedOut = new MaxLengthByteArrayOutputStream();
                TeeInputStream teeInputStream = new TeeInputStream(entityInputStream, teedOut)) {
            UniqueId uniqueId = deserialize(teeInputStream);

            return new ActionResponse(uniqueId, response.getStatus(), response.getLocation(),
                    teedOut.toByteArray());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:at.bitfire.davdroid.webdav.WebDavCollection.java

public boolean multiGet(String[] names, MultigetType type)
        throws IOException, IncapableResourceException, HttpException {
    DavMultiget multiget = (type == MultigetType.ADDRESS_BOOK) ? new DavAddressbookMultiget()
            : new DavCalendarMultiget();

    multiget.prop = new DavProp();
    multiget.prop.getetag = new DavProp.DavPropGetETag();

    if (type == MultigetType.ADDRESS_BOOK)
        multiget.prop.addressData = new DavProp.DavPropAddressData();
    else if (type == MultigetType.CALENDAR)
        multiget.prop.calendarData = new DavProp.DavPropCalendarData();

    multiget.hrefs = new ArrayList<DavHref>(names.length);
    for (String name : names)
        multiget.hrefs.add(new DavHref(location.resolve(name).getPath()));

    Serializer serializer = new Persister();
    StringWriter writer = new StringWriter();
    try {//from  www. j  av  a 2 s . c o m
        serializer.write(multiget, writer);
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
        return false;
    }

    HttpReport report = new HttpReport(location, writer.toString());
    HttpResponse response = client.execute(report);
    checkResponse(response);

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_MULTI_STATUS) {
        DavMultistatus multistatus;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream is = new TeeInputStream(response.getEntity().getContent(), baos);
            multistatus = serializer.read(DavMultistatus.class, is);

            Log.d(TAG, "Received multistatus response: " + baos.toString("UTF-8"));
        } catch (Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
            return false;
        }
        processMultiStatus(multistatus);

    } else
        throw new IncapableResourceException();
    return true;
}

From source file:de.hybris.platform.atddimpex.keywords.ImpexKeywordLibrary.java

private void importImpExFromInputStream(final String resourceName, InputStream inputStream)
        throws ImpExException, IOException {
    if (LOG.isInfoEnabled()) {
        LOG.info("Importing [" + resourceName + "]");

        final ByteArrayOutputStream impexData = new ByteArrayOutputStream();
        final TeeInputStream consoleTeeInputStream = new TeeInputStream(inputStream, impexData);

        LOG.info("ImpEx contents are:\n" + IOUtils.toString(consoleTeeInputStream, DEFAULT_ENCODING));

        inputStream = new ByteArrayInputStream(impexData.toByteArray());
    }/*from  w  w w . j av a2s . co m*/

    final OutputStream logOutputStream = getImpExLogOutputStream(resourceName);
    final InputStream fileFeeInputStream = new TeeInputStream(inputStream, logOutputStream);

    impExAdaptor.importStream(fileFeeInputStream, DEFAULT_ENCODING, resourceName);

    logOutputStream.flush();

    modelService.detachAll();
}

From source file:io.mapzone.arena.csw.CswRequest.java

@Override
public R execute(IProgressMonitor monitor) throws Exception {
    assert writer == null && buf == null;
    buf = new ByteArrayOutputStream(4096);
    writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buf, DEFAULT_XML_ENCODING);
    writer = new IndentingXMLStreamWriter(writer);

    // write content
    out().writeStartDocument(DEFAULT_XML_ENCODING, "1.0");
    writeRequest();// w  w  w.j  a v  a  2 s  .  c o  m
    prepare(monitor);
    out().writeEndElement();
    out().writeEndDocument();
    out().flush();

    String content = buf.toString(DEFAULT_XML_ENCODING);
    log.info(content);

    // execute POST
    // XXX streaming content would be cool, but probably waste of effort
    HttpPost post = new HttpPost(baseUrl.get());
    StringEntity entity = new StringEntity(content, DEFAULT_XML_ENCODING);
    entity.setContentType("application/xml");
    post.setEntity(entity);

    // read response
    try (CloseableHttpResponse response = (CloseableHttpResponse) httpClient.get().execute(post);
            InputStream in = response.getEntity().getContent();) {
        // check return code
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != 200) {
            throw new IOException(status.getReasonPhrase() + " (" + status.getStatusCode() + ")");
        }

        // handle response
        System.out.print("RESPONSE: ");
        TeeInputStream tee = new TeeInputStream(in, System.out);
        return handleResponse(tee, monitor);
    } finally {
        writer = null;
        buf = null;
    }
}

From source file:ee.ria.xroad.confproxy.util.OutputBuilder.java

/**
 * Generates global configuration directory content MIME.
 * @param mimeContent output stream to write to
 * @throws Exception if reading global configuration files fails
 *///from  w w  w.j  a va2  s.c  om
private void build(final ByteArrayOutputStream mimeContent) throws Exception {
    try (MultipartEncoder encoder = new MultipartEncoder(mimeContent, dataBoundary)) {
        DateTime expireDate = new DateTime().plusSeconds(conf.getValidityIntervalSeconds());
        encoder.startPart(null,
                new String[] { HEADER_EXPIRE_DATE + ": " + expireDate.toDateTime(DateTimeZone.UTC),
                        HEADER_VERSION + ": " + String.format("%d", version) });

        String instance = conf.getInstance();

        confDir.eachFile((metadata, inputStream) -> {
            try (FileOutputStream fos = createFileOutputStream(tempDirPath, metadata)) {
                TeeInputStream tis = new TeeInputStream(inputStream, fos);
                appendFileContent(encoder, instance, metadata, tis);
            }
        });
    }
}

From source file:ee.ria.xroad.common.message.SaxSoapParserImpl.java

private Soap parseMessage(InputStream is, String mimeType, String contentType, String charset)
        throws Exception {
    log.trace("parseMessage({}, {})", mimeType, charset);

    ByteArrayOutputStream rawXml = new ByteArrayOutputStream();
    ByteArrayOutputStream processedXml = new ByteArrayOutputStream();

    InputStream proxyStream = excludeUtf8Bom(contentType, new TeeInputStream(is, rawXml));
    Writer outputWriter = new OutputStreamWriter(processedXml, charset);
    XRoadSoapHandler handler = handleSoap(outputWriter, proxyStream);

    CodedException fault = handler.getFault();
    if (fault != null) {
        return createSoapFault(charset, rawXml, fault);
    }//from www.  ja v a  2s.c  o m

    byte[] xmlBytes = isProcessedXmlRequired() ? processedXml.toByteArray() : rawXml.toByteArray();

    return createSoapMessage(contentType, charset, handler, xmlBytes);
}

From source file:org.apache.james.mailbox.store.StoreMessageManager.java

/**
 * @see org.apache.james.mailbox.MessageManager#appendMessage(java.io.InputStream,
 *      java.util.Date, org.apache.james.mailbox.MailboxSession, boolean,
 *      javax.mail.Flags)//  w w w  . j  a v  a2s  .  c o m
 */
public long appendMessage(InputStream msgIn, Date internalDate, final MailboxSession mailboxSession,
        boolean isRecent, Flags flagsToBeSet) throws MailboxException {

    File file = null;
    TeeInputStream tmpMsgIn = null;
    BodyOffsetInputStream bIn = null;
    FileOutputStream out = null;
    SharedFileInputStream contentIn = null;

    if (!isWriteable(mailboxSession)) {
        throw new ReadOnlyException(getMailboxPath(), mailboxSession.getPathDelimiter());
    }

    try {
        // Create a temporary file and copy the message to it. We will work
        // with the file as
        // source for the InputStream
        file = File.createTempFile("imap", ".msg");
        out = new FileOutputStream(file);

        tmpMsgIn = new TeeInputStream(msgIn, out);

        bIn = new BodyOffsetInputStream(tmpMsgIn);
        // Disable line length... This should be handled by the smtp server
        // component and not the parser itself
        // https://issues.apache.org/jira/browse/IMAP-122
        MimeConfig config = MimeConfig.custom().setMaxLineLen(-1).setMaxHeaderLen(-1).build();

        final MimeTokenStream parser = new MimeTokenStream(config, new DefaultBodyDescriptorBuilder());

        parser.setRecursionMode(RecursionMode.M_NO_RECURSE);
        parser.parse(bIn);
        final HeaderImpl header = new HeaderImpl();

        EntityState next = parser.next();
        while (next != EntityState.T_BODY && next != EntityState.T_END_OF_STREAM
                && next != EntityState.T_START_MULTIPART) {
            if (next == EntityState.T_FIELD) {
                header.addField(parser.getField());
            }
            next = parser.next();
        }
        final MaximalBodyDescriptor descriptor = (MaximalBodyDescriptor) parser.getBodyDescriptor();
        final PropertyBuilder propertyBuilder = new PropertyBuilder();
        final String mediaType;
        final String mediaTypeFromHeader = descriptor.getMediaType();
        final String subType;
        if (mediaTypeFromHeader == null) {
            mediaType = "text";
            subType = "plain";
        } else {
            mediaType = mediaTypeFromHeader;
            subType = descriptor.getSubType();
        }
        propertyBuilder.setMediaType(mediaType);
        propertyBuilder.setSubType(subType);
        propertyBuilder.setContentID(descriptor.getContentId());
        propertyBuilder.setContentDescription(descriptor.getContentDescription());
        propertyBuilder.setContentLocation(descriptor.getContentLocation());
        propertyBuilder.setContentMD5(descriptor.getContentMD5Raw());
        propertyBuilder.setContentTransferEncoding(descriptor.getTransferEncoding());
        propertyBuilder.setContentLanguage(descriptor.getContentLanguage());
        propertyBuilder.setContentDispositionType(descriptor.getContentDispositionType());
        propertyBuilder.setContentDispositionParameters(descriptor.getContentDispositionParameters());
        propertyBuilder.setContentTypeParameters(descriptor.getContentTypeParameters());
        // Add missing types
        final String codeset = descriptor.getCharset();
        if (codeset == null) {
            if ("TEXT".equalsIgnoreCase(mediaType)) {
                propertyBuilder.setCharset("us-ascii");
            }
        } else {
            propertyBuilder.setCharset(codeset);
        }

        final String boundary = descriptor.getBoundary();
        if (boundary != null) {
            propertyBuilder.setBoundary(boundary);
        }
        if ("text".equalsIgnoreCase(mediaType)) {
            final CountingInputStream bodyStream = new CountingInputStream(parser.getInputStream());
            bodyStream.readAll();
            long lines = bodyStream.getLineCount();
            bodyStream.close();
            next = parser.next();
            if (next == EntityState.T_EPILOGUE) {
                final CountingInputStream epilogueStream = new CountingInputStream(parser.getInputStream());
                epilogueStream.readAll();
                lines += epilogueStream.getLineCount();
                epilogueStream.close();

            }
            propertyBuilder.setTextualLineCount(lines);
        }

        final Flags flags;
        if (flagsToBeSet == null) {
            flags = new Flags();
        } else {
            flags = flagsToBeSet;

            // Check if we need to trim the flags
            trimFlags(flags, mailboxSession);

        }
        if (isRecent) {
            flags.add(Flags.Flag.RECENT);
        }
        if (internalDate == null) {
            internalDate = new Date();
        }
        byte[] discard = new byte[4096];
        while (tmpMsgIn.read(discard) != -1) {
            // consume the rest of the stream so everything get copied to
            // the file now
            // via the TeeInputStream
        }
        int bodyStartOctet = (int) bIn.getBodyStartOffset();
        if (bodyStartOctet == -1) {
            bodyStartOctet = 0;
        }
        contentIn = new SharedFileInputStream(file);
        final int size = (int) file.length();

        final List<MessageAttachment> attachments = extractAttachments(contentIn);
        final MailboxMessage message = createMessage(internalDate, size, bodyStartOctet, contentIn, flags,
                propertyBuilder, attachments);

        new QuotaChecker(quotaManager, quotaRootResolver, mailbox).tryAddition(1, size);

        return locker.executeWithLock(mailboxSession, getMailboxPath(),
                new MailboxPathLocker.LockAwareExecution<Long>() {

                    @Override
                    public Long execute() throws MailboxException {
                        MessageMetaData data = appendMessageToStore(message, attachments, mailboxSession);

                        SortedMap<Long, MessageMetaData> uids = new TreeMap<Long, MessageMetaData>();
                        uids.put(data.getUid(), data);
                        dispatcher.added(mailboxSession, uids, getMailboxEntity());
                        return data.getUid();
                    }
                }, true);

    } catch (IOException e) {
        throw new MailboxException("Unable to parse message", e);
    } catch (MimeException e) {
        throw new MailboxException("Unable to parse message", e);
    } finally {
        IOUtils.closeQuietly(bIn);
        IOUtils.closeQuietly(tmpMsgIn);
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(contentIn);

        // delete the temporary file if one was specified
        if (file != null) {
            if (!file.delete()) {
                // Don't throw an IOException. The message could be appended
                // and the temporary file
                // will be deleted hopefully some day
            }
        }
    }

}

From source file:org.apache.nifi.minifi.bootstrap.configuration.ingestors.FileChangeIngestor.java

@Override
public void run() {
    logger.debug("Checking for a change");
    if (targetChanged()) {
        logger.debug("Target changed, checking if it's different than current flow.");
        try (FileInputStream configFile = new FileInputStream(configFilePath.toFile());
                ByteArrayOutputStream pipedOutputStream = new ByteArrayOutputStream();
                TeeInputStream teeInputStream = new TeeInputStream(configFile, pipedOutputStream)) {

            if (differentiator.isNew(teeInputStream)) {
                logger.debug("New change, notifying listener");
                // Fill the byteArrayOutputStream with the rest of the request data
                while (teeInputStream.available() != 0) {
                    teeInputStream.read();
                }/*www .j a v  a 2s  .  c  om*/

                ByteBuffer newConfig = ByteBuffer.wrap(pipedOutputStream.toByteArray());
                ByteBuffer readOnlyNewConfig = newConfig.asReadOnlyBuffer();

                configurationChangeNotifier.notifyListeners(readOnlyNewConfig);
                logger.debug("Listeners notified");
            }
        } catch (Exception e) {
            logger.error("Could not successfully notify listeners.", e);
        }
    }
}

From source file:org.apache.nifi.minifi.bootstrap.RunMiNiFi.java

private static ByteBuffer performTransformation(InputStream configIs, String configDestinationPath)
        throws ConfigurationChangeException, IOException {
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            TeeInputStream teeInputStream = new TeeInputStream(configIs, byteArrayOutputStream)) {

        ConfigTransformer.transformConfigFile(teeInputStream, configDestinationPath);

        return ByteBuffer.wrap(byteArrayOutputStream.toByteArray());
    } catch (ConfigurationChangeException e) {
        throw e;//from  w  ww  .  j a  va  2  s.c om
    } catch (Exception e) {
        throw new IOException("Unable to successfully transform the provided configuration", e);
    }
}