org.apache.commons.io.input.CloseShieldInputStream Java Examples

The following examples show how to use org.apache.commons.io.input.CloseShieldInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ActivityStreamInterceptor.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
private String addParamsFromRequestBody(String prev, InputStream is) {
    if (null == is) {
        return prev;
    }
    try {
        CloseShieldInputStream cloned = new CloseShieldInputStream(is);
        String body = FileTextReader.getText(cloned);
        int bodyLength = body.length();
        if (bodyLength > 0) {
            byte[] bytes = body.getBytes();
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, String> params;
            params = objectMapper.readValue(bytes, HashMap.class);
            return this.addParameters(prev, params);
        }
        return prev;
    } catch (Exception e) {
        logger.error("System exception {}", e.getMessage(), e);
        return prev;
    }
}
 
Example #2
Source File: InputStreamPump.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
public InputStreamPump(InputStream source, final Pump pump) throws IOException {
    this.source = source;

    out = new PipedOutputStream();
    in = new PipedInputStream(out, 8192);

    pumpThread = new Thread(new Runnable() {
        public void run() {
            try {
                pump.run(new CloseShieldInputStream(in));
                // ensure that input stream is pumping in case it didn't read to the end
                byte[] buffer = new byte[8192];
                while (in.read(buffer) >= 0);
            } catch (Exception e) {
                error = e;
                log.error("Error while processing input stream", e);
            }
        }
    });
    pumpThread.start();
}
 
Example #3
Source File: DocumentViewParserValidator.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
protected Collection<ValidationMessage> validateDocumentViewXml(InputStream input, @NotNull Path filePath, @NotNull Path basePath, String rootNodePath,
        Map<String, Integer> nodePathsAndLineNumbers) throws IOException, SAXException {
    List<ValidationMessage> enrichedMessages = new LinkedList<>();
    XMLReader xr = saxParser.getXMLReader();
    final DocumentViewXmlContentHandler handler = new DocumentViewXmlContentHandler(filePath, basePath, rootNodePath,
            docViewValidators);
    enrichedMessages.add(new ValidationMessage(ValidationMessageSeverity.DEBUG, "Detected DocView..."));
    xr.setContentHandler(handler);
    try {
        xr.parse(new InputSource(new CloseShieldInputStream(input)));
        enrichedMessages.addAll(ValidationViolation.wrapMessages(null, handler.getViolations(), filePath, basePath, rootNodePath, 0, 0));
    } catch (SAXException e) {
        enrichedMessages.add(new ValidationViolation(severity, "Invalid XML found: " + e.getMessage(), filePath, basePath, rootNodePath, 0, 0, e));
    }
    nodePathsAndLineNumbers.putAll(handler.getNodePaths());
    return enrichedMessages;
}
 
Example #4
Source File: FixedLengthInputStream.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 5 votes vote down vote up
public FixedLengthInputStream(InputStream stream, long maxLen) {
  super(new CountingInputStream(new CloseShieldInputStream(stream)));

  // Save a correctly-typed reference to the underlying stream.
  this.countingIn = (CountingInputStream) this.in;
  this.maxBytes = maxLen;
}
 
Example #5
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getStream() throws IOException {
  // we explicitly protect this servlet stream from being closed
  // so that it does not trip our test assert in our close shield
  // in SolrDispatchFilter - we must allow closes from getStream
  // due to the other impls of ContentStream
  return new CloseShieldInputStream(req.getInputStream());
}
 
