001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/java/org/apache/commons/httpclient/methods/StringRequestEntity.java $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032: package org.apache.commons.httpclient.methods;
033:
034: import java.io.IOException;
035: import java.io.OutputStream;
036: import java.io.UnsupportedEncodingException;
037:
038: import org.apache.commons.httpclient.HeaderElement;
039: import org.apache.commons.httpclient.NameValuePair;
040:
041: /**
042: * A RequestEntity that contains a String.
043: *
044: * @since 3.0
045: */
046: public class StringRequestEntity implements RequestEntity {
047:
048: /** The content */
049: private byte[] content;
050:
051: /** The charset */
052: private String charset;
053:
054: /** The content type (i.e. text/html; charset=EUC-JP). */
055: private String contentType;
056:
057: /**
058: * <p>Creates a new entity with the given content. This constructor
059: * will use the default platform charset to convert the content string
060: * and will provide no content type.</p>
061: *
062: * @see #StringRequestEntity(String, String, String)
063: *
064: * @param content The content to set.
065: *
066: * @deprecated use {@link #StringRequestEntity(String, String, String)} instead
067: */
068: public StringRequestEntity(String content) {
069: super ();
070: if (content == null) {
071: throw new IllegalArgumentException(
072: "The content cannot be null");
073: }
074: this .contentType = null;
075: this .charset = null;
076: this .content = content.getBytes();
077: }
078:
079: /**
080: * Creates a new entity with the given content, content type, and charset.
081: *
082: * @param content The content to set.
083: * @param contentType The type of the content, or <code>null</code>. The value retured
084: * by {@link #getContentType()}. If this content type contains a charset and the charset
085: * parameter is null, the content's type charset will be used.
086: * @param charset The charset of the content, or <code>null</code>. Used to convert the
087: * content to bytes. If the content type does not contain a charset and charset is not null,
088: * then the charset will be appended to the content type.
089: */
090: public StringRequestEntity(String content, String contentType,
091: String charset) throws UnsupportedEncodingException {
092: super ();
093: if (content == null) {
094: throw new IllegalArgumentException(
095: "The content cannot be null");
096: }
097:
098: this .contentType = contentType;
099: this .charset = charset;
100:
101: // resolve the content type and the charset
102: if (contentType != null) {
103: HeaderElement[] values = HeaderElement
104: .parseElements(contentType);
105: NameValuePair charsetPair = null;
106: for (int i = 0; i < values.length; i++) {
107: if ((charsetPair = values[i]
108: .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(
148: "Output stream may not be null");
149: }
150: out.write(this .content);
151: out.flush();
152: }
153:
154: /**
155: * @return The length of the content.
156: */
157: public long getContentLength() {
158: return this .content.length;
159: }
160:
161: /**
162: * @return Returns the content.
163: */
164: public String getContent() {
165: if (this .charset != null) {
166: try {
167: return new String(this .content, this .charset);
168: } catch (UnsupportedEncodingException e) {
169: return new String(this .content);
170: }
171: } else {
172: return new String(this .content);
173: }
174: }
175:
176: /**
177: * @return Returns the charset used to convert the content to bytes. <code>null</code> if
178: * no charset as been specified.
179: */
180: public String getCharset() {
181: return charset;
182: }
183: }
|