Search in sources :

Example 16 with TeeOutputStream

use of org.apache.commons.io.output.TeeOutputStream in project mycore by MyCoRe-Org.

the class MCRWebPagesSynchronizer method getOutputStream.

/**
 * Returns an OuputStream that writes to local webapp and to file inside <code>MCR.WCMS2.DataDir</code>.
 */
public static OutputStream getOutputStream(String path) throws IOException {
    String cleanPath = path.startsWith("/") ? path.substring(1) : path;
    File wcmsDataDir = getWCMSDataDir();
    File webappBaseDir = getWebAppBaseDir();
    File webappTarget = new File(webappBaseDir, cleanPath);
    if (!webappTarget.toPath().startsWith(webappBaseDir.toPath())) {
        throw new IOException(String.format(Locale.ROOT, "Cannot write %s outside the web application: %s", webappTarget, webappBaseDir));
    }
    File wcmsDataDirTarget = new File(wcmsDataDir, cleanPath);
    createDirectoryIfNeeded(webappTarget);
    createDirectoryIfNeeded(wcmsDataDirTarget);
    LOGGER.info(String.format(Locale.ROOT, "Writing content to %s and to %s.", webappTarget, wcmsDataDirTarget));
    return new TeeOutputStream(new FileOutputStream(wcmsDataDirTarget), new FileOutputStream(webappTarget));
}
Also used : TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 17 with TeeOutputStream

use of org.apache.commons.io.output.TeeOutputStream in project lobcder by skoulouzis.

the class WorkerServlet method copy.

/**
 * Copy the given byte range of the given input to the given output.
 *
 * @param input The input to copy the given range to the given output for.
 * @param output The output to copy the given range from the given input
 * for.
 * @param start Start of the byte range.
 * @param length Length of the byte range.
 * @throws IOException If something fails at I/O level.
 */
private void copy(PDRI input, OutputStream output, long start, long length, HttpServletRequest request) throws IOException, VlException, JAXBException, URISyntaxException {
    OutputStream tos = null;
    try {
        if (input.getLength() == length) {
            // Write full range.
            in = input.getData();
            byte[] buffer = new byte[bufferSize];
            if (!Catalogue.isPDRIOnWorker(new URI(input.getURI()))) {
                cacheFile = new File(Util.getCacheDir(), input.getFileName());
                if (!cacheFile.exists() || input.getLength() != cacheFile.length()) {
                    tos = new TeeOutputStream(new FileOutputStream(cacheFile), output);
                    File file = new File(System.getProperty("user.home"));
                    while (file.getUsableSpace() < Util.getCacheFreeSpaceLimit()) {
                        evictCache();
                    }
                }
            }
            if (tos == null) {
                tos = output;
            }
            if (!qosCopy) {
                int len;
                while ((len = in.read(buffer)) != -1) {
                    tos.write(buffer, 0, len);
                }
            // CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((bufferSize), in, tos);
            // cBuff.setMaxReadChunkSize(bufferSize);
            // if (responseBufferSize > 0) {
            // cBuff.setMaxWriteChunkSize(responseBufferSize);
            // }
            // cBuff.startTransfer(new Long(-1));
            } else {
                qoSCopy(buffer, tos, length, request);
            }
        } else {
            io.milton.http.Range range = new io.milton.http.Range(start, length - start);
            input.copyRange(range, output);
        // input.copyRange(output, start, length);
        }
    } finally {
        if (tos != null) {
            tos.flush();
        } else {
            output.flush();
        }
    }
}
Also used : TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) URI(java.net.URI) File(java.io.File)

Example 18 with TeeOutputStream

use of org.apache.commons.io.output.TeeOutputStream in project jmeter by apache.

