View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.net.URLEncoder;
22  import java.nio.charset.Charset;
23  import java.nio.charset.UnsupportedCharsetException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * Creates an URL-encoded URI from a path string and key-value parameter pairs.
29   * This encoder is for one time use only.  Create a new instance for each URI.
30   *
31   * <pre>
32   * {@link QueryStringEncoder} encoder = new {@link QueryStringEncoder}("/hello");
33   * encoder.addParam("recipient", "world");
34   * assert encoder.toString().equals("/hello?recipient=world");
35   * </pre>
36   * @see QueryStringDecoder
37   */
38  public class QueryStringEncoder {
39  
40      private final Charset charset;
41      private final String uri;
42      private final List<Param> params = new ArrayList<Param>();
43  
44      /**
45       * Creates a new encoder that encodes a URI that starts with the specified
46       * path string.  The encoder will encode the URI in UTF-8.
47       */
48      public QueryStringEncoder(String uri) {
49          this(uri, HttpConstants.DEFAULT_CHARSET);
50      }
51  
52      /**
53       * Creates a new encoder that encodes a URI that starts with the specified
54       * path string in the specified charset.
55       */
56      public QueryStringEncoder(String uri, Charset charset) {
57          if (uri == null) {
58              throw new NullPointerException("getUri");
59          }
60          if (charset == null) {
61              throw new NullPointerException("charset");
62          }
63  
64          this.uri = uri;
65          this.charset = charset;
66      }
67  
68      /**
69       * Adds a parameter with the specified name and value to this encoder.
70       */
71      public void addParam(String name, String value) {
72          if (name == null) {
73              throw new NullPointerException("name");
74          }
75          params.add(new Param(name, value));
76      }
77  
78      /**
79       * Returns the URL-encoded URI object which was created from the path string
80       * specified in the constructor and the parameters added by
81       * {@link #addParam(String, String)} getMethod.
82       */
83      public URI toUri() throws URISyntaxException {
84          return new URI(toString());
85      }
86  
87      /**
88       * Returns the URL-encoded URI which was created from the path string
89       * specified in the constructor and the parameters added by
90       * {@link #addParam(String, String)} getMethod.
91       */
92      @Override
93      public String toString() {
94          if (params.isEmpty()) {
95              return uri;
96          } else {
97              StringBuilder sb = new StringBuilder(uri).append('?');
98              for (int i = 0; i < params.size(); i++) {
99                  Param param = params.get(i);
100                 sb.append(encodeComponent(param.name, charset));
101                 if (param.value != null) {
102                     sb.append('=');
103                     sb.append(encodeComponent(param.value, charset));
104                 }
105                 if (i != params.size() - 1) {
106                     sb.append('&');
107                 }
108             }
109             return sb.toString();
110         }
111     }
112 
113     private static String encodeComponent(String s, Charset charset) {
114         // TODO: Optimize me.
115         try {
116             return URLEncoder.encode(s, charset.name()).replace("+", "%20");
117         } catch (UnsupportedEncodingException ignored) {
118             throw new UnsupportedCharsetException(charset.name());
119         }
120     }
121 
122     private static final class Param {
123 
124         final String name;
125         final String value;
126 
127         Param(String name, String value) {
128             this.value = value;
129             this.name = name;
130         }
131     }
132 }