org.apache.coyote.Response Java Examples

The following examples show how to use org.apache.coyote.Response. 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: InternalOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 7 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res) 
    throws IOException {

    int length = chunk.getLength();
    if (useSocketBuffer) {
        socketBuffer.append(chunk.getBuffer(), chunk.getStart(), 
                            length);
    } else {
        outputStream.write(chunk.getBuffer(), chunk.getStart(), 
                           length);
    }
    byteCount += chunk.getLength();
    return chunk.getLength();
}
 
Example #2
Source File: InternalAprOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res) 
    throws IOException {

    int len = chunk.getLength();
    int start = chunk.getStart();
    byte[] b = chunk.getBuffer();
    while (len > 0) {
        int thisTime = len;
        if (bbuf.position() == bbuf.capacity()) {
            flushBuffer();
        }
        if (thisTime > bbuf.capacity() - bbuf.position()) {
            thisTime = bbuf.capacity() - bbuf.position();
        }
        bbuf.put(b, start, thisTime);
        len = len - thisTime;
        start = start + thisTime;
    }
    byteCount += chunk.getLength();
    return chunk.getLength();
}
 
Example #3
Source File: Http11OutputBuffer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
protected Http11OutputBuffer(Response response, int headerBufferSize, boolean sendReasonPhrase) {

        this.response = response;
        this.sendReasonPhrase = sendReasonPhrase;

        headerBuffer = ByteBuffer.allocate(headerBufferSize);

        filterLibrary = new OutputFilter[0];
        activeFilters = new OutputFilter[0];
        lastActiveFilter = -1;

        responseFinished = false;

        outputStreamOutputBuffer = new SocketOutputBuffer();

        if (sendReasonPhrase) {
            // Cause loading of HttpMessages
            HttpMessages.getInstance(response.getLocale()).getMessage(200);
        }
    }
 
Example #4
Source File: InternalAprOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 */
public InternalAprOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];
    if (headerBufferSize < (8 * 1024)) {
        bbuf = ByteBuffer.allocateDirect(6 * 1500);
    } else {
        bbuf = ByteBuffer.allocateDirect((headerBufferSize / 1500 + 1) * 1500);
    }

    outputStreamOutputBuffer = new SocketOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    committed = false;
    finished = false;

    // Cause loading of HttpMessages
    HttpMessages.getInstance(response.getLocale()).getMessage(200);

}
 
Example #5
Source File: AbstractOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Write the contents of a byte chunk.
 * 
 * @param chunk byte chunk
 * @return number of bytes written
 * @throws IOException an underlying I/O error occurred
 */
@Override
public int doWrite(ByteChunk chunk, Response res) 
    throws IOException {

    if (!committed) {

        // Send the connector a request for commit. The connector should
        // then validate the headers, send them (using sendHeaders) and 
        // set the filters accordingly.
        response.action(ActionCode.COMMIT, null);

    }

    if (lastActiveFilter == -1)
        return outputStreamOutputBuffer.doWrite(chunk, res);
    else
        return activeFilters[lastActiveFilter].doWrite(chunk, res);

}
 
Example #6
Source File: InternalAprOutputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 */
public InternalAprOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];
    if (headerBufferSize < (8 * 1024)) {
        bbuf = ByteBuffer.allocateDirect(6 * 1500);
    } else {
        bbuf = ByteBuffer.allocateDirect((headerBufferSize / 1500 + 1) * 1500);
    }

    outputStreamOutputBuffer = new SocketOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    committed = false;
    finished = false;

    // Cause loading of HttpMessages
    HttpMessages.getInstance(response.getLocale()).getMessage(200);

}
 
Example #7
Source File: InternalNioOutputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res) throws IOException {
    try {
        int len = chunk.getLength();
        int start = chunk.getStart();
        byte[] b = chunk.getBuffer();
        addToBB(b, start, len);
        byteCount += chunk.getLength();
        return chunk.getLength();
    } catch (IOException ioe) {
        response.action(ActionCode.CLOSE_NOW, ioe);
        // Re-throw
        throw ioe;
    }
}
 
