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

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

Introduction

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

Prototype

public BoundedInputStream(InputStream in, long size) 

Source Link

Document

Creates a new BoundedInputStream that wraps the given input stream and limits it to a certain size.

Usage

From source file:org.alfresco.solr.SolrInformationServer.java

private void addContentPropertyToDocUsingAlfrescoRepository(SolrInputDocument doc, QName propertyQName,
        long dbId, String locale) throws AuthenticationException, IOException, UnsupportedEncodingException {
    long start = System.nanoTime();

    // Expensive call to be done with ContentTracker
    GetTextContentResponse response = repositoryClient.getTextContent(dbId, propertyQName, null);

    addContentPropertyMetadata(doc, propertyQName, AlfrescoSolrDataModel.ContentFieldType.TRANSFORMATION_STATUS,
            response);/*from  ww  w .ja  v  a  2  s.  c o  m*/
    addContentPropertyMetadata(doc, propertyQName,
            AlfrescoSolrDataModel.ContentFieldType.TRANSFORMATION_EXCEPTION, response);
    addContentPropertyMetadata(doc, propertyQName, AlfrescoSolrDataModel.ContentFieldType.TRANSFORMATION_TIME,
            response);

    InputStream ris = response.getContent();
    String textContent = "";
    try {
        if (ris != null) {
            // Get and copy content
            byte[] bytes = FileCopyUtils.copyToByteArray(new BoundedInputStream(ris, contentStreamLimit));
            textContent = new String(bytes, "UTF8");
        }
    } finally {
        // release the response only when the content has been read
        response.release();
    }

    long end = System.nanoTime();
    this.getTrackerStats().addDocTransformationTime(end - start);

    StringBuilder builder = new StringBuilder(textContent.length() + 16);
    builder.append("\u0000").append(locale).append("\u0000");
    builder.append(textContent);
    String localisedText = builder.toString();

    for (FieldInstance field : AlfrescoSolrDataModel.getInstance()
            .getIndexedFieldNamesForProperty(propertyQName).getFields()) {
        doc.removeField(field.getField());
        if (field.isLocalised()) {
            doc.addField(field.getField(), localisedText);
        } else {
            doc.addField(field.getField(), textContent);
        }
        addFieldIfNotSet(doc, field);
    }
}

From source file:org.apache.hadoop.hdfs.ByteRangeInputStream.java

@VisibleForTesting
protected InputStream openInputStream() throws IOException {
    // Use the original url if no resolved url exists, eg. if
    // it's the first time a request is made.
    final boolean resolved = resolvedURL.getURL() != null;
    final URLOpener opener = resolved ? resolvedURL : originalURL;

    final HttpURLConnection connection = opener.connect(startPos, resolved);
    resolvedURL.setURL(getResolvedUrl(connection));

    InputStream in = connection.getInputStream();
    final Map<String, List<String>> headers = connection.getHeaderFields();
    if (isChunkedTransferEncoding(headers)) {
        // file length is not known
        fileLength = null;//from w  w  w .  j  av a2s.co m
    } else {
        // for non-chunked transfer-encoding, get content-length
        final String cl = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH);
        if (cl == null) {
            throw new IOException(HttpHeaders.CONTENT_LENGTH + " is missing: " + headers);
        }
        final long streamlength = Long.parseLong(cl);
        fileLength = startPos + streamlength;

        // Java has a bug with >2GB request streams.  It won't bounds check
        // the reads so the transfer blocks until the server times out
        in = new BoundedInputStream(in, streamlength);
    }

    return in;
}

From source file:org.apache.hadoop.hdfs.web.ByteRangeInputStream.java

