List of usage examples for org.apache.commons.io.input CloseShieldInputStream CloseShieldInputStream
public CloseShieldInputStream(InputStream in)
From source file:org.exist.xquery.modules.httpclient.BaseHTTPClientFunction.java
/** * Takes the HTTP Response Body from the HTTP Method and attempts to insert it into the response tree we are building. * * <p>Conversion Preference - 1) Try and parse as XML, if successful returns a Node 2) Try and parse as HTML returning as XML compatible HTML, if * successful returns a Node 3) Return as base64Binary encoded data</p> * * @param context The context of the calling XQuery * @param method The HTTP Request Method * @param builder The MemTreeBuilder that is being used * * @throws IOException /*from w ww . j a va 2s.c o m*/ * @throws XPathException */ private void insertResponseBody(final XQueryContext context, final HttpMethod method, final MemTreeBuilder builder, final Map<String, Boolean> parserFeatures, final Map<String, String> parserProperties) throws IOException, XPathException { NodeImpl responseNode = null; final InputStream bodyAsStream = method.getResponseBodyAsStream(); // check if there is a response body if (bodyAsStream != null) { CachingFilterInputStream cfis = null; FilterInputStreamCache cache = null; try { //we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt) cache = FilterInputStreamCacheFactory .getCacheInstance(new FilterInputStreamCacheFactory.FilterInputStreamCacheConfiguration() { @Override public String getCacheClass() { return (String) context.getBroker().getConfiguration() .getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY); } }); cfis = new CachingFilterInputStream(cache, bodyAsStream); //mark the start of the stream cfis.mark(Integer.MAX_VALUE); // determine the type of the response document final Header responseContentType = method.getResponseHeader("Content-Type"); final MimeType responseMimeType = getResponseMimeType(responseContentType); if (responseContentType != null) { builder.addAttribute(new QName("mimetype", null, null), responseContentType.getValue()); } //try and parse the response as XML try { //we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread final InputStream shieldedInputStream = new CloseShieldInputStream(cfis); responseNode = (NodeImpl) ModuleUtils.streamToXML(context, shieldedInputStream); builder.addAttribute(new QName("type", null, null), "xml"); responseNode.copyTo(null, new DocumentBuilderReceiver(builder)); } catch (final SAXException se) { // could not parse to xml // not an error in itself, it will be treated either as HTML, // text or binary here below final String msg = "Request for URI '" + method.getURI().toString() + "' Could not parse http response content as XML (will try html, text or fallback to binary): " + se.getMessage(); if (logger.isDebugEnabled()) { logger.debug(msg, se); } else { logger.info(msg); } } catch (final IOException ioe) { final String msg = "Request for URI '" + method.getURI().toString() + "' Could not read http response content: " + ioe.getMessage(); logger.error(msg, ioe); throw new XPathException(msg, ioe); } if (responseNode == null) { //response is NOT parseable as XML //is it a html document? if (responseMimeType.getName().equals(MimeType.HTML_TYPE.getName())) { //html document try { //reset the stream to the start, as we need to reuse since attempting to parse to XML cfis.reset(); //parse html to xml(html) //we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread final InputStream shieldedInputStream = new CloseShieldInputStream(cfis); responseNode = (NodeImpl) ModuleUtils .htmlToXHtml(context, method.getURI().toString(), new InputSource(shieldedInputStream), parserFeatures, parserProperties) .getDocumentElement(); builder.addAttribute(new QName("type", null, null), "xhtml"); responseNode.copyTo(null, new DocumentBuilderReceiver(builder)); } catch (final URIException ue) { throw new XPathException(this, ue.getMessage(), ue); } catch (final SAXException se) { //could not parse to xml(html) logger.debug( "Could not parse http response content from HTML to XML: " + se.getMessage(), se); } } } if (responseNode == null) { //reset the stream to the start, as we need to reuse since attempting to parse to HTML->XML cfis.reset(); if (responseMimeType.getName().startsWith("text/")) { // Assume it's a text body and URL encode it builder.addAttribute(new QName("type", null, null), "text"); builder.addAttribute(new QName("encoding", null, null), "URLEncoded"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte buf[] = new byte[4096]; int read = -1; while ((read = cfis.read(buf)) > -1) { baos.write(buf); } builder.characters(URLEncoder.encode(EncodingUtil.getString(baos.toByteArray(), ((HttpMethodBase) method).getResponseCharSet()), "UTF-8")); baos.close(); } else { // Assume it's a binary body and Base64 encode it builder.addAttribute(new QName("type", null, null), "binary"); builder.addAttribute(new QName("encoding", null, null), "Base64Encoded"); BinaryValue binary = null; try { binary = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), cfis); builder.characters(binary.getStringValue()); } finally { // free resources if (binary != null) { binary.destroy(context, null); } } } } } finally { if (cache != null) { try { cache.invalidate(); } catch (final IOException ioe) { LOG.error(ioe.getMessage(), ioe); } } if (cfis != null) { try { cfis.close(); } catch (final IOException ioe) { LOG.error(ioe.getMessage(), ioe); } } } } }
From source file:org.expath.exist.ftclient.GetResourceMetadataFunction.java
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { Sequence result = new ValueSequence(); StreamResult resultAsStreamResult = null; try {//from w w w . j a v a 2s . com resultAsStreamResult = ro.kuberam.libs.java.ftclient.GetResourceMetadata .getResourceMetadata(ExistExpathFTClientModule.retrieveRemoteConnection(context, ((IntegerValue) args[0].itemAt(0)).getLong()), args[1].getStringValue()); } catch (Exception ex) { throw new XPathException(ex.getMessage()); } ByteArrayInputStream resultDocAsInputStream = null; try { resultDocAsInputStream = new ByteArrayInputStream( resultAsStreamResult.getWriter().toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException ex) { throw new XPathException(ex.getMessage()); } XMLReader reader = null; context.pushDocumentContext(); try { InputSource src = new InputSource(new CloseShieldInputStream(resultDocAsInputStream)); reader = context.getBroker().getBrokerPool().getParserPool().borrowXMLReader(); MemTreeBuilder builder = context.getDocumentBuilder(); DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true); reader.setContentHandler(receiver); reader.parse(src); Document doc = receiver.getDocument(); result = (NodeValue) doc; } catch (SAXException saxe) { // do nothing, we will default to trying to return a string below } catch (IOException ioe) { // do nothing, we will default to trying to return a string below } finally { context.popDocumentContext(); if (reader != null) { context.getBroker().getBrokerPool().getParserPool().returnXMLReader(reader); } } return result; }
From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java
private UnpackResult unpack(CacheableEntity entity, TarArchiveInputStream tarInput, OriginReader readOriginAction) throws IOException { ImmutableMap.Builder<String, CacheableTree> treesBuilder = ImmutableMap.builder(); entity.visitTrees((name, type, root) -> { if (root != null) { treesBuilder.put(name, new CacheableTree(type, root)); }/*from w w w . j ava 2s.c om*/ }); ImmutableMap<String, CacheableTree> treesByName = treesBuilder.build(); TarArchiveEntry tarEntry; OriginMetadata originMetadata = null; Map<String, FileSystemLocationSnapshot> snapshots = new HashMap<String, FileSystemLocationSnapshot>(); tarEntry = tarInput.getNextTarEntry(); MutableLong entries = new MutableLong(); while (tarEntry != null) { entries.increment(1); String path = tarEntry.getName(); if (path.equals(METADATA_PATH)) { // handle origin metadata originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput)); tarEntry = tarInput.getNextTarEntry(); } else { // handle tree Matcher matcher = TREE_PATH.matcher(path); if (!matcher.matches()) { throw new IllegalStateException("Cached entry format error, invalid contents: " + path); } String treeName = unescape(matcher.group(2)); CacheableTree tree = treesByName.get(treeName); if (tree == null) { throw new IllegalStateException(String.format("No tree '%s' registered", treeName)); } boolean missing = matcher.group(1) != null; String childPath = matcher.group(3); tarEntry = unpackTree(treeName, tree.getType(), tree.getRoot(), tarInput, tarEntry, childPath, missing, snapshots, entries); } } if (originMetadata == null) { throw new IllegalStateException("Cached result format error, no origin metadata was found."); } return new UnpackResult(originMetadata, entries.get(), snapshots); }
From source file:org.gradle.caching.internal.tasks.TarTaskOutputPacker.java
private void unpack(TaskOutputsInternal taskOutputs, TarInputStream tarInput, TaskOutputOriginReader readOriginAction) throws IOException { Map<String, TaskOutputFilePropertySpec> propertySpecs = Maps.uniqueIndex(taskOutputs.getFileProperties(), new Function<TaskFilePropertySpec, String>() { @Override/*from w w w . j a v a2 s .c o m*/ public String apply(TaskFilePropertySpec propertySpec) { return propertySpec.getPropertyName(); } }); boolean originSeen = false; TarEntry entry; while ((entry = tarInput.getNextEntry()) != null) { String name = entry.getName(); if (name.equals(METADATA_PATH)) { // handle origin metadata originSeen = true; readOriginAction.execute(new CloseShieldInputStream(tarInput)); } else { // handle output property Matcher matcher = PROPERTY_PATH.matcher(name); if (!matcher.matches()) { throw new IllegalStateException("Cached result format error, invalid contents: " + name); } String propertyName = matcher.group(1); CacheableTaskOutputFilePropertySpec propertySpec = (CacheableTaskOutputFilePropertySpec) propertySpecs .get(propertyName); if (propertySpec == null) { throw new IllegalStateException( String.format("No output property '%s' registered", propertyName)); } File specRoot = propertySpec.getOutputFile(); String path = matcher.group(2); File outputFile; if (Strings.isNullOrEmpty(path)) { outputFile = specRoot; } else { outputFile = new File(specRoot, path); } if (entry.isDirectory()) { if (propertySpec.getOutputType() != OutputType.DIRECTORY) { throw new IllegalStateException( "Property should be an output directory property: " + propertyName); } FileUtils.forceMkdir(outputFile); } else { Files.asByteSink(outputFile).writeFrom(tarInput); } //noinspection OctalInteger fileSystem.chmod(outputFile, entry.getMode() & 0777); long lastModified = getModificationTime(entry); if (!outputFile.setLastModified(lastModified)) { throw new UnsupportedOperationException( String.format("Could not set modification time for '%s'", outputFile)); } } } if (!originSeen) { throw new IllegalStateException("Cached result format error, no origin metadata was found."); } }
From source file:org.lockss.filter.ZipFilterInputStream.java
/** * <p>/*w ww. j av a 2s.c om*/ * Before reading bytes, ensure that the stream has not been closed, that * end of file has not been reached on the underlying Zip input stream, and * that the previous read did not exhaust an entry; if so, it opens the next * entry's input stream and prepends it with the entry's normalized named. * </p> * * @throws IOException */ private void ensureInput() throws IOException { if (zipInputStream == null) { throw new IOException("stream closed"); } while (!eof && currentInputStream == null) { ZipEntry ze = zipInputStream.getNextEntry(); if (ze == null) { eof = true; return; } String zipEntryName = ze.getName(); String normalizedZipEntryName = zipEntryName; if (normalizedZipEntryName.startsWith("./")) { normalizedZipEntryName = normalizedZipEntryName.substring(2); } if (keepZipEntry(ze, normalizedZipEntryName)) { currentInputStream = new SequenceInputStream( new ByteArrayInputStream(normalizedZipEntryName.getBytes(Constants.ENCODING_UTF_8)), new CloseShieldInputStream(zipInputStream)); } } }
From source file:org.voyanttools.trombone.input.expand.ArchiveExpander.java
/** * Get a list of stored document sources from the specified archive stream * (that corresponds to the specfied parent stored document source). * //from ww w. j a v a 2 s . co m * @param archiveInputStream the full archive input stream * @param parentStoredDocumentSource the parent stored document source * @return a list of stored document sources in this archive * @throws IOException thrown when an IO exception occurs during unarchiving */ private List<StoredDocumentSource> getExpandedDocumentSources(ArchiveInputStream archiveInputStream, StoredDocumentSource parentStoredDocumentSource) throws IOException { List<StoredDocumentSource> expandedDocumentSources = new ArrayList<StoredDocumentSource>(); ArchiveEntry archiveEntry = archiveInputStream.getNextEntry(); String parentId = parentStoredDocumentSource.getId(); DocumentMetadata parentMetadata = parentStoredDocumentSource.getMetadata(); while (archiveEntry != null) { if (archiveEntry.isDirectory() == false) { final String filename = archiveEntry.getName(); final File file = new File(filename); // skip directories and skippable files if (DocumentFormat.isSkippable(file) == false) { DocumentMetadata childMetadata = parentMetadata.asParent(parentStoredDocumentSource.getId(), DocumentMetadata.ParentType.EXPANSION); childMetadata.setLocation(file.toString()); childMetadata.setModified(archiveEntry.getLastModifiedDate().getTime()); childMetadata.setSource(Source.STREAM); childMetadata.setTitle(file.getName().replaceFirst("\\.\\w+$", "")); String id = DigestUtils.md5Hex(parentId + filename); InputSource inputSource = new InputStreamInputSource(id, childMetadata, new CloseShieldInputStream(archiveInputStream)); StoredDocumentSource storedDocumentSource = storedDocumentSourceStorage .getStoredDocumentSource(inputSource); expandedDocumentSources .addAll(this.expander.getExpandedStoredDocumentSources(storedDocumentSource)); // expand this recursively } } archiveEntry = archiveInputStream.getNextEntry(); } return expandedDocumentSources; }
From source file:org.xwiki.contrib.confluence.filter.internal.ConfluenceXMLPackage.java
private void fromStream(InputStream stream) throws IOException { // Get temporary folder this.directory = File.createTempFile("confluencexml", ""); this.directory.delete(); this.directory.mkdir(); this.temporaryDirectory = false; // Extract the zip ZipArchiveInputStream zais = new ZipArchiveInputStream(stream); for (ZipArchiveEntry zipEntry = zais.getNextZipEntry(); zipEntry != null; zipEntry = zais .getNextZipEntry()) {/*from w ww .j a va 2s . c om*/ if (!zipEntry.isDirectory()) { String path = zipEntry.getName(); File file = new File(this.directory, path); FileUtils.copyInputStreamToFile(new CloseShieldInputStream(zais), file); } } }
From source file:org.xwiki.contrib.dokuwiki.text.internal.input.DokuWikiInputFilterStream.java
private void saveEntryToDisk(ArchiveInputStream archiveInputStream, ArchiveEntry archiveEntry, File folderToSave) {/*from www .j ava 2s . co m*/ String entryName = archiveEntry.getName(); if (entryName.startsWith(KEY_DOKUWIKI)) { entryName = entryName.replaceFirst(KEY_DOKUWIKI + System.getProperty(KEY_FILE_SEPERATOR), ""); } if (!archiveEntry.isDirectory()) { try { /** * FileUtils.copyToFile closes the input stream, so, * Proxy stream prevents the archiveInputStream from being closed. * This is a workaround for a bug at commons-io side. * (https://issues.apache.org/jira/browse/IO-554). * If fixed on commons-io side, pass archiveInputStream to FileUtils.copyToFile() directly. */ CloseShieldInputStream proxy = new CloseShieldInputStream(archiveInputStream); FileUtils.copyToFile(proxy, new File(folderToSave, entryName)); } catch (IOException e) { this.logger.error("failed to write stream to file", e); } } }
From source file:org.xwiki.extension.xar.internal.handler.packager.DefaultPackager.java
public void parseDocument(InputStream in, ContentHandler documentHandler) throws ParserConfigurationException, SAXException, IOException, NotADocumentException { SAXParser saxParser = this.parserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); RootHandler handler = new RootHandler(this.componentManager); handler.setHandler("xwikidoc", documentHandler); xmlReader.setContentHandler(handler); try {//from www . jav a2 s . c o m xmlReader.parse(new InputSource(new CloseShieldInputStream(in))); } catch (UnknownRootElement e) { throw new NotADocumentException("Failed to parse stream", e); } }
From source file:org.xwiki.filter.confluence.xml.internal.ConfluenceXMLPackage.java
public ConfluenceXMLPackage(InputSource source) throws IOException, FilterException, XMLStreamException, FactoryConfigurationError, NumberFormatException, ConfigurationException { InputStream stream;// w ww . jav a 2s. co m if (source instanceof InputStreamInputSource) { stream = ((InputStreamInputSource) source).getInputStream(); } else { throw new FilterException( 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(); }