View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/methods/StringRequestEntity.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   * [Additional notices, if required by prior licensing conditions]
30   *
31   */
32  package org.apache.commons.httpclient.methods;
33  
34  import java.io.IOException;
35  import java.io.OutputStream;
36  import java.io.UnsupportedEncodingException;
37  
38  import org.apache.commons.httpclient.HeaderElement;
39  import org.apache.commons.httpclient.NameValuePair;
40  
41  /***
42   * A RequestEntity that contains a String.
43   * 
44   * @since 3.0
45   */
46  public class StringRequestEntity implements RequestEntity {
47  
48      /*** The content */
49      private byte[] content;
50      
51      /*** The charset */
52      private String charset;
53  
54      /*** The content type (i.e. text/html; charset=EUC-JP). */
55      private String contentType;
56      
57      /***
58       * <p>Creates a new entity with the given content. This constructor 
59       * will use the default platform charset to convert the content string 
60       * and will provide no content type.</p>
61       *  
62       * @see #StringRequestEntity(String, String, String)
63       * 
64       * @param content The content to set.
65       * 
66       * @deprecated use {@link #StringRequestEntity(String, String, String)} instead
67       */
68      public StringRequestEntity(String content) {
69          super();
70          if (content == null) {
71              throw new IllegalArgumentException("The content cannot be null");
72          }
73          this.contentType = null;
74          this.charset = null;
75          this.content = content.getBytes();
76      }
77  
78      /***
79       * Creates a new entity with the given content, content type, and charset.
80       *  
81       * @param content The content to set.
82       * @param contentType The type of the content, or <code>null</code>.  The value retured 
83       *   by {@link #getContentType()}.  If this content type contains a charset and the charset
84       *   parameter is null, the content's type charset will be used.
85       * @param charset The charset of the content, or <code>null</code>.  Used to convert the 
86       *   content to bytes.  If the content type does not contain a charset and charset is not null,
87       *   then the charset will be appended to the content type.
88       */
89      public StringRequestEntity(String content, String contentType, String charset) 
90          throws UnsupportedEncodingException {
91          super();
92          if (content == null) {
93              throw new IllegalArgumentException("The content cannot be null");
94          }
95          
96          this.contentType = contentType;
97          this.charset = charset;
98          
99          // resolve the content type and the charset
100         if (contentType != null) {
101             HeaderElement[] values = HeaderElement.parseElements(contentType);
102             NameValuePair charsetPair = null;
103             for (int i = 0; i < values.length; i++) {
104                 if ((charsetPair = values[i].getParameterByName("charset")) != null) {
105                     // charset found
106                     break;
107                 }
108             }
109             if (charset == null && charsetPair != null) {
110                 // use the charset from the content type
111                 this.charset = charsetPair.getValue();
112             } else if (charset != null && charsetPair == null) {
113                 // append the charset to the content type
114                 this.contentType = contentType + "; charset=" + charset; 
115             }
116         }
117         if (this.charset != null) {
118             this.content = content.getBytes(this.charset);
119         } else {
120             this.content = content.getBytes();
121         }
122     }
123 
124     /* (non-Javadoc)
125      * @see org.apache.commons.httpclient.methods.RequestEntity#getContentType()
126      */
127     public String getContentType() {
128         return contentType;
129     }
130 
131     /***
132      * @return <code>true</code>
133      */
134     public boolean isRepeatable() {
135         return true;
136     }
137 
138     /* (non-Javadoc)
139      * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
140      */
141     public void writeRequest(OutputStream out) throws IOException {
142         if (out == null) {
143             throw new IllegalArgumentException("Output stream may not be null");
144         }
145         out.write(this.content);
146         out.flush();
147     }
148 
149     /***
150      * @return The length of the content.
151      */
152     public long getContentLength() {
153         return this.content.length;
154     }
155 
156     /***
157      * @return Returns the content.
158      */
159     public String getContent() {
160         if (this.charset != null) {
161             try {
162                 return new String(this.content, this.charset);
163             } catch (UnsupportedEncodingException e) {
164                 return new String(this.content);
165             }
166         } else {
167             return new String(this.content);
168         }
169     }
170 
171     /***
172      * @return Returns the charset used to convert the content to bytes. <code>null</code> if
173      * no charset as been specified.
174      */
175     public String getCharset() {
176         return charset;
177     }
178 }