mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-20 00:47:27 +00:00
HTTPCLIENT-807: Added means to set mime type on default ContentBody implementations
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@709485 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e6a8741780
commit
997d0e0694
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* $HeadURL:$
|
||||||
|
* $Revision:$
|
||||||
|
* $Date:$
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.entity.mime.content;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.james.mime4j.message.AbstractBody;
|
||||||
|
|
||||||
|
public abstract class AbstractContentBody extends AbstractBody implements ContentBody {
|
||||||
|
|
||||||
|
private final String mimeType;
|
||||||
|
private final String mediaType;
|
||||||
|
private final String subType;
|
||||||
|
|
||||||
|
public AbstractContentBody(final String mimeType) {
|
||||||
|
super();
|
||||||
|
if (mimeType == null) {
|
||||||
|
throw new IllegalArgumentException("MIME type may not be null");
|
||||||
|
}
|
||||||
|
this.mimeType = mimeType;
|
||||||
|
int i = mimeType.indexOf('/');
|
||||||
|
if (i != -1) {
|
||||||
|
this.mediaType = mimeType.substring(0, i);
|
||||||
|
this.subType = mimeType.substring(i + 1);
|
||||||
|
} else {
|
||||||
|
this.mediaType = mimeType;
|
||||||
|
this.subType = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMimeType() {
|
||||||
|
return this.mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMediaType() {
|
||||||
|
return this.mediaType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSubType() {
|
||||||
|
return this.subType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<?, ?> getContentTypeParameters() {
|
||||||
|
return Collections.EMPTY_MAP;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -36,25 +36,26 @@
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.apache.http.entity.mime.MIME;
|
import org.apache.http.entity.mime.MIME;
|
||||||
import org.apache.james.mime4j.message.AbstractBody;
|
|
||||||
import org.apache.james.mime4j.message.BinaryBody;
|
import org.apache.james.mime4j.message.BinaryBody;
|
||||||
|
|
||||||
public class FileBody extends AbstractBody implements BinaryBody, ContentBody {
|
public class FileBody extends AbstractContentBody implements BinaryBody {
|
||||||
|
|
||||||
private final File file;
|
private final File file;
|
||||||
|
|
||||||
public FileBody(final File file) {
|
public FileBody(final File file, final String mimeType) {
|
||||||
super();
|
super(mimeType);
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
throw new IllegalArgumentException("File may not be null");
|
throw new IllegalArgumentException("File may not be null");
|
||||||
}
|
}
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FileBody(final File file) {
|
||||||
|
this(file, "application/octet-stream");
|
||||||
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() throws IOException {
|
public InputStream getInputStream() throws IOException {
|
||||||
return new FileInputStream(this.file);
|
return new FileInputStream(this.file);
|
||||||
}
|
}
|
||||||
@ -84,22 +85,6 @@ public String getCharset() {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMimeType() {
|
|
||||||
return "application/octet-stream";
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<?, ?> getContentTypeParameters() {
|
|
||||||
return Collections.EMPTY_MAP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMediaType() {
|
|
||||||
return "application";
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSubType() {
|
|
||||||
return "octet-stream";
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getContentLength() {
|
public long getContentLength() {
|
||||||
return this.file.length();
|
return this.file.length();
|
||||||
}
|
}
|
||||||
|
@ -34,20 +34,17 @@
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.apache.http.entity.mime.MIME;
|
import org.apache.http.entity.mime.MIME;
|
||||||
import org.apache.james.mime4j.message.AbstractBody;
|
|
||||||
import org.apache.james.mime4j.message.BinaryBody;
|
import org.apache.james.mime4j.message.BinaryBody;
|
||||||
|
|
||||||
public class InputStreamBody extends AbstractBody implements BinaryBody, ContentBody {
|
public class InputStreamBody extends AbstractContentBody implements BinaryBody {
|
||||||
|
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final String filename;
|
private final String filename;
|
||||||
|
|
||||||
public InputStreamBody(final InputStream in, final String filename) {
|
public InputStreamBody(final InputStream in, final String mimeType, final String filename) {
|
||||||
super();
|
super(mimeType);
|
||||||
if (in == null) {
|
if (in == null) {
|
||||||
throw new IllegalArgumentException("Input stream may not be null");
|
throw new IllegalArgumentException("Input stream may not be null");
|
||||||
}
|
}
|
||||||
@ -55,6 +52,10 @@ public InputStreamBody(final InputStream in, final String filename) {
|
|||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InputStreamBody(final InputStream in, final String filename) {
|
||||||
|
this(in, "application/octet-stream", filename);
|
||||||
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() throws IOException {
|
public InputStream getInputStream() throws IOException {
|
||||||
return this.in;
|
return this.in;
|
||||||
}
|
}
|
||||||
@ -83,22 +84,6 @@ public String getCharset() {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMimeType() {
|
|
||||||
return "application/octet-stream";
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<?, ?> getContentTypeParameters() {
|
|
||||||
return Collections.EMPTY_MAP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMediaType() {
|
|
||||||
return "application";
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSubType() {
|
|
||||||
return "octet-stream";
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getContentLength() {
|
public long getContentLength() {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -43,16 +43,18 @@
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.http.entity.mime.MIME;
|
import org.apache.http.entity.mime.MIME;
|
||||||
import org.apache.james.mime4j.message.AbstractBody;
|
|
||||||
import org.apache.james.mime4j.message.TextBody;
|
import org.apache.james.mime4j.message.TextBody;
|
||||||
|
|
||||||
public class StringBody extends AbstractBody implements TextBody, ContentBody {
|
public class StringBody extends AbstractContentBody implements TextBody {
|
||||||
|
|
||||||
private final byte[] content;
|
private final byte[] content;
|
||||||
private final Charset charset;
|
private final Charset charset;
|
||||||
|
|
||||||
public StringBody(final String text, Charset charset) throws UnsupportedEncodingException {
|
public StringBody(
|
||||||
super();
|
final String text,
|
||||||
|
final String mimeType,
|
||||||
|
Charset charset) throws UnsupportedEncodingException {
|
||||||
|
super(mimeType);
|
||||||
if (text == null) {
|
if (text == null) {
|
||||||
throw new IllegalArgumentException("Text may not be null");
|
throw new IllegalArgumentException("Text may not be null");
|
||||||
}
|
}
|
||||||
@ -63,8 +65,12 @@ public StringBody(final String text, Charset charset) throws UnsupportedEncoding
|
|||||||
this.charset = charset;
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StringBody(final String text, Charset charset) throws UnsupportedEncodingException {
|
||||||
|
this(text, "text/plain", charset);
|
||||||
|
}
|
||||||
|
|
||||||
public StringBody(final String text) throws UnsupportedEncodingException {
|
public StringBody(final String text) throws UnsupportedEncodingException {
|
||||||
this(text, null);
|
this(text, "text/plain", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Reader getReader() throws IOException {
|
public Reader getReader() throws IOException {
|
||||||
@ -94,18 +100,7 @@ public String getCharset() {
|
|||||||
return this.charset.name();
|
return this.charset.name();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMimeType() {
|
@Override
|
||||||
return "text/plain";
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMediaType() {
|
|
||||||
return "text";
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSubType() {
|
|
||||||
return "plain";
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<?, ?> getContentTypeParameters() {
|
public Map<?, ?> getContentTypeParameters() {
|
||||||
Map<Object, Object> map = new HashMap<Object, Object>();
|
Map<Object, Object> map = new HashMap<Object, Object>();
|
||||||
map.put("charset", this.charset.name());
|
map.put("charset", this.charset.name());
|
||||||
|
@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
* $HeadURL:$
|
||||||
|
* $Revision:$
|
||||||
|
* $Date:$
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.entity.mime;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.apache.http.entity.mime.content.InputStreamBody;
|
||||||
|
import org.apache.http.entity.mime.content.StringBody;
|
||||||
|
|
||||||
|
public class TestMultipartContentBody extends TestCase {
|
||||||
|
|
||||||
|
// ------------------------------------------------------------ Constructor
|
||||||
|
public TestMultipartContentBody(final String testName) throws IOException {
|
||||||
|
super(testName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------- Main
|
||||||
|
public static void main(String args[]) {
|
||||||
|
String[] testCaseName = { TestMultipartContentBody.class.getName() };
|
||||||
|
junit.textui.TestRunner.main(testCaseName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------- TestCase Methods
|
||||||
|
|
||||||
|
public static Test suite() {
|
||||||
|
return new TestSuite(TestMultipartContentBody.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testStringBody() throws Exception {
|
||||||
|
StringBody b1 = new StringBody("text");
|
||||||
|
assertEquals(4, b1.getContentLength());
|
||||||
|
|
||||||
|
Charset defCharset = Charset.defaultCharset();
|
||||||
|
|
||||||
|
assertEquals(defCharset.name(), b1.getCharset());
|
||||||
|
assertEquals(defCharset.name(), b1.getContentTypeParameters().get("charset"));
|
||||||
|
|
||||||
|
assertNull(b1.getFilename());
|
||||||
|
assertEquals("text/plain", b1.getMimeType());
|
||||||
|
assertEquals("text", b1.getMediaType());
|
||||||
|
assertEquals("plain", b1.getSubType());
|
||||||
|
|
||||||
|
assertEquals(MIME.ENC_8BIT, b1.getTransferEncoding());
|
||||||
|
|
||||||
|
StringBody b2 = new StringBody("more text", "text/other", MIME.DEFAULT_CHARSET);
|
||||||
|
assertEquals(9, b2.getContentLength());
|
||||||
|
assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getCharset());
|
||||||
|
assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getContentTypeParameters().get("charset"));
|
||||||
|
|
||||||
|
assertNull(b2.getFilename());
|
||||||
|
assertEquals("text/other", b2.getMimeType());
|
||||||
|
assertEquals("text", b2.getMediaType());
|
||||||
|
assertEquals("other", b2.getSubType());
|
||||||
|
|
||||||
|
assertEquals(MIME.ENC_8BIT, b2.getTransferEncoding());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInputStreamBody() throws Exception {
|
||||||
|
byte[] stuff = "Stuff".getBytes("US-ASCII");
|
||||||
|
InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
|
||||||
|
assertEquals(-1, b1.getContentLength());
|
||||||
|
|
||||||
|
assertNull(b1.getCharset());
|
||||||
|
assertEquals("stuff", b1.getFilename());
|
||||||
|
assertEquals("application/octet-stream", b1.getMimeType());
|
||||||
|
assertEquals("application", b1.getMediaType());
|
||||||
|
assertEquals("octet-stream", b1.getSubType());
|
||||||
|
|
||||||
|
assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
|
||||||
|
|
||||||
|
InputStreamBody b2 = new InputStreamBody(
|
||||||
|
new ByteArrayInputStream(stuff), "some/stuff", "stuff");
|
||||||
|
assertEquals(-1, b2.getContentLength());
|
||||||
|
assertNull(b2.getCharset());
|
||||||
|
assertEquals("stuff", b2.getFilename());
|
||||||
|
assertEquals("some/stuff", b2.getMimeType());
|
||||||
|
assertEquals("some", b2.getMediaType());
|
||||||
|
assertEquals("stuff", b2.getSubType());
|
||||||
|
|
||||||
|
assertEquals(MIME.ENC_BINARY, b2.getTransferEncoding());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user