org.apache.tomcat.util.http.fileupload.util.Streams Java Examples

The following examples show how to use org.apache.tomcat.util.http.fileupload.util.Streams. 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: ContextInProxiedInstancesTest.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Test
public void fields() throws IOException {
    try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder()
            .randomHttpPort()
            .includePackages(ContextInProxiedInstancesTest.class.getName())).bake()) {
        // proxies can use @Context
        try (final InputStream stream = new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/app").openStream()) {
            assertEquals("app", Streams.asString(stream, "UTF-8"));
        }
        try (final InputStream stream = new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/req").openStream()) {
            assertEquals("req", Streams.asString(stream, "UTF-8"));
        }
        // not proxied can also
        try (final InputStream stream = new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/dep").openStream()) {
            assertEquals("dep", Streams.asString(stream, "UTF-8"));
        }
        assertEquals(Dep.class, CDI.current().select(Dep.class).get().getClass()); // ensure it is not proxied but injection works (thanks CXF)
    }
}
 
Example #2
Source File: InstanceCustomizerTest.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Test
public void instanceCustomizer() throws IOException {
    try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder()
            .randomHttpPort()
            .instanceCustomizer(t -> t.getHost().getPipeline().addValve(new ValveBase() {
                @Override
                public void invoke(final Request request, final Response response) throws IOException, ServletException {
                    response.getWriter().write("custom");
                }
            }))
            .includePackages(InstanceCustomizerTest.class.getName())).bake()) {
        try (final InputStream stream = new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/whatever").openStream()) {
            assertEquals("custom", Streams.asString(stream, "UTF-8"));
        }
    }
}
 
Example #3
Source File: OctetStreamMediaTypeTest.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@Test
public void fields() throws IOException {
    try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder()
        .randomHttpPort()
        .includePackages(OctetStreamMediaTypeTest.class.getName())).bake()) {
        try (final InputStream stream = new URL(
            "http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/OctetStreamMediaTypeTest/response").openStream()) {
            assertEquals("resp", Streams.asString(stream, "UTF-8"));
        }
        try (final InputStream stream = new URL(
            "http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/OctetStreamMediaTypeTest/responseBytes").openStream()) {
            assertEquals("resp", Streams.asString(stream, "UTF-8"));
        }
        try (final InputStream stream = new URL(
            "http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/OctetStreamMediaTypeTest/streaming").openStream()) {
            assertEquals("stream", Streams.asString(stream, "UTF-8"));
        }
        try (final InputStream stream = new URL(
            "http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/OctetStreamMediaTypeTest/string").openStream()) {
            assertEquals("string", Streams.asString(stream, "UTF-8"));
        }
        try (final InputStream stream = new URL(
                "http://localhost:" + meecrowave.getConfiguration().getHttpPort() + "/OctetStreamMediaTypeTest/bytes").openStream()) {
            assertEquals("bytes", Streams.asString(stream, "UTF-8"));
        }
    }
}
 
Example #4
Source File: MultipartStream.java    From Tomcat8-Source-Read with MIT License 2 votes vote down vote up
/**
 * <p>Reads <code>body-data</code> from the current
 * <code>encapsulation</code> and writes its contents into the
 * output <code>Stream</code>.
 *
 * <p>Arbitrary large amounts of data can be processed by this
 * method using a constant size buffer. (see {@link
 * #MultipartStream(InputStream,byte[],int,
 *   MultipartStream.ProgressNotifier) constructor}).
 *
 * @param output The <code>Stream</code> to write data into. May
 *               be null, in which case this method is equivalent
 *               to {@link #discardBodyData()}.
 *
 * @return the amount of data written.
 *
 * @throws MalformedStreamException if the stream ends unexpectedly.
 * @throws IOException              if an i/o error occurs.
 */
public int readBodyData(OutputStream output)
        throws MalformedStreamException, IOException {
    return (int) Streams.copy(newInputStream(), output, false); // N.B. Streams.copy closes the input stream
}
 
Example #5
Source File: DiskFileItem.java    From Tomcat8-Source-Read with MIT License 2 votes vote down vote up
/**
 * Returns the original file name in the client's file system.
 *
 * @return The original file name in the client's file system.
 * @throws org.apache.tomcat.util.http.fileupload.InvalidFileNameException
 *   The file name contains a NUL character, which might be an indicator of
 *   a security attack. If you intend to use the file name anyways, catch
 *   the exception and use {@link
 *   org.apache.tomcat.util.http.fileupload.InvalidFileNameException#getName()}.
 */
