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