|
| 1 | +/* |
| 2 | + * Copyright 2010 Ning, Inc. |
| 3 | + * |
| 4 | + * Ning licenses this file to you under the Apache License, version 2.0 |
| 5 | + * (the "License"); you may not use this file except in compliance with the |
| 6 | + * License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package org.asynchttpclient.multipart; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.OutputStream; |
| 20 | + |
| 21 | +/** |
| 22 | + * This class is an adaptation of the Apache HttpClient implementation |
| 23 | + * |
| 24 | + * @link http://hc.apache.org/httpclient-3.x/ |
| 25 | + */ |
| 26 | +public abstract class AbstractFilePart extends PartBase { |
| 27 | + |
| 28 | + /** |
| 29 | + * Default content encoding of file attachments. |
| 30 | + */ |
| 31 | + public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream"; |
| 32 | + |
| 33 | + /** |
| 34 | + * Default charset of file attachments. |
| 35 | + */ |
| 36 | + public static final String DEFAULT_CHARSET = "ISO-8859-1"; |
| 37 | + |
| 38 | + /** |
| 39 | + * Default transfer encoding of file attachments. |
| 40 | + */ |
| 41 | + public static final String DEFAULT_TRANSFER_ENCODING = "binary"; |
| 42 | + |
| 43 | + /** |
| 44 | + * Attachment's file name |
| 45 | + */ |
| 46 | + protected static final String FILE_NAME = "; filename="; |
| 47 | + |
| 48 | + /** |
| 49 | + * Attachment's file name as a byte array |
| 50 | + */ |
| 51 | + private static final byte[] FILE_NAME_BYTES = MultipartEncodingUtil.getAsciiBytes(FILE_NAME); |
| 52 | + |
| 53 | + // FIXME used? |
| 54 | + private long _stalledTime = -1; |
| 55 | + |
| 56 | + /** |
| 57 | + * FilePart Constructor. |
| 58 | + * |
| 59 | + * @param name |
| 60 | + * the name for this part |
| 61 | + * @param partSource |
| 62 | + * the source for this part |
| 63 | + * @param contentType |
| 64 | + * the content type for this part, if <code>null</code> the {@link #DEFAULT_CONTENT_TYPE default} is used |
| 65 | + * @param charset |
| 66 | + * the charset encoding for this part, if <code>null</code> the {@link #DEFAULT_CHARSET default} is used |
| 67 | + * @param contentId |
| 68 | + */ |
| 69 | + public AbstractFilePart(String name, String contentType, String charset, String contentId) { |
| 70 | + super(name, contentType == null ? DEFAULT_CONTENT_TYPE : contentType, charset == null ? "ISO-8859-1" : charset, DEFAULT_TRANSFER_ENCODING, contentId); |
| 71 | + } |
| 72 | + |
| 73 | + public abstract String getFileName(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Write the disposition header to the output stream |
| 77 | + * |
| 78 | + * @param out |
| 79 | + * The output stream |
| 80 | + * @throws java.io.IOException |
| 81 | + * If an IO problem occurs |
| 82 | + */ |
| 83 | + protected void sendDispositionHeader(OutputStream out) throws IOException { |
| 84 | + super.sendDispositionHeader(out); |
| 85 | + String filename = getFileName(); |
| 86 | + if (filename != null) { |
| 87 | + out.write(FILE_NAME_BYTES); |
| 88 | + out.write(QUOTE_BYTES); |
| 89 | + out.write(MultipartEncodingUtil.getAsciiBytes(filename)); |
| 90 | + out.write(QUOTE_BYTES); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + protected long dispositionHeaderLength() { |
| 95 | + String filename = this.getFileName(); |
| 96 | + long length = super.dispositionHeaderLength(); |
| 97 | + if (filename != null) { |
| 98 | + length += FILE_NAME_BYTES.length; |
| 99 | + length += QUOTE_BYTES.length; |
| 100 | + length += MultipartEncodingUtil.getAsciiBytes(filename).length; |
| 101 | + length += QUOTE_BYTES.length; |
| 102 | + } |
| 103 | + return length; |
| 104 | + } |
| 105 | + |
| 106 | + public void setStalledTime(long ms) { |
| 107 | + _stalledTime = ms; |
| 108 | + } |
| 109 | + |
| 110 | + public long getStalledTime() { |
| 111 | + return _stalledTime; |
| 112 | + } |
| 113 | +} |
0 commit comments