mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-02 19:15:29 -04:00
Various code clean-up. No functional changes.
This commit is contained in:
parent
e75741ccd3
commit
af7d5fa94a
@ -568,6 +568,7 @@ public final class FileUtil {
|
|||||||
if (!pFile.exists()) {
|
if (!pFile.exists()) {
|
||||||
throw new FileNotFoundException(pFile.toString());
|
throw new FileNotFoundException(pFile.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = new byte[(int) pFile.length()];
|
byte[] bytes = new byte[(int) pFile.length()];
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
|
|
||||||
@ -586,6 +587,7 @@ public final class FileUtil {
|
|||||||
finally {
|
finally {
|
||||||
close(in);
|
close(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,8 +46,8 @@ import java.util.zip.Deflater;
|
|||||||
*/
|
*/
|
||||||
final class DeflateEncoder implements Encoder {
|
final class DeflateEncoder implements Encoder {
|
||||||
|
|
||||||
private final Deflater mDeflater;
|
private final Deflater deflater;
|
||||||
private final byte[] mBuffer = new byte[1024];
|
private final byte[] buffer = new byte[1024];
|
||||||
|
|
||||||
public DeflateEncoder() {
|
public DeflateEncoder() {
|
||||||
// this(new Deflater());
|
// this(new Deflater());
|
||||||
@ -59,32 +59,32 @@ final class DeflateEncoder implements Encoder {
|
|||||||
throw new IllegalArgumentException("deflater == null");
|
throw new IllegalArgumentException("deflater == null");
|
||||||
}
|
}
|
||||||
|
|
||||||
mDeflater = pDeflater;
|
deflater = pDeflater;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void encode(final OutputStream pStream, final byte[] pBuffer, final int pOffset, final int pLength)
|
public void encode(final OutputStream pStream, final byte[] pBuffer, final int pOffset, final int pLength)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
System.out.println("DeflateEncoder.encode");
|
System.out.println("DeflateEncoder.encode");
|
||||||
mDeflater.setInput(pBuffer, pOffset, pLength);
|
deflater.setInput(pBuffer, pOffset, pLength);
|
||||||
flushInputToStream(pStream);
|
flushInputToStream(pStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void flushInputToStream(final OutputStream pStream) throws IOException {
|
private void flushInputToStream(final OutputStream pStream) throws IOException {
|
||||||
System.out.println("DeflateEncoder.flushInputToStream");
|
System.out.println("DeflateEncoder.flushInputToStream");
|
||||||
|
|
||||||
if (mDeflater.needsInput()) {
|
if (deflater.needsInput()) {
|
||||||
System.out.println("Foo");
|
System.out.println("Foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!mDeflater.needsInput()) {
|
while (!deflater.needsInput()) {
|
||||||
int deflated = mDeflater.deflate(mBuffer, 0, mBuffer.length);
|
int deflated = deflater.deflate(buffer, 0, buffer.length);
|
||||||
pStream.write(mBuffer, 0, deflated);
|
pStream.write(buffer, 0, deflated);
|
||||||
System.out.println("flushed " + deflated);
|
System.out.println("flushed " + deflated);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void flush() {
|
// public void flush() {
|
||||||
// mDeflater.finish();
|
// deflater.finish();
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
@ -49,9 +49,9 @@ import java.util.zip.Inflater;
|
|||||||
*/
|
*/
|
||||||
final class InflateDecoder implements Decoder {
|
final class InflateDecoder implements Decoder {
|
||||||
|
|
||||||
private final Inflater mInflater;
|
private final Inflater inflater;
|
||||||
|
|
||||||
private final byte[] mBuffer;
|
private final byte[] buffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@code InflateDecoder}
|
* Creates an {@code InflateDecoder}
|
||||||
@ -71,20 +71,20 @@ final class InflateDecoder implements Decoder {
|
|||||||
throw new IllegalArgumentException("inflater == null");
|
throw new IllegalArgumentException("inflater == null");
|
||||||
}
|
}
|
||||||
|
|
||||||
mInflater = pInflater;
|
inflater = pInflater;
|
||||||
mBuffer = new byte[1024];
|
buffer = new byte[1024];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int decode(final InputStream pStream, final byte[] pBuffer) throws IOException {
|
public int decode(final InputStream pStream, final byte[] pBuffer) throws IOException {
|
||||||
try {
|
try {
|
||||||
int decoded;
|
int decoded;
|
||||||
|
|
||||||
while ((decoded = mInflater.inflate(pBuffer, 0, pBuffer.length)) == 0) {
|
while ((decoded = inflater.inflate(pBuffer, 0, pBuffer.length)) == 0) {
|
||||||
if (mInflater.finished() || mInflater.needsDictionary()) {
|
if (inflater.finished() || inflater.needsDictionary()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mInflater.needsInput()) {
|
if (inflater.needsInput()) {
|
||||||
fill(pStream);
|
fill(pStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,12 +98,12 @@ final class InflateDecoder implements Decoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void fill(final InputStream pStream) throws IOException {
|
private void fill(final InputStream pStream) throws IOException {
|
||||||
int available = pStream.read(mBuffer, 0, mBuffer.length);
|
int available = pStream.read(buffer, 0, buffer.length);
|
||||||
|
|
||||||
if (available == -1) {
|
if (available == -1) {
|
||||||
throw new EOFException("Unexpected end of ZLIB stream");
|
throw new EOFException("Unexpected end of ZLIB stream");
|
||||||
}
|
}
|
||||||
|
|
||||||
mInflater.setInput(mBuffer, 0, available);
|
inflater.setInput(buffer, 0, available);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -67,7 +67,7 @@ public class CacheFilter extends GenericFilter {
|
|||||||
public void init() throws ServletException {
|
public void init() throws ServletException {
|
||||||
FilterConfig config = getFilterConfig();
|
FilterConfig config = getFilterConfig();
|
||||||
|
|
||||||
// Default don't delete cache files on exit (peristent cache)
|
// Default don't delete cache files on exit (persistent cache)
|
||||||
boolean deleteCacheOnExit = "TRUE".equalsIgnoreCase(config.getInitParameter("deleteCacheOnExit"));
|
boolean deleteCacheOnExit = "TRUE".equalsIgnoreCase(config.getInitParameter("deleteCacheOnExit"));
|
||||||
|
|
||||||
// Default expiry time 10 minutes
|
// Default expiry time 10 minutes
|
||||||
@ -76,6 +76,7 @@ public class CacheFilter extends GenericFilter {
|
|||||||
String expiryTimeStr = config.getInitParameter("expiryTime");
|
String expiryTimeStr = config.getInitParameter("expiryTime");
|
||||||
if (!StringUtil.isEmpty(expiryTimeStr)) {
|
if (!StringUtil.isEmpty(expiryTimeStr)) {
|
||||||
try {
|
try {
|
||||||
|
// TODO: This is insane.. :-P Let the expiry time be in minutes or seconds..
|
||||||
expiryTime = Integer.parseInt(expiryTimeStr);
|
expiryTime = Integer.parseInt(expiryTimeStr);
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e) {
|
catch (NumberFormatException e) {
|
||||||
@ -179,21 +180,21 @@ public class CacheFilter extends GenericFilter {
|
|||||||
// TODO: Extract, complete and document this class, might be useful in other cases
|
// TODO: Extract, complete and document this class, might be useful in other cases
|
||||||
// Maybe add it to the ServletUtil class
|
// Maybe add it to the ServletUtil class
|
||||||
static class ServletContextLoggerAdapter extends Logger {
|
static class ServletContextLoggerAdapter extends Logger {
|
||||||
private final ServletContext mContext;
|
private final ServletContext context;
|
||||||
|
|
||||||
public ServletContextLoggerAdapter(String pName, ServletContext pContext) {
|
public ServletContextLoggerAdapter(String pName, ServletContext pContext) {
|
||||||
super(pName, null);
|
super(pName, null);
|
||||||
mContext = pContext;
|
context = pContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void log(Level pLevel, String pMessage) {
|
public void log(Level pLevel, String pMessage) {
|
||||||
mContext.log(pMessage);
|
context.log(pMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void log(Level pLevel, String pMessage, Throwable pThrowable) {
|
public void log(Level pLevel, String pMessage, Throwable pThrowable) {
|
||||||
mContext.log(pMessage, pThrowable);
|
context.log(pMessage, pThrowable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -69,7 +69,7 @@ class CacheResponseWrapper extends HttpServletResponseWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
NOTE: This class defers determining if a response is cachable until the
|
NOTE: This class defers determining if a response is cacheable until the
|
||||||
output stream is needed.
|
output stream is needed.
|
||||||
This it the reason for the somewhat complicated logic in the add/setHeader
|
This it the reason for the somewhat complicated logic in the add/setHeader
|
||||||
methods below.
|
methods below.
|
||||||
@ -80,7 +80,7 @@ class CacheResponseWrapper extends HttpServletResponseWrapper {
|
|||||||
cachedResponse = cached.createCachedResponse();
|
cachedResponse = cached.createCachedResponse();
|
||||||
streamDelegate = new ServletResponseStreamDelegate(this) {
|
streamDelegate = new ServletResponseStreamDelegate(this) {
|
||||||
protected OutputStream createOutputStream() throws IOException {
|
protected OutputStream createOutputStream() throws IOException {
|
||||||
// Test if this request is really cachable, otherwise,
|
// Test if this request is really cacheable, otherwise,
|
||||||
// just write through to underlying response, and don't cache
|
// just write through to underlying response, and don't cache
|
||||||
if (isCachable()) {
|
if (isCachable()) {
|
||||||
return cachedResponse.getOutputStream();
|
return cachedResponse.getOutputStream();
|
||||||
|
@ -35,10 +35,7 @@ import com.twelvemonkeys.util.LinkedMap;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CachedResponseImpl
|
* CachedResponseImpl
|
||||||
@ -53,7 +50,7 @@ class CachedResponseImpl implements CachedResponse {
|
|||||||
int status;
|
int status;
|
||||||
|
|
||||||
protected CachedResponseImpl() {
|
protected CachedResponseImpl() {
|
||||||
headers = new LinkedMap<String, List<String>>(); // Keep headers in insertion order
|
headers = new LinkedHashMap<String, List<String>>(); // Keep headers in insertion order
|
||||||
}
|
}
|
||||||
|
|
||||||
// For use by HTTPCache, when recreating CachedResponses from disk cache
|
// For use by HTTPCache, when recreating CachedResponses from disk cache
|
||||||
@ -82,7 +79,7 @@ class CachedResponseImpl implements CachedResponse {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Replace Last-Modified with X-Cached-At? See CachedEntityImpl, line 50
|
// TODO: Replace Last-Modified with X-Cached-At? See CachedEntityImpl
|
||||||
|
|
||||||
String[] headerValues = getHeaderValues(header);
|
String[] headerValues = getHeaderValues(header);
|
||||||
|
|
||||||
@ -99,7 +96,7 @@ class CachedResponseImpl implements CachedResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the cahced content to the response
|
* Writes the cached content to the response
|
||||||
*
|
*
|
||||||
* @param pStream the response stream
|
* @param pStream the response stream
|
||||||
* @throws java.io.IOException
|
* @throws java.io.IOException
|
||||||
@ -132,6 +129,7 @@ class CachedResponseImpl implements CachedResponse {
|
|||||||
*/
|
*/
|
||||||
public String[] getHeaderValues(final String pHeaderName) {
|
public String[] getHeaderValues(final String pHeaderName) {
|
||||||
List<String> values = headers.get(pHeaderName);
|
List<String> values = headers.get(pHeaderName);
|
||||||
|
|
||||||
if (values == null) {
|
if (values == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -152,6 +150,7 @@ class CachedResponseImpl implements CachedResponse {
|
|||||||
*/
|
*/
|
||||||
public String getHeaderValue(final String pHeaderName) {
|
public String getHeaderValue(final String pHeaderName) {
|
||||||
List<String> values = headers.get(pHeaderName);
|
List<String> values = headers.get(pHeaderName);
|
||||||
|
|
||||||
return (values != null && values.size() > 0) ? values.get(0) : null;
|
return (values != null && values.size() > 0) ? values.get(0) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import java.io.OutputStream;
|
|||||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-servlet/src/main/java/com/twelvemonkeys/servlet/cache/ClientCacheResponse.java#2 $
|
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-servlet/src/main/java/com/twelvemonkeys/servlet/cache/ClientCacheResponse.java#2 $
|
||||||
*/
|
*/
|
||||||
public final class ClientCacheResponse extends AbstractCacheResponse {
|
public final class ClientCacheResponse extends AbstractCacheResponse {
|
||||||
// It's quite useless to cahce the data either on disk or in memory, as it already is cached in the client's cache...
|
// It's quite useless to cache the data either on disk or in memory, as it already is cached in the client's cache...
|
||||||
// It would be nice if we could bypass that...
|
// It would be nice if we could bypass that...
|
||||||
|
|
||||||
public OutputStream getOutputStream() throws IOException {
|
public OutputStream getOutputStream() throws IOException {
|
||||||
|
@ -357,8 +357,7 @@ public class HTTPCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// else if (not cached || stale), resolve through wrapped (caching) response
|
||||||
// else if (not cached ||<EFBFBD>stale), resolve through wrapped (caching) response
|
|
||||||
// else render to response
|
// else render to response
|
||||||
|
|
||||||
// TODO: This is a bottleneck for uncachable resources. Should not
|
// TODO: This is a bottleneck for uncachable resources. Should not
|
||||||
|
Loading…
x
Reference in New Issue
Block a user