@VisibleForTesting
protected InputStream openInputStream() throws IOException {
    // Use the original url if no resolved url exists, eg. if
    // it's the first time a request is made.
    final boolean resolved = resolvedURL.getURL() != null;
    final URLOpener opener = resolved ? resolvedURL : originalURL;

    final HttpURLConnection connection = opener.connect(startPos, resolved);
    resolvedURL.setURL(getResolvedUrl(connection));

    InputStream in = connection.getInputStream();
    final Map<String, List<String>> headers = connection.getHeaderFields();
    if (isChunkedTransferEncoding(headers)) {
        // file length is not known
        fileLength = null;//  ww  w .  j a  v a  2  s  .  c om
    } else {
        // for non-chunked transfer-encoding, get content-length
        long streamlength = getStreamLength(connection, headers);
        fileLength = startPos + streamlength;

        // Java has a bug with >2GB request streams.  It won't bounds check
        // the reads so the transfer blocks until the server times out
        in = new BoundedInputStream(in, streamlength);
    }

    return in;
}

From source file:org.apache.james.imapserver.netty.NettyImapRequestLineReader.java

/**
 * Return a {@link ChannelBufferInputStream} if the wrapped
 * {@link ChannelBuffer} contains enough data. If not it will throw a
 * {@link NotEnoughDataException}// www  .j  av a2s .  c  o  m
 */
public InputStream read(int size, boolean extraCRLF) throws DecodingException {
    int crlf = 0;
    if (extraCRLF) {
        crlf = 2;
    }

    if (maxLiteralSize > 0 && maxLiteralSize > size) {
        throw new DecodingException(HumanReadableText.FAILED,
                "Specified literal is greater then the allowed size");
    }
    // Check if we have enough data
    if (size + crlf > buffer.readableBytes()) {
        // ok let us throw a exception which till the decoder how many more
        // bytes we need
        throw new NotEnoughDataException(size + read + crlf);
    }

    // Unset the next char.
    nextSeen = false;
    nextChar = 0;

    // limit the size via commons-io as ChannelBufferInputStream size limiting is buggy
    InputStream in = new BoundedInputStream(new ChannelBufferInputStream(buffer), size);
    if (extraCRLF) {
        return new EolInputStream(this, in);
    } else {
        return in;
    }
}

From source file:org.apache.james.mailbox.jcr.mail.model.JCRMailboxMessage.java

@Override
public InputStream getHeaderContent() throws IOException {
    long limit = getBodyStartOctet();
    if (limit < 0) {
        limit = 0;//from  www  .j  a v  a 2  s. co  m
    }
    return new BoundedInputStream(getFullContent(), limit);
}

From source file:org.apache.james.mailbox.jcr.mail.model.JCRMessage.java

public InputStream getHeaderContent() throws IOException {
    long limit = getBodyStartOctet();
    if (limit < 0) {
        limit = 0;//  ww  w  . j  a v  a  2 s  .  co m
    }
    return new BoundedInputStream(getFullContent(), limit);
}

From source file:org.apache.nifi.toolkit.tls.service.client.TlsCertificateSigningRequestPerformer.java

/**
 * Submits a CSR to the Certificate authority, checks the resulting hmac, and returns the chain if everything succeeds
 *
 * @param keyPair the keypair to generate the csr for
 * @throws IOException if there is a problem during the process
 * @return the resulting certificate chain
 *///from w  w  w.  j  a  va  2 s .com