Example #8
Source File: InternalOutputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res) throws IOException {
    try {
        int length = chunk.getLength();
        if (useSocketBuffer) {
            socketBuffer.append(chunk.getBuffer(), chunk.getStart(),
                                length);
        } else {
            outputStream.write(chunk.getBuffer(), chunk.getStart(),
                               length);
        }
        byteCount += chunk.getLength();
        return chunk.getLength();
    } catch (IOException ioe) {
        response.action(ActionCode.CLOSE_NOW, ioe);
        // Re-throw
        throw ioe;
    }
}
 
Example #9
Source File: AbstractOutputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Write the contents of a byte chunk.
 * 
 * @param chunk byte chunk
 * @return number of bytes written
 * @throws IOException an underlying I/O error occurred
 */
@Override
public int doWrite(ByteChunk chunk, Response res) 
    throws IOException {

    if (!committed) {

        // Send the connector a request for commit. The connector should
        // then validate the headers, send them (using sendHeaders) and 
        // set the filters accordingly.
        response.action(ActionCode.COMMIT, null);

    }

    if (lastActiveFilter == -1)
        return outputStreamOutputBuffer.doWrite(chunk, res);
    else
        return activeFilters[lastActiveFilter].doWrite(chunk, res);

}
 
Example #10
Source File: IdentityOutputFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    int result = -1;

    if (contentLength >= 0) {
        if (remaining > 0) {
            result = chunk.getLength();
            if (result > remaining) {
                // The chunk is longer than the number of bytes remaining
                // in the body; changing the chunk length to the number
                // of bytes remaining
                chunk.setBytes(chunk.getBytes(), chunk.getStart(), 
                               (int) remaining);
                result = (int) remaining;
                remaining = 0;
            } else {
                remaining = remaining - result;
            }
            buffer.doWrite(chunk, res);
        } else {
            // No more bytes left to be written : return -1 and clear the 
            // buffer
            chunk.recycle();
            result = -1;
        }
    } else {
        // If no content length was set, just write the bytes
        buffer.doWrite(chunk, res);
        result = chunk.getLength();
    }

    return result;

}
 
Example #11
Source File: VoidOutputFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    return chunk.getLength();

}
 
Example #12
Source File: GzipOutputFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {
    if (compressionStream == null) {
        compressionStream = new FlushableGZIPOutputStream(fakeOutputStream);
    }
    compressionStream.write(chunk.getBytes(), chunk.getStart(), 
                            chunk.getLength());
    return chunk.getLength();
}
 
Example #13
Source File: ChunkedOutputFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    int result = chunk.getLength();

    if (result <= 0) {
        return 0;
    }

    // Calculate chunk header
    int pos = 7;
    int current = result;
    while (current > 0) {
        int digit = current % 16;
        current = current / 16;
        chunkLength[pos--] = HexUtils.getHex(digit);
    }
    chunkHeader.setBytes(chunkLength, pos + 1, 9 - pos);
    buffer.doWrite(chunkHeader, res);

    buffer.doWrite(chunk, res);

    chunkHeader.setBytes(chunkLength, 8, 2);
    buffer.doWrite(chunkHeader, res);

    return result;

}
 
Example #14
Source File: InternalNioOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res) 
    throws IOException {

    int len = chunk.getLength();
    int start = chunk.getStart();
    byte[] b = chunk.getBuffer();
    addToBB(b, start, len);
    byteCount += chunk.getLength();
    return chunk.getLength();
}
 
Example #15
Source File: AbstractAjpProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    if (!response.isCommitted()) {
        // Validate and write response headers
        try {
            prepareResponse();
        } catch (IOException e) {
            setErrorState(ErrorState.CLOSE_NOW, e);
        }
    }

    if (!swallowResponse) {
        int len = chunk.getLength();
        // 4 - hardcoded, byte[] marshaling overhead
        // Adjust allowed size if packetSize != default (Constants.MAX_PACKET_SIZE)
        int chunkSize = Constants.MAX_SEND_SIZE + packetSize - Constants.MAX_PACKET_SIZE;
        int off = 0;
        while (len > 0) {
            int thisTime = len;
            if (thisTime > chunkSize) {
                thisTime = chunkSize;
            }
            len -= thisTime;
            responseMessage.reset();
            responseMessage.appendByte(Constants.JK_AJP13_SEND_BODY_CHUNK);
            responseMessage.appendBytes(chunk.getBytes(), chunk.getOffset() + off, thisTime);
            responseMessage.end();
            output(responseMessage.getBuffer(), 0, responseMessage.getLen());

            off += thisTime;
        }

        bytesWritten += chunk.getLength();
    }
    return chunk.getLength();
}
 
