You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@apache.org on 2008/03/25 07:01:57 UTC
svn commit: r640706 [2/12] - in /tomcat/sandbox/tomcat-lite/tomcat-coyote:
./ org/ org/apache/ org/apache/coyote/ org/apache/coyote/http11/
org/apache/coyote/http11/filters/ org/apache/juli/ org/apache/juli/logging/
org/apache/tomcat/ org/apache/tomcat...
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/OutputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/OutputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/OutputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/OutputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11;
+
+import java.io.IOException;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+
+import org.apache.coyote.OutputBuffer;
+import org.apache.coyote.Response;
+
+/**
+ * Output filter.
+ *
+ * @author Remy Maucherat
+ */
+public interface OutputFilter extends OutputBuffer {
+
+
+ /**
+ * Write some bytes.
+ *
+ * @return number of bytes written by the filter
+ */
+ public int doWrite(ByteChunk chunk, Response unused)
+ throws IOException;
+
+
+ /**
+ * 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.
+ */
+ public void setResponse(Response response);
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle();
+
+
+ /**
+ * Get the name of the encoding handled by this filter.
+ */
+ public ByteChunk getEncodingName();
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(OutputBuffer buffer);
+
+
+ /**
+ * End the current request. It is acceptable to write extra bytes using
+ * buffer.doWrite during the execution of this method.
+ *
+ * @return Should return 0 unless the filter does some content length
+ * delimitation, in which case the number is the amount of extra bytes or
+ * missing bytes, which would indicate an error.
+ * Note: It is recommended that extra bytes be swallowed by the filter.
+ */
+ public long end()
+ throws IOException;
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/BufferedInputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/BufferedInputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/BufferedInputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/BufferedInputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+import org.apache.coyote.Request;
+import org.apache.coyote.InputBuffer;
+import org.apache.coyote.http11.InputFilter;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+/**
+ * Input filter responsible for reading and buffering the request body, so that
+ * it does not interfere with client SSL handshake messages.
+ */
+public class BufferedInputFilter implements InputFilter {
+
+ // -------------------------------------------------------------- Constants
+
+ private static final String ENCODING_NAME = "buffered";
+ private static final ByteChunk ENCODING = new ByteChunk();
+
+
+ // ----------------------------------------------------- Instance Variables
+
+ private ByteChunk buffered = null;
+ private ByteChunk tempRead = new ByteChunk(1024);
+ private InputBuffer buffer;
+ private boolean hasRead = false;
+
+
+ // ----------------------------------------------------- Static Initializer
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ }
+
+
+ // --------------------------------------------------------- Public Methods
+
+
+ /**
+ * Set the buffering limit. This should be reset every time the buffer is
+ * used.
+ */
+ public void setLimit(int limit) {
+ if (buffered == null) {
+ buffered = new ByteChunk(4048);
+ buffered.setLimit(limit);
+ }
+ }
+
+
+ // ---------------------------------------------------- InputBuffer Methods
+
+
+ /**
+ * Reads the request body and buffers it.
+ */
+ public void setRequest(Request request) {
+ // save off the Request body
+ try {
+ while (buffer.doRead(tempRead, request) >= 0) {
+ buffered.append(tempRead);
+ tempRead.recycle();
+ }
+ } catch(IOException iex) {
+ // Ignore
+ }
+ }
+
+ /**
+ * Fills the given ByteChunk with the buffered request body.
+ */
+ public int doRead(ByteChunk chunk, Request request) throws IOException {
+ if (hasRead || buffered.getLength() <= 0) {
+ return -1;
+ } else {
+ chunk.setBytes(buffered.getBytes(), buffered.getStart(),
+ buffered.getLength());
+ hasRead = true;
+ }
+ return chunk.getLength();
+ }
+
+ public void setBuffer(InputBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+ public void recycle() {
+ if (buffered.getBuffer().length > 65536) {
+ buffered = null;
+ } else {
+ buffered.recycle();
+ }
+ tempRead.recycle();
+ hasRead = false;
+ buffer = null;
+ }
+
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+ public long end() throws IOException {
+ return 0;
+ }
+
+ public int available() {
+ return buffered.getLength();
+ }
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedInputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedInputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedInputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedInputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,361 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.buf.HexUtils;
+
+import org.apache.coyote.InputBuffer;
+import org.apache.coyote.Request;
+import org.apache.coyote.http11.Constants;
+import org.apache.coyote.http11.InputFilter;
+
+/**
+ * Chunked input filter. Parses chunked data according to
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1">http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1</a><br>
+ *
+ * @author Remy Maucherat
+ * @author Filip Hanik
+ */
+public class ChunkedInputFilter implements InputFilter {
+
+
+ // -------------------------------------------------------------- Constants
+
+
+ protected static final String ENCODING_NAME = "chunked";
+ protected static final ByteChunk ENCODING = new ByteChunk();
+
+
+ // ----------------------------------------------------- Static Initializer
+
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * Next buffer in the pipeline.
+ */
+ protected InputBuffer buffer;
+
+
+ /**
+ * Number of bytes remaining in the current chunk.
+ */
+ protected int remaining = 0;
+
+
+ /**
+ * Position in the buffer.
+ */
+ protected int pos = 0;
+
+
+ /**
+ * Last valid byte in the buffer.
+ */
+ protected int lastValid = 0;
+
+
+ /**
+ * Read bytes buffer.
+ */
+ protected byte[] buf = null;
+
+
+ /**
+ * Byte chunk used to read bytes.
+ */
+ protected ByteChunk readChunk = new ByteChunk();
+
+
+ /**
+ * Flag set to true when the end chunk has been read.
+ */
+ protected boolean endChunk = false;
+
+ /**
+ * Flag set to true if the next call to doRead() must parse a CRLF pair
+ * before doing anything else.
+ */
+ protected boolean needCRLFParse = false;
+
+ // ------------------------------------------------------------- Properties
+
+
+ // ---------------------------------------------------- InputBuffer Methods
+
+
+ /**
+ * Read bytes.
+ *
+ * @return If the filter does request length control, this value is
+ * significant; it should be the number of bytes consumed from the buffer,
+ * up until the end of the current request body, or the buffer length,
+ * whichever is greater. If the filter does not do request body length
+ * control, the returned value should be -1.
+ */
+ public int doRead(ByteChunk chunk, Request req)
+ throws IOException {
+
+ if (endChunk)
+ return -1;
+
+ if(needCRLFParse) {
+ needCRLFParse = false;
+ parseCRLF();
+ }
+
+ if (remaining <= 0) {
+ if (!parseChunkHeader()) {
+ throw new IOException("Invalid chunk header");
+ }
+ if (endChunk) {
+ parseEndChunk();
+ return -1;
+ }
+ }
+
+ int result = 0;
+
+ if (pos >= lastValid) {
+ readBytes();
+ }
+
+ if (remaining > (lastValid - pos)) {
+ result = lastValid - pos;
+ remaining = remaining - result;
+ chunk.setBytes(buf, pos, result);
+ pos = lastValid;
+ } else {
+ result = remaining;
+ chunk.setBytes(buf, pos, remaining);
+ pos = pos + remaining;
+ remaining = 0;
+ //we need a CRLF
+ if ((pos+1) >= lastValid) {
+ //if we call parseCRLF we overrun the buffer here
+ //so we defer it to the next call BZ 11117
+ needCRLFParse = true;
+ } else {
+ parseCRLF(); //parse the CRLF immediately
+ }
+ }
+
+ return result;
+
+ }
+
+
+ // ---------------------------------------------------- InputFilter Methods
+
+
+ /**
+ * Read the content length from the request.
+ */
+ public void setRequest(Request request) {
+ }
+
+
+ /**
+ * End the current request.
+ */
+ public long end()
+ throws IOException {
+
+ // Consume extra bytes : parse the stream until the end chunk is found
+ while (doRead(readChunk, null) >= 0) {
+ }
+
+ // Return the number of extra bytes which were consumed
+ return (lastValid - pos);
+
+ }
+
+
+ /**
+ * Amount of bytes still available in a buffer.
+ */
+ public int available() {
+ return (lastValid - pos);
+ }
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(InputBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ remaining = 0;
+ pos = 0;
+ lastValid = 0;
+ endChunk = false;
+ }
+
+
+ /**
+ * Return the name of the associated encoding; Here, the value is
+ * "identity".
+ */
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+
+ // ------------------------------------------------------ Protected Methods
+
+
+ /**
+ * Read bytes from the previous buffer.
+ */
+ protected int readBytes()
+ throws IOException {
+
+ int nRead = buffer.doRead(readChunk, null);
+ pos = readChunk.getStart();
+ lastValid = pos + nRead;
+ buf = readChunk.getBytes();
+
+ return nRead;
+
+ }
+
+
+ /**
+ * Parse the header of a chunk.
+ * A chunk header can look like
+ * A10CRLF
+ * F23;chunk-extension to be ignoredCRLF
+ * The letters before CRLF but after the trailer mark, must be valid hex digits,
+ * we should not parse F23IAMGONNAMESSTHISUP34CRLF as a valid header
+ * according to spec
+ */
+ protected boolean parseChunkHeader()
+ throws IOException {
+
+ int result = 0;
+ boolean eol = false;
+ boolean readDigit = false;
+ boolean trailer = false;
+
+ while (!eol) {
+
+ if (pos >= lastValid) {
+ if (readBytes() <= 0)
+ return false;
+ }
+
+ if (buf[pos] == Constants.CR) {
+ } else if (buf[pos] == Constants.LF) {
+ eol = true;
+ } else if (buf[pos] == Constants.SEMI_COLON) {
+ trailer = true;
+ } else if (!trailer) {
+ //don't read data after the trailer
+ if (HexUtils.DEC[buf[pos]] != -1) {
+ readDigit = true;
+ result *= 16;
+ result += HexUtils.DEC[buf[pos]];
+ } else {
+ //we shouldn't allow invalid, non hex characters
+ //in the chunked header
+ return false;
+ }
+ }
+
+ pos++;
+
+ }
+
+ if (!readDigit)
+ return false;
+
+ if (result == 0)
+ endChunk = true;
+
+ remaining = result;
+ if (remaining < 0)
+ return false;
+
+ return true;
+
+ }
+
+
+ /**
+ * Parse CRLF at end of chunk.
+ */
+ protected boolean parseCRLF()
+ throws IOException {
+
+ boolean eol = false;
+ boolean crfound = false;
+
+ while (!eol) {
+
+ if (pos >= lastValid) {
+ if (readBytes() <= 0)
+ throw new IOException("Invalid CRLF");
+ }
+
+ if (buf[pos] == Constants.CR) {
+ if (crfound) throw new IOException("Invalid CRLF, two CR characters encountered.");
+ crfound = true;
+ } else if (buf[pos] == Constants.LF) {
+ if (!crfound) throw new IOException("Invalid CRLF, no CR character encountered.");
+ eol = true;
+ } else {
+ throw new IOException("Invalid CRLF");
+ }
+
+ pos++;
+
+ }
+
+ return true;
+
+ }
+
+
+ /**
+ * Parse end chunk data.
+ * FIXME: Handle trailers
+ */
+ protected boolean parseEndChunk()
+ throws IOException {
+
+ return parseCRLF(); // FIXME
+
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedOutputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedOutputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedOutputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/ChunkedOutputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.buf.HexUtils;
+
+import org.apache.coyote.OutputBuffer;
+import org.apache.coyote.Response;
+import org.apache.coyote.http11.OutputFilter;
+
+/**
+ * Chunked output filter.
+ *
+ * @author Remy Maucherat
+ */
+public class ChunkedOutputFilter implements OutputFilter {
+
+
+ // -------------------------------------------------------------- Constants
+
+
+ protected static final String ENCODING_NAME = "chunked";
+ protected static final ByteChunk ENCODING = new ByteChunk();
+
+
+ /**
+ * End chunk.
+ */
+ protected static final ByteChunk END_CHUNK = new ByteChunk();
+
+
+ // ----------------------------------------------------- Static Initializer
+
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ byte[] END_CHUNK_BYTES = {(byte) '0', (byte) '\r', (byte) '\n',
+ (byte) '\r', (byte) '\n'};
+ END_CHUNK.setBytes(END_CHUNK_BYTES, 0, END_CHUNK_BYTES.length);
+ }
+
+
+ // ------------------------------------------------------------ Constructor
+
+
+ /**
+ * Default constructor.
+ */
+ public ChunkedOutputFilter() {
+ chunkLength = new byte[10];
+ chunkLength[8] = (byte) '\r';
+ chunkLength[9] = (byte) '\n';
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * Next buffer in the pipeline.
+ */
+ protected OutputBuffer buffer;
+
+
+ /**
+ * Buffer used for chunk length conversion.
+ */
+ protected byte[] chunkLength = new byte[10];
+
+
+ /**
+ * Chunk header.
+ */
+ protected ByteChunk chunkHeader = new ByteChunk();
+
+
+ // ------------------------------------------------------------- Properties
+
+
+ // --------------------------------------------------- OutputBuffer Methods
+
+
+ /**
+ * Write some bytes.
+ *
+ * @return number of bytes written by the filter
+ */
+ 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.HEX[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;
+
+ }
+
+
+ // --------------------------------------------------- OutputFilter Methods
+
+
+ /**
+ * 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.
+ */
+ public void setResponse(Response response) {
+ }
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(OutputBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+
+ /**
+ * End the current request. It is acceptable to write extra bytes using
+ * buffer.doWrite during the execution of this method.
+ */
+ public long end()
+ throws IOException {
+
+ // Write end chunk
+ buffer.doWrite(END_CHUNK, null);
+
+ return 0;
+
+ }
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ }
+
+
+ /**
+ * Return the name of the associated encoding; Here, the value is
+ * "identity".
+ */
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/GzipOutputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/GzipOutputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/GzipOutputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/GzipOutputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.GZIPOutputStream;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+
+import org.apache.coyote.OutputBuffer;
+import org.apache.coyote.Response;
+import org.apache.coyote.http11.OutputFilter;
+
+/**
+ * Gzip output filter.
+ *
+ * @author Remy Maucherat
+ */
+public class GzipOutputFilter implements OutputFilter {
+
+
+ // -------------------------------------------------------------- Constants
+
+
+ protected static final String ENCODING_NAME = "gzip";
+ protected static final ByteChunk ENCODING = new ByteChunk();
+
+
+ // ----------------------------------------------------- Static Initializer
+
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * Next buffer in the pipeline.
+ */
+ protected OutputBuffer buffer;
+
+
+ /**
+ * Compression output stream.
+ */
+ protected GZIPOutputStream compressionStream = null;
+
+
+ /**
+ * Fake internal output stream.
+ */
+ protected OutputStream fakeOutputStream = new FakeOutputStream();
+
+
+ // --------------------------------------------------- OutputBuffer Methods
+
+
+ /**
+ * Write some bytes.
+ *
+ * @return number of bytes written by the filter
+ */
+ public int doWrite(ByteChunk chunk, Response res)
+ throws IOException {
+ if (compressionStream == null) {
+ compressionStream = new GZIPOutputStream(fakeOutputStream);
+ }
+ compressionStream.write(chunk.getBytes(), chunk.getStart(),
+ chunk.getLength());
+ return chunk.getLength();
+ }
+
+
+ // --------------------------------------------------- OutputFilter Methods
+
+
+ /**
+ * 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.
+ */
+ public void setResponse(Response response) {
+ }
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(OutputBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+
+ /**
+ * End the current request. It is acceptable to write extra bytes using
+ * buffer.doWrite during the execution of this method.
+ */
+ public long end()
+ throws IOException {
+ if (compressionStream == null) {
+ compressionStream = new GZIPOutputStream(fakeOutputStream);
+ }
+ compressionStream.finish();
+ compressionStream.close();
+ return ((OutputFilter) buffer).end();
+ }
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ // Set compression stream to null
+ compressionStream = null;
+ }
+
+
+ /**
+ * Return the name of the associated encoding; Here, the value is
+ * "identity".
+ */
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+
+ // ------------------------------------------- FakeOutputStream Inner Class
+
+
+ protected class FakeOutputStream
+ extends OutputStream {
+ protected ByteChunk outputChunk = new ByteChunk();
+ protected byte[] singleByteBuffer = new byte[1];
+ public void write(int b)
+ throws IOException {
+ // Shouldn't get used for good performance, but is needed for
+ // compatibility with Sun JDK 1.4.0
+ singleByteBuffer[0] = (byte) (b & 0xff);
+ outputChunk.setBytes(singleByteBuffer, 0, 1);
+ buffer.doWrite(outputChunk, null);
+ }
+ public void write(byte[] b, int off, int len)
+ throws IOException {
+ outputChunk.setBytes(b, off, len);
+ buffer.doWrite(outputChunk, null);
+ }
+ public void flush() throws IOException {}
+ public void close() throws IOException {}
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityInputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityInputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityInputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityInputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+
+import org.apache.coyote.InputBuffer;
+import org.apache.coyote.Request;
+import org.apache.coyote.http11.InputFilter;
+
+/**
+ * Identity input filter.
+ *
+ * @author Remy Maucherat
+ */
+public class IdentityInputFilter implements InputFilter {
+
+
+ // -------------------------------------------------------------- Constants
+
+
+ protected static final String ENCODING_NAME = "identity";
+ protected static final ByteChunk ENCODING = new ByteChunk();
+
+
+ // ----------------------------------------------------- Static Initializer
+
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * Content length.
+ */
+ protected long contentLength = -1;
+
+
+ /**
+ * Remaining bytes.
+ */
+ protected long remaining = 0;
+
+
+ /**
+ * Next buffer in the pipeline.
+ */
+ protected InputBuffer buffer;
+
+
+ /**
+ * Chunk used to read leftover bytes.
+ */
+ protected ByteChunk endChunk = new ByteChunk();
+
+
+ // ------------------------------------------------------------- Properties
+
+
+ /**
+ * Get content length.
+ */
+ public long getContentLength() {
+ return contentLength;
+ }
+
+
+ /**
+ * Get remaining bytes.
+ */
+ public long getRemaining() {
+ return remaining;
+ }
+
+
+ // ---------------------------------------------------- InputBuffer Methods
+
+
+ /**
+ * Read bytes.
+ *
+ * @return If the filter does request length control, this value is
+ * significant; it should be the number of bytes consumed from the buffer,
+ * up until the end of the current request body, or the buffer length,
+ * whichever is greater. If the filter does not do request body length
+ * control, the returned value should be -1.
+ */
+ public int doRead(ByteChunk chunk, Request req)
+ throws IOException {
+
+ int result = -1;
+
+ if (contentLength >= 0) {
+ if (remaining > 0) {
+ int nRead = buffer.doRead(chunk, req);
+ if (nRead > 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;
+ } else {
+ result = nRead;
+ }
+ remaining = remaining - nRead;
+ } else {
+ // No more bytes left to be read : return -1 and clear the
+ // buffer
+ chunk.recycle();
+ result = -1;
+ }
+ }
+
+ return result;
+
+ }
+
+
+ // ---------------------------------------------------- InputFilter Methods
+
+
+ /**
+ * Read the content length from the request.
+ */
+ public void setRequest(Request request) {
+ contentLength = request.getContentLengthLong();
+ remaining = contentLength;
+ }
+
+
+ /**
+ * End the current request.
+ */
+ public long end()
+ throws IOException {
+
+ // Consume extra bytes.
+ while (remaining > 0) {
+ int nread = buffer.doRead(endChunk, null);
+ if (nread > 0 ) {
+ remaining = remaining - nread;
+ } else { // errors are handled higher up.
+ remaining = 0;
+ }
+ }
+
+ // If too many bytes were read, return the amount.
+ return -remaining;
+
+ }
+
+
+ /**
+ * Amount of bytes still available in a buffer.
+ */
+ public int available() {
+ return 0;
+ }
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(InputBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ contentLength = -1;
+ remaining = 0;
+ endChunk.recycle();
+ }
+
+
+ /**
+ * Return the name of the associated encoding; Here, the value is
+ * "identity".
+ */
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityOutputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityOutputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityOutputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/IdentityOutputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+
+import org.apache.coyote.OutputBuffer;
+import org.apache.coyote.Response;
+import org.apache.coyote.http11.OutputFilter;
+
+/**
+ * Identity output filter.
+ *
+ * @author Remy Maucherat
+ */
+public class IdentityOutputFilter implements OutputFilter {
+
+
+ // -------------------------------------------------------------- Constants
+
+
+ protected static final String ENCODING_NAME = "identity";
+ protected static final ByteChunk ENCODING = new ByteChunk();
+
+
+ // ----------------------------------------------------- Static Initializer
+
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * Content length.
+ */
+ protected long contentLength = -1;
+
+
+ /**
+ * Remaining bytes.
+ */
+ protected long remaining = 0;
+
+
+ /**
+ * Next buffer in the pipeline.
+ */
+ protected OutputBuffer buffer;
+
+
+ // ------------------------------------------------------------- Properties
+
+
+ /**
+ * Get content length.
+ */
+ public long getContentLength() {
+ return contentLength;
+ }
+
+
+ /**
+ * Get remaining bytes.
+ */
+ public long getRemaining() {
+ return remaining;
+ }
+
+
+ // --------------------------------------------------- OutputBuffer Methods
+
+
+ /**
+ * Write some bytes.
+ *
+ * @return number of bytes written by the filter
+ */
+ 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;
+
+ }
+
+
+ // --------------------------------------------------- OutputFilter Methods
+
+
+ /**
+ * 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.
+ */
+ public void setResponse(Response response) {
+ contentLength = response.getContentLengthLong();
+ remaining = contentLength;
+ }
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(OutputBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+
+ /**
+ * End the current request. It is acceptable to write extra bytes using
+ * buffer.doWrite during the execution of this method.
+ */
+ public long end()
+ throws IOException {
+
+ if (remaining > 0)
+ return remaining;
+ return 0;
+
+ }
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ contentLength = -1;
+ remaining = 0;
+ }
+
+
+ /**
+ * Return the name of the associated encoding; Here, the value is
+ * "identity".
+ */
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/SavedRequestInputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/SavedRequestInputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/SavedRequestInputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/SavedRequestInputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+
+import org.apache.coyote.InputBuffer;
+import org.apache.coyote.http11.InputFilter;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+/**
+ * Input filter responsible for replaying the request body when restoring the
+ * saved request after FORM authentication.
+ */
+public class SavedRequestInputFilter implements InputFilter {
+
+ /**
+ * The original request body.
+ */
+ protected ByteChunk input = null;
+
+ /**
+ * Create a new SavedRequestInputFilter.
+ *
+ * @param input The saved request body to be replayed.
+ */
+ public SavedRequestInputFilter(ByteChunk input) {
+ this.input = input;
+ }
+
+ /**
+ * Read bytes.
+ */
+ public int doRead(ByteChunk chunk, org.apache.coyote.Request request)
+ throws IOException {
+ int writeLength = 0;
+
+ if (chunk.getLimit() > 0 && chunk.getLimit() < input.getLength()) {
+ writeLength = chunk.getLimit();
+ } else {
+ writeLength = input.getLength();
+ }
+
+ if(input.getOffset()>= input.getEnd())
+ return -1;
+
+ input.substract(chunk.getBuffer(), 0, writeLength);
+ chunk.setOffset(0);
+ chunk.setEnd(writeLength);
+
+ return writeLength;
+ }
+
+ /**
+ * Set the content length on the request.
+ */
+ public void setRequest(org.apache.coyote.Request request) {
+ request.setContentLength(input.getLength());
+ }
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ input = null;
+ }
+
+ /**
+ * Return the name of the associated encoding; here, the value is null.
+ */
+ public ByteChunk getEncodingName() {
+ return null;
+ }
+
+ /**
+ * Set the next buffer in the filter pipeline (has no effect).
+ */
+ public void setBuffer(InputBuffer buffer) {
+ }
+
+ /**
+ * Amount of bytes still available in a buffer.
+ */
+ public int available() {
+ return input.getLength();
+ }
+
+ /**
+ * End the current request (has no effect).
+ */
+ public long end() throws IOException {
+ return 0;
+ }
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidInputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidInputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidInputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidInputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+
+import org.apache.coyote.InputBuffer;
+import org.apache.coyote.Request;
+import org.apache.coyote.http11.InputFilter;
+
+/**
+ * Void input filter, which returns -1 when attempting a read. Used with a GET,
+ * HEAD, or a similar request.
+ *
+ * @author Remy Maucherat
+ */
+public class VoidInputFilter implements InputFilter {
+
+
+ // -------------------------------------------------------------- Constants
+
+
+ protected static final String ENCODING_NAME = "void";
+ protected static final ByteChunk ENCODING = new ByteChunk();
+
+
+ // ----------------------------------------------------- Static Initializer
+
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ // --------------------------------------------------- OutputBuffer Methods
+
+
+ /**
+ * Write some bytes.
+ *
+ * @return number of bytes written by the filter
+ */
+ public int doRead(ByteChunk chunk, Request req)
+ throws IOException {
+
+ return -1;
+
+ }
+
+
+ // --------------------------------------------------- OutputFilter Methods
+
+
+ /**
+ * Set the associated reauest.
+ */
+ public void setRequest(Request request) {
+ }
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(InputBuffer buffer) {
+ }
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ }
+
+
+ /**
+ * Return the name of the associated encoding; Here, the value is
+ * "void".
+ */
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+
+ /**
+ * End the current request. It is acceptable to write extra bytes using
+ * buffer.doWrite during the execution of this method.
+ *
+ * @return Should return 0 unless the filter does some content length
+ * delimitation, in which case the number is the amount of extra bytes or
+ * missing bytes, which would indicate an error.
+ * Note: It is recommended that extra bytes be swallowed by the filter.
+ */
+ public long end()
+ throws IOException {
+ return 0;
+ }
+
+
+ /**
+ * Amount of bytes still available in a buffer.
+ */
+ public int available() {
+ return 0;
+ }
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidOutputFilter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidOutputFilter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidOutputFilter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/coyote/http11/filters/VoidOutputFilter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.coyote.http11.filters;
+
+import java.io.IOException;
+
+import org.apache.tomcat.util.buf.ByteChunk;
+
+import org.apache.coyote.OutputBuffer;
+import org.apache.coyote.Response;
+import org.apache.coyote.http11.OutputFilter;
+
+/**
+ * Void output filter, which silently swallows bytes written. Used with a 204
+ * status (no content) or a HEAD request.
+ *
+ * @author Remy Maucherat
+ */
+public class VoidOutputFilter implements OutputFilter {
+
+
+ // -------------------------------------------------------------- Constants
+
+
+ protected static final String ENCODING_NAME = "void";
+ protected static final ByteChunk ENCODING = new ByteChunk();
+
+
+ // ----------------------------------------------------- Static Initializer
+
+
+ static {
+ ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * Next buffer in the pipeline.
+ */
+ protected OutputBuffer buffer;
+
+
+ // --------------------------------------------------- OutputBuffer Methods
+
+
+ /**
+ * Write some bytes.
+ *
+ * @return number of bytes written by the filter
+ */
+ public int doWrite(ByteChunk chunk, Response res)
+ throws IOException {
+
+ return chunk.getLength();
+
+ }
+
+
+ // --------------------------------------------------- OutputFilter Methods
+
+
+ /**
+ * 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.
+ */
+ public void setResponse(Response response) {
+ }
+
+
+ /**
+ * Set the next buffer in the filter pipeline.
+ */
+ public void setBuffer(OutputBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+
+ /**
+ * Make the filter ready to process the next request.
+ */
+ public void recycle() {
+ }
+
+
+ /**
+ * Return the name of the associated encoding; Here, the value is
+ * "identity".
+ */
+ public ByteChunk getEncodingName() {
+ return ENCODING;
+ }
+
+
+ /**
+ * End the current request. It is acceptable to write extra bytes using
+ * buffer.doWrite during the execution of this method.
+ *
+ * @return Should return 0 unless the filter does some content length
+ * delimitation, in which case the number is the amount of extra bytes or
+ * missing bytes, which would indicate an error.
+ * Note: It is recommended that extra bytes be swallowed by the filter.
+ */
+ public long end()
+ throws IOException {
+ return 0;
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/ClassLoaderLogManager.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/ClassLoaderLogManager.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/ClassLoaderLogManager.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/ClassLoaderLogManager.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.juli;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URLClassLoader;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.StringTokenizer;
+import java.util.WeakHashMap;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+
+
+/**
+ * Per classloader LogManager implementation.
+ */
+public class ClassLoaderLogManager extends LogManager {
+
+
+ // -------------------------------------------------------------- Variables
+
+
+ /**
+ * Map containing the classloader information, keyed per classloader. A
+ * weak hashmap is used to ensure no classloader reference is leaked from
+ * application redeployment.
+ */
+ protected final Map<ClassLoader, ClassLoaderLogInfo> classLoaderLoggers =
+ new WeakHashMap<ClassLoader, ClassLoaderLogInfo>();
+
+
+ /**
+ * This prefix is used to allow using prefixes for the properties names
+ * of handlers and their subcomponents.
+ */
+ protected ThreadLocal<String> prefix = new ThreadLocal<String>();
+
+
+ // --------------------------------------------------------- Public Methods
+
+
+ /**
+ * Add the specified logger to the classloader local configuration.
+ *
+ * @param logger The logger to be added
+ */
+ public synchronized boolean addLogger(final Logger logger) {
+
+ final String loggerName = logger.getName();
+
+ ClassLoader classLoader =
+ Thread.currentThread().getContextClassLoader();
+ ClassLoaderLogInfo info = getClassLoaderInfo(classLoader);
+ if (info.loggers.containsKey(loggerName)) {
+ return false;
+ }
+ info.loggers.put(loggerName, logger);
+
+ // Apply initial level for new logger
+ final String levelString = getProperty(loggerName + ".level");
+ if (levelString != null) {
+ try {
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ logger.setLevel(Level.parse(levelString.trim()));
+ return null;
+ }
+ });
+ } catch (IllegalArgumentException e) {
+ // Leave level set to null
+ }
+ }
+
+ // If any parent loggers have levels definied, make sure they are
+ // instantiated
+ int dotIndex = loggerName.lastIndexOf('.');
+ while (dotIndex >= 0) {
+ final String parentName = loggerName.substring(0, dotIndex);
+ if (getProperty(parentName + ".level") != null) {
+ Logger.getLogger(parentName);
+ break;
+ }
+ dotIndex = loggerName.lastIndexOf('.', dotIndex - 1);
+ }
+
+ // Find associated node
+ LogNode node = info.rootNode.findNode(loggerName);
+ node.logger = logger;
+
+ // Set parent logger
+ Logger parentLogger = node.findParentLogger();
+ if (parentLogger != null) {
+ doSetParentLogger(logger, parentLogger);
+ }
+
+ // Tell children we are their new parent
+ node.setParentLogger(logger);
+
+ // Add associated handlers, if any are defined using the .handlers property.
+ // In this case, handlers of the parent logger(s) will not be used
+ String handlers = getProperty(loggerName + ".handlers");
+ if (handlers != null) {
+ logger.setUseParentHandlers(false);
+ StringTokenizer tok = new StringTokenizer(handlers, ",");
+ while (tok.hasMoreTokens()) {
+ String handlerName = (tok.nextToken().trim());
+ Handler handler = null;
+ ClassLoader current = classLoader;
+ while (current != null) {
+ info = (ClassLoaderLogInfo) classLoaderLoggers.get(current);
+ if (info != null) {
+ handler = (Handler) info.handlers.get(handlerName);
+ if (handler != null) {
+ break;
+ }
+ }
+ current = current.getParent();
+ }
+ if (handler != null) {
+ logger.addHandler(handler);
+ }
+ }
+ }
+
+ // Parse useParentHandlers to set if the logger should delegate to its parent.
+ // Unlike java.util.logging, the default is to not delegate if a list of handlers
+ // has been specified for the logger.
+ String useParentHandlersString = getProperty(loggerName + ".useParentHandlers");
+ if (Boolean.valueOf(useParentHandlersString).booleanValue()) {
+ logger.setUseParentHandlers(true);
+ }
+
+ return true;
+ }
+
+
+ /**
+ * Get the logger associated with the specified name inside
+ * the classloader local configuration. If this returns null,
+ * and the call originated for Logger.getLogger, a new
+ * logger with the specified name will be instantiated and
+ * added using addLogger.
+ *
+ * @param name The name of the logger to retrieve
+ */
+ public synchronized Logger getLogger(final String name) {
+ ClassLoader classLoader = Thread.currentThread()
+ .getContextClassLoader();
+ return (Logger) getClassLoaderInfo(classLoader).loggers.get(name);
+ }
+
+
+ /**
+ * Get an enumeration of the logger names currently defined in the
+ * classloader local configuration.
+ */
+ public synchronized Enumeration<String> getLoggerNames() {
+ ClassLoader classLoader = Thread.currentThread()
+ .getContextClassLoader();
+ return Collections.enumeration(getClassLoaderInfo(classLoader).loggers.keySet());
+ }
+
+
+ /**
+ * Get the value of the specified property in the classloader local
+ * configuration.
+ *
+ * @param name The property name
+ */
+ public String getProperty(String name) {
+ ClassLoader classLoader = Thread.currentThread()
+ .getContextClassLoader();
+ String prefix = (String) this.prefix.get();
+ if (prefix != null) {
+ name = prefix + name;
+ }
+ ClassLoaderLogInfo info = getClassLoaderInfo(classLoader);
+ String result = info.props.getProperty(name);
+ // If the property was not found, and the current classloader had no
+ // configuration (property list is empty), look for the parent classloader
+ // properties.
+ if ((result == null) && (info.props.isEmpty())) {
+ ClassLoader current = classLoader.getParent();
+ while (current != null) {
+ info = (ClassLoaderLogInfo) classLoaderLoggers.get(current);
+ if (info != null) {
+ result = info.props.getProperty(name);
+ if ((result != null) || (!info.props.isEmpty())) {
+ break;
+ }
+ }
+ current = current.getParent();
+ }
+ if (result == null) {
+ result = super.getProperty(name);
+ }
+ }
+ // Simple property replacement (mostly for folder names)
+ if (result != null) {
+ result = replace(result);
+ }
+ return result;
+ }
+
+
+ public void readConfiguration()
+ throws IOException, SecurityException {
+
+ checkAccess();
+
+ readConfiguration(Thread.currentThread().getContextClassLoader());
+
+ }
+
+ public void readConfiguration(InputStream is)
+ throws IOException, SecurityException {
+
+ checkAccess();
+ reset();
+
+ readConfiguration(is, Thread.currentThread().getContextClassLoader());
+
+ }
+
+ // ------------------------------------------------------ Protected Methods
+
+
+ /**
+ * Retrieve the configuration associated with the specified classloader. If
+ * it does not exist, it will be created.
+ *
+ * @param classLoader The classloader for which we will retrieve or build the
+ * configuration
+ */
+ protected ClassLoaderLogInfo getClassLoaderInfo(ClassLoader classLoader) {
+
+ if (classLoader == null) {
+ classLoader = ClassLoader.getSystemClassLoader();
+ }
+ ClassLoaderLogInfo info = (ClassLoaderLogInfo) classLoaderLoggers
+ .get(classLoader);
+ if (info == null) {
+ final ClassLoader classLoaderParam = classLoader;
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ try {
+ readConfiguration(classLoaderParam);
+ } catch (IOException e) {
+ // Ignore
+ }
+ return null;
+ }
+ });
+ info = (ClassLoaderLogInfo) classLoaderLoggers.get(classLoader);
+ }
+ return info;
+ }
+
+
+ /**
+ * Read configuration for the specified classloader.
+ *
+ * @param classLoader
+ * @throws IOException Errot
+ */
+ protected void readConfiguration(ClassLoader classLoader)
+ throws IOException {
+
+ InputStream is = null;
+ // Special case for URL classloaders which are used in containers:
+ // only look in the local repositories to avoid redefining loggers 20 times
+ if ((classLoader instanceof URLClassLoader)
+ && (((URLClassLoader) classLoader).findResource("logging.properties") != null)) {
+ is = classLoader.getResourceAsStream("logging.properties");
+ }
+ if ((is == null) && (classLoader == ClassLoader.getSystemClassLoader())) {
+ String configFileStr = System.getProperty("java.util.logging.config.file");
+ if (configFileStr != null) {
+ try {
+ is = new FileInputStream(replace(configFileStr));
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+ // Try the default JVM configuration
+ if (is == null) {
+ File defaultFile = new File(new File(System.getProperty("java.home"), "lib"),
+ "logging.properties");
+ try {
+ is = new FileInputStream(defaultFile);
+ } catch (IOException e) {
+ // Critical problem, do something ...
+ }
+ }
+ }
+
+ Logger localRootLogger = new RootLogger();
+ if (is == null) {
+ // Retrieve the root logger of the parent classloader instead
+ ClassLoader current = classLoader.getParent();
+ ClassLoaderLogInfo info = null;
+ while (current != null && info == null) {
+ info = getClassLoaderInfo(current);
+ current = current.getParent();
+ }
+ if (info != null) {
+ localRootLogger.setParent(info.rootNode.logger);
+ }
+ }
+ ClassLoaderLogInfo info =
+ new ClassLoaderLogInfo(new LogNode(null, localRootLogger));
+ classLoaderLoggers.put(classLoader, info);
+
+ if (is != null) {
+ readConfiguration(is, classLoader);
+ }
+ addLogger(localRootLogger);
+
+ }
+
+
+ /**
+ * Load specified configuration.
+ *
+ * @param is InputStream to the properties file
+ * @param classLoader for which the configuration will be loaded
+ * @throws IOException If something wrong happens during loading
+ */
+ protected void readConfiguration(InputStream is, ClassLoader classLoader)
+ throws IOException {
+
+ ClassLoaderLogInfo info =
+ (ClassLoaderLogInfo) classLoaderLoggers.get(classLoader);
+
+ try {
+ info.props.load(is);
+ } catch (IOException e) {
+ // Report error
+ System.err.println("Configuration error");
+ e.printStackTrace();
+ } finally {
+ try {
+ is.close();
+ } catch (Throwable t) {}
+ }
+
+ // Create handlers for the root logger of this classloader
+ String rootHandlers = info.props.getProperty(".handlers");
+ String handlers = info.props.getProperty("handlers");
+ Logger localRootLogger = info.rootNode.logger;
+ if (handlers != null) {
+ StringTokenizer tok = new StringTokenizer(handlers, ",");
+ while (tok.hasMoreTokens()) {
+ String handlerName = (tok.nextToken().trim());
+ String handlerClassName = handlerName;
+ String prefix = "";
+ if (handlerClassName.length() <= 0) {
+ continue;
+ }
+ // Parse and remove a prefix (prefix start with a digit, such as
+ // "10WebappFooHanlder.")
+ if (Character.isDigit(handlerClassName.charAt(0))) {
+ int pos = handlerClassName.indexOf('.');
+ if (pos >= 0) {
+ prefix = handlerClassName.substring(0, pos + 1);
+ handlerClassName = handlerClassName.substring(pos + 1);
+ }
+ }
+ try {
+ this.prefix.set(prefix);
+ Handler handler =
+ (Handler) classLoader.loadClass(handlerClassName).newInstance();
+ // The specification strongly implies all configuration should be done
+ // during the creation of the handler object.
+ // This includes setting level, filter, formatter and encoding.
+ this.prefix.set(null);
+ info.handlers.put(handlerName, handler);
+ if (rootHandlers == null) {
+ localRootLogger.addHandler(handler);
+ }
+ } catch (Exception e) {
+ // Report error
+ System.err.println("Handler error");
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+ }
+
+
+ /**
+ * Set parent child relationship between the two specified loggers.
+ *
+ * @param logger
+ * @param parent
+ */
+ protected static void doSetParentLogger(final Logger logger,
+ final Logger parent) {
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ logger.setParent(parent);
+ return null;
+ }
+ });
+ }
+
+
+ /**
+ * System property replacement in the given string.
+ *
+ * @param str The original string
+ * @return the modified string
+ */
+ protected String replace(String str) {
+ String result = str;
+ int pos_start = result.indexOf("${");
+ if (pos_start != -1) {
+ int pos_end = result.indexOf('}');
+ if (pos_end != -1) {
+ String propName = result.substring(pos_start + 2, pos_end);
+ String replacement = System.getProperty(propName);
+ if (replacement != null) {
+ if(pos_start >0) {
+ result = result.substring(0,pos_start) +
+ replacement + replace(result.substring(pos_end + 1));
+ } else {
+ result = replacement + replace(result.substring(pos_end + 1));
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+
+ // ---------------------------------------------------- LogNode Inner Class
+
+
+ protected static final class LogNode {
+ Logger logger;
+
+ protected final Map<String, LogNode> children =
+ new HashMap<String, LogNode>();
+
+ protected final LogNode parent;
+
+ LogNode(final LogNode parent, final Logger logger) {
+ this.parent = parent;
+ this.logger = logger;
+ }
+
+ LogNode(final LogNode parent) {
+ this(parent, null);
+ }
+
+ LogNode findNode(String name) {
+ LogNode currentNode = this;
+ if (logger.getName().equals(name)) {
+ return this;
+ }
+ while (name != null) {
+ final int dotIndex = name.indexOf('.');
+ final String nextName;
+ if (dotIndex < 0) {
+ nextName = name;
+ name = null;
+ } else {
+ nextName = name.substring(0, dotIndex);
+ name = name.substring(dotIndex + 1);
+ }
+ LogNode childNode = (LogNode) currentNode.children
+ .get(nextName);
+ if (childNode == null) {
+ childNode = new LogNode(currentNode);
+ currentNode.children.put(nextName, childNode);
+ }
+ currentNode = childNode;
+ }
+ return currentNode;
+ }
+
+ Logger findParentLogger() {
+ Logger logger = null;
+ LogNode node = parent;
+ while (node != null && logger == null) {
+ logger = node.logger;
+ node = node.parent;
+ }
+ return logger;
+ }
+
+ void setParentLogger(final Logger parent) {
+ for (final Iterator iter = children.values().iterator(); iter
+ .hasNext();) {
+ final LogNode childNode = (LogNode) iter.next();
+ if (childNode.logger == null) {
+ childNode.setParentLogger(parent);
+ } else {
+ doSetParentLogger(childNode.logger, parent);
+ }
+ }
+ }
+
+ }
+
+
+ // -------------------------------------------- ClassLoaderInfo Inner Class
+
+
+ protected static final class ClassLoaderLogInfo {
+ final LogNode rootNode;
+ final Map<String, Logger> loggers = new HashMap<String, Logger>();
+ final Map<String, Handler> handlers = new HashMap<String, Handler>();
+ final Properties props = new Properties();
+
+ ClassLoaderLogInfo(final LogNode rootNode) {
+ this.rootNode = rootNode;
+ }
+
+ }
+
+
+ // ------------------------------------------------- RootLogger Inner Class
+
+
+ /**
+ * This class is needed to instantiate the root of each per classloader
+ * hierarchy.
+ */
+ protected class RootLogger extends Logger {
+ public RootLogger() {
+ super("", null);
+ }
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/FileHandler.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/FileHandler.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/FileHandler.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/FileHandler.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,269 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.juli;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.PrintWriter;
+import java.sql.Timestamp;
+import java.util.logging.ErrorManager;
+import java.util.logging.Filter;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.SimpleFormatter;
+
+/**
+ * Implementation of <b>Handler</b> that appends log messages to a file
+ * named {prefix}.{date}.{suffix} in a configured directory, with an
+ * optional preceding timestamp.
+ *
+ * @version $Revision: 483782 $ $Date: 2006-12-07 18:24:30 -0800 (Thu, 07 Dec 2006) $
+ */
+
+public class FileHandler
+ extends Handler {
+
+
+ // ------------------------------------------------------------ Constructor
+
+
+ public FileHandler() {
+ this(null, null, null);
+ }
+
+
+ public FileHandler(String directory, String prefix, String suffix) {
+ this.directory = directory;
+ this.prefix = prefix;
+ this.suffix = suffix;
+ configure();
+ open();
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * The as-of date for the currently open log file, or a zero-length
+ * string if there is no open log file.
+ */
+ private String date = "";
+
+
+ /**
+ * The directory in which log files are created.
+ */
+ private String directory = null;
+
+
+ /**
+ * The prefix that is added to log file filenames.
+ */
+ private String prefix = null;
+
+
+ /**
+ * The suffix that is added to log file filenames.
+ */
+ private String suffix = null;
+
+
+ /**
+ * The PrintWriter to which we are currently logging, if any.
+ */
+ private PrintWriter writer = null;
+
+
+ // --------------------------------------------------------- Public Methods
+
+
+ /**
+ * Format and publish a <tt>LogRecord</tt>.
+ *
+ * @param record description of the log event
+ */
+ public void publish(LogRecord record) {
+
+ if (!isLoggable(record)) {
+ return;
+ }
+
+ // Construct the timestamp we will use, if requested
+ Timestamp ts = new Timestamp(System.currentTimeMillis());
+ String tsString = ts.toString().substring(0, 19);
+ String tsDate = tsString.substring(0, 10);
+
+ // If the date has changed, switch log files
+ if (!date.equals(tsDate)) {
+ synchronized (this) {
+ if (!date.equals(tsDate)) {
+ close();
+ date = tsDate;
+ open();
+ }
+ }
+ }
+
+ String result = null;
+ try {
+ result = getFormatter().format(record);
+ } catch (Exception e) {
+ reportError(null, e, ErrorManager.FORMAT_FAILURE);
+ return;
+ }
+
+ try {
+ writer.write(result);
+ writer.flush();
+ } catch (Exception e) {
+ reportError(null, e, ErrorManager.WRITE_FAILURE);
+ return;
+ }
+
+ }
+
+
+ // -------------------------------------------------------- Private Methods
+
+
+ /**
+ * Close the currently open log file (if any).
+ */
+ public void close() {
+
+ try {
+ if (writer == null)
+ return;
+ writer.write(getFormatter().getTail(this));
+ writer.flush();
+ writer.close();
+ writer = null;
+ date = "";
+ } catch (Exception e) {
+ reportError(null, e, ErrorManager.CLOSE_FAILURE);
+ }
+
+ }
+
+
+ /**
+ * Flush the writer.
+ */
+ public void flush() {
+
+ try {
+ writer.flush();
+ } catch (Exception e) {
+ reportError(null, e, ErrorManager.FLUSH_FAILURE);
+ }
+
+ }
+
+
+ /**
+ * Configure from <code>LogManager</code> properties.
+ */
+ private void configure() {
+
+ Timestamp ts = new Timestamp(System.currentTimeMillis());
+ String tsString = ts.toString().substring(0, 19);
+ date = tsString.substring(0, 10);
+
+ String className = FileHandler.class.getName();
+
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+
+ // Retrieve configuration of logging file name
+ if (directory == null)
+ directory = getProperty(className + ".directory", "logs");
+ if (prefix == null)
+ prefix = getProperty(className + ".prefix", "juli.");
+ if (suffix == null)
+ suffix = getProperty(className + ".suffix", ".log");
+
+ // Get logging level for the handler
+ setLevel(Level.parse(getProperty(className + ".level", "" + Level.ALL)));
+
+ // Get filter configuration
+ String filterName = getProperty(className + ".filter", null);
+ if (filterName != null) {
+ try {
+ setFilter((Filter) cl.loadClass(filterName).newInstance());
+ } catch (Exception e) {
+ // Ignore
+ }
+ }
+
+ // Set formatter
+ String formatterName = getProperty(className + ".formatter", null);
+ if (formatterName != null) {
+ try {
+ setFormatter((Formatter) cl.loadClass(formatterName).newInstance());
+ } catch (Exception e) {
+ // Ignore
+ }
+ } else {
+ setFormatter(new SimpleFormatter());
+ }
+
+ // Set error manager
+ setErrorManager(new ErrorManager());
+
+ }
+
+
+ private String getProperty(String name, String defaultValue) {
+ String value = LogManager.getLogManager().getProperty(name);
+ if (value == null) {
+ value = defaultValue;
+ } else {
+ value = value.trim();
+ }
+ return value;
+ }
+
+
+ /**
+ * Open the new log file for the date specified by <code>date</code>.
+ */
+ private void open() {
+
+ // Create the directory if necessary
+ File dir = new File(directory);
+ dir.mkdirs();
+
+ // Open the current log file
+ try {
+ String pathname = dir.getAbsolutePath() + File.separator +
+ prefix + date + suffix;
+ writer = new PrintWriter(new FileWriter(pathname, true), true);
+ writer.write(getFormatter().getHead(this));
+ } catch (Exception e) {
+ reportError(null, e, ErrorManager.OPEN_FAILURE);
+ writer = null;
+ }
+
+ }
+
+
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerConfig.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerConfig.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerConfig.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerConfig.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2004 Costin Manolache
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.juli;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.logging.LogManager;
+
+/**
+ */
+public class JdkLoggerConfig {
+
+ public JdkLoggerConfig() {
+ InputStream is=getConfig( "logging" );
+ if( is!=null ) {
+ try {
+ LogManager.getLogManager().readConfiguration(is);
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /** Locate a config stream:
+ *
+ * Uses:
+ * - logging.configuration system property
+ * - ./logging.properties file
+ * - conf/logging.properties
+ * - logging.properties in classpath
+ *
+ * @return
+ */
+ public static InputStream getConfig(String base) {
+ //String base="logging"; // "log4j"
+ // Initialize:
+ // 1. Find config file name
+ String confF=System.getProperty(base + ".configuration");
+ if( confF!=null ) {
+ // Check if it is a path
+ } else {
+ confF=base + ".properties";
+ }
+
+ // URL
+ try {
+ URL url=new URL( confF );
+ InputStream is=url.openStream();
+ if( is!=null ) return is;
+ } catch( Throwable t ) {
+
+ }
+
+ // 2. Try to get the config from a file ( or conf/ )
+ File f=new File( confF );
+ if( ! f.exists() ) {
+ f=new File( "conf/" + confF );
+ }
+
+ if( f.exists() ) {
+ try {
+ return new FileInputStream( f );
+ } catch (FileNotFoundException e) {
+ // ignore
+ }
+ }
+
+ // 3. Load it from CLASSPATH
+ InputStream is=JdkLoggerConfig.class.getResourceAsStream( confF );
+
+ //No thread class loader
+ if( is!= null ) return is;
+
+ f=new File( System.getProperty("java.home"));
+ f=new File( f, "lib/logging.properties");
+ if( f.exists() ) {
+ try {
+ return new FileInputStream(f);
+ } catch (FileNotFoundException e) {
+ //e.printStackTrace();
+ }
+ }
+ System.err.println("default logging doesn't exists" + f);
+
+ return null;
+ }
+}
Added: tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerFormatter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerFormatter.java?rev=640706&view=auto
==============================================================================
--- tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerFormatter.java (added)
+++ tomcat/sandbox/tomcat-lite/tomcat-coyote/org/apache/juli/JdkLoggerFormatter.java Mon Mar 24 23:01:43 2008
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.juli;
+
+import java.util.logging.Formatter;
+import java.util.logging.LogRecord;
+
+
+
+/**
+ * A more compact formatter.
+ *
+ * Equivalent log4j config:
+ * <pre>
+ * log4j.rootCategory=WARN, A1
+ * log4j.appender.A1=org.apache.log4j.ConsoleAppender
+ * log4j.appender.A1.layout=org.apache.log4j.PatternLayout
+ * log4j.appender.A1.Target=System.err
+ * log4j.appender.A1.layout.ConversionPattern=%r %-15.15c{2} %-1.1p %m %n
+ * </pre>
+ *
+ * Example:
+ * 1130122891846 Http11BaseProtocol I Initializing Coyote HTTP/1.1 on http-8800
+ *
+ *
+ * @author Costin Manolache
+ */
+public class JdkLoggerFormatter extends Formatter {
+ // values from JDK Level
+ public static final int LOG_LEVEL_TRACE = 400;
+ public static final int LOG_LEVEL_DEBUG = 500;
+ public static final int LOG_LEVEL_INFO = 800;
+ public static final int LOG_LEVEL_WARN = 900;
+ public static final int LOG_LEVEL_ERROR = 1000;
+ public static final int LOG_LEVEL_FATAL = 1000;
+
+ public String format(LogRecord record) {
+ Throwable t=record.getThrown();
+ int level=record.getLevel().intValue();
+ String name=record.getLoggerName();
+ long time=record.getMillis();
+ String message=formatMessage(record);
+
+
+ if( name.indexOf(".") >= 0 )
+ name = name.substring(name.lastIndexOf(".") + 1);
+
+ // Use a string buffer for better performance
+ StringBuffer buf = new StringBuffer();
+
+ buf.append(time);
+ buf.append(" ");
+
+ // pad to 8 to make it more readable
+ for( int i=0; i<8-buf.length(); i++ ) { buf.append(" "); }
+
+ // Append a readable representation of the log level.
+ switch(level) {
+ case LOG_LEVEL_TRACE: buf.append(" T "); break;
+ case LOG_LEVEL_DEBUG: buf.append(" D "); break;
+ case LOG_LEVEL_INFO: buf.append(" I "); break;
+ case LOG_LEVEL_WARN: buf.append(" W "); break;
+ case LOG_LEVEL_ERROR: buf.append(" E "); break;
+ //case : buf.append(" F "); break;
+ default: buf.append(" ");
+ }
+
+
+ // Append the name of the log instance if so configured
+ buf.append(name);
+
+ // pad to 20 chars
+ for( int i=0; i<8-buf.length(); i++ ) { buf.append(" "); }
+
+ // Append the message
+ buf.append(message);
+
+ // Append stack trace if not null
+ if(t != null) {
+ buf.append(" \n");
+
+ java.io.StringWriter sw= new java.io.StringWriter(1024);
+ java.io.PrintWriter pw= new java.io.PrintWriter(sw);
+ t.printStackTrace(pw);
+ pw.close();
+ buf.append(sw.toString());
+ }
+
+ buf.append("\n");
+ // Print to the appropriate destination
+ return buf.toString();
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org