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

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

Introduction

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

Prototype

public void close() 

Source Link

Document

Replaces the underlying input stream with a ClosedInputStream sentinel.

Usage

From source file:com.adobe.communities.ugc.migration.importer.ImportFileUploadServlet.java

private void saveExplodedFiles(final ResourceResolver resolver, final Resource folder, final JSONWriter writer,
        final ZipInputStream zipInputStream, final String basePath) throws ServletException {

    // we need the closeShieldInputStream to prevent the zipInputStream from being closed during resolver.create()
    final CloseShieldInputStream closeShieldInputStream = new CloseShieldInputStream(zipInputStream);

    // fileResourceProperties and folderProperties will be reused inside the while loop
    final Map<String, Object> fileResourceProperties = new HashMap<String, Object>();
    fileResourceProperties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_FILE);

    final Map<String, Object> folderProperties = new HashMap<String, Object>();
    folderProperties.put(JcrConstants.JCR_PRIMARYTYPE, "sling:Folder");

    ZipEntry zipEntry;/*ww  w . j a  v  a 2s . c o  m*/
    try {
        zipEntry = zipInputStream.getNextEntry();
    } catch (final IOException e) {
        throw new ServletException("Unable to read entries from uploaded zip archive", e);
    }
    final List<Resource> toDelete = new ArrayList<Resource>();
    while (zipEntry != null) {
        // store files under the provided folder
        try {
            final String name = ResourceUtil.normalize("/" + zipEntry.getName());
            if (null == name) {
                // normalize filename and if they aren't inside upload path, don't store them
                continue;
            }
            Resource parent = folder;
            Resource subFolder = null;
            for (final String subFolderName : name.split("/")) {
                // check if the sub-folder already exists
                subFolder = resolver.getResource(parent, subFolderName);
                if (null == subFolder || subFolder instanceof NonExistingResource) {
                    // create the sub-folder
                    subFolder = resolver.create(parent, subFolderName, folderProperties);
                }
                parent = subFolder;
            }
            if (!zipEntry.isDirectory()) {
                // first represent the file as a resource
                final Resource file = resolver.create(subFolder, "file", fileResourceProperties);
                // now store its data as a jcr:content node
                final Map<String, Object> fileProperties = new HashMap<String, Object>();
                byte[] bytes = IOUtils.toByteArray(closeShieldInputStream);
                fileProperties.put(JcrConstants.JCR_DATA, new String(bytes, "UTF8"));
                fileProperties.put(JcrConstants.JCR_PRIMARYTYPE, "nt:resource");
                resolver.create(file, JcrConstants.JCR_CONTENT, fileProperties);

                // if provided a basePath, import immediately
                if (StringUtils.isNotBlank(basePath) && null != file
                        && !(file instanceof NonExistingResource)) {
                    Resource fileContent = file.getChild(JcrConstants.JCR_CONTENT);
                    if (null != fileContent && !(fileContent instanceof NonExistingResource)) {
                        final ValueMap contentVM = fileContent.getValueMap();
                        InputStream inputStream = (InputStream) contentVM.get(JcrConstants.JCR_DATA);
                        if (inputStream != null) {
                            final JsonParser jsonParser = new JsonFactory().createParser(inputStream);
                            jsonParser.nextToken(); // get the first token
                            String resName = basePath + name.substring(0, name.lastIndexOf(".json"));
                            Resource resource = resolver.getResource(resName);
                            if (resource == null) {
                                // voting does not have a node under articles
                                resource = resolver.getResource(resName.substring(0, resName.lastIndexOf("/")));
                            }
                            try {
                                importFile(jsonParser, resource, resolver);
                                toDelete.add(file);
                            } catch (final Exception e) {
                                // add the file name to our response ONLY if we failed to import it
                                writer.value(name);
                                // we want to log the reason we weren't able to import, but don't stop importing
                                LOG.error(e.getMessage());
                            }
                        }
                    }
                } else if (StringUtils.isBlank(basePath) && null != file
                        && !(file instanceof NonExistingResource)) {
                    // add the file name to our response
                    writer.value(name);
                }
            }
            resolver.commit();
            zipEntry = zipInputStream.getNextEntry();
        } catch (final IOException e) {
            // convert any IOExceptions into ServletExceptions
            throw new ServletException(e.getMessage(), e);
        } catch (final JSONException e) {
            // convert any JSONExceptions into ServletExceptions
            throw new ServletException(e.getMessage(), e);
        }
    }
    closeShieldInputStream.close();
    // delete any files that were successfully imported
    if (!toDelete.isEmpty()) {
        for (final Resource deleteResource : toDelete) {
            deleteResource(deleteResource);
        }
    }
}

From source file:org.erdc.cobie.shared.COBieUtility.java

public static boolean isValidSchemaDocument(InputStream inputStream, SchemaType type) {
    boolean valid = false;
    try {/*from w  ww . j  av  a  2s. c  o m*/
        CloseShieldInputStream inputStreamCopy = new CloseShieldInputStream(inputStream);
        org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse(inputStreamCopy, type, null);
        valid = true;
        inputStreamCopy.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return valid;
}

From source file:org.erdc.cobie.shared.spreadsheetml.transformation.cobietab.COBieSpreadSheet.java

License:asdf

public static boolean isWorkbook(InputStream candidateWorksheet) {
    CloseShieldInputStream inputStreamCopy = new CloseShieldInputStream(candidateWorksheet);
    boolean isWorkbook = false;
    try {//  www  . j  a  va 2s  .com
        nl.fountain.xelem.lex.ExcelReader rdr = new nl.fountain.xelem.lex.ExcelReader();
        Workbook workbook = rdr.getWorkbook(new InputSource(inputStreamCopy));
        isWorkbook = ((workbook != null) && workbook.hasExcelWorkbook());
    } catch (Exception ex) {

    } finally {
        inputStreamCopy.close();
    }
    return isWorkbook;
}