1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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          
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                     
106                     break;
107                 }
108             }
109             if (charset == null && charsetPair != null) {
110                 
111                 this.charset = charsetPair.getValue();
112             } else if (charset != null && charsetPair == null) {
113                 
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     
125 
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     
139 
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 }