Example #16
Source File: TomcatHttpHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static HttpHeaders createTomcatHttpHeaders(HttpServletResponse response) {
	ResponseFacade responseFacade = getResponseFacade(response);
	org.apache.catalina.connector.Response connectorResponse = (org.apache.catalina.connector.Response)
			ReflectionUtils.getField(COYOTE_RESPONSE_FIELD, responseFacade);
	Assert.state(connectorResponse != null, "No Tomcat connector response");
	Response tomcatResponse = connectorResponse.getCoyoteResponse();
	TomcatHeadersAdapter headers = new TomcatHeadersAdapter(tomcatResponse.getMimeHeaders());
	return new HttpHeaders(headers);
}
 
Example #17
Source File: TomcatService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static ResponseHeaders convertResponse(Response coyoteRes) {
    final ResponseHeadersBuilder headers = ResponseHeaders.builder();
    headers.status(coyoteRes.getStatus());

    final String contentType = coyoteRes.getContentType();
    if (contentType != null && !contentType.isEmpty()) {
        headers.set(HttpHeaderNames.CONTENT_TYPE, contentType);
    }

    final long contentLength = coyoteRes.getBytesWritten(true); // 'true' will trigger flush.
    final String method = coyoteRes.getRequest().method().toString();
    if (!"HEAD".equals(method)) {
        headers.setLong(HttpHeaderNames.CONTENT_LENGTH, contentLength);
    }

    final MimeHeaders cHeaders = coyoteRes.getMimeHeaders();
    final int numHeaders = cHeaders.size();
    for (int i = 0; i < numHeaders; i++) {
        final AsciiString name = toHeaderName(cHeaders.getName(i));
        if (name == null) {
            continue;
        }

        final String value = toHeaderValue(cHeaders.getValue(i));
        if (value == null) {
            continue;
        }

        headers.add(name.toLowerCase(), value);
    }

    return headers.build();
}
 
Example #18
Source File: ChunkedOutputFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    int result = chunk.getLength();

    if (result <= 0) {
        return 0;
    }

    // Calculate chunk header
    int pos = 7;
    int current = result;
    while (current > 0) {
        int digit = current % 16;
        current = current / 16;
        chunkLength[pos--] = HexUtils.getHex(digit);
    }
    chunkHeader.setBytes(chunkLength, pos + 1, 9 - pos);
    buffer.doWrite(chunkHeader, res);

    buffer.doWrite(chunk, res);

    chunkHeader.setBytes(chunkLength, 8, 2);
    buffer.doWrite(chunkHeader, res);

    return result;

}
 
Example #19
Source File: IdentityOutputFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    int result = -1;

    if (contentLength >= 0) {
        if (remaining > 0) {
            result = chunk.getLength();
            if (result > remaining) {
                // The chunk is longer than the number of bytes remaining
                // in the body; changing the chunk length to the number
                // of bytes remaining
                chunk.setBytes(chunk.getBytes(), chunk.getStart(), 
                               (int) remaining);
                result = (int) remaining;
                remaining = 0;
            } else {
                remaining = remaining - result;
            }
            buffer.doWrite(chunk, res);
        } else {
            // No more bytes left to be written : return -1 and clear the 
            // buffer
            chunk.recycle();
            result = -1;
        }
    } else {
        // If no content length was set, just write the bytes
        buffer.doWrite(chunk, res);
        result = chunk.getLength();
    }

    return result;

}
 
Example #20
Source File: VoidOutputFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    return chunk.getLength();

}
 
Example #21
Source File: GzipOutputFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {
    if (compressionStream == null) {
        compressionStream = new FlushableGZIPOutputStream(fakeOutputStream);
    }
    compressionStream.write(chunk.getBytes(), chunk.getStart(), 
                            chunk.getLength());
    return chunk.getLength();
}
 
