netty5/src/main/java/org/jboss/netty/handler/codec/http/QueryStringEncoder.java

148 lines
4.7 KiB
Java
Raw Normal View History

2008-11-19 08:22:15 +01:00
/*
* JBoss, Home of Professional Open Source
2009-06-19 19:48:17 +02:00
*
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
2008-11-19 08:22:15 +01:00
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.handler.codec.http;
import java.io.UnsupportedEncodingException;
2008-11-19 08:22:15 +01:00
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.UnsupportedCharsetException;
2008-11-19 08:22:15 +01:00
import java.util.ArrayList;
import java.util.List;
/**
2009-06-19 16:49:26 +02:00
* 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.
*
2009-06-19 17:05:47 +02:00
* <pre>
* QueryStringEncoder encoder = new QueryStringDecoder("/hello");
* encoder.addParam("recipient", "world");
* assert encoder.toString().equals("/hello?recipient=world");
* </pre>
*
2008-11-19 08:22:15 +01:00
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Andy Taylor (andy.taylor@jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
2008-12-03 10:05:54 +01:00
*
2009-06-19 17:05:47 +02:00
* @see QueryStringDecoder
*
* @apiviz.stereotype utility
* @apiviz.has org.jboss.netty.handler.codec.http.HttpRequest oneway - - encodes
2008-11-19 08:22:15 +01:00
*/
public class QueryStringEncoder {
private final String charset;
private final String uri;
private final List<Param> params = new ArrayList<Param>();
2008-11-19 08:22:15 +01:00
2009-06-19 16:49:26 +02:00
/**
* 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) {
this(uri, HttpCodecUtil.DEFAULT_CHARSET);
}
2009-06-19 16:49:26 +02:00
/**
* 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) {
if (uri == null) {
throw new NullPointerException("uri");
}
if (charset == null) {
throw new NullPointerException("charset");
}
this.uri = uri;
this.charset = charset;
2008-11-19 08:22:15 +01:00
}
2009-06-19 16:49:26 +02:00
/**
* Adds a parameter with the specified name and value to this encoder.
*/
2008-11-19 08:22:15 +01:00
public void addParam(String name, String value) {
if (name == null) {
throw new NullPointerException("name");
2008-11-19 08:22:15 +01:00
}
if (value == null) {
throw new NullPointerException("value");
2008-11-19 08:22:15 +01:00
}
params.add(new Param(name, value));
}
2009-06-19 16:49:26 +02:00
/**
* 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.
*/
2008-11-19 08:22:15 +01:00
public URI toUri() throws URISyntaxException {
2008-12-01 06:29:59 +01:00
return new URI(toString());
}
2009-06-19 16:49:26 +02:00
/**
* 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.
*/
2008-12-01 06:29:59 +01:00
@Override
public String toString() {
if (params.isEmpty()) {
return uri;
} else {
StringBuilder sb = new StringBuilder(uri).append("?");
2008-11-19 08:22:15 +01:00
for (int i = 0; i < params.size(); i++) {
Param param = params.get(i);
sb.append(encodeComponent(param.name, charset));
sb.append("=");
sb.append(encodeComponent(param.value, charset));
2008-11-19 08:22:15 +01:00
if(i != params.size() - 1) {
sb.append("&");
}
}
2008-12-01 06:29:59 +01:00
return sb.toString();
2008-11-19 08:22:15 +01:00
}
}
private static String encodeComponent(String s, String charset) {
try {
return URLEncoder.encode(s, charset).replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
throw new UnsupportedCharsetException(charset);
}
2008-11-19 08:22:15 +01:00
}
2008-11-28 06:28:50 +01:00
private static final class Param {
2008-11-19 08:22:15 +01:00
2008-11-19 10:15:19 +01:00
final String name;
2008-11-19 08:22:15 +01:00
final String value;
2008-11-19 10:15:19 +01:00
Param(String name, String value) {
2008-11-19 08:22:15 +01:00
this.value = value;
this.name = name;
}
}
}