View Javadoc

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