Example #22
Source File: InternalAprOutputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res) throws IOException {
    try {
        int len = chunk.getLength();
        int start = chunk.getStart();
        byte[] b = chunk.getBuffer();
        while (len > 0) {
            int thisTime = len;
            if (bbuf.position() == bbuf.capacity()) {
                flushBuffer();
            }
            if (thisTime > bbuf.capacity() - bbuf.position()) {
                thisTime = bbuf.capacity() - bbuf.position();
            }
            bbuf.put(b, start, thisTime);
            len = len - thisTime;
            start = start + thisTime;
        }
        byteCount += chunk.getLength();
        return chunk.getLength();
    } catch (IOException ioe) {
        response.action(ActionCode.CLOSE_NOW, ioe);
        // Re-throw
        throw ioe;
    }
}
 
Example #23
Source File: TestGzipOutputFilter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testFlushingWithGzip() throws Exception {
    // set up response, InternalOutputBuffer, and ByteArrayOutputStream
    Response res = new Response();
    TesterOutputBuffer tob = new TesterOutputBuffer(res, 8 * 1024);
    res.setOutputBuffer(tob);

    // set up GzipOutputFilter to attach to the TesterOutputBuffer
    GzipOutputFilter gf = new GzipOutputFilter();
    tob.addFilter(gf);
    tob.addActiveFilter(gf);

    // write a chunk out
    byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
    ByteBuffer bb = ByteBuffer.wrap(d);
    tob.doWrite(bb);

    // flush the InternalOutputBuffer
    tob.flush();

    // read from the ByteArrayOutputStream to find out what's being written
    // out (flushed)
    byte[] dataFound = tob.toByteArray();

    // find out what's expected by writing to GZIPOutputStream and close it
    // (to force flushing)
    ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
    GZIPOutputStream gos = new GZIPOutputStream(gbos);
    gos.write(d);
    gos.close();

    // read the expected data
    byte[] dataExpected = gbos.toByteArray();

    // most of the data should have been flushed out
    Assert.assertTrue(dataFound.length >= (dataExpected.length - 20));
}
 
Example #24
Source File: TomcatHttpHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static HttpHeaders createTomcatHttpHeaders(HttpServletResponse response) {
	ResponseFacade responseFacade = getResponseFacade(response);
	org.apache.catalina.connector.Response connectorResponse = (org.apache.catalina.connector.Response)
			ReflectionUtils.getField(COYOTE_RESPONSE_FIELD, responseFacade);
	Assert.state(connectorResponse != null, "No Tomcat connector response");
	Response tomcatResponse = connectorResponse.getCoyoteResponse();
	TomcatHeadersAdapter headers = new TomcatHeadersAdapter(tomcatResponse.getMimeHeaders());
	return new HttpHeaders(headers);
}
 
Example #25
Source File: AbstractAjpProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Write chunk.
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    if (!response.isCommitted()) {
        // Validate and write response headers
        try {
            prepareResponse();
        } catch (IOException e) {
            setErrorState(ErrorState.CLOSE_NOW, e);
        }
    }

    if (!swallowResponse) {
        try {
            int len = chunk.getLength();
            // 4 - hardcoded, byte[] marshaling overhead
            // Adjust allowed size if packetSize != default (Constants.MAX_PACKET_SIZE)
            int chunkSize = Constants.MAX_SEND_SIZE + packetSize - Constants.MAX_PACKET_SIZE;
            int off = 0;
            while (len > 0) {
                int thisTime = len;
                if (thisTime > chunkSize) {
                    thisTime = chunkSize;
                }
                len -= thisTime;
                responseMessage.reset();
                responseMessage.appendByte(Constants.JK_AJP13_SEND_BODY_CHUNK);
                responseMessage.appendBytes(chunk.getBytes(), chunk.getOffset() + off, thisTime);
                responseMessage.end();
                output(responseMessage.getBuffer(), 0, responseMessage.getLen());

                off += thisTime;
            }

            bytesWritten += chunk.getLength();
        } catch (IOException ioe) {
            response.action(ActionCode.CLOSE_NOW, ioe);
            // Re-throw
            throw ioe;
        }
    }
    return chunk.getLength();
}
 
Example #26
Source File: InternalOutputBuffer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public InternalOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];

    outputStreamOutputBuffer = new OutputStreamOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    socketBuffer = new ByteChunk();
    socketBuffer.setByteOutputChannel(this);

    committed = false;
    finished = false;

}
 
Example #27
Source File: InternalNioOutputBuffer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public InternalNioOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];

    outputStreamOutputBuffer = new SocketOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    committed = false;
    finished = false;

    // Cause loading of HttpMessages
    HttpMessages.getInstance(response.getLocale()).getMessage(200);

}
 
