View Javadoc

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