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

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

Introduction

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

Prototype

public CloseShieldInputStream(InputStream in) 

Source Link

Document

Creates a proxy that shields the given input stream from being closed.

Usage

From source file:org.xwiki.wikistream.confluence.xml.internal.ConfluenceXMLPackage.java

public ConfluenceXMLPackage(InputSource source) throws IOException, WikiStreamException, XMLStreamException,
        FactoryConfigurationError, NumberFormatException, ConfigurationException {
    InputStream stream;//from  ww w. java 2s.  co  m

    if (source instanceof InputStreamInputSource) {
        stream = ((InputStreamInputSource) source).getInputStream();
    } else {
        throw new WikiStreamException(
                String.format("Unsupported input source of type [%s]", source.getClass().getName()));
    }

    try {
        // Get temporary folder
        this.directory = File.createTempFile("confluencexml", "");
        this.directory.delete();
        this.directory.mkdir();
        this.directory.deleteOnExit();

        // Extract the zip
        ZipArchiveInputStream zais = new ZipArchiveInputStream(stream);
        for (ZipArchiveEntry zipEntry = zais.getNextZipEntry(); zipEntry != null; zipEntry = zais
                .getNextZipEntry()) {
            if (!zipEntry.isDirectory()) {
                String path = zipEntry.getName();
                File file = new File(this.directory, path);

                if (path.equals("entities.xml")) {
                    this.entities = file;
                } else if (path.equals("exportDescriptor.properties")) {
                    this.descriptor = file;
                }

                FileUtils.copyInputStreamToFile(new CloseShieldInputStream(zais), file);
            }
        }
    } finally {
        source.close();
    }

    // Initialize

    createTree();
}

From source file:org.xwiki.xar.XarPackage.java

/**
 * Read a XML descriptor of a XAR package (usually names package.xml).
 * //w  ww . j a  v  a 2s  . c om
 * @param stream the input stream to the XML file to parse
 * @throws XarException when failing to parse the descriptor
 * @throws IOException when failing to read the file
 */
public void readDescriptor(InputStream stream) throws XarException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder dBuilder;
    try {
        dBuilder = dbFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new XarException("Failed to create a new Document builder", e);
    }

    Document doc;
    try {
        // DocumentBuilder#parse close the passed stream which is not what we want
        doc = dBuilder.parse(new CloseShieldInputStream(stream));
    } catch (SAXException e) {
        throw new XarException("Failed to parse XML document", e);
    }

    // Normalize the document
    doc.getDocumentElement().normalize();

    // Read the document
    NodeList children = doc.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getTagName().equals(XarModel.ELEMENT_PACKAGE)) {
                readDescriptorPackage(element);
                break;
            }
        }
    }
}

From source file:wsattacker.plugin.intelligentdos.model.ResultModel.java

public void readIn(File file) {
    attacks = Lists.newArrayList();//w  w w  . j a  v a  2  s . com

    ZipInputStream stream = null;
    try {
        stream = new ZipInputStream(new FileInputStream(file));

        ZipEntry entry;
        while ((entry = stream.getNextEntry()) != null) {
            if (FILENAME_METADATA.equals(entry.getName())) {
                // create JAXB context and instantiate marshaller
                JAXBContext context = JAXBContext.newInstance(AttackMetaDataJAXB.class);
                Unmarshaller um = context.createUnmarshaller();
                AttackMetaDataJAXB amjaxb = (AttackMetaDataJAXB) um
                        .unmarshal(new CloseShieldInputStream(stream));

                ResultModel r = amjaxb.toResultModel();
                setStartDate(r.getStartDate());
                setStopDate(r.getStopDate());
                setMaximumRequestsPerSecond(r.getMaximumRequestsPerSecond());
                setNotPossible(r.getNotPossible());
                setThresholds(r.getThresholds());
            } else {
                attacks.add(readIn(stream));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:wsattacker.plugin.intelligentdos.model.ResultModel.java

private SuccessfulAttack readIn(ZipInputStream stream) throws IOException, JAXBException {
    ZipInputStream inputStream = new ZipInputStream(stream);

    SuccessfulAttack sa = null;/* w  w  w.  java2  s .  c o m*/
    String xmlWithPlaceholder = "";
    String utrContent = "";
    String trContent = "";
    List<Metric> utrMetrics = Lists.newArrayList();
    List<Metric> trMetrics = Lists.newArrayList();
    List<Metric> tpMetrics = Lists.newArrayList();
    ZipEntry entry;
    while ((entry = inputStream.getNextEntry()) != null) {
        if (FILENAME_METADATA.equals(entry.getName())) {
            // create JAXB context and instantiate marshaller
            JAXBContext context = JAXBContext.newInstance(SuccessfulAttackJAXB.class);
            Unmarshaller um = context.createUnmarshaller();
            SuccessfulAttackJAXB amjaxb = (SuccessfulAttackJAXB) um
                    .unmarshal(new CloseShieldInputStream(inputStream));

            sa = amjaxb.toSuccessfulAttack();
        } else if (FILENAME_UTR_DUR.equals(entry.getName())) {
            utrMetrics = streamToMetric(inputStream);
        } else if (FILENAME_TR_DUR.equals(entry.getName())) {
            trMetrics = streamToMetric(inputStream);
        } else if (FILENAME_TP_DUR.equals(entry.getName())) {
            tpMetrics = streamToMetric(inputStream);
        } else if (FILENAME_XML_PLACEHOLDER.equals(entry.getName())) {
            xmlWithPlaceholder = IOUtils.toString(inputStream);
        } else if (FILENAME_URT_CONTENT.equals(entry.getName())) {
            utrContent = IOUtils.toString(inputStream);
        } else if (FILENAME_TR_CONTENT.equals(entry.getName())) {
            trContent = IOUtils.toString(inputStream);
        } else {
            // nothing to do
        }
    }

    if (sa != null) {
        sa.setXmlWithPlaceholder(xmlWithPlaceholder);
        sa.setUntamperedContent(utrContent);
        sa.setTamperedContent(trContent);
        Long[] run1 = new Long[utrMetrics.size()];
        Long[] run2 = new Long[trMetrics.size()];

        int index = 0;
        for (Metric metric : utrMetrics) {
            sa.getUntamperedMetrics().add(metric);
            run1[index++] = metric.getDuration();
        }

        index = 0;
        for (Metric metric : trMetrics) {
            sa.getTamperedMetrics().add(metric);
            run2[index++] = metric.getDuration();
        }

        for (Metric metric : tpMetrics) {
            sa.getTestProbeMetrics().add(metric);
        }

        sa.setEfficiency(successDecider.getEfficency(run1, run2));
        sa.setRatio(successDecider.calculateRatio(run1, run2));
    }

    return sa;
}