diff options
Diffstat (limited to 'mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime')
20 files changed, 2014 insertions, 0 deletions
diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/AbstractMultipartForm.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/AbstractMultipartForm.java new file mode 100644 index 000000000..ade6bfe4b --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/AbstractMultipartForm.java @@ -0,0 +1,211 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.util.List; + +import ch.boye.httpclientandroidlib.entity.mime.content.ContentBody; +import ch.boye.httpclientandroidlib.util.Args; +import ch.boye.httpclientandroidlib.util.ByteArrayBuffer; + +/** + * HttpMultipart represents a collection of MIME multipart encoded content bodies. This class is + * capable of operating either in the strict (RFC 822, RFC 2045, RFC 2046 compliant) or + * the browser compatible modes. + * + * @since 4.3 + */ +abstract class AbstractMultipartForm { + + private static ByteArrayBuffer encode( + final Charset charset, final String string) { + final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); + final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); + bab.append(encoded.array(), encoded.position(), encoded.remaining()); + return bab; + } + + private static void writeBytes( + final ByteArrayBuffer b, final OutputStream out) throws IOException { + out.write(b.buffer(), 0, b.length()); + } + + private static void writeBytes( + final String s, final Charset charset, final OutputStream out) throws IOException { + final ByteArrayBuffer b = encode(charset, s); + writeBytes(b, out); + } + + private static void writeBytes( + final String s, final OutputStream out) throws IOException { + final ByteArrayBuffer b = encode(MIME.DEFAULT_CHARSET, s); + writeBytes(b, out); + } + + protected static void writeField( + final MinimalField field, final OutputStream out) throws IOException { + writeBytes(field.getName(), out); + writeBytes(FIELD_SEP, out); + writeBytes(field.getBody(), out); + writeBytes(CR_LF, out); + } + + protected static void writeField( + final MinimalField field, final Charset charset, final OutputStream out) throws IOException { + writeBytes(field.getName(), charset, out); + writeBytes(FIELD_SEP, out); + writeBytes(field.getBody(), charset, out); + writeBytes(CR_LF, out); + } + + private static final ByteArrayBuffer FIELD_SEP = encode(MIME.DEFAULT_CHARSET, ": "); + private static final ByteArrayBuffer CR_LF = encode(MIME.DEFAULT_CHARSET, "\r\n"); + private static final ByteArrayBuffer TWO_DASHES = encode(MIME.DEFAULT_CHARSET, "--"); + + private final String subType; + protected final Charset charset; + private final String boundary; + + /** + * Creates an instance with the specified settings. + * + * @param subType MIME subtype - must not be {@code null} + * @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used. + * @param boundary to use - must not be {@code null} + * @throws IllegalArgumentException if charset is null or boundary is null + */ + public AbstractMultipartForm(final String subType, final Charset charset, final String boundary) { + super(); + Args.notNull(subType, "Multipart subtype"); + Args.notNull(boundary, "Multipart boundary"); + this.subType = subType; + this.charset = charset != null ? charset : MIME.DEFAULT_CHARSET; + this.boundary = boundary; + } + + public AbstractMultipartForm(final String subType, final String boundary) { + this(subType, null, boundary); + } + + public String getSubType() { + return this.subType; + } + + public Charset getCharset() { + return this.charset; + } + + public abstract List<FormBodyPart> getBodyParts(); + + public String getBoundary() { + return this.boundary; + } + + void doWriteTo( + final OutputStream out, + final boolean writeContent) throws IOException { + + final ByteArrayBuffer boundary = encode(this.charset, getBoundary()); + for (final FormBodyPart part: getBodyParts()) { + writeBytes(TWO_DASHES, out); + writeBytes(boundary, out); + writeBytes(CR_LF, out); + + formatMultipartHeader(part, out); + + writeBytes(CR_LF, out); + + if (writeContent) { + part.getBody().writeTo(out); + } + writeBytes(CR_LF, out); + } + writeBytes(TWO_DASHES, out); + writeBytes(boundary, out); + writeBytes(TWO_DASHES, out); + writeBytes(CR_LF, out); + } + + /** + * Write the multipart header fields; depends on the style. + */ + protected abstract void formatMultipartHeader( + final FormBodyPart part, + final OutputStream out) throws IOException; + + /** + * Writes out the content in the multipart/form encoding. This method + * produces slightly different formatting depending on its compatibility + * mode. + */ + public void writeTo(final OutputStream out) throws IOException { + doWriteTo(out, true); + } + + /** + * Determines the total length of the multipart content (content length of + * individual parts plus that of extra elements required to delimit the parts + * from one another). If any of the @{link BodyPart}s contained in this object + * is of a streaming entity of unknown length the total length is also unknown. + * <p/> + * This method buffers only a small amount of data in order to determine the + * total length of the entire entity. The content of individual parts is not + * buffered. + * + * @return total length of the multipart entity if known, <code>-1</code> + * otherwise. + */ + public long getTotalLength() { + long contentLen = 0; + for (final FormBodyPart part: getBodyParts()) { + final ContentBody body = part.getBody(); + final long len = body.getContentLength(); + if (len >= 0) { + contentLen += len; + } else { + return -1; + } + } + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + doWriteTo(out, false); + final byte[] extra = out.toByteArray(); + return contentLen + extra.length; + } catch (final IOException ex) { + // Should never happen + return -1; + } + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/FormBodyPart.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/FormBodyPart.java new file mode 100644 index 000000000..a3d2c0954 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/FormBodyPart.java @@ -0,0 +1,116 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.mime.content.AbstractContentBody; +import ch.boye.httpclientandroidlib.entity.mime.content.ContentBody; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * FormBodyPart class represents a content body that can be used as a part of multipart encoded + * entities. This class automatically populates the header with standard fields based on + * the content description of the enclosed body. + * + * @since 4.0 + */ +public class FormBodyPart { + + private final String name; + private final Header header; + + private final ContentBody body; + + public FormBodyPart(final String name, final ContentBody body) { + super(); + Args.notNull(name, "Name"); + Args.notNull(body, "Body"); + this.name = name; + this.body = body; + this.header = new Header(); + + generateContentDisp(body); + generateContentType(body); + generateTransferEncoding(body); + } + + public String getName() { + return this.name; + } + + public ContentBody getBody() { + return this.body; + } + + public Header getHeader() { + return this.header; + } + + public void addField(final String name, final String value) { + Args.notNull(name, "Field name"); + this.header.addField(new MinimalField(name, value)); + } + + protected void generateContentDisp(final ContentBody body) { + final StringBuilder buffer = new StringBuilder(); + buffer.append("form-data; name=\""); + buffer.append(getName()); + buffer.append("\""); + if (body.getFilename() != null) { + buffer.append("; filename=\""); + buffer.append(body.getFilename()); + buffer.append("\""); + } + addField(MIME.CONTENT_DISPOSITION, buffer.toString()); + } + + protected void generateContentType(final ContentBody body) { + final ContentType contentType; + if (body instanceof AbstractContentBody) { + contentType = ((AbstractContentBody) body).getContentType(); + } else { + contentType = null; + } + if (contentType != null) { + addField(MIME.CONTENT_TYPE, contentType.toString()); + } else { + final StringBuilder buffer = new StringBuilder(); + buffer.append(body.getMimeType()); // MimeType cannot be null + if (body.getCharset() != null) { // charset may legitimately be null + buffer.append("; charset="); + buffer.append(body.getCharset()); + } + addField(MIME.CONTENT_TYPE, buffer.toString()); + } + } + + protected void generateTransferEncoding(final ContentBody body) { + addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()); // TE cannot be null + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/Header.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/Header.java new file mode 100644 index 000000000..15d70acc9 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/Header.java @@ -0,0 +1,144 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + * The header of an entity (see RFC 2045). + */ +public class Header implements Iterable<MinimalField> { + + private final List<MinimalField> fields; + private final Map<String, List<MinimalField>> fieldMap; + + public Header() { + super(); + this.fields = new LinkedList<MinimalField>(); + this.fieldMap = new HashMap<String, List<MinimalField>>(); + } + + public void addField(final MinimalField field) { + if (field == null) { + return; + } + final String key = field.getName().toLowerCase(Locale.ENGLISH); + List<MinimalField> values = this.fieldMap.get(key); + if (values == null) { + values = new LinkedList<MinimalField>(); + this.fieldMap.put(key, values); + } + values.add(field); + this.fields.add(field); + } + + public List<MinimalField> getFields() { + return new ArrayList<MinimalField>(this.fields); + } + + public MinimalField getField(final String name) { + if (name == null) { + return null; + } + final String key = name.toLowerCase(Locale.ENGLISH); + final List<MinimalField> list = this.fieldMap.get(key); + if (list != null && !list.isEmpty()) { + return list.get(0); + } + return null; + } + + public List<MinimalField> getFields(final String name) { + if (name == null) { + return null; + } + final String key = name.toLowerCase(Locale.ENGLISH); + final List<MinimalField> list = this.fieldMap.get(key); + if (list == null || list.isEmpty()) { + return Collections.emptyList(); + } else { + return new ArrayList<MinimalField>(list); + } + } + + public int removeFields(final String name) { + if (name == null) { + return 0; + } + final String key = name.toLowerCase(Locale.ENGLISH); + final List<MinimalField> removed = fieldMap.remove(key); + if (removed == null || removed.isEmpty()) { + return 0; + } + this.fields.removeAll(removed); + return removed.size(); + } + + public void setField(final MinimalField field) { + if (field == null) { + return; + } + final String key = field.getName().toLowerCase(Locale.ENGLISH); + final List<MinimalField> list = fieldMap.get(key); + if (list == null || list.isEmpty()) { + addField(field); + return; + } + list.clear(); + list.add(field); + int firstOccurrence = -1; + int index = 0; + for (final Iterator<MinimalField> it = this.fields.iterator(); it.hasNext(); index++) { + final MinimalField f = it.next(); + if (f.getName().equalsIgnoreCase(field.getName())) { + it.remove(); + if (firstOccurrence == -1) { + firstOccurrence = index; + } + } + } + this.fields.add(firstOccurrence, field); + } + + public Iterator<MinimalField> iterator() { + return Collections.unmodifiableList(fields).iterator(); + } + + @Override + public String toString() { + return this.fields.toString(); + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpBrowserCompatibleMultipart.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpBrowserCompatibleMultipart.java new file mode 100644 index 000000000..dfc0a657e --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpBrowserCompatibleMultipart.java @@ -0,0 +1,79 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.List; + +/** + * HttpBrowserCompatibleMultipart represents a collection of MIME multipart encoded + * content bodies. This class is emulates browser compatibility, e.g. IE 5 or earlier. + * + * @since 4.3 + */ +class HttpBrowserCompatibleMultipart extends AbstractMultipartForm { + + private final List<FormBodyPart> parts; + + public HttpBrowserCompatibleMultipart( + final String subType, + final Charset charset, + final String boundary, + final List<FormBodyPart> parts) { + super(subType, charset, boundary); + this.parts = parts; + } + + @Override + public List<FormBodyPart> getBodyParts() { + return this.parts; + } + + /** + * Write the multipart header fields; depends on the style. + */ + @Override + protected void formatMultipartHeader( + final FormBodyPart part, + final OutputStream out) throws IOException { + // For browser-compatible, only write Content-Disposition + // Use content charset + final Header header = part.getHeader(); + final MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION); + writeField(cd, this.charset, out); + final String filename = part.getBody().getFilename(); + if (filename != null) { + final MinimalField ct = header.getField(MIME.CONTENT_TYPE); + writeField(ct, this.charset, out); + } + + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpMultipartMode.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpMultipartMode.java new file mode 100644 index 000000000..ab73e4983 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpMultipartMode.java @@ -0,0 +1,43 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +/** + * + * @since 4.0 + */ +public enum HttpMultipartMode { + + /** RFC 822, RFC 2045, RFC 2046 compliant */ + STRICT, + /** browser-compatible mode, i.e. only write Content-Disposition; use content charset */ + BROWSER_COMPATIBLE, + /** RFC 6532 compliant */ + RFC6532 + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpRFC6532Multipart.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpRFC6532Multipart.java new file mode 100644 index 000000000..634a12b23 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpRFC6532Multipart.java @@ -0,0 +1,72 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.List; + +/** + * HttpRFC6532Multipart represents a collection of MIME multipart encoded content bodies, + * implementing the strict (RFC 822, RFC 2045, RFC 2046 compliant) interpretation + * of the spec, with the exception of allowing UTF-8 headers, as per RFC6532. + * + * @since 4.3 + */ +class HttpRFC6532Multipart extends AbstractMultipartForm { + + private final List<FormBodyPart> parts; + + public HttpRFC6532Multipart( + final String subType, + final Charset charset, + final String boundary, + final List<FormBodyPart> parts) { + super(subType, charset, boundary); + this.parts = parts; + } + + @Override + public List<FormBodyPart> getBodyParts() { + return this.parts; + } + + @Override + protected void formatMultipartHeader( + final FormBodyPart part, + final OutputStream out) throws IOException { + + // For RFC6532, we output all fields with UTF-8 encoding. + final Header header = part.getHeader(); + for (final MinimalField field: header) { + writeField(field, MIME.UTF8_CHARSET, out); + } + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpStrictMultipart.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpStrictMultipart.java new file mode 100644 index 000000000..d6e68c6e2 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/HttpStrictMultipart.java @@ -0,0 +1,72 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.List; + +/** + * HttpStrictMultipart represents a collection of MIME multipart encoded content bodies, + * implementing the strict (RFC 822, RFC 2045, RFC 2046 compliant) interpretation + * of the spec. + * + * @since 4.3 + */ +class HttpStrictMultipart extends AbstractMultipartForm { + + private final List<FormBodyPart> parts; + + public HttpStrictMultipart( + final String subType, + final Charset charset, + final String boundary, + final List<FormBodyPart> parts) { + super(subType, charset, boundary); + this.parts = parts; + } + + @Override + public List<FormBodyPart> getBodyParts() { + return this.parts; + } + + @Override + protected void formatMultipartHeader( + final FormBodyPart part, + final OutputStream out) throws IOException { + + // For strict, we output all fields with MIME-standard encoding. + final Header header = part.getHeader(); + for (final MinimalField field: header) { + writeField(field, out); + } + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MIME.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MIME.java new file mode 100644 index 000000000..d5896312d --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MIME.java @@ -0,0 +1,53 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import ch.boye.httpclientandroidlib.Consts; + +import java.nio.charset.Charset; + +/** + * + * @since 4.0 + */ +public final class MIME { + + public static final String CONTENT_TYPE = "Content-Type"; + public static final String CONTENT_TRANSFER_ENC = "Content-Transfer-Encoding"; + public static final String CONTENT_DISPOSITION = "Content-Disposition"; + + public static final String ENC_8BIT = "8bit"; + public static final String ENC_BINARY = "binary"; + + /** The default character set to be used, i.e. "US-ASCII" */ + public static final Charset DEFAULT_CHARSET = Consts.ASCII; + + /** UTF-8 is used for RFC6532 */ + public static final Charset UTF8_CHARSET = Consts.UTF_8; + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MinimalField.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MinimalField.java new file mode 100644 index 000000000..35af51233 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MinimalField.java @@ -0,0 +1,63 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +/** + * Minimal MIME field. + * + * @since 4.0 + */ +public class MinimalField { + + private final String name; + private final String value; + + public MinimalField(final String name, final String value) { + super(); + this.name = name; + this.value = value; + } + + public String getName() { + return this.name; + } + + public String getBody() { + return this.value; + } + + @Override + public String toString() { + final StringBuilder buffer = new StringBuilder(); + buffer.append(this.name); + buffer.append(": "); + buffer.append(this.value); + return buffer.toString(); + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MultipartEntityBuilder.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MultipartEntityBuilder.java new file mode 100644 index 000000000..5074001d7 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MultipartEntityBuilder.java @@ -0,0 +1,207 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import java.io.File; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import ch.boye.httpclientandroidlib.HttpEntity; +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.mime.content.ByteArrayBody; +import ch.boye.httpclientandroidlib.entity.mime.content.ContentBody; +import ch.boye.httpclientandroidlib.entity.mime.content.FileBody; +import ch.boye.httpclientandroidlib.entity.mime.content.InputStreamBody; +import ch.boye.httpclientandroidlib.entity.mime.content.StringBody; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * Builder for multipart {@link HttpEntity}s. + * + * @since 4.3 + */ +public class MultipartEntityBuilder { + + /** + * The pool of ASCII chars to be used for generating a multipart boundary. + */ + private final static char[] MULTIPART_CHARS = + "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + .toCharArray(); + + private final static String DEFAULT_SUBTYPE = "form-data"; + + private String subType = DEFAULT_SUBTYPE; + private HttpMultipartMode mode = HttpMultipartMode.STRICT; + private String boundary = null; + private Charset charset = null; + private List<FormBodyPart> bodyParts = null; + + public static MultipartEntityBuilder create() { + return new MultipartEntityBuilder(); + } + + MultipartEntityBuilder() { + super(); + } + + public MultipartEntityBuilder setMode(final HttpMultipartMode mode) { + this.mode = mode; + return this; + } + + public MultipartEntityBuilder setLaxMode() { + this.mode = HttpMultipartMode.BROWSER_COMPATIBLE; + return this; + } + + public MultipartEntityBuilder setStrictMode() { + this.mode = HttpMultipartMode.STRICT; + return this; + } + + public MultipartEntityBuilder setBoundary(final String boundary) { + this.boundary = boundary; + return this; + } + + public MultipartEntityBuilder setCharset(final Charset charset) { + this.charset = charset; + return this; + } + + MultipartEntityBuilder addPart(final FormBodyPart bodyPart) { + if (bodyPart == null) { + return this; + } + if (this.bodyParts == null) { + this.bodyParts = new ArrayList<FormBodyPart>(); + } + this.bodyParts.add(bodyPart); + return this; + } + + public MultipartEntityBuilder addPart(final String name, final ContentBody contentBody) { + Args.notNull(name, "Name"); + Args.notNull(contentBody, "Content body"); + return addPart(new FormBodyPart(name, contentBody)); + } + + public MultipartEntityBuilder addTextBody( + final String name, final String text, final ContentType contentType) { + return addPart(name, new StringBody(text, contentType)); + } + + public MultipartEntityBuilder addTextBody( + final String name, final String text) { + return addTextBody(name, text, ContentType.DEFAULT_TEXT); + } + + public MultipartEntityBuilder addBinaryBody( + final String name, final byte[] b, final ContentType contentType, final String filename) { + return addPart(name, new ByteArrayBody(b, contentType, filename)); + } + + public MultipartEntityBuilder addBinaryBody( + final String name, final byte[] b) { + return addBinaryBody(name, b, ContentType.DEFAULT_BINARY, null); + } + + public MultipartEntityBuilder addBinaryBody( + final String name, final File file, final ContentType contentType, final String filename) { + return addPart(name, new FileBody(file, contentType, filename)); + } + + public MultipartEntityBuilder addBinaryBody( + final String name, final File file) { + return addBinaryBody(name, file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null); + } + + public MultipartEntityBuilder addBinaryBody( + final String name, final InputStream stream, final ContentType contentType, + final String filename) { + return addPart(name, new InputStreamBody(stream, contentType, filename)); + } + + public MultipartEntityBuilder addBinaryBody(final String name, final InputStream stream) { + return addBinaryBody(name, stream, ContentType.DEFAULT_BINARY, null); + } + + private String generateContentType( + final String boundary, + final Charset charset) { + final StringBuilder buffer = new StringBuilder(); + buffer.append("multipart/form-data; boundary="); + buffer.append(boundary); + if (charset != null) { + buffer.append("; charset="); + buffer.append(charset.name()); + } + return buffer.toString(); + } + + private String generateBoundary() { + final StringBuilder buffer = new StringBuilder(); + final Random rand = new Random(); + final int count = rand.nextInt(11) + 30; // a random size from 30 to 40 + for (int i = 0; i < count; i++) { + buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]); + } + return buffer.toString(); + } + + MultipartFormEntity buildEntity() { + final String st = subType != null ? subType : DEFAULT_SUBTYPE; + final Charset cs = charset; + final String b = boundary != null ? boundary : generateBoundary(); + final List<FormBodyPart> bps = bodyParts != null ? new ArrayList<FormBodyPart>(bodyParts) : + Collections.<FormBodyPart>emptyList(); + final HttpMultipartMode m = mode != null ? mode : HttpMultipartMode.STRICT; + final AbstractMultipartForm form; + switch (m) { + case BROWSER_COMPATIBLE: + form = new HttpBrowserCompatibleMultipart(st, cs, b, bps); + break; + case RFC6532: + form = new HttpRFC6532Multipart(st, cs, b, bps); + break; + default: + form = new HttpStrictMultipart(st, cs, b, bps); + } + return new MultipartFormEntity(form, generateContentType(b, cs), form.getTotalLength()); + } + + public HttpEntity build() { + return buildEntity(); + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MultipartFormEntity.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MultipartFormEntity.java new file mode 100644 index 000000000..9da2f4724 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/MultipartFormEntity.java @@ -0,0 +1,100 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime; + +import ch.boye.httpclientandroidlib.Header; +import ch.boye.httpclientandroidlib.HttpEntity; +import ch.boye.httpclientandroidlib.message.BasicHeader; +import ch.boye.httpclientandroidlib.protocol.HTTP; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +class MultipartFormEntity implements HttpEntity { + + private final AbstractMultipartForm multipart; + private final Header contentType; + private final long contentLength; + + MultipartFormEntity( + final AbstractMultipartForm multipart, + final String contentType, + final long contentLength) { + super(); + this.multipart = multipart; + this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType); + this.contentLength = contentLength; + } + + AbstractMultipartForm getMultipart() { + return this.multipart; + } + + public boolean isRepeatable() { + return this.contentLength != -1; + } + + public boolean isChunked() { + return !isRepeatable(); + } + + public boolean isStreaming() { + return !isRepeatable(); + } + + public long getContentLength() { + return this.contentLength; + } + + public Header getContentType() { + return this.contentType; + } + + public Header getContentEncoding() { + return null; + } + + public void consumeContent() + throws IOException, UnsupportedOperationException{ + if (isStreaming()) { + throw new UnsupportedOperationException( + "Streaming entity does not implement #consumeContent()"); + } + } + + public InputStream getContent() throws IOException { + throw new UnsupportedOperationException( + "Multipart form entity does not implement #getContent()"); + } + + public void writeTo(final OutputStream outstream) throws IOException { + this.multipart.writeTo(outstream); + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/AbstractContentBody.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/AbstractContentBody.java new file mode 100644 index 000000000..b6b58ad35 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/AbstractContentBody.java @@ -0,0 +1,96 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime.content; + +import java.nio.charset.Charset; + +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * + * @since 4.0 + */ +public abstract class AbstractContentBody implements ContentBody { + + private final ContentType contentType; + + /** + * @since 4.3 + */ + public AbstractContentBody(final ContentType contentType) { + super(); + Args.notNull(contentType, "Content type"); + this.contentType = contentType; + } + + /** + * @deprecated (4.3) use {@link AbstractContentBody#AbstractContentBody(ContentType)} + */ + @Deprecated + public AbstractContentBody(final String mimeType) { + this(ContentType.parse(mimeType)); + } + + /** + * @since 4.3 + */ + public ContentType getContentType() { + return this.contentType; + } + + public String getMimeType() { + return this.contentType.getMimeType(); + } + + public String getMediaType() { + final String mimeType = this.contentType.getMimeType(); + final int i = mimeType.indexOf('/'); + if (i != -1) { + return mimeType.substring(0, i); + } else { + return mimeType; + } + } + + public String getSubType() { + final String mimeType = this.contentType.getMimeType(); + final int i = mimeType.indexOf('/'); + if (i != -1) { + return mimeType.substring(i + 1); + } else { + return null; + } + } + + public String getCharset() { + final Charset charset = this.contentType.getCharset(); + return charset != null ? charset.name() : null; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ByteArrayBody.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ByteArrayBody.java new file mode 100644 index 000000000..ea60fa047 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ByteArrayBody.java @@ -0,0 +1,111 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime.content; + +import java.io.IOException; +import java.io.OutputStream; + +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.mime.MIME; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * Binary body part backed by a byte array. + * + * @see ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder + * + * @since 4.1 + */ +public class ByteArrayBody extends AbstractContentBody { + + /** + * The contents of the file contained in this part. + */ + private final byte[] data; + + /** + * The name of the file contained in this part. + */ + private final String filename; + + /** + * Creates a new ByteArrayBody. + * + * @param data The contents of the file contained in this part. + * @param mimeType The MIME type of the file contained in this part. + * @param filename The name of the file contained in this part. + * + * @deprecated (4.3) use {@link ByteArrayBody#ByteArrayBody(byte[], ContentType, String)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public ByteArrayBody(final byte[] data, final String mimeType, final String filename) { + this(data, ContentType.create(mimeType), filename); + } + + /** + * @since 4.3 + */ + public ByteArrayBody(final byte[] data, final ContentType contentType, final String filename) { + super(contentType); + Args.notNull(data, "byte[]"); + this.data = data; + this.filename = filename; + } + + /** + * Creates a new ByteArrayBody. + * + * @param data The contents of the file contained in this part. + * @param filename The name of the file contained in this part. + */ + public ByteArrayBody(final byte[] data, final String filename) { + this(data, "application/octet-stream", filename); + } + + public String getFilename() { + return filename; + } + + public void writeTo(final OutputStream out) throws IOException { + out.write(data); + } + + @Override + public String getCharset() { + return null; + } + + public String getTransferEncoding() { + return MIME.ENC_BINARY; + } + + public long getContentLength() { + return data.length; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ContentBody.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ContentBody.java new file mode 100644 index 000000000..d62aaff54 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ContentBody.java @@ -0,0 +1,43 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime.content; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * + * @since 4.0 + */ +public interface ContentBody extends ContentDescriptor { + + String getFilename(); + + void writeTo(OutputStream out) throws IOException; + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ContentDescriptor.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ContentDescriptor.java new file mode 100644 index 000000000..87237ec23 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/ContentDescriptor.java @@ -0,0 +1,89 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime.content; + +/** + * Represents common content properties. + */ +public interface ContentDescriptor { + + /** + * Returns the body descriptors MIME type. + * @see #getMediaType() + * @see #getSubType() + * @return The MIME type, which has been parsed from the + * content-type definition. Must not be null, but + * "text/plain", if no content-type was specified. + */ + String getMimeType(); + + /** + * Gets the defaulted MIME media type for this content. + * For example <code>TEXT</code>, <code>IMAGE</code>, <code>MULTIPART</code> + * @see #getMimeType() + * @return the MIME media type when content-type specified, + * otherwise the correct default (<code>TEXT</code>) + */ + String getMediaType(); + + /** + * Gets the defaulted MIME sub type for this content. + * @see #getMimeType() + * @return the MIME media type when content-type is specified, + * otherwise the correct default (<code>PLAIN</code>) + */ + String getSubType(); + + /** + * <p>The body descriptors character set, defaulted appropriately for the MIME type.</p> + * <p> + * For <code>TEXT</code> types, this will be defaulted to <code>us-ascii</code>. + * For other types, when the charset parameter is missing this property will be null. + * </p> + * @return Character set, which has been parsed from the + * content-type definition. Not null for <code>TEXT</code> types, when unset will + * be set to default <code>us-ascii</code>. For other types, when unset, + * null will be returned. + */ + String getCharset(); + + /** + * Returns the body descriptors transfer encoding. + * @return The transfer encoding. Must not be null, but "7bit", + * if no transfer-encoding was specified. + */ + String getTransferEncoding(); + + /** + * Returns the body descriptors content-length. + * @return Content length, if known, or -1, to indicate the absence of a + * content-length header. + */ + long getContentLength(); + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/FileBody.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/FileBody.java new file mode 100644 index 000000000..7641236a4 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/FileBody.java @@ -0,0 +1,144 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime.content; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.mime.MIME; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * Binary body part backed by a file. + * + * @see ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder + * + * @since 4.0 + */ +public class FileBody extends AbstractContentBody { + + private final File file; + private final String filename; + + /** + * @since 4.1 + * + * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType, String)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public FileBody(final File file, + final String filename, + final String mimeType, + final String charset) { + this(file, ContentType.create(mimeType, charset), filename); + } + + /** + * @since 4.1 + * + * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public FileBody(final File file, + final String mimeType, + final String charset) { + this(file, null, mimeType, charset); + } + + /** + * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public FileBody(final File file, final String mimeType) { + this(file, ContentType.create(mimeType), null); + } + + public FileBody(final File file) { + this(file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null); + } + + /** + * @since 4.3 + */ + public FileBody(final File file, final ContentType contentType, final String filename) { + super(contentType); + Args.notNull(file, "File"); + this.file = file; + this.filename = filename; + } + + /** + * @since 4.3 + */ + public FileBody(final File file, final ContentType contentType) { + this(file, contentType, null); + } + + public InputStream getInputStream() throws IOException { + return new FileInputStream(this.file); + } + + public void writeTo(final OutputStream out) throws IOException { + Args.notNull(out, "Output stream"); + final InputStream in = new FileInputStream(this.file); + try { + final byte[] tmp = new byte[4096]; + int l; + while ((l = in.read(tmp)) != -1) { + out.write(tmp, 0, l); + } + out.flush(); + } finally { + in.close(); + } + } + + public String getTransferEncoding() { + return MIME.ENC_BINARY; + } + + public long getContentLength() { + return this.file.length(); + } + + public String getFilename() { + return filename; + } + + public File getFile() { + return this.file; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/InputStreamBody.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/InputStreamBody.java new file mode 100644 index 000000000..267a78ea5 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/InputStreamBody.java @@ -0,0 +1,112 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime.content; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.mime.MIME; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * Binary body part backed by an input stream. + * + * @see ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder + * + * @since 4.0 + */ +public class InputStreamBody extends AbstractContentBody { + + private final InputStream in; + private final String filename; + + /** + * @since 4.1 + * + * @deprecated (4.3) use {@link InputStreamBody#InputStreamBody(InputStream, ContentType, + * String)} or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public InputStreamBody(final InputStream in, final String mimeType, final String filename) { + this(in, ContentType.create(mimeType), filename); + } + + public InputStreamBody(final InputStream in, final String filename) { + this(in, ContentType.DEFAULT_BINARY, filename); + } + + /** + * @since 4.3 + */ + public InputStreamBody(final InputStream in, final ContentType contentType, final String filename) { + super(contentType); + Args.notNull(in, "Input stream"); + this.in = in; + this.filename = filename; + } + + /** + * @since 4.3 + */ + public InputStreamBody(final InputStream in, final ContentType contentType) { + this(in, contentType, null); + } + + public InputStream getInputStream() { + return this.in; + } + + public void writeTo(final OutputStream out) throws IOException { + Args.notNull(out, "Output stream"); + try { + final byte[] tmp = new byte[4096]; + int l; + while ((l = this.in.read(tmp)) != -1) { + out.write(tmp, 0, l); + } + out.flush(); + } finally { + this.in.close(); + } + } + + public String getTransferEncoding() { + return MIME.ENC_BINARY; + } + + public long getContentLength() { + return -1; + } + + public String getFilename() { + return this.filename; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/StringBody.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/StringBody.java new file mode 100644 index 000000000..c5ad5632d --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/StringBody.java @@ -0,0 +1,197 @@ +/* + * ==================================================================== + * 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 ch.boye.httpclientandroidlib.entity.mime.content; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; + +import ch.boye.httpclientandroidlib.Consts; +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.mime.MIME; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * Text body part backed by a byte array. + * + * @see ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder + * + * @since 4.0 + */ +public class StringBody extends AbstractContentBody { + + private final byte[] content; + + /** + * @since 4.1 + * + * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public static StringBody create( + final String text, + final String mimeType, + final Charset charset) throws IllegalArgumentException { + try { + return new StringBody(text, mimeType, charset); + } catch (final UnsupportedEncodingException ex) { + throw new IllegalArgumentException("Charset " + charset + " is not supported", ex); + } + } + + /** + * @since 4.1 + * + * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public static StringBody create( + final String text, final Charset charset) throws IllegalArgumentException { + return create(text, null, charset); + } + + /** + * @since 4.1 + * + * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public static StringBody create(final String text) throws IllegalArgumentException { + return create(text, null, null); + } + + /** + * Create a StringBody from the specified text, MIME type and character set. + * + * @param text to be used for the body, not {@code null} + * @param mimeType the MIME type, not {@code null} + * @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used + * @throws UnsupportedEncodingException + * @throws IllegalArgumentException if the {@code text} parameter is null + * + * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public StringBody( + final String text, + final String mimeType, + final Charset charset) throws UnsupportedEncodingException { + this(text, ContentType.create(mimeType, charset)); + } + + /** + * Create a StringBody from the specified text and character set. + * The MIME type is set to "text/plain". + * + * @param text to be used for the body, not {@code null} + * @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used + * @throws UnsupportedEncodingException + * @throws IllegalArgumentException if the {@code text} parameter is null + * + * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public StringBody(final String text, final Charset charset) throws UnsupportedEncodingException { + this(text, "text/plain", charset); + } + + /** + * Create a StringBody from the specified text. + * The MIME type is set to "text/plain". + * The {@linkplain Consts#ASCII ASCII} charset is used. + * + * @param text to be used for the body, not {@code null} + * @throws UnsupportedEncodingException + * @throws IllegalArgumentException if the {@code text} parameter is null + * + * @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)} + * or {@link ch.boye.httpclientandroidlib.entity.mime.MultipartEntityBuilder} + */ + @Deprecated + public StringBody(final String text) throws UnsupportedEncodingException { + this(text, "text/plain", Consts.ASCII); + } + + /** + * @since 4.3 + */ + public StringBody(final String text, final ContentType contentType) { + super(contentType); + Args.notNull(text, "Text"); + final Charset charset = contentType.getCharset(); + final String csname = charset != null ? charset.name() : Consts.ASCII.name(); + try { + this.content = text.getBytes(csname); + } catch (final UnsupportedEncodingException ex) { + // Should never happen + throw new UnsupportedCharsetException(csname); + } + } + + public Reader getReader() { + final Charset charset = getContentType().getCharset(); + return new InputStreamReader( + new ByteArrayInputStream(this.content), + charset != null ? charset : Consts.ASCII); + } + + public void writeTo(final OutputStream out) throws IOException { + Args.notNull(out, "Output stream"); + final InputStream in = new ByteArrayInputStream(this.content); + final byte[] tmp = new byte[4096]; + int l; + while ((l = in.read(tmp)) != -1) { + out.write(tmp, 0, l); + } + out.flush(); + } + + public String getTransferEncoding() { + return MIME.ENC_8BIT; + } + + public long getContentLength() { + return this.content.length; + } + + public String getFilename() { + return null; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/package-info.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/package-info.java new file mode 100644 index 000000000..ff5e2ed0d --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/content/package-info.java @@ -0,0 +1,31 @@ +/* + * ==================================================================== + * 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/>. + * + */ + +/** + * MIME body part implementations. + */ +package ch.boye.httpclientandroidlib.entity.mime.content; diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/package-info.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/package-info.java new file mode 100644 index 000000000..0ef01f9ed --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/mime/package-info.java @@ -0,0 +1,31 @@ +/* + * ==================================================================== + * 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/>. + * + */ + +/** + * MIME coded HTTP entity implementations. + */ +package ch.boye.httpclientandroidlib.entity.mime; |