001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.entity;
029
030import java.io.ByteArrayInputStream;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.OutputStream;
034import java.io.UnsupportedEncodingException;
035import java.nio.charset.Charset;
036import java.nio.charset.UnsupportedCharsetException;
037
038import org.apache.http.protocol.HTTP;
039import org.apache.http.util.Args;
040
041/**
042 * A self contained, repeatable entity that obtains its content from
043 * a {@link String}.
044 *
045 * @since 4.0
046 */
047public class StringEntity extends AbstractHttpEntity implements Cloneable {
048
049    protected final byte[] content;
050
051    /**
052     * Creates a StringEntity with the specified content and content type.
053     *
054     * @param string content to be used. Not {@code null}.
055     * @param contentType content type to be used. May be {@code null}, in which case the default
056     *   MIME type {@link ContentType#TEXT_PLAIN} is assumed.
057     *
058     * @throws IllegalArgumentException if the string parameter is null
059     * @throws UnsupportedCharsetException Thrown when the named charset is not available in
060     * this instance of the Java virtual machine
061     * @since 4.2
062     */
063    public StringEntity(final String string, final ContentType contentType) throws UnsupportedCharsetException {
064        super();
065        Args.notNull(string, "Source string");
066        Charset charset = contentType != null ? contentType.getCharset() : null;
067        if (charset == null) {
068            charset = HTTP.DEF_CONTENT_CHARSET;
069        }
070        this.content = string.getBytes(charset);
071        if (contentType != null) {
072            setContentType(contentType.toString());
073        }
074    }
075
076    /**
077     * Creates a StringEntity with the specified content, MIME type and charset
078     *
079     * @param string content to be used. Not {@code null}.
080     * @param mimeType MIME type to be used. May be {@code null}, in which case the default
081     *   is {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain"
082     * @param charset character set to be used. May be {@code null}, in which case the default
083     *   is {@link HTTP#DEF_CONTENT_CHARSET} i.e. "ISO-8859-1"
084     * @throws  UnsupportedEncodingException If the named charset is not supported.
085     *
086     * @since 4.1
087     * @throws IllegalArgumentException if the string parameter is null
088     *
089     * @deprecated (4.1.3) use {@link #StringEntity(String, ContentType)}
090     */
091    @Deprecated
092    public StringEntity(
093            final String string, final String mimeType, final String charset) throws UnsupportedEncodingException {
094        super();
095        Args.notNull(string, "Source string");
096        final String mt = mimeType != null ? mimeType : HTTP.PLAIN_TEXT_TYPE;
097        final String cs = charset != null ? charset :HTTP.DEFAULT_CONTENT_CHARSET;
098        this.content = string.getBytes(cs);
099        setContentType(mt + HTTP.CHARSET_PARAM + cs);
100    }
101
102    /**
103     * Creates a StringEntity with the specified content and charset. The MIME type defaults
104     * to "text/plain".
105     *
106     * @param string content to be used. Not {@code null}.
107     * @param charset character set to be used. May be {@code null}, in which case the default
108     *   is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
109     *
110     * @throws IllegalArgumentException if the string parameter is null
111     * @throws UnsupportedCharsetException Thrown when the named charset is not available in
112     * this instance of the Java virtual machine
113     */
114    public StringEntity(final String string, final String charset)
115            throws UnsupportedCharsetException {
116        this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
117    }
118
119    /**
120     * Creates a StringEntity with the specified content and charset. The MIME type defaults
121     * to "text/plain".
122     *
123     * @param string content to be used. Not {@code null}.
124     * @param charset character set to be used. May be {@code null}, in which case the default
125     *   is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
126     *
127     * @throws IllegalArgumentException if the string parameter is null
128     *
129     * @since 4.2
130     */
131    public StringEntity(final String string, final Charset charset) {
132        this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
133    }
134
135    /**
136     * Creates a StringEntity with the specified content. The content type defaults to
137     * {@link ContentType#TEXT_PLAIN}.
138     *
139     * @param string content to be used. Not {@code null}.
140     *
141     * @throws IllegalArgumentException if the string parameter is null
142     * @throws UnsupportedEncodingException if the default HTTP charset is not supported.
143     */
144    public StringEntity(final String string)
145            throws UnsupportedEncodingException {
146        this(string, ContentType.DEFAULT_TEXT);
147    }
148
149    @Override
150    public boolean isRepeatable() {
151        return true;
152    }
153
154    @Override
155    public long getContentLength() {
156        return this.content.length;
157    }
158
159    @Override
160    public InputStream getContent() throws IOException {
161        return new ByteArrayInputStream(this.content);
162    }
163
164    @Override
165    public void writeTo(final OutputStream outstream) throws IOException {
166        Args.notNull(outstream, "Output stream");
167        outstream.write(this.content);
168        outstream.flush();
169    }
170
171    /**
172     * Tells that this entity is not streaming.
173     *
174     * @return {@code false}
175     */
176    @Override
177    public boolean isStreaming() {
178        return false;
179    }
180
181    @Override
182    public Object clone() throws CloneNotSupportedException {
183        return super.clone();
184    }
185
186} // class StringEntity