You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2007/11/06 09:59:44 UTC
svn commit: r592337 [2/7] - in /mina/sandbox/asyncweb:
core/src/main/java/org/safehaus/asyncweb/codec/
core/src/main/java/org/safehaus/asyncweb/codec/decoder/
core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/
core/src/main/java/org/safeha...
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToDynamicTerminatorDecodingState.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToDynamicTerminatorDecodingState.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToDynamicTerminatorDecodingState.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToDynamicTerminatorDecodingState.java Tue Nov 6 00:59:36 2007
@@ -22,76 +22,78 @@
import org.apache.mina.common.IoBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
-
/**
* Consumes until a fixed (ASCII) character is reached.
* The terminator is skipped.
- *
+ *
* @author irvingd
* @author trustin
* @version $Rev$, $Date$
*/
-public abstract class ConsumeToDynamicTerminatorDecodingState implements DecodingState {
+public abstract class ConsumeToDynamicTerminatorDecodingState implements
+ DecodingState {
+
+ private IoBuffer buffer;
- private IoBuffer buffer;
-
- /**
- * Creates a new instance.
- */
- public ConsumeToDynamicTerminatorDecodingState() {
- }
-
- public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
- int beginPos = in.position();
- int terminatorPos = -1;
- int limit = in.limit();
-
- for (int i = beginPos; i < limit; i ++) {
- byte b = in.get(i);
- if (isTerminator(b)) {
- terminatorPos = i;
- break;
- }
+ /**
+ * Creates a new instance.
+ */
+ public ConsumeToDynamicTerminatorDecodingState() {
}
-
- if (terminatorPos >= 0) {
- IoBuffer product;
-
- if (beginPos < terminatorPos) {
- in.limit(terminatorPos);
-
- if (buffer == null) {
- product = in.slice();
- } else {
- buffer.put(in);
- product = buffer.flip();
- buffer = null;
+
+ public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
+ throws Exception {
+ int beginPos = in.position();
+ int terminatorPos = -1;
+ int limit = in.limit();
+
+ for (int i = beginPos; i < limit; i++) {
+ byte b = in.get(i);
+ if (isTerminator(b)) {
+ terminatorPos = i;
+ break;
+ }
}
-
- in.limit(limit);
- } else {
- // When input contained only terminator rather than actual data...
- if (buffer == null) {
- product = IoBuffer.allocate(1);
- product.limit(0);
+
+ if (terminatorPos >= 0) {
+ IoBuffer product;
+
+ if (beginPos < terminatorPos) {
+ in.limit(terminatorPos);
+
+ if (buffer == null) {
+ product = in.slice();
+ } else {
+ buffer.put(in);
+ product = buffer.flip();
+ buffer = null;
+ }
+
+ in.limit(limit);
+ } else {
+ // When input contained only terminator rather than actual data...
+ if (buffer == null) {
+ product = IoBuffer.allocate(1);
+ product.limit(0);
+ } else {
+ product = buffer.flip();
+ buffer = null;
+ }
+ }
+ in.position(terminatorPos + 1);
+ return finishDecode(product, out);
} else {
- product = buffer.flip();
- buffer = null;
+ if (buffer == null) {
+ buffer = IoBuffer.allocate(in.remaining());
+ buffer.setAutoExpand(true);
+ }
+ buffer.put(in);
+ return this;
}
- }
- in.position(terminatorPos + 1);
- return finishDecode(product, out);
- } else {
- if (buffer == null) {
- buffer = IoBuffer.allocate(in.remaining());
- buffer.setAutoExpand(true);
- }
- buffer.put(in);
- return this;
}
- }
-
- protected abstract boolean isTerminator(byte b);
-
- protected abstract DecodingState finishDecode(IoBuffer product, ProtocolDecoderOutput out) throws Exception;
+
+ protected abstract boolean isTerminator(byte b);
+
+ protected abstract DecodingState finishDecode(IoBuffer product,
+ ProtocolDecoderOutput out) throws Exception;
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToLinearWhitespaceDecodingState.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToLinearWhitespaceDecodingState.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToLinearWhitespaceDecodingState.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToLinearWhitespaceDecodingState.java Tue Nov 6 00:59:36 2007
@@ -19,12 +19,11 @@
*/
package org.safehaus.asyncweb.codec.decoder.support;
-
public abstract class ConsumeToLinearWhitespaceDecodingState extends
- ConsumeToDynamicTerminatorDecodingState {
+ ConsumeToDynamicTerminatorDecodingState {
- @Override
- protected boolean isTerminator(byte b) {
- return (b == 32 || b == 9);
- }
+ @Override
+ protected boolean isTerminator(byte b) {
+ return b == 32 || b == 9;
+ }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToTerminatorDecodingState.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToTerminatorDecodingState.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToTerminatorDecodingState.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/ConsumeToTerminatorDecodingState.java Tue Nov 6 00:59:36 2007
@@ -22,67 +22,69 @@
import org.apache.mina.common.IoBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
-
/**
* Consumes until a fixed (ASCII) character is reached.
* The terminator is skipped.
- *
+ *
* @author irvingd
* @author trustin
* @version $Rev$, $Date$
*/
public abstract class ConsumeToTerminatorDecodingState implements DecodingState {
- private final byte terminator;
- private IoBuffer buffer;
-
- /**
- * @param terminator The terminator character
- */
- public ConsumeToTerminatorDecodingState(byte terminator) {
- this.terminator = terminator;
- }
-
- public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
- int terminatorPos = in.indexOf(terminator);
-
- if (terminatorPos >= 0) {
- int limit = in.limit();
- IoBuffer product;
-
- if (in.position() < terminatorPos) {
- in.limit(terminatorPos);
-
- if (buffer == null) {
- product = in.slice();
- } else {
- buffer.put(in);
- product = buffer.flip();
- buffer = null;
- }
-
- in.limit(limit);
- } else {
- // When input contained only terminator rather than actual data...
- if (buffer == null) {
- product = IoBuffer.allocate(1);
- product.limit(0);
+ private final byte terminator;
+
+ private IoBuffer buffer;
+
+ /**
+ * @param terminator The terminator character
+ */
+ public ConsumeToTerminatorDecodingState(byte terminator) {
+ this.terminator = terminator;
+ }
+
+ public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
+ throws Exception {
+ int terminatorPos = in.indexOf(terminator);
+
+ if (terminatorPos >= 0) {
+ int limit = in.limit();
+ IoBuffer product;
+
+ if (in.position() < terminatorPos) {
+ in.limit(terminatorPos);
+
+ if (buffer == null) {
+ product = in.slice();
+ } else {
+ buffer.put(in);
+ product = buffer.flip();
+ buffer = null;
+ }
+
+ in.limit(limit);
+ } else {
+ // When input contained only terminator rather than actual data...
+ if (buffer == null) {
+ product = IoBuffer.allocate(1);
+ product.limit(0);
+ } else {
+ product = buffer.flip();
+ buffer = null;
+ }
+ }
+ in.position(terminatorPos + 1);
+ return finishDecode(product, out);
} else {
- product = buffer.flip();
- buffer = null;
+ if (buffer == null) {
+ buffer = IoBuffer.allocate(in.remaining());
+ buffer.setAutoExpand(true);
+ }
+ buffer.put(in);
+ return this;
}
- }
- in.position(terminatorPos + 1);
- return finishDecode(product, out);
- } else {
- if (buffer == null) {
- buffer = IoBuffer.allocate(in.remaining());
- buffer.setAutoExpand(true);
- }
- buffer.put(in);
- return this;
}
- }
-
- protected abstract DecodingState finishDecode(IoBuffer product, ProtocolDecoderOutput out) throws Exception;
+
+ protected abstract DecodingState finishDecode(IoBuffer product,
+ ProtocolDecoderOutput out) throws Exception;
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingState.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingState.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingState.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingState.java Tue Nov 6 00:59:36 2007
@@ -23,6 +23,7 @@
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
public interface DecodingState {
- // TODO Find out the best way to provide init/destroy.
- DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception;
+ // TODO Find out the best way to provide init/destroy.
+ DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
+ throws Exception;
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingStateMachine.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingStateMachine.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingStateMachine.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/DecodingStateMachine.java Tue Nov 6 00:59:36 2007
@@ -28,72 +28,79 @@
import org.slf4j.LoggerFactory;
public abstract class DecodingStateMachine implements DecodingState {
- private final Logger log = LoggerFactory.getLogger(DecodingStateMachine.class);
+ private final Logger log = LoggerFactory
+ .getLogger(DecodingStateMachine.class);
- private final List<Object> childProducts = new ArrayList<Object>();
- private final ProtocolDecoderOutput childOutput = new ProtocolDecoderOutput() {
- public void flush() {
- }
+ private final List<Object> childProducts = new ArrayList<Object>();
- public void write(Object message) {
- childProducts.add(message);
- }
- };
- private DecodingState currentState;
+ private final ProtocolDecoderOutput childOutput = new ProtocolDecoderOutput() {
+ public void flush() {
+ }
- protected abstract DecodingState init() throws Exception;
- protected abstract DecodingState finishDecode(List<Object> childProducts, ProtocolDecoderOutput out) throws Exception;
- protected abstract void destroy() throws Exception;
-
- public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
- DecodingState state = this.currentState;
- if (state == null) {
- state = init();
- }
-
- final int limit = in.limit();
- int pos = in.position();
-
- try {
- for (;;) {
- // Wait for more data if all data is consumed.
- if (pos == limit) {
- break;
+ public void write(Object message) {
+ childProducts.add(message);
}
-
- DecodingState oldState = state;
- state = state.decode(in, childOutput);
-
- // If finished, call finishDecode
+ };
+
+ private DecodingState currentState;
+
+ protected abstract DecodingState init() throws Exception;
+
+ protected abstract DecodingState finishDecode(List<Object> childProducts,
+ ProtocolDecoderOutput out) throws Exception;
+
+ protected abstract void destroy() throws Exception;
+
+ public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
+ throws Exception {
+ DecodingState state = this.currentState;
if (state == null) {
- return finishDecode(childProducts, out);
+ state = init();
}
-
- int newPos = in.position();
-
- // Wait for more data if nothing is consumed and state didn't change.
- if (newPos == pos && oldState == state) {
- break;
- }
- pos = newPos;
- }
-
- return this;
- } catch (Exception e) {
- state = null;
- throw e;
- } finally {
- this.currentState = state;
-
- // Destroy if decoding is finished or failed.
- if (state == null) {
- childProducts.clear();
+
+ final int limit = in.limit();
+ int pos = in.position();
+
try {
- destroy();
- } catch (Exception e2) {
- log.warn("Failed to destroy a decoding state machine.", e2);
+ for (;;) {
+ // Wait for more data if all data is consumed.
+ if (pos == limit) {
+ break;
+ }
+
+ DecodingState oldState = state;
+ state = state.decode(in, childOutput);
+
+ // If finished, call finishDecode
+ if (state == null) {
+ return finishDecode(childProducts, out);
+ }
+
+ int newPos = in.position();
+
+ // Wait for more data if nothing is consumed and state didn't change.
+ if (newPos == pos && oldState == state) {
+ break;
+ }
+ pos = newPos;
+ }
+
+ return this;
+ } catch (Exception e) {
+ state = null;
+ throw e;
+ } finally {
+ this.currentState = state;
+
+ // Destroy if decoding is finished or failed.
+ if (state == null) {
+ childProducts.clear();
+ try {
+ destroy();
+ } catch (Exception e2) {
+ log.warn("Failed to destroy a decoding state machine.", e2);
+ }
+ }
}
- }
}
- }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/FixedLengthDecodingState.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/FixedLengthDecodingState.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/FixedLengthDecodingState.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/FixedLengthDecodingState.java Tue Nov 6 00:59:36 2007
@@ -22,58 +22,60 @@
import org.apache.mina.common.IoBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
-
/**
* A {@link DecodingState} which consumes all received bytes until a configured
* number of read bytes has been reached.
- *
+ *
* @author irvingd
* @author trustin
* @version $Rev$, $Date$
*/
public abstract class FixedLengthDecodingState implements DecodingState {
- private final int length;
- private IoBuffer buffer;
-
- /**
- * Constructs with a known decode length.
- *
- * @param length The decode length
- */
- public FixedLengthDecodingState(int length) {
- this.length = length;
- }
-
- public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
- if (buffer == null) {
- if (in.remaining() >= length) {
- int limit = in.limit();
- in.limit(in.position() + length);
- IoBuffer product = in.slice();
- in.position(in.position() + length);
- in.limit(limit);
- return finishDecode(product, out);
- } else {
- buffer = IoBuffer.allocate(length);
- buffer.put(in);
- return this;
- }
- } else {
- if (in.remaining() >= length - buffer.position()) {
- int limit = in.limit();
- in.limit(in.position() + length - buffer.position());
- buffer.put(in);
- in.limit(limit);
- IoBuffer product = this.buffer;
- this.buffer = null;
- return finishDecode(product.flip(), out);
- } else {
- buffer.put(in);
- return this;
- }
+ private final int length;
+
+ private IoBuffer buffer;
+
+ /**
+ * Constructs with a known decode length.
+ *
+ * @param length The decode length
+ */
+ public FixedLengthDecodingState(int length) {
+ this.length = length;
}
- }
-
- protected abstract DecodingState finishDecode(IoBuffer readData, ProtocolDecoderOutput out) throws Exception;
+
+ public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
+ throws Exception {
+ if (buffer == null) {
+ if (in.remaining() >= length) {
+ int limit = in.limit();
+ in.limit(in.position() + length);
+ IoBuffer product = in.slice();
+ in.position(in.position() + length);
+ in.limit(limit);
+ return finishDecode(product, out);
+ } else {
+ buffer = IoBuffer.allocate(length);
+ buffer.put(in);
+ return this;
+ }
+ } else {
+ if (in.remaining() >= length - buffer.position()) {
+ int limit = in.limit();
+ in.limit(in.position() + length - buffer.position());
+ buffer.put(in);
+ in.limit(limit);
+ IoBuffer product = this.buffer;
+ this.buffer = null;
+ return finishDecode(product.flip(), out);
+ } else {
+ buffer.put(in);
+ return this;
+ }
+ }
+ }
+
+ protected abstract DecodingState finishDecode(IoBuffer readData,
+ ProtocolDecoderOutput out) throws Exception;
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/LinearWhitespaceSkippingState.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/LinearWhitespaceSkippingState.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/LinearWhitespaceSkippingState.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/LinearWhitespaceSkippingState.java Tue Nov 6 00:59:36 2007
@@ -19,11 +19,10 @@
*/
package org.safehaus.asyncweb.codec.decoder.support;
-
public abstract class LinearWhitespaceSkippingState extends SkippingState {
- @Override
- protected boolean canSkip(byte b) {
- return (b == 32 || b == 9);
- }
+ @Override
+ protected boolean canSkip(byte b) {
+ return b == 32 || b == 9;
+ }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/SkippingState.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/SkippingState.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/SkippingState.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/SkippingState.java Tue Nov 6 00:59:36 2007
@@ -22,43 +22,44 @@
import org.apache.mina.common.IoBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
-
/**
* Skips data until {@link #canSkip(byte)} returns <tt>false</tt>.
- *
+ *
* @author trustin
* @version $Rev$, $Date$
*/
public abstract class SkippingState implements DecodingState {
- private int skippedBytes;
+ private int skippedBytes;
+
+ /**
+ * Creates a new instance.
+ */
+ public SkippingState() {
+ }
+
+ public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
+ throws Exception {
+ int beginPos = in.position();
+ int limit = in.limit();
+ for (int i = beginPos; i < limit; i++) {
+ byte b = in.get(i);
+ if (!canSkip(b)) {
+ in.position(i);
+ int answer = this.skippedBytes;
+ this.skippedBytes = 0;
+ return finishDecode(answer);
+ } else {
+ skippedBytes++;
+ }
+ }
- /**
- * Creates a new instance.
- */
- public SkippingState() {
- }
-
- public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
- int beginPos = in.position();
- int limit = in.limit();
- for (int i = beginPos; i < limit; i++) {
- byte b = in.get(i);
- if (!canSkip(b)) {
- in.position(i);
- int answer = this.skippedBytes;
- this.skippedBytes = 0;
- return finishDecode(answer);
- } else {
- skippedBytes ++;
- }
+ in.position(limit);
+ return this;
}
-
- in.position(limit);
- return this;
- }
-
- protected abstract boolean canSkip(byte b);
-
- protected abstract DecodingState finishDecode(int skippedBytes) throws Exception;
+
+ protected abstract boolean canSkip(byte b);
+
+ protected abstract DecodingState finishDecode(int skippedBytes)
+ throws Exception;
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/StateMachineProtocolDecoder.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/StateMachineProtocolDecoder.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/StateMachineProtocolDecoder.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/decoder/support/StateMachineProtocolDecoder.java Tue Nov 6 00:59:36 2007
@@ -26,55 +26,58 @@
public class StateMachineProtocolDecoder implements ProtocolDecoder {
- private final DecodingStateMachine stateMachine;
- private DecodingState currentState;
+ private final DecodingStateMachine stateMachine;
- public StateMachineProtocolDecoder(DecodingStateMachine stateMachine) {
- if (stateMachine == null) {
- throw new NullPointerException("stateMachine");
- }
- this.stateMachine = stateMachine;
- }
-
- public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
- DecodingState state = this.currentState;
- if (state == null) {
- state = stateMachine.init();
- }
+ private DecodingState currentState;
- try {
- for (;;) {
- int remaining = in.remaining();
-
- // Wait for more data if all data is consumed.
- if (remaining == 0) {
- break;
+ public StateMachineProtocolDecoder(DecodingStateMachine stateMachine) {
+ if (stateMachine == null) {
+ throw new NullPointerException("stateMachine");
}
-
- DecodingState oldState = state;
- state = state.decode(in, out);
-
+ this.stateMachine = stateMachine;
+ }
+
+ public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
+ throws Exception {
+ DecodingState state = this.currentState;
if (state == null) {
- // Finished
- break;
+ state = stateMachine.init();
}
-
- // Wait for more data if nothing is consumed and state didn't change.
- if (in.remaining() == remaining && oldState == state) {
- break;
+
+ try {
+ for (;;) {
+ int remaining = in.remaining();
+
+ // Wait for more data if all data is consumed.
+ if (remaining == 0) {
+ break;
+ }
+
+ DecodingState oldState = state;
+ state = state.decode(in, out);
+
+ if (state == null) {
+ // Finished
+ break;
+ }
+
+ // Wait for more data if nothing is consumed and state didn't change.
+ if (in.remaining() == remaining && oldState == state) {
+ break;
+ }
+ }
+ } catch (Exception e) {
+ state = null;
+ throw e;
+ } finally {
+ this.currentState = state;
}
- }
- } catch (Exception e) {
- state = null;
- throw e;
- } finally {
- this.currentState = state;
}
- }
- public void dispose(IoSession session) throws Exception {
- }
+ public void dispose(IoSession session) throws Exception {
+ }
- public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
- }
+ public void finishDecode(IoSession session, ProtocolDecoderOutput out)
+ throws Exception {
+ }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CompatibilityCookieEncoder.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CompatibilityCookieEncoder.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CompatibilityCookieEncoder.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CompatibilityCookieEncoder.java Tue Nov 6 00:59:36 2007
@@ -33,94 +33,110 @@
/**
* A cookie encoder which writes cookies in a form suitable for maximum
* compatibility - rather than to strict accordance to RFC2965
- *
+ *
* @author irvingd
*
*/
public class CompatibilityCookieEncoder implements CookieEncoder {
- private static final byte[] HEADER_BYTES = HttpCodecUtils.getASCIIBytes("Set-Cookie: ");
- private static final byte[] VERSION_BYTES = HttpCodecUtils.getASCIIBytes("; version=");
- private static final byte[] PATH_BYTES = HttpCodecUtils.getASCIIBytes("; path=");
- private static final byte[] EXPIRY_BYTES = HttpCodecUtils.getASCIIBytes("; max-age=");
- private static final byte[] EXPIRES_BYTES = HttpCodecUtils.getASCIIBytes("; Expires=");
- private static final byte[] SECURE_BYTES = HttpCodecUtils.getASCIIBytes("; secure;");
-
- private static final TimeZone FORMAT_TIME_ZONE = TimeZone.getTimeZone("GMT");
-
- /**
- * Thread-local DateFormat for old-style cookies
- */
- private static final ThreadLocal<DateFormat> EXPIRY_FORMAT_LOACAL = new ThreadLocal<DateFormat>() {
- @Override
- protected DateFormat initialValue() {
- SimpleDateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
- format.setTimeZone(FORMAT_TIME_ZONE);
- return format;
- }
-
- };
-
- /**
- * A date long long ago, formatted in the old style cookie expire format
- */
- private static final String EXPIRED_DATE = getFormattedExpiry(0);
-
- public void encodeCookie(Collection<Cookie> cookies, IoBuffer buffer) {
- for (Cookie c: cookies) {
- buffer.put(HEADER_BYTES);
- encodeCookieValue(c, buffer);
- HttpCodecUtils.appendCRLF(buffer);
- }
- }
-
- private static String getFormattedExpiry(long time) {
- SimpleDateFormat format = (SimpleDateFormat) EXPIRY_FORMAT_LOACAL.get();
- return format.format(new Date(time));
- }
-
- private void encodeCookieValue(Cookie cookie, IoBuffer buffer) {
- HttpCodecUtils.appendString(buffer, cookie.getName());
- buffer.put(HttpCodecUtils.EQUALS);
- HttpCodecUtils.appendString(buffer, cookie.getValue());
- if (cookie.getVersion() > 0) {
- buffer.put(VERSION_BYTES);
- HttpCodecUtils.appendString(buffer, String.valueOf(cookie.getVersion()));
+ private static final byte[] HEADER_BYTES = HttpCodecUtils
+ .getASCIIBytes("Set-Cookie: ");
+
+ private static final byte[] VERSION_BYTES = HttpCodecUtils
+ .getASCIIBytes("; version=");
+
+ private static final byte[] PATH_BYTES = HttpCodecUtils
+ .getASCIIBytes("; path=");
+
+ private static final byte[] EXPIRY_BYTES = HttpCodecUtils
+ .getASCIIBytes("; max-age=");
+
+ private static final byte[] EXPIRES_BYTES = HttpCodecUtils
+ .getASCIIBytes("; Expires=");
+
+ private static final byte[] SECURE_BYTES = HttpCodecUtils
+ .getASCIIBytes("; secure;");
+
+ private static final TimeZone FORMAT_TIME_ZONE = TimeZone
+ .getTimeZone("GMT");
+
+ /**
+ * Thread-local DateFormat for old-style cookies
+ */
+ private static final ThreadLocal<DateFormat> EXPIRY_FORMAT_LOACAL = new ThreadLocal<DateFormat>() {
+ @Override
+ protected DateFormat initialValue() {
+ SimpleDateFormat format = new SimpleDateFormat(
+ "EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
+ format.setTimeZone(FORMAT_TIME_ZONE);
+ return format;
+ }
+
+ };
+
+ /**
+ * A date long long ago, formatted in the old style cookie expire format
+ */
+ private static final String EXPIRED_DATE = getFormattedExpiry(0);
+
+ public void encodeCookie(Collection<Cookie> cookies, IoBuffer buffer) {
+ for (Cookie c : cookies) {
+ buffer.put(HEADER_BYTES);
+ encodeCookieValue(c, buffer);
+ HttpCodecUtils.appendCRLF(buffer);
+ }
}
- String path = cookie.getPath();
- if (path != null) {
- buffer.put(PATH_BYTES);
- HttpCodecUtils.appendString(buffer, path);
+
+ private static String getFormattedExpiry(long time) {
+ SimpleDateFormat format = (SimpleDateFormat) EXPIRY_FORMAT_LOACAL.get();
+ return format.format(new Date(time));
}
- encodeExpiry(cookie, buffer);
- if (cookie.isSecure()) {
- buffer.put(SECURE_BYTES);
- } else {
- buffer.put(HttpCodecUtils.SEMI_COLON);
+
+ private void encodeCookieValue(Cookie cookie, IoBuffer buffer) {
+ HttpCodecUtils.appendString(buffer, cookie.getName());
+ buffer.put(HttpCodecUtils.EQUALS);
+ HttpCodecUtils.appendString(buffer, cookie.getValue());
+ if (cookie.getVersion() > 0) {
+ buffer.put(VERSION_BYTES);
+ HttpCodecUtils.appendString(buffer, String.valueOf(cookie
+ .getVersion()));
+ }
+ String path = cookie.getPath();
+ if (path != null) {
+ buffer.put(PATH_BYTES);
+ HttpCodecUtils.appendString(buffer, path);
+ }
+ encodeExpiry(cookie, buffer);
+ if (cookie.isSecure()) {
+ buffer.put(SECURE_BYTES);
+ } else {
+ buffer.put(HttpCodecUtils.SEMI_COLON);
+ }
}
- }
-
- /**
- * Encodes the cookie expiry time in to the specfied buffer.
- * The format used is selected based on the cookie version.
- *
- * @param cookie The cookie
- * @param buffer The buffer
- */
- private void encodeExpiry(Cookie cookie, IoBuffer buffer) {
- long expiry = cookie.getMaxAge();
- int version = cookie.getVersion();
- if (expiry >= 0) {
- if (version == 0) {
- String expires = expiry == 0 ? EXPIRED_DATE :
- getFormattedExpiry(System.currentTimeMillis() + (1000 * expiry));
- buffer.put(EXPIRES_BYTES);
- HttpCodecUtils.appendString(buffer, expires);
- } else {
- buffer.put(EXPIRY_BYTES);
- HttpCodecUtils.appendString(buffer, String.valueOf(cookie.getMaxAge()));
- }
+
+ /**
+ * Encodes the cookie expiry time in to the specfied buffer.
+ * The format used is selected based on the cookie version.
+ *
+ * @param cookie The cookie
+ * @param buffer The buffer
+ */
+ private void encodeExpiry(Cookie cookie, IoBuffer buffer) {
+ long expiry = cookie.getMaxAge();
+ int version = cookie.getVersion();
+ if (expiry >= 0) {
+ if (version == 0) {
+ String expires = expiry == 0 ? EXPIRED_DATE
+ : getFormattedExpiry(System.currentTimeMillis()
+ + 1000 * expiry);
+ buffer.put(EXPIRES_BYTES);
+ HttpCodecUtils.appendString(buffer, expires);
+ } else {
+ buffer.put(EXPIRY_BYTES);
+ HttpCodecUtils.appendString(buffer, String.valueOf(cookie
+ .getMaxAge()));
+ }
+ }
}
- }
-
+
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CookieEncoder.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CookieEncoder.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CookieEncoder.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/CookieEncoder.java Tue Nov 6 00:59:36 2007
@@ -26,17 +26,17 @@
/**
* Defines a strategy for encoding cookies
- *
+ *
* @author irvingd
*
*/
public interface CookieEncoder {
- /**
- * Encodes a list of {@link Cookie}s in to the specified buffer
- *
- * @param cookies The cookies
- * @param buffer The buffer
- */
- public void encodeCookie(Collection<Cookie> cookies, IoBuffer buffer);
+ /**
+ * Encodes a list of {@link Cookie}s in to the specified buffer
+ *
+ * @param cookies The cookies
+ * @param buffer The buffer
+ */
+ public void encodeCookie(Collection<Cookie> cookies, IoBuffer buffer);
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/OneShotHttpResponseEncoder.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/OneShotHttpResponseEncoder.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/OneShotHttpResponseEncoder.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/codec/encoder/OneShotHttpResponseEncoder.java Tue Nov 6 00:59:36 2007
@@ -37,108 +37,113 @@
import org.safehaus.asyncweb.common.HttpResponseStatus;
import org.safehaus.asyncweb.common.content.ByteBufferContent;
-
public class OneShotHttpResponseEncoder implements ProtocolEncoder {
- private static final Charset US_ASCII = Charset.forName("US-ASCII");
-
- private final CharsetEncoder asciiEncoder = US_ASCII.newEncoder();
-
- private CookieEncoder cookieEncoderStrategy = new CompatibilityCookieEncoder();
-
- public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
- asciiEncoder.reset();
- HttpResponse response = (HttpResponse) message;
- IoBuffer buffer = IoBuffer.allocate(512);
- buffer.setAutoExpand(true);
-
- encodeStatusLine(response, buffer);
- encodeHeaders(response, buffer);
- encodeBody(response, buffer);
-
- buffer.flip();
- out.write(buffer);
- }
-
- public void dispose(IoSession session) throws Exception {
- }
-
- /**
- * Encodes the status line of a <code>Response</code> to a specified
- * buffer.
- * The status line takes the form:<br/>
- * <pre>
- * HTTP-Version SP Status-Code SP Reason-Phrase CRLF
- * </pre>
- *
- * @param response The response
- * @param buffer The buffer
- * @throws CharacterCodingException
- */
- private void encodeStatusLine(HttpResponse response, IoBuffer buffer) throws CharacterCodingException {
- // Write protocol version.
- buffer.putString(response.getProtocolVersion().toString(), asciiEncoder);
- buffer.put(HttpCodecUtils.SP);
-
- // Write status code.
- HttpResponseStatus status = response.getStatus();
- // TODO: Cached buffers for response codes / descriptions?
- HttpCodecUtils.appendString(buffer, String.valueOf(status.getCode()));
- buffer.put(HttpCodecUtils.SP);
-
- // Write reason phrase.
- HttpCodecUtils.appendString(buffer, response.getStatusReasonPhrase());
- HttpCodecUtils.appendCRLF(buffer);
- }
-
- /**
- * Encodes the headers of a <code>Response</code> to a specified buffer.
- * This encoder does not make smart decisions about which headers to write -
- * the response is expected to already contain self-consistent headers.
- *
- * @param response The response whos headers are to be encoded
- * @param buffer The buffer
- * @throws CharacterCodingException
- */
- private void encodeHeaders(HttpResponse response, IoBuffer buffer) throws CharacterCodingException {
- for (Map.Entry<String, List<String>> header: response.getHeaders().entrySet()) {
- for (String value: header.getValue()) {
- buffer.putString(header.getKey(), asciiEncoder );
- buffer.put((byte) ':');
- buffer.put((byte) ' ');
- buffer.putString(value, asciiEncoder);
+ private static final Charset US_ASCII = Charset.forName("US-ASCII");
+
+ private final CharsetEncoder asciiEncoder = US_ASCII.newEncoder();
+
+ private CookieEncoder cookieEncoderStrategy = new CompatibilityCookieEncoder();
+
+ public void encode(IoSession session, Object message,
+ ProtocolEncoderOutput out) throws Exception {
+ asciiEncoder.reset();
+ HttpResponse response = (HttpResponse) message;
+ IoBuffer buffer = IoBuffer.allocate(512);
+ buffer.setAutoExpand(true);
+
+ encodeStatusLine(response, buffer);
+ encodeHeaders(response, buffer);
+ encodeBody(response, buffer);
+
+ buffer.flip();
+ out.write(buffer);
+ }
+
+ public void dispose(IoSession session) throws Exception {
+ }
+
+ /**
+ * Encodes the status line of a <code>Response</code> to a specified
+ * buffer.
+ * The status line takes the form:<br/>
+ * <pre>
+ * HTTP-Version SP Status-Code SP Reason-Phrase CRLF
+ * </pre>
+ *
+ * @param response The response
+ * @param buffer The buffer
+ * @throws CharacterCodingException
+ */
+ private void encodeStatusLine(HttpResponse response, IoBuffer buffer)
+ throws CharacterCodingException {
+ // Write protocol version.
+ buffer
+ .putString(response.getProtocolVersion().toString(),
+ asciiEncoder);
+ buffer.put(HttpCodecUtils.SP);
+
+ // Write status code.
+ HttpResponseStatus status = response.getStatus();
+ // TODO: Cached buffers for response codes / descriptions?
+ HttpCodecUtils.appendString(buffer, String.valueOf(status.getCode()));
+ buffer.put(HttpCodecUtils.SP);
+
+ // Write reason phrase.
+ HttpCodecUtils.appendString(buffer, response.getStatusReasonPhrase());
HttpCodecUtils.appendCRLF(buffer);
- }
}
- Collection<Cookie> cookies = response.getCookies();
- if (!cookies.isEmpty()) {
- encodeCookies(cookies, buffer);
- }
- HttpCodecUtils.appendCRLF(buffer);
- }
-
- /**
- * Encodes all new or modified cookies to our cookie encoder
- *
- * @param cookies The cookies to encode
- * @param buffer The buffer to encode to
- */
- private void encodeCookies(Collection<Cookie> cookies, IoBuffer buffer) {
- cookieEncoderStrategy.encodeCookie(cookies, buffer);
- }
-
- /**
- * Writes the response body bytes, if any, to the specified buffer
- *
- * @param response The response
- * @param buffer The buffer to write to
- */
- private void encodeBody(HttpResponse response, IoBuffer buffer) {
- // FIXME Should be able to handle contents of various types.
- Content content = response.getContent();
- if (content instanceof ByteBufferContent) {
- ByteBufferContent bbc = (ByteBufferContent) response.getContent();
- buffer.put(bbc.getByteBuffer());
+
+ /**
+ * Encodes the headers of a <code>Response</code> to a specified buffer.
+ * This encoder does not make smart decisions about which headers to write -
+ * the response is expected to already contain self-consistent headers.
+ *
+ * @param response The response whos headers are to be encoded
+ * @param buffer The buffer
+ * @throws CharacterCodingException
+ */
+ private void encodeHeaders(HttpResponse response, IoBuffer buffer)
+ throws CharacterCodingException {
+ for (Map.Entry<String, List<String>> header : response.getHeaders()
+ .entrySet()) {
+ for (String value : header.getValue()) {
+ buffer.putString(header.getKey(), asciiEncoder);
+ buffer.put((byte) ':');
+ buffer.put((byte) ' ');
+ buffer.putString(value, asciiEncoder);
+ HttpCodecUtils.appendCRLF(buffer);
+ }
+ }
+ Collection<Cookie> cookies = response.getCookies();
+ if (!cookies.isEmpty()) {
+ encodeCookies(cookies, buffer);
+ }
+ HttpCodecUtils.appendCRLF(buffer);
+ }
+
+ /**
+ * Encodes all new or modified cookies to our cookie encoder
+ *
+ * @param cookies The cookies to encode
+ * @param buffer The buffer to encode to
+ */
+ private void encodeCookies(Collection<Cookie> cookies, IoBuffer buffer) {
+ cookieEncoderStrategy.encodeCookie(cookies, buffer);
+ }
+
+ /**
+ * Writes the response body bytes, if any, to the specified buffer
+ *
+ * @param response The response
+ * @param buffer The buffer to write to
+ */
+ private void encodeBody(HttpResponse response, IoBuffer buffer) {
+ // FIXME Should be able to handle contents of various types.
+ Content content = response.getContent();
+ if (content instanceof ByteBufferContent) {
+ ByteBufferContent bbc = (ByteBufferContent) response.getContent();
+ buffer.put(bbc.getByteBuffer());
+ }
}
- }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Content.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Content.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Content.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Content.java Tue Nov 6 00:59:36 2007
@@ -23,7 +23,7 @@
/**
* Represents the body content of an HTTP message.
- *
+ *
* @author trustin
* @version $Rev$, $Date$
*/
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Cookie.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Cookie.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Cookie.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/Cookie.java Tue Nov 6 00:59:36 2007
@@ -23,63 +23,63 @@
/**
* An HTTP cookie.
- *
+ *
* @author irvingd
* @author trustin
* @version $Rev$, $Date$
*/
public interface Cookie extends Serializable, Comparable<Cookie> {
- /**
- * Returns the cookie version number. The default (if not specified) is 0
- *
- * @return The version number
- */
- int getVersion();
-
- /**
- * Returns the name of this cookie
- *
- * @return The cookie name
- */
- String getName();
-
- /**
- * Returns the value of this cookie
- *
- * @return The cookie value - or <code>null</code> if this cookie does not have
- * a value
- */
- String getValue();
-
- /**
- * Returns the domain of this cookie.
- */
- String getDomain();
-
- /**
- * Returns the path on the server to which the client returns this cookie.
- *
- * @return The path
- */
- String getPath();
-
- /**
- * Returns if this cookie is marked as "secure".
- * Secure cookies should be sent back by a client over a transport as least as
- * secure as that upon which they were received
- */
- boolean isSecure();
-
- /**
- * Returns the maximum age of this cookie in seconds.
- */
- int getMaxAge();
-
- /**
- * Returns the comment of this cookie. Comments are not supported by version 0 cookies.
- *
- * @return <tt>null</tt> if no comment is specified
- */
- String getComment();
+ /**
+ * Returns the cookie version number. The default (if not specified) is 0
+ *
+ * @return The version number
+ */
+ int getVersion();
+
+ /**
+ * Returns the name of this cookie
+ *
+ * @return The cookie name
+ */
+ String getName();
+
+ /**
+ * Returns the value of this cookie
+ *
+ * @return The cookie value - or <code>null</code> if this cookie does not have
+ * a value
+ */
+ String getValue();
+
+ /**
+ * Returns the domain of this cookie.
+ */
+ String getDomain();
+
+ /**
+ * Returns the path on the server to which the client returns this cookie.
+ *
+ * @return The path
+ */
+ String getPath();
+
+ /**
+ * Returns if this cookie is marked as "secure".
+ * Secure cookies should be sent back by a client over a transport as least as
+ * secure as that upon which they were received
+ */
+ boolean isSecure();
+
+ /**
+ * Returns the maximum age of this cookie in seconds.
+ */
+ int getMaxAge();
+
+ /**
+ * Returns the comment of this cookie. Comments are not supported by version 0 cookies.
+ *
+ * @return <tt>null</tt> if no comment is specified
+ */
+ String getComment();
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/CookieComparator.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/CookieComparator.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/CookieComparator.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/CookieComparator.java Tue Nov 6 00:59:36 2007
@@ -23,50 +23,50 @@
import java.util.Comparator;
public class CookieComparator implements Serializable, Comparator<Cookie> {
-
- private static final long serialVersionUID = -222644341851192813L;
- public static final CookieComparator INSTANCE = new CookieComparator();
+ private static final long serialVersionUID = -222644341851192813L;
- public int compare(Cookie o1, Cookie o2) {
- int answer;
-
- // Compare the name first.
- answer = o1.getName().compareTo(o2.getName());
- if (answer != 0) {
- return answer;
- }
-
- // and then path
- if (o1.getPath() == null) {
- if (o2.getPath() != null) {
- answer = -1;
- } else {
- answer = 0;
- }
- } else {
- answer = o1.getPath().compareTo(o2.getPath());
- }
-
- if (answer != 0) {
- return answer;
+ public static final CookieComparator INSTANCE = new CookieComparator();
+
+ public int compare(Cookie o1, Cookie o2) {
+ int answer;
+
+ // Compare the name first.
+ answer = o1.getName().compareTo(o2.getName());
+ if (answer != 0) {
+ return answer;
+ }
+
+ // and then path
+ if (o1.getPath() == null) {
+ if (o2.getPath() != null) {
+ answer = -1;
+ } else {
+ answer = 0;
+ }
+ } else {
+ answer = o1.getPath().compareTo(o2.getPath());
+ }
+
+ if (answer != 0) {
+ return answer;
+ }
+
+ // and then domain
+ if (o1.getDomain() == null) {
+ if (o2.getDomain() != null) {
+ answer = -1;
+ } else {
+ answer = 0;
+ }
+ } else {
+ answer = o1.getDomain().compareTo(o2.getDomain());
+ }
+
+ return answer;
}
-
- // and then domain
- if (o1.getDomain() == null) {
- if (o2.getDomain() != null) {
- answer = -1;
- } else {
- answer = 0;
- }
- } else {
- answer = o1.getDomain().compareTo(o2.getDomain());
+
+ private Object readResolve() {
+ return INSTANCE;
}
-
- return answer;
- }
-
- private Object readResolve() {
- return INSTANCE;
- }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultCookie.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultCookie.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultCookie.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultCookie.java Tue Nov 6 00:59:36 2007
@@ -19,181 +19,191 @@
*/
package org.safehaus.asyncweb.common;
-
/**
- * Default cookie implementation.
+ * Default cookie implementation.
* Content built using <code>Bytes</code> is cached as a <code>LazyDecodedString</code>
* until the content item is needed.
* <code>DefaultCookie</code> tracks changes to content items - allowing changed cookies
* to be written automatically when a response is written back to the client
- *
+ *
* @author irvingd
*
*/
public class DefaultCookie implements MutableCookie {
- private static final long serialVersionUID = 5713305385740914300L;
+ private static final long serialVersionUID = 5713305385740914300L;
+
+ private final String name;
+
+ private String value;
+
+ private String domain;
+
+ private String path;
+
+ private String comment;
+
+ private boolean secure;
+
+ private int version = 0;
+
+ private int maxAge = -1;
+
+ /**
+ * Constructor used to allow users to create new cookies to be sent
+ * back to the client.
+ *
+ * @param name the name of the new cookie.
+ */
+ public DefaultCookie(String name) {
+ if (name == null) {
+ throw new NullPointerException("name");
+ }
+
+ this.name = name;
+ }
+
+ public String getComment() {
+ return comment;
+ }
+
+ public void setComment(String comment) {
+ this.comment = comment;
+ }
+
+ public String getDomain() {
+ return domain;
+ }
+
+ public void setDomain(String domain) {
+ this.domain = domain;
+ }
+
+ public int getMaxAge() {
+ return maxAge;
+ }
+
+ public void setMaxAge(int maxAge) {
+ this.maxAge = maxAge;
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public void setPath(String path) {
+ this.path = path;
+ }
+
+ public boolean isSecure() {
+ return secure;
+ }
+
+ public void setSecure(boolean secure) {
+ this.secure = secure;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ public int getVersion() {
+ return version;
+ }
+
+ public void setVersion(int version) {
+ this.version = version;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public int hashCode() {
+ return getName().hashCode();
+ }
- private final String name;
- private String value;
- private String domain;
- private String path;
- private String comment;
- private boolean secure;
- private int version = 0;
- private int maxAge = -1;
-
- /**
- * Constructor used to allow users to create new cookies to be sent
- * back to the client.
- *
- * @param name the name of the new cookie.
- */
- public DefaultCookie(String name) {
- if (name == null) {
- throw new NullPointerException("name");
- }
-
- this.name = name;
- }
-
- public String getComment() {
- return comment;
- }
-
- public void setComment(String comment) {
- this.comment = comment;
- }
-
- public String getDomain() {
- return domain;
- }
-
- public void setDomain(String domain) {
- this.domain = domain;
- }
-
- public int getMaxAge() {
- return maxAge;
- }
-
- public void setMaxAge(int maxAge) {
- this.maxAge = maxAge;
- }
-
- public String getPath() {
- return path;
- }
-
- public void setPath(String path) {
- this.path = path;
- }
-
- public boolean isSecure() {
- return secure;
- }
-
- public void setSecure(boolean secure) {
- this.secure = secure;
- }
-
- public String getValue() {
- return value;
- }
-
- public void setValue(String value) {
- this.value = value;
- }
-
- public int getVersion() {
- return version;
- }
-
- public void setVersion(int version) {
- this.version = version;
- }
-
- public String getName() {
- return name;
- }
-
- public int hashCode() {
- return getName().hashCode();
- }
-
- public boolean equals(Object o) {
- if (o == this) {
- return true;
- }
-
- if (!(o instanceof Cookie)) {
- return false;
- }
-
- Cookie that = (Cookie) o;
- if (!name.equals(that.getName())) {
- return false;
- }
-
- if (path == null) {
- if (that.getPath() != null) {
- return false;
- }
- } else if (!path.equals(that.getPath())) {
- return false;
- }
-
- if (domain == null) {
- if (that.getDomain() != null) {
- return false;
- }
- } else if (!domain.equals(that.getDomain())) {
- return false;
- }
-
- return true;
- }
-
- public int compareTo(Cookie o) {
- int answer;
-
- // Compare the name first.
- answer = name.compareTo(o.getName());
- if (answer != 0) {
- return answer;
- }
-
- // and then path
- if (path == null) {
- if (o.getPath() != null) {
- answer = -1;
- } else {
- answer = 0;
- }
- } else {
- answer = path.compareTo(o.getPath());
- }
-
- if (answer != 0) {
- return answer;
- }
-
- // and then domain
- if (domain == null) {
- if (o.getDomain() != null) {
- answer = -1;
- } else {
- answer = 0;
- }
- } else {
- answer = domain.compareTo(o.getDomain());
- }
-
- return answer;
- }
-
- public String toString() {
- return "name=" + getName() + " value=" + getValue() + " domain=" + getDomain() +
- " path=" + getPath() + " maxAge=" + getMaxAge() + " secure=" + isSecure();
- }
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+
+ if (!(o instanceof Cookie)) {
+ return false;
+ }
+
+ Cookie that = (Cookie) o;
+ if (!name.equals(that.getName())) {
+ return false;
+ }
+
+ if (path == null) {
+ if (that.getPath() != null) {
+ return false;
+ }
+ } else if (!path.equals(that.getPath())) {
+ return false;
+ }
+
+ if (domain == null) {
+ if (that.getDomain() != null) {
+ return false;
+ }
+ } else if (!domain.equals(that.getDomain())) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public int compareTo(Cookie o) {
+ int answer;
+
+ // Compare the name first.
+ answer = name.compareTo(o.getName());
+ if (answer != 0) {
+ return answer;
+ }
+
+ // and then path
+ if (path == null) {
+ if (o.getPath() != null) {
+ answer = -1;
+ } else {
+ answer = 0;
+ }
+ } else {
+ answer = path.compareTo(o.getPath());
+ }
+
+ if (answer != 0) {
+ return answer;
+ }
+
+ // and then domain
+ if (domain == null) {
+ if (o.getDomain() != null) {
+ answer = -1;
+ } else {
+ answer = 0;
+ }
+ } else {
+ answer = domain.compareTo(o.getDomain());
+ }
+
+ return answer;
+ }
+
+ @Override
+ public String toString() {
+ return "name=" + getName() + " value=" + getValue() + " domain="
+ + getDomain() + " path=" + getPath() + " maxAge=" + getMaxAge()
+ + " secure=" + isSecure();
+ }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpMessage.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpMessage.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpMessage.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpMessage.java Tue Nov 6 00:59:36 2007
@@ -32,183 +32,190 @@
import org.safehaus.asyncweb.common.content.EmptyContent;
import org.safehaus.asyncweb.util.HttpHeaderConstants;
-
/**
* A default implementation of {@link MutableHttpMessage}.
- *
+ *
* @author trustin
* @version $Rev$, $Date$
*/
public class DefaultHttpMessage implements MutableHttpMessage {
- private static final long serialVersionUID = -7559479748566065541L;
-
- private HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
- private final Map<String, List<String>> headers =
- new TreeMap<String, List<String>>(HeaderNameComparator.INSTANCE);
- private final Set<Cookie> cookies =
- new TreeSet<Cookie>(CookieComparator.INSTANCE);
- private Content content = EmptyContent.INSTANCE;
-
- public HttpVersion getProtocolVersion() {
- return protocolVersion;
- }
-
- public void setProtocolVersion(HttpVersion protocolVersion) {
- if (protocolVersion == null) {
- throw new NullPointerException("protocolVersion");
- }
- this.protocolVersion = protocolVersion;
- }
-
- public boolean containsHeader(String name) {
- return headers.containsKey(name);
- }
-
- public String getHeader(String name) {
- List<String> values = headers.get(name);
- if (values == null) {
- return null;
- }
-
- return values.get(0);
- }
-
- public Map<String, List<String>> getHeaders() {
- return Collections.unmodifiableMap(headers);
- }
-
- public void addHeader(String name, String value) {
- validateHeaderName(name);
- if (value == null) {
- throw new NullPointerException("value");
- }
-
- List<String> values = headers.get(name);
- if (values == null) {
- values = new ArrayList<String>();
- headers.put(name, values);
- }
- values.add(value);
- }
-
- public boolean removeHeader(String name) {
- return headers.remove(name) != null;
- }
-
- public void setHeader(String name, String value) {
- validateHeaderName(name);
- if (value == null) {
- throw new NullPointerException("value");
- }
-
- List<String> values = new ArrayList<String>();
- values.add(value);
- headers.put(name, values);
- }
-
- public void setHeaders(Map<String, List<String>> headers) {
- clearHeaders();
-
- for (Map.Entry<String, List<String>> entry: headers.entrySet()) {
- validateHeaderName(entry.getKey());
- for (String value: entry.getValue()) {
+ private static final long serialVersionUID = -7559479748566065541L;
+
+ private HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
+
+ private final Map<String, List<String>> headers = new TreeMap<String, List<String>>(
+ HeaderNameComparator.INSTANCE);
+
+ private final Set<Cookie> cookies = new TreeSet<Cookie>(
+ CookieComparator.INSTANCE);
+
+ private Content content = EmptyContent.INSTANCE;
+
+ public HttpVersion getProtocolVersion() {
+ return protocolVersion;
+ }
+
+ public void setProtocolVersion(HttpVersion protocolVersion) {
+ if (protocolVersion == null) {
+ throw new NullPointerException("protocolVersion");
+ }
+ this.protocolVersion = protocolVersion;
+ }
+
+ public boolean containsHeader(String name) {
+ return headers.containsKey(name);
+ }
+
+ public String getHeader(String name) {
+ List<String> values = headers.get(name);
+ if (values == null) {
+ return null;
+ }
+
+ return values.get(0);
+ }
+
+ public Map<String, List<String>> getHeaders() {
+ return Collections.unmodifiableMap(headers);
+ }
+
+ public void addHeader(String name, String value) {
+ validateHeaderName(name);
+ if (value == null) {
+ throw new NullPointerException("value");
+ }
+
+ List<String> values = headers.get(name);
+ if (values == null) {
+ values = new ArrayList<String>();
+ headers.put(name, values);
+ }
+ values.add(value);
+ }
+
+ public boolean removeHeader(String name) {
+ return headers.remove(name) != null;
+ }
+
+ public void setHeader(String name, String value) {
+ validateHeaderName(name);
if (value == null) {
- throw new NullPointerException("Header '" + entry.getKey() + "' contains null.");
+ throw new NullPointerException("value");
+ }
+
+ List<String> values = new ArrayList<String>();
+ values.add(value);
+ headers.put(name, values);
+ }
+
+ public void setHeaders(Map<String, List<String>> headers) {
+ clearHeaders();
+
+ for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
+ validateHeaderName(entry.getKey());
+ for (String value : entry.getValue()) {
+ if (value == null) {
+ throw new NullPointerException("Header '" + entry.getKey()
+ + "' contains null.");
+ }
+ }
+ if (entry.getValue().size() > 0) {
+ this.headers.put(entry.getKey(), entry.getValue());
+ }
+ }
+ }
+
+ public void clearHeaders() {
+ this.headers.clear();
+ }
+
+ public String getContentType() {
+ return getHeader("Content-Type");
+ }
+
+ public void setContentType(String type) {
+ setHeader("Content-Type", type);
+ }
+
+ public boolean isKeepAlive() {
+ String connection = getHeader(HttpHeaderConstants.KEY_CONNECTION);
+ if (getProtocolVersion() == HttpVersion.HTTP_1_1) {
+ return !HttpHeaderConstants.VALUE_CLOSE
+ .equalsIgnoreCase(connection);
+ } else {
+ return HttpHeaderConstants.VALUE_KEEP_ALIVE
+ .equalsIgnoreCase(connection);
+ }
+ }
+
+ public void setKeepAlive(boolean keepAlive) {
+ setHeader(HttpHeaderConstants.KEY_CONNECTION,
+ keepAlive ? HttpHeaderConstants.VALUE_KEEP_ALIVE
+ : HttpHeaderConstants.VALUE_CLOSE);
+ }
+
+ public Content getContent() {
+ return content;
+ }
+
+ public void setContent(Content content) {
+ if (content == null) {
+ this.content = EmptyContent.INSTANCE;
+ } else {
+ this.content = content;
+ }
+ }
+
+ public void removeCookie(String name) {
+ cookies.remove(name);
+ }
+
+ public void addCookie(Cookie cookie) {
+ cookies.add(cookie);
+ }
+
+ public boolean removeCookie(Cookie cookie) {
+ return cookies.remove(cookie);
+ }
+
+ public void setCookies(Collection<Cookie> cookies) {
+ clearCookies();
+
+ for (Cookie c : cookies) {
+ if (c == null) {
+ throw new NullPointerException("cookies contains null.");
+ }
+ this.cookies.add(c);
+ }
+ }
+
+ public void clearCookies() {
+ this.cookies.clear();
+ }
+
+ public Set<Cookie> getCookies() {
+ return Collections.unmodifiableSet(cookies);
+ }
+
+ private static void validateHeaderName(String name) {
+ if (name == null) {
+ throw new NullPointerException("name");
+ }
+
+ for (int i = 0; i < name.length(); i++) {
+ char c = name.charAt(i);
+ if (c > 127) {
+ throw new IllegalArgumentException(
+ "Name contains an illegal character: " + name);
+ }
+
+ byte b = (byte) c;
+ if (HttpCodecUtils.isHttpControl(b)
+ || HttpCodecUtils.isHttpSeparator(b)) {
+ throw new IllegalArgumentException(
+ "Name contains an illegal character: " + name);
+ }
}
- }
- if (entry.getValue().size() > 0) {
- this.headers.put(entry.getKey(), entry.getValue());
- }
- }
- }
-
- public void clearHeaders() {
- this.headers.clear();
- }
-
- public String getContentType() {
- return getHeader("Content-Type");
- }
-
- public void setContentType(String type) {
- setHeader("Content-Type", type);
- }
-
- public boolean isKeepAlive() {
- String connection = getHeader(HttpHeaderConstants.KEY_CONNECTION);
- if (getProtocolVersion() == HttpVersion.HTTP_1_1) {
- return !HttpHeaderConstants.VALUE_CLOSE.equalsIgnoreCase(connection);
- } else {
- return HttpHeaderConstants.VALUE_KEEP_ALIVE.equalsIgnoreCase(connection);
- }
- }
-
- public void setKeepAlive(boolean keepAlive) {
- setHeader(
- HttpHeaderConstants.KEY_CONNECTION,
- keepAlive ? HttpHeaderConstants.VALUE_KEEP_ALIVE
- : HttpHeaderConstants.VALUE_CLOSE);
- }
-
- public Content getContent() {
- return content;
- }
-
- public void setContent(Content content) {
- if (content == null) {
- this.content = EmptyContent.INSTANCE;
- } else {
- this.content = content;
- }
- }
-
- public void removeCookie(String name) {
- cookies.remove(name);
- }
-
- public void addCookie(Cookie cookie) {
- cookies.add(cookie);
- }
-
- public boolean removeCookie(Cookie cookie) {
- return cookies.remove(cookie);
- }
-
- public void setCookies(Collection<Cookie> cookies) {
- clearCookies();
-
- for (Cookie c: cookies) {
- if (c == null) {
- throw new NullPointerException("cookies contains null.");
- }
- this.cookies.add(c);
- }
- }
-
- public void clearCookies() {
- this.cookies.clear();
- }
-
- public Set<Cookie> getCookies() {
- return Collections.unmodifiableSet(cookies);
- }
-
- private static void validateHeaderName(String name) {
- if (name == null) {
- throw new NullPointerException("name");
- }
-
- for (int i = 0; i < name.length(); i ++) {
- char c = name.charAt(i);
- if (c > 127) {
- throw new IllegalArgumentException("Name contains an illegal character: " + name);
- }
-
- byte b = (byte) c;
- if (HttpCodecUtils.isHttpControl(b) || HttpCodecUtils.isHttpSeparator(b)) {
- throw new IllegalArgumentException("Name contains an illegal character: " + name);
- }
}
- }
}
Modified: mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpRequest.java
URL: http://svn.apache.org/viewvc/mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpRequest.java?rev=592337&r1=592336&r2=592337&view=diff
==============================================================================
--- mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpRequest.java (original)
+++ mina/sandbox/asyncweb/core/src/main/java/org/safehaus/asyncweb/common/DefaultHttpRequest.java Tue Nov 6 00:59:36 2007
@@ -33,228 +33,236 @@
/**
* A default implementation of {@link MutableHttpRequest}.
-// *
+// *
* @author trustin
* @author irvingd
* @version $Rev$, $Date$
*/
-public class DefaultHttpRequest extends DefaultHttpMessage implements MutableHttpRequest {
+public class DefaultHttpRequest extends DefaultHttpMessage implements
+ MutableHttpRequest {
- private static final long serialVersionUID = 3044997961372568928L;
+ private static final long serialVersionUID = 3044997961372568928L;
- private HttpMethod method;
- private URI requestUri;
-
- private Map<String, List<String>> parameters = new HashMap<String, List<String>>();
-
- /**
- * Creates a new instance.
- */
- public DefaultHttpRequest() {
- }
-
- public void setCookies(String headerValue) {
- clearCookies();
-
- int version = -1; // -1 means version is not parsed yet.
- int fieldIdx = 0;
- MutableCookie currentCookie = null;
-
- StringTokenizer tk = new StringTokenizer(headerValue, ";,");
-
- while (tk.hasMoreTokens()) {
- String pair = tk.nextToken();
- String key;
- String value;
-
- int equalsPos = pair.indexOf('=');
- if (equalsPos >= 0) {
- key = pair.substring(0, equalsPos).trim();
- value = pair.substring(equalsPos + 1).trim();
- } else {
- key = pair.trim();
- value = "";
- }
-
- if (version < 0) {
- if (!key.equalsIgnoreCase("$Version")) {
- // $Version is not specified. Use the default (0).
- version = 0;
- } else {
- version = Integer.parseInt(value);
- if (version != 0 && version != 1) {
- throw new IllegalArgumentException("Invalid version: " + version + " (" + headerValue + ")");
- }
- }
- }
-
- if (version >= 0) {
- try {
- switch (fieldIdx) {
- case 1:
- if (key.equalsIgnoreCase("$Path")) {
- currentCookie.setPath(value);
- fieldIdx ++;
+ private HttpMethod method;
+
+ private URI requestUri;
+
+ private Map<String, List<String>> parameters = new HashMap<String, List<String>>();
+
+ /**
+ * Creates a new instance.
+ */
+ public DefaultHttpRequest() {
+ }
+
+ public void setCookies(String headerValue) {
+ clearCookies();
+
+ int version = -1; // -1 means version is not parsed yet.
+ int fieldIdx = 0;
+ MutableCookie currentCookie = null;
+
+ StringTokenizer tk = new StringTokenizer(headerValue, ";,");
+
+ while (tk.hasMoreTokens()) {
+ String pair = tk.nextToken();
+ String key;
+ String value;
+
+ int equalsPos = pair.indexOf('=');
+ if (equalsPos >= 0) {
+ key = pair.substring(0, equalsPos).trim();
+ value = pair.substring(equalsPos + 1).trim();
} else {
- fieldIdx = 0;
+ key = pair.trim();
+ value = "";
+ }
+
+ if (version < 0) {
+ if (!key.equalsIgnoreCase("$Version")) {
+ // $Version is not specified. Use the default (0).
+ version = 0;
+ } else {
+ version = Integer.parseInt(value);
+ if (version != 0 && version != 1) {
+ throw new IllegalArgumentException("Invalid version: "
+ + version + " (" + headerValue + ")");
+ }
+ }
+ }
+
+ if (version >= 0) {
+ try {
+ switch (fieldIdx) {
+ case 1:
+ if (key.equalsIgnoreCase("$Path")) {
+ currentCookie.setPath(value);
+ fieldIdx++;
+ } else {
+ fieldIdx = 0;
+ }
+ break;
+ case 2:
+ if (key.equalsIgnoreCase("$Domain")) {
+ currentCookie.setDomain(value);
+ fieldIdx++;
+ } else {
+ fieldIdx = 0;
+ }
+ break;
+ case 3:
+ if (key.equalsIgnoreCase("$Port")) {
+ // Ignoring for now
+ fieldIdx++;
+ } else {
+ fieldIdx = 0;
+ }
+ break;
+ }
+ } catch (NullPointerException e) {
+ throw new IllegalArgumentException(
+ "Cookie key-value pair not found (" + headerValue
+ + ")");
+ }
+
+ if (fieldIdx == 0) {
+ currentCookie = new DefaultCookie(key);
+ currentCookie.setVersion(version);
+ currentCookie.setValue(value);
+ addCookie(currentCookie);
+ fieldIdx++;
+ }
}
- break;
- case 2:
- if (key.equalsIgnoreCase("$Domain")) {
- currentCookie.setDomain(value);
- fieldIdx ++;
+ }
+ }
+
+ public URI getRequestUri() {
+ return requestUri;
+ }
+
+ public void setRequestUri(URI requestUri) {
+ if (requestUri == null) {
+ throw new NullPointerException("requestUri");
+ }
+ this.requestUri = requestUri;
+ }
+
+ public boolean requiresContinuationResponse() {
+ if (getProtocolVersion() == HttpVersion.HTTP_1_1) {
+ String expectations = getHeader(HttpHeaderConstants.KEY_EXPECT);
+ if (expectations != null) {
+ return expectations
+ .indexOf(HttpHeaderConstants.VALUE_CONTINUE_EXPECTATION) >= 0;
+ }
+ }
+ return false;
+ }
+
+ public HttpMethod getMethod() {
+ return method;
+ }
+
+ public void setMethod(HttpMethod method) {
+ if (method == null) {
+ throw new NullPointerException("method");
+ }
+ this.method = method;
+ }
+
+ public void addParameter(String name, String value) {
+ List<String> values = parameters.get(name);
+ if (values == null) {
+ values = new ArrayList<String>();
+ parameters.put(name, values);
+ }
+ values.add(value);
+ }
+
+ public boolean removeParameter(String name) {
+ return parameters.remove(name) != null;
+ }
+
+ public void setParameter(String name, String value) {
+ List<String> values = new ArrayList<String>();
+ values.add(value);
+ parameters.put(name, values);
+ }
+
+ public void setParameters(Map<String, List<String>> parameters) {
+ clearParameters();
+
+ for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
+ for (String value : entry.getValue()) {
+ if (value == null) {
+ throw new NullPointerException("Parameter '"
+ + entry.getKey() + "' contains null.");
+ }
+ }
+ if (entry.getValue().size() > 0) {
+ this.parameters.put(entry.getKey(), entry.getValue());
+ }
+ }
+ }
+
+ public void setParameters(String queryString) {
+ try {
+ this.setParameters(queryString, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new InternalError("UTF-8 decoder must be provided by JDK.");
+ }
+ }
+
+ public void setParameters(String queryString, String encoding)
+ throws UnsupportedEncodingException {
+ clearParameters();
+
+ if (queryString == null || queryString.length() == 0) {
+ return;
+ }
+
+ int pos = 0;
+ while (pos < queryString.length()) {
+ int ampPos = queryString.indexOf('&', pos);
+
+ String value;
+ if (ampPos < 0) {
+ value = queryString.substring(pos);
+ ampPos = queryString.length();
} else {
- fieldIdx = 0;
+ value = queryString.substring(pos, ampPos);
}
- break;
- case 3:
- if (key.equalsIgnoreCase("$Port")) {
- // Ignoring for now
- fieldIdx ++;
+
+ int equalPos = value.indexOf('=');
+ if (equalPos < 0) {
+ this.addParameter(URLDecoder.decode(value, encoding), "");
} else {
- fieldIdx = 0;
+ this.addParameter(URLDecoder.decode(value
+ .substring(0, equalPos), encoding), URLDecoder.decode(
+ value.substring(equalPos + 1), encoding));
}
- break;
- }
- } catch (NullPointerException e) {
- throw new IllegalArgumentException("Cookie key-value pair not found (" + headerValue + ")");
- }
-
- if (fieldIdx == 0) {
- currentCookie = new DefaultCookie(key);
- currentCookie.setVersion(version);
- currentCookie.setValue(value);
- addCookie(currentCookie);
- fieldIdx ++;
- }
- }
- }
- }
-
- public URI getRequestUri() {
- return requestUri;
- }
-
- public void setRequestUri(URI requestUri) {
- if (requestUri == null) {
- throw new NullPointerException("requestUri");
- }
- this.requestUri = requestUri;
- }
-
- public boolean requiresContinuationResponse() {
- if (getProtocolVersion() == HttpVersion.HTTP_1_1) {
- String expectations = getHeader(HttpHeaderConstants.KEY_EXPECT);
- if (expectations != null) {
- return expectations.indexOf(HttpHeaderConstants.VALUE_CONTINUE_EXPECTATION) >= 0;
- }
- }
- return false;
- }
-
- public HttpMethod getMethod() {
- return method;
- }
-
- public void setMethod(HttpMethod method) {
- if (method == null) {
- throw new NullPointerException("method");
- }
- this.method = method;
- }
-
- public void addParameter(String name, String value) {
- List<String> values = parameters.get(name);
- if (values == null) {
- values = new ArrayList<String>();
- parameters.put(name, values);
- }
- values.add(value);
- }
-
- public boolean removeParameter(String name) {
- return parameters.remove(name) != null;
- }
-
- public void setParameter(String name, String value) {
- List<String> values = new ArrayList<String>();
- values.add(value);
- parameters.put(name, values);
- }
-
- public void setParameters(Map<String, List<String>> parameters) {
- clearParameters();
-
- for (Map.Entry<String, List<String>> entry: parameters.entrySet()) {
- for (String value: entry.getValue()) {
- if (value == null) {
- throw new NullPointerException("Parameter '" + entry.getKey() + "' contains null.");
- }
- }
- if (entry.getValue().size() > 0) {
- this.parameters.put(entry.getKey(), entry.getValue());
- }
- }
- }
-
- public void setParameters(String queryString) {
- try {
- this.setParameters(queryString, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new InternalError("UTF-8 decoder must be provided by JDK.");
- }
- }
-
- public void setParameters(String queryString, String encoding) throws UnsupportedEncodingException {
- clearParameters();
-
- if (queryString == null || queryString.length() == 0) {
- return;
- }
-
- int pos = 0;
- while (pos < queryString.length()) {
- int ampPos = queryString.indexOf('&', pos);
-
- String value;
- if (ampPos < 0) {
- value = queryString.substring(pos);
- ampPos = queryString.length();
- } else {
- value = queryString.substring(pos, ampPos);
- }
-
- int equalPos = value.indexOf('=');
- if (equalPos < 0) {
- this.addParameter(URLDecoder.decode(value, encoding), "");
- } else {
- this.addParameter(
- URLDecoder.decode(value.substring(0, equalPos), encoding),
- URLDecoder.decode(value.substring(equalPos + 1), encoding));
- }
-
- pos = ampPos + 1;
- }
- }
-
- public void clearParameters() {
- this.parameters.clear();
- }
-
- public boolean containsParameter(String name) {
- return parameters.containsKey(name);
- }
-
- public String getParameter(String name) {
- List<String> values = parameters.get(name);
- if (values == null) {
- return null;
- }
-
- return values.get(0);
- }
-
- public Map<String, List<String>> getParameters() {
- return Collections.unmodifiableMap(parameters);
- }
+
+ pos = ampPos + 1;
+ }
+ }
+
+ public void clearParameters() {
+ this.parameters.clear();
+ }
+
+ public boolean containsParameter(String name) {
+ return parameters.containsKey(name);
+ }
+
+ public String getParameter(String name) {
+ List<String> values = parameters.get(name);
+ if (values == null) {
+ return null;
+ }
+
+ return values.get(0);
+ }
+
+ public Map<String, List<String>> getParameters() {
+ return Collections.unmodifiableMap(parameters);
+ }
}