Javadoc
This commit is contained in:
parent
673312b22e
commit
997dc9081a
@ -24,32 +24,125 @@ package org.jboss.netty.handler.codec.http;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* An <a href="http://en.wikipedia.org/wiki/HTTP_cookie">HTTP Cookie</a>.
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
* @version $Rev$, $Date$
|
* @version $Rev$, $Date$
|
||||||
*/
|
*/
|
||||||
public interface Cookie extends Comparable<Cookie> {
|
public interface Cookie extends Comparable<Cookie> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of this cookie.
|
||||||
|
*/
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of this cookie.
|
||||||
|
*/
|
||||||
String getValue();
|
String getValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of this cookie.
|
||||||
|
*/
|
||||||
void setValue(String value);
|
void setValue(String value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the domain of this cookie.
|
||||||
|
*/
|
||||||
String getDomain();
|
String getDomain();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the domain of this cookie.
|
||||||
|
*/
|
||||||
void setDomain(String domain);
|
void setDomain(String domain);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the path of this cookie.
|
||||||
|
*/
|
||||||
String getPath();
|
String getPath();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the path of this cookie.
|
||||||
|
*/
|
||||||
void setPath(String path);
|
void setPath(String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the comment of this cookie.
|
||||||
|
*/
|
||||||
String getComment();
|
String getComment();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the comment of this cookie.
|
||||||
|
*/
|
||||||
void setComment(String comment);
|
void setComment(String comment);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the max age of this cookie in seconds.
|
||||||
|
*/
|
||||||
int getMaxAge();
|
int getMaxAge();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the max age of this cookie in seconds. If {@code 0} is specified,
|
||||||
|
* this cookie will be removed by browser because it will be expired
|
||||||
|
* immediately. If {@code -1} is specified, this cookie will be removed
|
||||||
|
* when a user terminates browser.
|
||||||
|
*/
|
||||||
void setMaxAge(int maxAge);
|
void setMaxAge(int maxAge);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the version of this cookie.
|
||||||
|
*/
|
||||||
int getVersion();
|
int getVersion();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the version of this cookie.
|
||||||
|
*/
|
||||||
void setVersion(int version);
|
void setVersion(int version);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the secure flag of this cookie.
|
||||||
|
*/
|
||||||
boolean isSecure();
|
boolean isSecure();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the secure flag of this cookie.
|
||||||
|
*/
|
||||||
void setSecure(boolean secure);
|
void setSecure(boolean secure);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the comment URL of this cookie.
|
||||||
|
*/
|
||||||
String getCommentUrl();
|
String getCommentUrl();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the comment URL of this cookie.
|
||||||
|
*/
|
||||||
void setCommentUrl(String commentUrl);
|
void setCommentUrl(String commentUrl);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the discard flag of this cookie.
|
||||||
|
*/
|
||||||
boolean isDiscard();
|
boolean isDiscard();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the discard flag of this cookie.
|
||||||
|
*/
|
||||||
void setDiscard(boolean discard);
|
void setDiscard(boolean discard);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the ports of this cookie.
|
||||||
|
*/
|
||||||
Set<Integer> getPorts();
|
Set<Integer> getPorts();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the ports of this cookie.
|
||||||
|
*/
|
||||||
void setPorts(int... ports);
|
void setPorts(int... ports);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the ports of this cookie.
|
||||||
|
*/
|
||||||
void setPorts(Iterable<Integer> ports);
|
void setPorts(Iterable<Integer> ports);
|
||||||
}
|
}
|
@ -31,6 +31,9 @@ import java.util.regex.Matcher;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Decodes an HTTP header value into {@link Cookie}s. This decoder can decode
|
||||||
|
* the HTTP cookie version 0, 1, and 2.
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
@ -43,10 +46,18 @@ public class CookieDecoder {
|
|||||||
|
|
||||||
private final static String COMMA = ",";
|
private final static String COMMA = ",";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new decoder.
|
||||||
|
*/
|
||||||
public CookieDecoder() {
|
public CookieDecoder() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes the specified HTTP header value into {@link Cookie}s.
|
||||||
|
*
|
||||||
|
* @return the decoded {@link Cookie}s
|
||||||
|
*/
|
||||||
public Set<Cookie> decode(String header) {
|
public Set<Cookie> decode(String header) {
|
||||||
Matcher m = PATTERN.matcher(header);
|
Matcher m = PATTERN.matcher(header);
|
||||||
List<String> names = new ArrayList<String>(8);
|
List<String> names = new ArrayList<String>(8);
|
||||||
|
@ -26,6 +26,15 @@ import java.util.Set;
|
|||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Encodes {@link Cookie}s into an HTTP header value. This encoder can encode
|
||||||
|
* the HTTP cookie version 0, 1, and 2.
|
||||||
|
* <p>
|
||||||
|
* This encoder is stateful. It maintains an internal data structure that
|
||||||
|
* holds the {@link Cookie}s added by the {@link #addCookie(String, String)}
|
||||||
|
* method. Once {@link #encode()} is called, all added {@link Cookie}s are
|
||||||
|
* encoded into an HTTP header value and all {@link Cookie}s in the internal
|
||||||
|
* data structure are removed so that the encoder can start over.
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
@ -36,24 +45,46 @@ public class CookieEncoder {
|
|||||||
private final Set<Cookie> cookies = new TreeSet<Cookie>();
|
private final Set<Cookie> cookies = new TreeSet<Cookie>();
|
||||||
private final boolean server;
|
private final boolean server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new encoder.
|
||||||
|
*
|
||||||
|
* @param server {@code true} if and only if this encoder is supposed to
|
||||||
|
* encode server-side cookies. {@code false} if and only if
|
||||||
|
* this encoder is supposed to encode client-side cookies.
|
||||||
|
*/
|
||||||
public CookieEncoder(boolean server) {
|
public CookieEncoder(boolean server) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new {@link Cookie} created with the specified name and value to
|
||||||
|
* this encoder.
|
||||||
|
*/
|
||||||
public void addCookie(String name, String value) {
|
public void addCookie(String name, String value) {
|
||||||
cookies.add(new DefaultCookie(name, value));
|
cookies.add(new DefaultCookie(name, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the specified {@link Cookie} to this encoder.
|
||||||
|
*/
|
||||||
public void addCookie(Cookie cookie) {
|
public void addCookie(Cookie cookie) {
|
||||||
cookies.add(cookie);
|
cookies.add(cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes the {@link Cookie}s which were added by {@link #addCookie(Cookie)}
|
||||||
|
* so far into an HTTP header value. If no {@link Cookie}s were added,
|
||||||
|
* an empty string is returned.
|
||||||
|
*/
|
||||||
public String encode() {
|
public String encode() {
|
||||||
|
String answer;
|
||||||
if (server) {
|
if (server) {
|
||||||
return encodeServerSide();
|
answer = encodeServerSide();
|
||||||
} else {
|
} else {
|
||||||
return encodeClientSide();
|
answer = encodeClientSide();
|
||||||
}
|
}
|
||||||
|
cookies.clear();
|
||||||
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String encodeServerSide() {
|
private String encodeServerSide() {
|
||||||
|
@ -38,7 +38,7 @@ public class DefaultHttpChunk implements HttpChunk {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance with the specified chunk content. If an empty
|
* Creates a new instance with the specified chunk content. If an empty
|
||||||
* buffer is specified, this chunk becomes the 'end of chunk' marker.
|
* buffer is specified, this chunk becomes the 'end of content' marker.
|
||||||
*/
|
*/
|
||||||
public DefaultHttpChunk(ChannelBuffer content) {
|
public DefaultHttpChunk(ChannelBuffer content) {
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
|
@ -26,14 +26,29 @@ import org.jboss.netty.buffer.ChannelBuffer;
|
|||||||
import org.jboss.netty.buffer.ChannelBuffers;
|
import org.jboss.netty.buffer.ChannelBuffers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* An <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">HTTP</a>
|
||||||
|
* chunk.
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
* @version $Rev$, $Date$
|
* @version $Rev$, $Date$
|
||||||
*/
|
*/
|
||||||
public interface HttpChunk {
|
public interface HttpChunk {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The 'end of content' maker in chunked encoding.
|
||||||
|
*/
|
||||||
static HttpChunk LAST_CHUNK = new DefaultHttpChunk(ChannelBuffers.EMPTY_BUFFER);
|
static HttpChunk LAST_CHUNK = new DefaultHttpChunk(ChannelBuffers.EMPTY_BUFFER);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} if and only if this chunk is the 'end of content'
|
||||||
|
* marker.
|
||||||
|
*/
|
||||||
boolean isLast();
|
boolean isLast();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the content of this chunk. If this is the 'end of content'
|
||||||
|
* maker, {@link ChannelBuffers#EMPTY_BUFFER} will be returned.
|
||||||
|
*/
|
||||||
ChannelBuffer getContent();
|
ChannelBuffer getContent();
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,9 @@ import java.util.regex.Matcher;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Splits an HTTP query string into a path string and key-value parameter pairs.
|
||||||
|
* This decoder is for one time use only. Create a new instance for each URI.
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
@ -49,10 +52,18 @@ public class QueryStringDecoder {
|
|||||||
private String path;
|
private String path;
|
||||||
private final Map<String, List<String>> params = new HashMap<String, List<String>>();
|
private final Map<String, List<String>> params = new HashMap<String, List<String>>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new decoder that decodes the specified URI. The decoder will
|
||||||
|
* assume that the query string is encoded in UTF-8.
|
||||||
|
*/
|
||||||
public QueryStringDecoder(String uri) {
|
public QueryStringDecoder(String uri) {
|
||||||
this(uri, HttpCodecUtil.DEFAULT_CHARSET);
|
this(uri, HttpCodecUtil.DEFAULT_CHARSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new decoder that decodes the specified URI encoded in the
|
||||||
|
* specified charset.
|
||||||
|
*/
|
||||||
public QueryStringDecoder(String uri, String charset) {
|
public QueryStringDecoder(String uri, String charset) {
|
||||||
if (uri == null) {
|
if (uri == null) {
|
||||||
throw new NullPointerException("uri");
|
throw new NullPointerException("uri");
|
||||||
@ -65,10 +76,18 @@ public class QueryStringDecoder {
|
|||||||
this.charset = charset;
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new decoder that decodes the specified URI. The decoder will
|
||||||
|
* assume that the query string is encoded in UTF-8.
|
||||||
|
*/
|
||||||
public QueryStringDecoder(URI uri) {
|
public QueryStringDecoder(URI uri) {
|
||||||
this(uri, HttpCodecUtil.DEFAULT_CHARSET);
|
this(uri, HttpCodecUtil.DEFAULT_CHARSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new decoder that decodes the specified URI encoded in the
|
||||||
|
* specified charset.
|
||||||
|
*/
|
||||||
public QueryStringDecoder(URI uri, String charset){
|
public QueryStringDecoder(URI uri, String charset){
|
||||||
if (uri == null) {
|
if (uri == null) {
|
||||||
throw new NullPointerException("uri");
|
throw new NullPointerException("uri");
|
||||||
@ -81,6 +100,9 @@ public class QueryStringDecoder {
|
|||||||
this.charset = charset;
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the decoded path string of the URI.
|
||||||
|
*/
|
||||||
public String getPath() {
|
public String getPath() {
|
||||||
//decode lazily
|
//decode lazily
|
||||||
if(path == null) {
|
if(path == null) {
|
||||||
@ -94,6 +116,9 @@ public class QueryStringDecoder {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the decoded key-value parameter pairs of the URI.
|
||||||
|
*/
|
||||||
public Map<String, List<String>> getParameters() {
|
public Map<String, List<String>> getParameters() {
|
||||||
if(path == null){
|
if(path == null){
|
||||||
if(uri.contains("?")) {
|
if(uri.contains("?")) {
|
||||||
|
@ -30,6 +30,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Creates an URL-encoded URI from a path string and key-value parameter pairs.
|
||||||
|
* This encoder is for one time use only. Create a new instance for each URI.
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
@ -43,10 +46,18 @@ public class QueryStringEncoder {
|
|||||||
private final String uri;
|
private final String uri;
|
||||||
private final List<Param> params = new ArrayList<Param>();
|
private final List<Param> params = new ArrayList<Param>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new encoder that encodes a URI that starts with the specified
|
||||||
|
* path string. The encoder will encode the URI in UTF-8.
|
||||||
|
*/
|
||||||
public QueryStringEncoder(String uri) {
|
public QueryStringEncoder(String uri) {
|
||||||
this(uri, HttpCodecUtil.DEFAULT_CHARSET);
|
this(uri, HttpCodecUtil.DEFAULT_CHARSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new encoder that encodes a URI that starts with the specified
|
||||||
|
* path string in the specified charset.
|
||||||
|
*/
|
||||||
public QueryStringEncoder(String uri, String charset) {
|
public QueryStringEncoder(String uri, String charset) {
|
||||||
if (uri == null) {
|
if (uri == null) {
|
||||||
throw new NullPointerException("uri");
|
throw new NullPointerException("uri");
|
||||||
@ -59,6 +70,9 @@ public class QueryStringEncoder {
|
|||||||
this.charset = charset;
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a parameter with the specified name and value to this encoder.
|
||||||
|
*/
|
||||||
public void addParam(String name, String value) {
|
public void addParam(String name, String value) {
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
throw new NullPointerException("name");
|
throw new NullPointerException("name");
|
||||||
@ -69,10 +83,20 @@ public class QueryStringEncoder {
|
|||||||
params.add(new Param(name, value));
|
params.add(new Param(name, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the URL-encoded URI object which was created from the path string
|
||||||
|
* specified in the constructor and the parameters added by
|
||||||
|
* {@link #addParam(String, String)} method.
|
||||||
|
*/
|
||||||
public URI toUri() throws URISyntaxException {
|
public URI toUri() throws URISyntaxException {
|
||||||
return new URI(toString());
|
return new URI(toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the URL-encoded URI which was created from the path string
|
||||||
|
* specified in the constructor and the parameters added by
|
||||||
|
* {@link #addParam(String, String)} method.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (params.isEmpty()) {
|
if (params.isEmpty()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user