the class FTPSampler method sample.

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    // Assume failure
    res.setSuccessful(false);
    String remote = getRemoteFilename();
    String local = getLocalFilename();
    boolean binaryTransfer = isBinaryMode();
    res.setSampleLabel(getName());
    final String label = getLabel();
    res.setSamplerData(label);
    try {
        res.setURL(new URL(label));
    } catch (MalformedURLException e1) {
        log.warn("Cannot set URL: " + e1.getLocalizedMessage());
    }
    InputStream input = null;
    FileInputStream fileIS = null;
    res.sampleStart();
    FTPClient ftp = new FTPClient();
    try {
        savedClient = ftp;
        final int port = getPortAsInt();
        if (port > 0) {
            ftp.connect(getServer(), port);
        } else {
            ftp.connect(getServer());
        }
        res.latencyEnd();
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            if (ftp.login(getUsername(), getPassword())) {
                if (binaryTransfer) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                // should probably come from the setup dialog
                ftp.enterLocalPassiveMode();
                boolean ftpOK = false;
                if (isUpload()) {
                    String contents = getLocalFileContents();
                    if (contents.length() > 0) {
                        @SuppressWarnings("DefaultCharset") byte[] // TODO - charset?
                        bytes = contents.getBytes();
                        input = new ByteArrayInputStream(bytes);
                        res.setSentBytes((long) bytes.length);
                    } else {
                        File infile = new File(local);
                        res.setSentBytes(infile.length());
                        // NOSONAR False positive, fileIS is closed in finally and not overwritten
                        fileIS = new FileInputStream(infile);
                        input = new BufferedInputStream(fileIS);
                    }
                    ftpOK = ftp.storeFile(remote, input);
                } else {
                    final boolean saveResponse = isSaveResponse();
                    // No need to close this
                    ByteArrayOutputStream baos = null;
                    OutputStream target = null;
                    OutputStream output = null;
                    try {
                        if (saveResponse) {
                            baos = new ByteArrayOutputStream();
                            target = baos;
                        }
                        if (local.length() > 0) {
                            // NOSONAR False positive, the output is closed in finally and not overwritten
                            output = new FileOutputStream(local);
                            if (target == null) {
                                target = output;
                            } else {
                                target = new TeeOutputStream(output, baos);
                            }
                        }
                        if (target == null) {
                            target = NullOutputStream.NULL_OUTPUT_STREAM;
                        }
                        input = ftp.retrieveFileStream(remote);
                        if (input == null) {
                            // Could not access file or other error
                            res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                            res.setResponseMessage(ftp.getReplyString());
                        } else {
                            long bytes = IOUtils.copy(input, target);
                            ftpOK = bytes > 0;
                            if (saveResponse) {
                                saveResponse(res, binaryTransfer, baos);
                            } else {
                                res.setBytes(bytes);
                            }
                        }
                    } finally {
                        IOUtils.closeQuietly(target, null);
                        IOUtils.closeQuietly(output, null);
                    }
                }
                if (ftpOK) {
                    res.setResponseCodeOK();
                    res.setResponseMessageOK();
                    res.setSuccessful(true);
                } else {
                    res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                    res.setResponseMessage(ftp.getReplyString());
                }
            } else {
                res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                res.setResponseMessage(ftp.getReplyString());
            }
        } else {
            res.setResponseCode("501");
            String replyString = ftp.getReplyString();
            if (StringUtils.isEmpty(replyString)) {
                res.setResponseMessage("Could not connect");
            } else {
                res.setResponseMessage(replyString);
            }
        }
    } catch (IOException ex) {
        res.setResponseCode("000");
        res.setResponseMessage(ex.toString());
    } finally {
        savedClient = null;
        if (ftp.isConnected()) {
            try {
                ftp.logout();
            } catch (IOException ignored) {
            // NOOP
            }
            try {
                ftp.disconnect();
            } catch (IOException ignored) {
            // NOOP
            }
        }
        IOUtils.closeQuietly(input, null);
        IOUtils.closeQuietly(fileIS, null);
    }
    res.sampleEnd();
    return res;
}
Also used : MalformedURLException(java.net.MalformedURLException) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) FTPClient(org.apache.commons.net.ftp.FTPClient) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) SampleResult(org.apache.jmeter.samplers.SampleResult) File(java.io.File)

Example 19 with TeeOutputStream

use of org.apache.commons.io.output.TeeOutputStream in project pentaho-kettle by pentaho.

the class Spoon method main.

/**
 * This is the main procedure for Spoon.
 *
 * @param a
 *     Arguments are available in the "Get System Info" step.
 */