Example #6
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SolrParams parseParamsAndFillStreams(HttpServletRequest req, ArrayList<ContentStream> streams, InputStream in) throws Exception {
  final Map<String,String[]> map = new HashMap<>();

  // also add possible URL parameters and include into the map (parsed using UTF-8):
  final String qs = req.getQueryString();
  if (qs != null) {
    parseQueryString(qs, map);
  }

  // may be -1, so we check again later. But if it's already greater we can stop processing!
  final long totalLength = req.getContentLength();
  final long maxLength = ((long) uploadLimitKB) * 1024L;
  if (totalLength > maxLength) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "application/x-www-form-urlencoded content length (" +
        totalLength + " bytes) exceeds upload limit of " + uploadLimitKB + " KB");
  }

  // get query String from request body, using the charset given in content-type:
  final String cs = ContentStreamBase.getCharsetFromContentType(req.getContentType());
  final Charset charset = (cs == null) ? StandardCharsets.UTF_8 : Charset.forName(cs);

  try {
    // Protect container owned streams from being closed by us, see SOLR-8933
    in = FastInputStream.wrap( in == null ? new CloseShieldInputStream(req.getInputStream()) : in );

    final long bytesRead = parseFormDataContent(in, maxLength, charset, map, false);
    if (bytesRead == 0L && totalLength > 0L) {
      throw getParameterIncompatibilityException();
    }
  } catch (IOException ioe) {
    throw new SolrException(ErrorCode.BAD_REQUEST, ioe);
  } catch (IllegalStateException ise) {
    throw (SolrException) getParameterIncompatibilityException().initCause(ise);
  } finally {
    IOUtils.closeWhileHandlingException(in);
  }

  return new MultiMapSolrParams(map);
}
 
Example #7
Source File: PointFileReaderWriter.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
@VisibleForTesting
static void readStream(final InputStream stream, final Consumer<String> lineParser)
    throws IOException {
  try (Reader inputStreamReader =
          new InputStreamReader(new CloseShieldInputStream(stream), StandardCharsets.UTF_8);
      BufferedReader reader = new BufferedReader(inputStreamReader)) {
    reader.lines().filter(current -> current.trim().length() != 0).forEachOrdered(lineParser);
  } catch (final IllegalArgumentException e) {
    throw new IOException(e);
  }
}
 
Example #8
Source File: LoadReportTask.java    From extract with MIT License 5 votes vote down vote up
@Override
public Void call() throws Exception {
	final DocumentFactory factory = new DocumentFactory().configure(options);

	try (final InputStream input = new CloseShieldInputStream(System.in);
	     final ReportMap reportMap = new ReportMapFactory(options)
			     .withDocumentFactory(factory)
			     .createShared()) {
		load(factory, reportMap, input);
	}

	return null;
}
 
Example #9
Source File: LoadQueueTask.java    From extract with MIT License 5 votes vote down vote up
@Override
public Void call() throws Exception {
	final DocumentFactory factory = new DocumentFactory().configure(options);

	try (final InputStream input = new CloseShieldInputStream(System.in);
	     final DocumentQueue queue = new DocumentQueueFactory(options)
			     .withDocumentFactory(factory)
			     .createShared()) {
		load(factory, queue, input);
	}

	return null;
}
 
Example #10
Source File: P2Unzipper.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private void processMetaData ( final Context context, final InputStream in, final String filename, final String xpath ) throws Exception
{
    // parse input
    final Document doc = this.xml.newDocumentBuilder ().parse ( new CloseShieldInputStream ( in ) );
    final XPathExpression path = this.xml.newXPathFactory ().newXPath ().compile ( xpath );

    // filter
    final NodeList result = XmlHelper.executePath ( doc, path );

    // write filtered output
    final Document fragmentDoc = this.xml.newDocumentBuilder ().newDocument ();
    Node node = result.item ( 0 );
    node = fragmentDoc.adoptNode ( node );
    fragmentDoc.appendChild ( node );

    // create artifact
    context.createVirtualArtifact ( filename, out -> {
        try
        {
            XmlHelper.write ( this.xml.newTransformerFactory (), fragmentDoc, new StreamResult ( out ) );
        }
        catch ( final Exception e )
        {
            throw new IOException ( e );
        }
    }, null );
}
 
Example #11
Source File: DefaultMetaInf.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * <p>The specified stream remains open after this method returns.
 * @param in
 * @param systemId
 * @throws IOException
 */
public void loadProperties(@NotNull InputStream in, @NotNull String systemId)
        throws IOException {
    Properties props = new Properties();
    // prevent the input stream from being closed for achieving a consistent behaviour
    props.loadFromXML(new CloseShieldInputStream(in));
    setProperties(props);
    log.trace("Loaded properties from {}.", systemId);
}
 