@Override
public String getName() {
    return Streams.checkFileName(fileName);
}
 
Example #6
Source File: FileUploadBase.java    From Tomcat8-Source-Read with MIT License 2 votes vote down vote up
/**
 * Returns the items file name.
 *
 * @return File name, if known, or null.
 * @throws InvalidFileNameException The file name contains a NUL character,
 *   which might be an indicator of a security attack. If you intend to
 *   use the file name anyways, catch the exception and use
 *   InvalidFileNameException#getName().
 */
@Override
public String getName() {
    return Streams.checkFileName(name);
}
 
Example #7
Source File: MultipartStream.java    From Tomcat7.0.67 with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Reads <code>body-data</code> from the current
 * <code>encapsulation</code> and writes its contents into the
 * output <code>Stream</code>.
 *
 * <p>Arbitrary large amounts of data can be processed by this
 * method using a constant size buffer. (see {@link
 * #MultipartStream(InputStream,byte[],int,
 *   MultipartStream.ProgressNotifier) constructor}).
 *
 * @param output The <code>Stream</code> to write data into. May
 *               be null, in which case this method is equivalent
 *               to {@link #discardBodyData()}.
 *
 * @return the amount of data written.
 *
 * @throws MalformedStreamException if the stream ends unexpectedly.
 * @throws IOException              if an i/o error occurs.
 */
public int readBodyData(OutputStream output)
        throws MalformedStreamException, IOException {
    final InputStream istream = newInputStream();
    return (int) Streams.copy(istream, output, false);
}
 
Example #8
Source File: DiskFileItem.java    From Tomcat7.0.67 with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the original filename in the client's filesystem.
 *
 * @return The original filename in the client's filesystem.
 * @throws org.apache.tomcat.util.http.fileupload.InvalidFileNameException
 *   The file name contains a NUL character, which might be an indicator of
 *   a security attack. If you intend to use the file name anyways, catch
 *   the exception and use {@link
 *   org.apache.tomcat.util.http.fileupload.InvalidFileNameException#getName()}.
 */
@Override
public String getName() {
    return Streams.checkFileName(fileName);
}
 
Example #9
Source File: FileUploadBase.java    From Tomcat7.0.67 with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the items file name.
 *
 * @return File name, if known, or null.
 * @throws InvalidFileNameException The file name contains a NUL character,
 *   which might be an indicator of a security attack. If you intend to
 *   use the file name anyways, catch the exception and use
 *   InvalidFileNameException#getName().
 */
@Override
public String getName() {
    return Streams.checkFileName(name);
}
 
Example #10
Source File: MultipartStream.java    From tomcatsrc with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Reads <code>body-data</code> from the current
 * <code>encapsulation</code> and writes its contents into the
 * output <code>Stream</code>.
 *
 * <p>Arbitrary large amounts of data can be processed by this
 * method using a constant size buffer. (see {@link
 * #MultipartStream(InputStream,byte[],int,
 *   MultipartStream.ProgressNotifier) constructor}).
 *
 * @param output The <code>Stream</code> to write data into. May
 *               be null, in which case this method is equivalent
 *               to {@link #discardBodyData()}.
 *
 * @return the amount of data written.
 *
 * @throws MalformedStreamException if the stream ends unexpectedly.
 * @throws IOException              if an i/o error occurs.
 */
public int readBodyData(OutputStream output)
        throws MalformedStreamException, IOException {
    return (int) Streams.copy(newInputStream(), output, false); // N.B. Streams.copy closes the input stream
}
 
Example #11
Source File: DiskFileItem.java    From tomcatsrc with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the original filename in the client's filesystem.
 *
 * @return The original filename in the client's filesystem.
 * @throws org.apache.tomcat.util.http.fileupload.InvalidFileNameException
 *   The file name contains a NUL character, which might be an indicator of
 *   a security attack. If you intend to use the file name anyways, catch
 *   the exception and use {@link
 *   org.apache.tomcat.util.http.fileupload.InvalidFileNameException#getName()}.
 */
@Override
public String getName() {
    return Streams.checkFileName(fileName);
}
 
Example #12
Source File: FileUploadBase.java    From tomcatsrc with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the items file name.
 *
 * @return File name, if known, or null.
 * @throws InvalidFileNameException The file name contains a NUL character,
 *   which might be an indicator of a security attack. If you intend to
 *   use the file name anyways, catch the exception and use
 *   InvalidFileNameException#getName().
 */
@Override
public String getName() {
    return Streams.checkFileName(name);
}