Skip to content

Commit c6d8c55

Browse files
author
Stephane Landelle
committed
Rework Part API, first take
1 parent f59c78b commit c6d8c55

File tree

12 files changed

+328
-588
lines changed

12 files changed

+328
-588
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
public class ByteArrayPart extends AbstractFilePart {
22+
23+
private final byte[] bytes;
24+
private final String fileName;
25+
26+
public ByteArrayPart(String name, byte[] bytes) {
27+
this(name, bytes, null);
28+
}
29+
30+
public ByteArrayPart(String name, byte[] bytes, String contentType) {
31+
this(name, bytes, contentType, null);
32+
}
33+
34+
public ByteArrayPart(String name, byte[] bytes, String contentType, String charset) {
35+
this(name, bytes, contentType, charset, null);
36+
}
37+
38+
public ByteArrayPart(String name, byte[] bytes, String contentType, String charset, String fileName) {
39+
this(name, bytes, contentType, charset, fileName, null);
40+
}
41+
42+
public ByteArrayPart(String name, byte[] bytes, String contentType, String charset, String fileName, String contentId) {
43+
super(name, contentType, charset, contentId);
44+
if (bytes == null) {
45+
throw new NullPointerException("bytes");
46+
}
47+
this.bytes = bytes;
48+
this.fileName = fileName;
49+
}
50+
51+
@Override
52+
public String getFileName() {
53+
return fileName;
54+
}
55+
56+
@Override
57+
protected void sendData(OutputStream out) throws IOException {
58+
out.write(bytes);
59+
}
60+
61+
@Override
62+
protected long lengthOfData() {
63+
return bytes.length;
64+
}
65+
66+
public byte[] getBytes() {
67+
return bytes;
68+
}
69+
}

api/src/main/java/org/asynchttpclient/multipart/ByteArrayPartSource.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)