Example #12
Source File: DocumentViewParserValidator.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/** @param input the given input stream must be reset later on
 * @param path
 * @return either the path of the root node of the given docview xml or {@code null} if no docview xml given
 * @throws IOException */
private static Path getDocumentViewXmlRootPath(BufferedInputStream input, Path path) throws IOException {
    Path name = path.getFileName();
    Path rootPath = null;

    int nameCount = path.getNameCount();
    if (name.equals(Paths.get(Constants.DOT_CONTENT_XML))) {
        if (nameCount > 1) {
            rootPath = path.subpath(0, nameCount - 1);
        } else {
            rootPath = Paths.get("");
        }
        // correct suffix matching
    } else if (name.toString().endsWith(".xml")) {

        // we need to rely on a buffered input stream to be able to reset it later
        input.mark(1024);
        // analyze content
        // this closes the input source internally, therefore protect against closing
        // make sure to initialize the SLF4J logger appropriately (for the XmlAnalyzer)
        try {
            SerializationType type = XmlAnalyzer.analyze(new InputSource(new CloseShieldInputStream(input)));
            if (type == SerializationType.XML_DOCVIEW) {
                //  remove .xml extension
                String fileName = path.getFileName().toString();
                fileName = fileName.substring(0, fileName.length() - ".xml".length());
                if (nameCount > 1) {
                    rootPath = path.subpath(0, nameCount - 1).resolve(fileName);
                } else {
                    rootPath = Paths.get(fileName);
                }
            }
        } finally {
            input.reset();
        }
    }
    return rootPath;
}
 
Example #13
Source File: ConfigFactory.java    From lancoder with GNU General Public License v3.0 5 votes vote down vote up
private T generateConfigFromUserPrompt() {
	System.out.println("Please enter the following fields. Default values are in [].");
	System.out.println("To use the default value, hit return.");
	T config = null;

	try (Scanner s = new Scanner(new CloseShieldInputStream(System.in))) {
		// Finish setting up the scanner
		s.useDelimiter(System.lineSeparator());

		// Determine if the user wants to change advanced configuration
		boolean useAdvancedSettings = promptUserForAdvancedSettings(s);

		// Load fields dynamically from the provided configuration class
		config = clazz.newInstance();
		ArrayList<Entry<Field, Prompt>> fields = getFields(useAdvancedSettings);

		for (Entry<Field, Prompt> entry : fields) {
			Field field = entry.getKey();
			String message = entry.getValue().message();
			field.setAccessible(true);

			System.out.printf("%s [%s]: ", message, field.get(config));
			String input = s.nextLine();

			if (!input.isEmpty()) {
				if (field.getType() == java.lang.Integer.TYPE) {
					field.set(config, Integer.valueOf(input));
				} else {
					field.set(config, input);
				}
			}
		}
	} catch (InstantiationException | IllegalAccessException e) {
		e.printStackTrace();
	}
	return config;
}
 
Example #14
Source File: SafeXMLParsing.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Parses the given InputStream as XML, disabling any external entities with secure processing enabled.
 * The given InputStream is not closed. */
public static Document parseUntrustedXML(Logger log, InputStream in) throws SAXException, IOException {
  return getUntrustedDocumentBuilder(log).parse(new CloseShieldInputStream(in), SYSTEMID_UNTRUSTED);
}
 
Example #15
Source File: DefaultPackageProperties.java    From jackrabbit-filevault with Apache License 2.0 4 votes vote down vote up
protected static Properties getPropertiesMap(InputStream input) throws InvalidPropertiesFormatException, IOException {
    Properties propertyMap = new Properties();
    propertyMap.loadFromXML(new CloseShieldInputStream(input));
    return propertyMap;
}
 
Example #16
Source File: ArchiveInput.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Archive input stream needs to be wrapped to prevent it from being closed.
 * 
 * @param is
 * @return
 * @throws IOException
 */
protected InputStream wrap(InputStream is) throws IOException {
	return new CloseShieldInputStream(is);
}