View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/StringRequestEntity.java,v 1.3 2004/07/03 14:27:03 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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      /***
58       * Creates a new entity with the given content
59       *  
60       * @param content The content to set.
61       */
62      public StringRequestEntity(String content) {
63          super();
64          if (content == null) {
65              throw new IllegalArgumentException("The content cannot be null");
66          }
67          this.contentType = null;
68          this.charset = null;
69          this.content = content.getBytes();
70      }
71  
72      /***
73       * Creates a new entity with the given content, content type, and charset.
74       *  
75       * @param content The content to set.
76       * @param contentType The type of the content, or <code>null</code>.  The value retured 
77       *   by {@link #getContentType()}.  If this content type contains a charset and the charset
78       *   parameter is null, the content's type charset will be used.
79       * @param charset The charset of the content, or <code>null</code>.  Used to convert the 
80       *   content to bytes.  If the content type does not contain a charset and charset is not null,
81       *   then the charset will be appended to the content type.
82       */
83      public StringRequestEntity(String content, String contentType, String charset) 
84          throws UnsupportedEncodingException {
85          super();
86          if (content == null) {
87              throw new IllegalArgumentException("The content cannot be null");
88          }
89          
90          this.contentType = contentType;
91          this.charset = charset;
92          
93          // resolve the content type and the charset
94          if (contentType != null) {
95              HeaderElement[] values = HeaderElement.parseElements(contentType);
96              NameValuePair charsetPair = null;
97              for (int i = 0; i < values.length; i++) {
98                  if ((charsetPair = values[i].getParameterByName("charset")) != null) {
99                      // charset found
100                     break;
101                 }
102             }
103             if (charset == null && charsetPair != null) {
104                 // use the charset from the content type
105                 this.charset = charsetPair.getValue();
106             } else if (charset != null && charsetPair == null) {
107                 // append the charset to the content type
108                 this.contentType = contentType + "; charset=" + charset; 
109             }
110         }
111         if (this.charset != null) {
112             this.content = content.getBytes(this.charset);
113         } else {
114             this.content = content.getBytes();
115         }
116     }
117 
118     /* (non-Javadoc)
119      * @see org.apache.commons.httpclient.methods.RequestEntity#getContentType()
120      */
121     public String getContentType() {
122         return contentType;
123     }
124 
125     /***
126      * @return <code>true</code>
127      */
128     public boolean isRepeatable() {
129         return true;
130     }
131 
132     /* (non-Javadoc)
133      * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
134      */
135     public void writeRequest(OutputStream out) throws IOException {
136         if (out == null) {
137             throw new IllegalArgumentException("Output stream may not be null");
138         }
139         out.write(this.content);
140         out.flush();
141     }
142 
143     /***
144      * @return The length of the content.
145      */
146     public long getContentLength() {
147         return this.content.length;
148     }
149 
150     /***
151      * @return Returns the content.
152      */
153     public String getContent() {
154         if (this.charset != null) {
155             try {
156                 return new String(this.content, this.charset);
157             } catch (UnsupportedEncodingException e) {
158                 return new String(this.content);
159             }
160         } else {
161             return new String(this.content);
162         }
163     }
164 
165     /***
166      * @return Returns the charset used to convert the content to bytes. <code>null</code> if
167      * no charset as been specified.
168      */
169     public String getCharset() {
170         return charset;
171     }
172 }