Example #28
Source File: IdentityOutputFilter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Some filters need additional parameters from the response. All the 
 * necessary reading can occur in that method, as this method is called
 * after the response header processing is complete.
 */
@Override
public void setResponse(Response response) {
    contentLength = response.getContentLengthLong();
    remaining = contentLength;
}
 
Example #29
Source File: TestGzipOutputFilter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Test the interaction betwen gzip and flushing. The idea is to: 1. create
 * a internal output buffer, response, and attach an active gzipoutputfilter
 * to the output buffer 2. set the output stream of the internal buffer to
 * be a ByteArrayOutputStream so we can inspect the output bytes 3. write a
 * chunk out using the gzipoutputfilter and invoke a flush on the
 * InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out
 * what's being written out (flushed) 5. find out what's expected by wrting
 * to GZIPOutputStream and close it (to force flushing) 6. Compare the size
 * of the two arrays, they should be close (instead of one being much
 * shorter than the other one)
 *
 * @throws Exception
 */
@Test
public void testFlushingWithGzip() throws Exception {
    // set up response, InternalOutputBuffer, and ByteArrayOutputStream
    Response res = new Response();
    InternalOutputBuffer iob = new InternalOutputBuffer(res, 8 * 1024);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    iob.outputStream = bos;
    res.setOutputBuffer(iob);

    // set up GzipOutputFilter to attach to the InternalOutputBuffer
    GzipOutputFilter gf = new GzipOutputFilter();
    iob.addFilter(gf);
    iob.addActiveFilter(gf);

    // write a chunk out
    ByteChunk chunk = new ByteChunk(1024);
    byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
    chunk.append(d, 0, d.length);
    iob.doWrite(chunk, res);

    // flush the InternalOutputBuffer
    iob.flush();

    // read from the ByteArrayOutputStream to find out what's being written
    // out (flushed)
    byte[] dataFound = bos.toByteArray();

    // find out what's expected by wrting to GZIPOutputStream and close it
    // (to force flushing)
    ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
    GZIPOutputStream gos = new GZIPOutputStream(gbos);
    gos.write(d);
    gos.close();

    // read the expected data
    byte[] dataExpected = gbos.toByteArray();

    // most of the data should have been flushed out
    assertTrue(dataFound.length >= (dataExpected.length - 20));
}
 
Example #30
Source File: TestGzipOutputFilter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Test the interaction betwen gzip and flushing. The idea is to: 1. create
 * a internal output buffer, response, and attach an active gzipoutputfilter
 * to the output buffer 2. set the output stream of the internal buffer to
 * be a ByteArrayOutputStream so we can inspect the output bytes 3. write a
 * chunk out using the gzipoutputfilter and invoke a flush on the
 * InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out
 * what's being written out (flushed) 5. find out what's expected by wrting
 * to GZIPOutputStream and close it (to force flushing) 6. Compare the size
 * of the two arrays, they should be close (instead of one being much
 * shorter than the other one)
 *
 * @throws Exception
 */
@Test
public void testFlushingWithGzip() throws Exception {
    // set up response, InternalOutputBuffer, and ByteArrayOutputStream
    Response res = new Response();
    InternalOutputBuffer iob = new InternalOutputBuffer(res, 8 * 1024);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    iob.outputStream = bos;
    res.setOutputBuffer(iob);

    // set up GzipOutputFilter to attach to the InternalOutputBuffer
    GzipOutputFilter gf = new GzipOutputFilter();
    iob.addFilter(gf);
    iob.addActiveFilter(gf);

    // write a chunk out
    ByteChunk chunk = new ByteChunk(1024);
    byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
    chunk.append(d, 0, d.length);
    iob.doWrite(chunk, res);

    // flush the InternalOutputBuffer
    iob.flush();

    // read from the ByteArrayOutputStream to find out what's being written
    // out (flushed)
    byte[] dataFound = bos.toByteArray();

    // find out what's expected by wrting to GZIPOutputStream and close it
    // (to force flushing)
    ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
    GZIPOutputStream gos = new GZIPOutputStream(gbos);
    gos.write(d);
    gos.close();

    // read the expected data
    byte[] dataExpected = gbos.toByteArray();

    // most of the data should have been flushed out
    assertTrue(dataFound.length >= (dataExpected.length - 20));
}