public X509Certificate[] perform(KeyPair keyPair) throws IOException {
    try {
        List<X509Certificate> certificates = new ArrayList<>();

        HttpClientBuilder httpClientBuilder = httpClientBuilderSupplier.get();
        SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
        sslContextBuilder.useProtocol("TLSv1.2");

        // We will be validating that we are talking to the correct host once we get the response's hmac of the token and public key of the ca
        sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        httpClientBuilder.setSSLSocketFactory(new TlsCertificateAuthorityClientSocketFactory(
                sslContextBuilder.build(), caHostname, certificates));

        String jsonResponseString;
        int responseCode;
        try (CloseableHttpClient client = httpClientBuilder.build()) {
            JcaPKCS10CertificationRequest request = TlsHelper.generateCertificationRequest(dn,
                    domainAlternativeNames, keyPair, signingAlgorithm);
            TlsCertificateAuthorityRequest tlsCertificateAuthorityRequest = new TlsCertificateAuthorityRequest(
                    TlsHelper.calculateHMac(token, request.getPublicKey()),
                    TlsHelper.pemEncodeJcaObject(request));

            HttpPost httpPost = new HttpPost();
            httpPost.setEntity(
                    new ByteArrayEntity(objectMapper.writeValueAsBytes(tlsCertificateAuthorityRequest)));

            if (logger.isInfoEnabled()) {
                logger.info("Requesting certificate with dn " + dn + " from " + caHostname + ":" + port);
            }
            try (CloseableHttpResponse response = client.execute(new HttpHost(caHostname, port, "https"),
                    httpPost)) {
                jsonResponseString = IOUtils.toString(
                        new BoundedInputStream(response.getEntity().getContent(), 1024 * 1024),
                        StandardCharsets.UTF_8);
                responseCode = response.getStatusLine().getStatusCode();
            }
        }

        if (responseCode != Response.SC_OK) {
            throw new IOException(
                    RECEIVED_RESPONSE_CODE + responseCode + " with payload " + jsonResponseString);
        }

        if (certificates.size() != 1) {
            throw new IOException(EXPECTED_ONE_CERTIFICATE);
        }

        TlsCertificateAuthorityResponse tlsCertificateAuthorityResponse = objectMapper
                .readValue(jsonResponseString, TlsCertificateAuthorityResponse.class);
        if (!tlsCertificateAuthorityResponse.hasHmac()) {
            throw new IOException(EXPECTED_RESPONSE_TO_CONTAIN_HMAC);
        }

        X509Certificate caCertificate = certificates.get(0);
        byte[] expectedHmac = TlsHelper.calculateHMac(token, caCertificate.getPublicKey());

        if (!MessageDigest.isEqual(expectedHmac, tlsCertificateAuthorityResponse.getHmac())) {
            throw new IOException(UNEXPECTED_HMAC_RECEIVED_POSSIBLE_MAN_IN_THE_MIDDLE);
        }

        if (!tlsCertificateAuthorityResponse.hasCertificate()) {
            throw new IOException(EXPECTED_RESPONSE_TO_CONTAIN_CERTIFICATE);
        }
        X509Certificate x509Certificate = TlsHelper
                .parseCertificate(new StringReader(tlsCertificateAuthorityResponse.getPemEncodedCertificate()));
        x509Certificate.verify(caCertificate.getPublicKey());
        if (logger.isInfoEnabled()) {
            logger.info("Got certificate with dn " + x509Certificate.getSubjectX500Principal());
        }
        return new X509Certificate[] { x509Certificate, caCertificate };
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:org.apache.openmeetings.web.util.RecordingResourceReference.java

@Override
public IResource getResource() {
    return new AbstractResource() {
        private static final long serialVersionUID = 1L;
        private final static String ACCEPT_RANGES_HEADER = "Accept-Ranges";
        private final static String RANGE_HEADER = "Range";
        private final static String CONTENT_RANGE_HEADER = "Content-Range";
        private final static String RANGES_BYTES = "bytes";
        private File file;
        private boolean isRange = false;
        private long start = 0;
        private long end = 0;

        private long getChunkLength() {
            return isRange ? end - start + 1 : (file == null ? -1 : file.length());
        }/*from w w  w .  ja v a 2s . co  m*/

        private IResourceStream getResourceStream() {
            return file == null ? null : new FileResourceStream(file) {
                private static final long serialVersionUID = 2546785247219805747L;
                private transient BoundedInputStream bi;

                @Override
                public InputStream getInputStream() throws ResourceStreamNotFoundException {
                    if (bi == null) {
                        //bi = new BoundedInputStream(super.getInputStream(), end + 1);
                        bi = new BoundedInputStream(super.getInputStream(),
                                isRange ? end + 1 : (file == null ? -1 : file.length()));
                        try {
                            bi.skip(start);
                        } catch (IOException e) {
                            throw new ResourceStreamNotFoundException(e);
                        }
                    }
                    return bi;
                }

                @Override
                public Bytes length() {
                    return Bytes.bytes(getChunkLength());
                }

                @Override
                public void close() throws IOException {
                    if (bi != null) {
                        bi.close(); //also will close original stream
                        bi = null;
                    }
                }

                @Override
                public String getContentType() {
                    return RecordingResourceReference.this.getContentType();
                }
            };
        }

        @Override
        protected void setResponseHeaders(ResourceResponse data, Attributes attributes) {
            Response response = attributes.getResponse();
            if (response instanceof WebResponse) {
                WebResponse webResponse = (WebResponse) response;
                webResponse.setStatus(
                        isRange ? HttpServletResponse.SC_PARTIAL_CONTENT : HttpServletResponse.SC_OK);
            }
            super.setResponseHeaders(data, attributes);
        }

        @Override
        protected ResourceResponse newResourceResponse(Attributes attributes) {
            ResourceResponse rr = new ResourceResponse();
            FlvRecording r = getRecording(attributes);
            if (r != null) {
                isRange = false;
                file = getFile(r);
                rr.setFileName(getFileName(r));
                rr.setContentType(RecordingResourceReference.this.getContentType());
                rr.setContentDisposition(ContentDisposition.INLINE);
                rr.setLastModified(Time.millis(file.lastModified()));
                rr.getHeaders().addHeader(ACCEPT_RANGES_HEADER, RANGES_BYTES);
                String range = ((HttpServletRequest) attributes.getRequest().getContainerRequest())
                        .getHeader(RANGE_HEADER);
                if (range != null && range.startsWith(RANGES_BYTES)) {
                    String[] bounds = range.substring(RANGES_BYTES.length() + 1).split("-");
                    if (bounds != null && bounds.length > 0) {
                        long length = file.length();
                        isRange = true;
                        start = Long.parseLong(bounds[0]);
                        end = bounds.length > 1 ? Long.parseLong(bounds[1]) : length - 1;
                        //Content-Range: bytes 229376-232468/232469
                        rr.getHeaders().addHeader(CONTENT_RANGE_HEADER,
                                String.format("%s %d-%d/%d", RANGES_BYTES, start, end, length));
                    }
                }
                rr.setContentLength(getChunkLength());
                rr.setWriteCallback(new WriteCallback() {
                    @Override
                    public void writeData(Attributes attributes) throws IOException {
                        IResourceStream rStream = getResourceStream();
                        try {
                            writeStream(attributes, rStream.getInputStream());
                        } catch (ResourceStreamNotFoundException e1) {
                        } catch (ResponseIOException e) {
                            // in case of range operations we expecting such exceptions
                            if (!isRange) {
                                log.error("Error while playing the stream", e);
                            }
                        } finally {
                            rStream.close();
                        }
                    }
                });
            } else {
                rr.setError(HttpServletResponse.SC_NOT_FOUND);
            }
            return rr;
        }
    };
}

From source file:org.apache.streams.facebook.test.data.FacebookActivityActorSerDeIT.java

@Test
public void Tests() throws Exception {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookActivityActorSerDeIT.class.getResourceAsStream("/testpage.json");
    is = new BoundedInputStream(is, 10000);
    String json = String.join(" ", IOUtils.readLines(is, Charset.defaultCharset()));
    LOGGER.debug(json);//  ww w.  ja va  2s .c om

    Page page = mapper.readValue(json, Page.class);

    Activity activity = serializer.deserialize(page);

    LOGGER.debug(mapper.writeValueAsString(activity));
}

From source file:org.apache.streams.facebook.test.data.FacebookActivitySerDeIT.java

@Test
public void Tests() {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookActivitySerDeIT.class.getResourceAsStream("/testpost.json");
    is = new BoundedInputStream(is, 10000);
    String json;//from   w w  w  .  j a v a 2  s.c  o  m

    try {
        json = String.join(" ", IOUtils.readLines(is, Charset.defaultCharset()));
        LOGGER.debug(json);

        Post post = mapper.readValue(json, Post.class);

        Activity activity = serializer.deserialize(post);

        LOGGER.debug(mapper.writeValueAsString(activity));

    } catch (Exception e) {
        LOGGER.error("Exception: ", e);
        Assert.fail();
    }
}