public static void main(String[] a) throws KettleException {
    boolean doConsoleRedirect = !Boolean.getBoolean("Spoon.Console.Redirect.Disabled");
    if (doConsoleRedirect) {
        try {
            Path parent = Paths.get(System.getProperty("user.dir") + File.separator + "logs");
            Files.createDirectories(parent);
            Files.deleteIfExists(Paths.get(parent.toString(), "spoon.log"));
            Path path = Files.createFile(Paths.get(parent.toString(), "spoon.log"));
            System.setProperty("LOG_PATH", path.toString());
            final FileOutputStream fos = new FileOutputStream(path.toFile());
            System.setOut(new PrintStream(new TeeOutputStream(originalSystemOut, fos)));
            System.setErr(new PrintStream(new TeeOutputStream(originalSystemErr, fos)));
            KettleLogStore.OriginalSystemOut = System.out;
            KettleLogStore.OriginalSystemErr = System.err;
        } catch (Throwable ignored) {
        // ignored
        }
    }
    ExecutorService executor = Executors.newCachedThreadPool();
    Future<KettleException> pluginRegistryFuture = executor.submit(new Callable<KettleException>() {

        @Override
        public KettleException call() throws Exception {
            registerUIPluginObjectTypes();
            KettleClientEnvironment.getInstance().setClient(KettleClientEnvironment.ClientType.SPOON);
            try {
                KettleEnvironment.init();
            } catch (KettleException e) {
                return e;
            }
            return null;
        }
    });
    try {
        OsHelper.setAppName();
        // Bootstrap Kettle
        // 
        Display display;
        if (System.getProperties().containsKey("SLEAK")) {
            DeviceData data = new DeviceData();
            data.tracking = true;
            display = new Display(data);
            Sleak sleak = new Sleak();
            Shell sleakShell = new Shell(display);
            sleakShell.setText("S-Leak");
            org.eclipse.swt.graphics.Point size = sleakShell.getSize();
            sleakShell.setSize(size.x / 2, size.y / 2);
            sleak.create(sleakShell);
            sleakShell.open();
        } else {
            display = new Display();
        }
        // Note: this needs to be done before the look and feel is set
        OsHelper.initOsHandlers(display);
        UIManager.setLookAndFeel(new MetalLookAndFeel());
        // The core plugin types don't know about UI classes. Add them in now
        // before the PluginRegistry is inited.
        splash = new Splash(display);
        List<String> args = new ArrayList<>(Arrays.asList(a));
        CommandLineOption[] commandLineOptions = getCommandLineArgs(args);
        KettleException registryException = pluginRegistryFuture.get();
        if (registryException != null) {
            throw registryException;
        }
        PropsUI.init(display, Props.TYPE_PROPERTIES_SPOON);
        KettleLogStore.init(PropsUI.getInstance().getMaxNrLinesInLog(), PropsUI.getInstance().getMaxLogLineTimeoutMinutes());
        initLogging(commandLineOptions);
        // remember...
        staticSpoon = new Spoon();
        staticSpoon.commandLineOptions = commandLineOptions;
        // pull the startup perspective id from the command line options and hand it to Spoon
        String pId;
        StringBuilder perspectiveIdBuff = Spoon.getCommandLineOption(commandLineOptions, "perspective").getArgument();
        pId = perspectiveIdBuff.toString();
        if (!Utils.isEmpty(pId)) {
            Spoon.staticSpoon.startupPerspective = pId;
        }
        SpoonFactory.setSpoonInstance(staticSpoon);
        staticSpoon.setDestroy(true);
        GUIFactory.setThreadDialogs(new ThreadGuiResources());
        staticSpoon.setArguments(args.toArray(new String[args.size()]));
        staticSpoon.start();
    } catch (Throwable t) {
        // avoid calls to Messages i18n method getString() in this block
        // We do this to (hopefully) also catch Out of Memory Exceptions
        // 
        t.printStackTrace();
        if (staticSpoon != null) {
            log.logError("Fatal error : " + Const.NVL(t.toString(), Const.NVL(t.getMessage(), "Unknown error")));
            log.logError(Const.getStackTracker(t));
        }
    }
    // Kill all remaining things in this VM!
    System.exit(0);
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) ArrayList(java.util.ArrayList) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) MetalLookAndFeel(javax.swing.plaf.metal.MetalLookAndFeel) CommandLineOption(org.pentaho.di.pan.CommandLineOption) Shell(org.eclipse.swt.widgets.Shell) DeviceData(org.eclipse.swt.graphics.DeviceData) Path(java.nio.file.Path) PrintStream(java.io.PrintStream) SWTException(org.eclipse.swt.SWTException) KettleRowException(org.pentaho.di.core.exception.KettleRowException) FileSystemException(org.apache.commons.vfs2.FileSystemException) MetaStoreException(org.pentaho.metastore.api.exceptions.MetaStoreException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleAuthException(org.pentaho.di.core.exception.KettleAuthException) KettleRepositoryLostException(org.pentaho.di.repository.KettleRepositoryLostException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) LifecycleException(org.pentaho.di.core.lifecycle.LifecycleException) KettleMissingPluginsException(org.pentaho.di.core.exception.KettleMissingPluginsException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) KettleException(org.pentaho.di.core.exception.KettleException) MalformedURLException(java.net.MalformedURLException) ThreadGuiResources(org.pentaho.di.ui.util.ThreadGuiResources) FileOutputStream(java.io.FileOutputStream) ExecutorService(java.util.concurrent.ExecutorService) Splash(org.pentaho.di.ui.core.dialog.Splash) Display(org.eclipse.swt.widgets.Display)

Aggregations

TeeOutputStream (org.apache.commons.io.output.TeeOutputStream)19 OutputStream (java.io.OutputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 FileOutputStream (java.io.FileOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 File (java.io.File)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)4 JsonParser (com.fasterxml.jackson.core.JsonParser)3 BufferedOutputStream (java.io.BufferedOutputStream)3 InputStreamReader (java.io.InputStreamReader)3 PrintStream (java.io.PrintStream)3 MalformedURLException (java.net.MalformedURLException)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2 List (java.util.List)2 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)2 NullOutputStream (org.apache.commons.io.output.NullOutputStream)2 DataSet (org.talend.dataprep.api.dataset.DataSet)2 TransformationCacheKey (org.talend.dataprep.cache.TransformationCacheKey)2