Skip to content

Commit b28d338

Browse files
committed
Fixed errors from unit test.
1 parent 6f58cf2 commit b28d338

File tree

4 files changed

+150
-70
lines changed

4 files changed

+150
-70
lines changed

src/main/java/com/ning/http/multipart/MultipartBody.java

Lines changed: 142 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import com.ning.http.client.RandomAccessBody;
55

6+
import java.io.ByteArrayInputStream;
67
import java.io.ByteArrayOutputStream;
78
import java.io.File;
9+
import java.io.FileNotFoundException;
810
import java.io.IOException;
911
import java.io.RandomAccessFile;
1012
import java.nio.ByteBuffer;
@@ -49,78 +51,18 @@ public long transferTo(long position, long count, WritableByteChannel target)
4951
return overallLength;
5052
}
5153

52-
long availableLength = count;
53-
5454
int tempPart = _startPart;
55-
long totalLength = 0;
56-
boolean full = false;
57-
58-
while(!full && tempPart < _parts.size()) {
59-
Part currentPart = (Part) _parts.get(tempPart);
60-
61-
currentPart.setPartBoundary(_boundary);
6255

63-
long length = currentPart.length();
64-
65-
if((length + totalLength) < availableLength ) {
66-
totalLength += length;
67-
tempPart++;
68-
69-
if(currentPart.getClass().equals(StringPart.class)) {
70-
71-
ByteArrayOutputStream outputStream =
72-
new ByteArrayOutputStream();
73-
74-
Part.sendPart(outputStream, currentPart, _boundary);
75-
76-
overallLength += writeToTarget(target, outputStream);
77-
}
78-
else if(currentPart.getClass().equals(FilePart.class)) {
79-
80-
FilePart filePart = (FilePart)currentPart;
81-
82-
ByteArrayOutputStream overhead =
83-
new ByteArrayOutputStream();
84-
85-
filePart.setPartBoundary(_boundary);
86-
87-
filePart.sendStart(overhead);
88-
filePart.sendDispositionHeader(overhead);
89-
filePart.sendContentTypeHeader(overhead);
90-
filePart.sendTransferEncodingHeader(overhead);
91-
filePart.sendEndOfHeader(overhead);
92-
93-
overallLength += writeToTarget(target, overhead);
94-
95-
FilePartSource source = (FilePartSource)filePart.getSource();
96-
97-
File file = source.getFile();
98-
99-
RandomAccessFile raf = new RandomAccessFile(file, "r");
100-
_files.add(raf);
101-
102-
FileChannel fc = raf.getChannel();
103-
104-
105-
long fileLength = fc.transferTo(0, file.length(), target);
106-
107-
if(fileLength != file.length()) {
108-
System.out.println("Did not complete file.");
109-
}
110-
111-
ByteArrayOutputStream endOverhead =
112-
new ByteArrayOutputStream();
113-
114-
filePart.sendEnd(endOverhead);
115-
116-
overallLength += this.writeToTarget(target, endOverhead);
117-
}
56+
for(com.ning.http.client.Part part : _parts) {
57+
if(part instanceof Part) {
58+
overallLength += handleMultiPart(target, (Part)part);
11859
}
11960
else {
120-
full = true;
61+
overallLength += handleClientPart(target, part);
12162
}
122-
}
12363

64+
tempPart++;
65+
}
12466
ByteArrayOutputStream endWriter =
12567
new ByteArrayOutputStream();
12668

@@ -133,6 +75,140 @@ else if(currentPart.getClass().equals(FilePart.class)) {
13375
return overallLength;
13476
}
13577

78+
private long handleClientPart(
79+
WritableByteChannel target, com.ning.http.client.Part part) throws IOException {
80+
81+
if(part.getClass().equals(com.ning.http.client.StringPart.class)) {
82+
com.ning.http.client.StringPart stringPart = (com.ning.http.client.StringPart)part;
83+
84+
StringPart currentPart = new StringPart(stringPart.getName(), stringPart.getValue());
85+
86+
return handleStringPart(target,currentPart);
87+
}
88+
else if(part.getClass().equals(com.ning.http.client.FilePart.class)) {
89+
com.ning.http.client.FilePart currentPart = (com.ning.http.client.FilePart)part;
90+
91+
FilePart filePart = new FilePart(currentPart.getName(), currentPart.getFile());
92+
93+
return handleFilePart(target, filePart);
94+
}
95+
else if(part.getClass().equals(com.ning.http.client.ByteArrayPart.class)) {
96+
com.ning.http.client.ByteArrayPart bytePart = (com.ning.http.client.ByteArrayPart)part;
97+
98+
ByteArrayPartSource source = new ByteArrayPartSource(bytePart.getFileName(), bytePart.getData());
99+
100+
FilePart filePart = new FilePart(bytePart.getName(), source, bytePart.getMimeType(), bytePart.getCharSet());
101+
102+
return handleByteArrayPart(target, filePart, bytePart.getData());
103+
}
104+
105+
return 0;
106+
}
107+
108+
private long handleByteArrayPart(WritableByteChannel target,
109+
FilePart filePart, byte[] data) throws IOException {
110+
111+
int length = 0;
112+
113+
//length += handleFileHeaders(target, filePart);
114+
115+
ByteArrayOutputStream output = new ByteArrayOutputStream();
116+
117+
Part.sendPart(output, filePart, _boundary);
118+
119+
length += writeToTarget(target, output);
120+
121+
//length += handleFileEnd(target, filePart);
122+
123+
return length;
124+
125+
}
126+
127+
private long handleFileEnd(WritableByteChannel target, FilePart filePart)
128+
throws IOException {
129+
130+
ByteArrayOutputStream endOverhead =
131+
new ByteArrayOutputStream();
132+
133+
filePart.sendEnd(endOverhead);
134+
135+
return this.writeToTarget(target, endOverhead);
136+
}
137+
138+
private long handleFileHeaders(WritableByteChannel target,
139+
FilePart filePart) throws IOException {
140+
filePart.setPartBoundary(_boundary);
141+
142+
ByteArrayOutputStream overhead = new ByteArrayOutputStream();
143+
144+
filePart.setPartBoundary(_boundary);
145+
146+
filePart.sendStart(overhead);
147+
filePart.sendDispositionHeader(overhead);
148+
filePart.sendContentTypeHeader(overhead);
149+
filePart.sendTransferEncodingHeader(overhead);
150+
filePart.sendEndOfHeader(overhead);
151+
152+
return writeToTarget(target, overhead);
153+
}
154+
155+
private long handleFilePart(WritableByteChannel target, FilePart filePart)
156+
throws IOException, FileNotFoundException {
157+
158+
int length = 0;
159+
160+
length += handleFileHeaders(target, filePart);
161+
162+
FilePartSource source = (FilePartSource)filePart.getSource();
163+
164+
File file = source.getFile();
165+
166+
RandomAccessFile raf = new RandomAccessFile(file, "r");
167+
_files.add(raf);
168+
169+
FileChannel fc = raf.getChannel();
170+
171+
172+
long fileLength = fc.transferTo(0, file.length(), target);
173+
174+
if(fileLength != file.length()) {
175+
System.out.println("Did not complete file.");
176+
}
177+
178+
length += handleFileEnd(target, filePart);
179+
180+
return length;
181+
}
182+
183+
private long handleStringPart(WritableByteChannel target, StringPart currentPart)
184+
throws IOException {
185+
186+
currentPart.setPartBoundary(_boundary);
187+
188+
ByteArrayOutputStream outputStream =
189+
new ByteArrayOutputStream();
190+
191+
Part.sendPart(outputStream, currentPart, _boundary);
192+
193+
return writeToTarget(target, outputStream);
194+
}
195+
196+
private long handleMultiPart(WritableByteChannel target, Part currentPart)
197+
throws IOException, FileNotFoundException {
198+
199+
currentPart.setPartBoundary(_boundary);
200+
201+
if(currentPart.getClass().equals(StringPart.class)) {
202+
return handleStringPart(target, (StringPart)currentPart);
203+
}
204+
else if(currentPart.getClass().equals(FilePart.class)) {
205+
FilePart filePart = (FilePart)currentPart;
206+
207+
return handleFilePart(target, filePart);
208+
}
209+
return 0;
210+
}
211+
136212
private long writeToTarget(
137213
WritableByteChannel target, ByteArrayOutputStream byteWriter)
138214
throws IOException {

src/test/java/com/ning/http/client/async/AuthTimeoutTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import javax.servlet.http.HttpServletResponse;
3939
import java.io.IOException;
4040
import java.io.OutputStream;
41+
import java.util.Arrays;
4142
import java.util.HashSet;
4243
import java.util.Set;
4344
import java.util.concurrent.Future;
@@ -88,7 +89,8 @@ public void setUpServer(String auth)
8889
knownRoles.add(admin);
8990

9091
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
91-
security.setConstraintMappings(new ConstraintMapping[]{mapping}, knownRoles);
92+
93+
security.setConstraintMappings(Arrays.asList(new ConstraintMapping[]{mapping}), knownRoles);
9294
security.setAuthenticator(new BasicAuthenticator());
9395
security.setLoginService(loginService);
9496
security.setStrict(false);

src/test/java/com/ning/http/client/async/BasicAuthTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.io.FileInputStream;
5454
import java.io.IOException;
5555
import java.net.URL;
56+
import java.util.Arrays;
5657
import java.util.HashSet;
5758
import java.util.Set;
5859
import java.util.concurrent.ExecutionException;
@@ -106,7 +107,7 @@ public void setUpGlobal() throws Exception {
106107
knownRoles.add(admin);
107108

108109
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
109-
security.setConstraintMappings(new ConstraintMapping[]{mapping}, knownRoles);
110+
security.setConstraintMappings(Arrays.asList(new ConstraintMapping[]{mapping}), knownRoles);
110111
security.setAuthenticator(new BasicAuthenticator());
111112
security.setLoginService(loginService);
112113
security.setStrict(false);
@@ -181,7 +182,7 @@ public void handle(String arg0, Request arg1, HttpServletRequest arg2, HttpServl
181182
super.handle(arg0, arg1, arg2, arg3);
182183
}
183184
};
184-
security.setConstraintMappings(new ConstraintMapping[]{mapping}, knownRoles);
185+
security.setConstraintMappings(Arrays.asList(new ConstraintMapping[]{mapping}), knownRoles);
185186
security.setAuthenticator(new DigestAuthenticator());
186187
security.setLoginService(loginService);
187188
security.setStrict(true);

src/test/java/com/ning/http/client/async/DigestAuthTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import javax.servlet.http.HttpServletRequest;
3838
import javax.servlet.http.HttpServletResponse;
3939
import java.io.IOException;
40+
import java.util.Arrays;
4041
import java.util.HashSet;
4142
import java.util.Set;
4243
import java.util.concurrent.ExecutionException;
@@ -86,7 +87,7 @@ public void setUpGlobal() throws Exception {
8687
knownRoles.add(admin);
8788

8889
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
89-
security.setConstraintMappings(new ConstraintMapping[]{mapping}, knownRoles);
90+
security.setConstraintMappings(Arrays.asList(new ConstraintMapping[]{mapping}), knownRoles);
9091
security.setAuthenticator(new DigestAuthenticator());
9192
security.setLoginService(loginService);
9293
security.setStrict(false);

0 commit comments

Comments
 (0)