use of org.apache.http.entity.mime.content.FileBody in project undertow by undertow-io.
the class MultipartFormDataParserTestCase method testFileUploadWithLargeFileSizeThreshold.
@Test
public void testFileUploadWithLargeFileSizeThreshold() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(10_000)));
TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
entity.addPart("file", new FileBody(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile())));
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String resp = HttpClientUtils.readResponse(result);
Map<String, String> parsedResponse = parse(resp);
Assert.assertEquals("true", parsedResponse.get("in_memory"));
Assert.assertEquals("uploadfile.txt", parsedResponse.get("file_name"));
Assert.assertEquals(DigestUtils.md5Hex(new FileInputStream(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile()))), parsedResponse.get("hash"));
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.entity.mime.content.FileBody in project undertow by undertow-io.
the class MultipartFormDataParserTestCase method testFileUploadWithEagerParsingAndNonASCIIFilename.
@Test
public void testFileUploadWithEagerParsingAndNonASCIIFilename() throws Exception {
DefaultServer.setRootHandler(new EagerFormParsingHandler().setNext(createHandler()));
TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
MultipartEntity entity = new MultipartEntity();
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
File uploadfile = new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile());
FormBodyPart filePart = new FormBodyPart("file", new FileBody(uploadfile, "τεστ", "application/octet-stream", Charsets.UTF_8.toString()));
filePart.addField("Content-Disposition", "form-data; name=\"file\"; filename*=\"utf-8''%CF%84%CE%B5%CF%83%CF%84.txt\"");
entity.addPart(filePart);
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.entity.mime.content.FileBody in project undertow by undertow-io.
the class MultipartFormDataParserTestCase method testFileUploadWithMediumFileSizeThresholdAndLargeFile.
@Test
public void testFileUploadWithMediumFileSizeThresholdAndLargeFile() throws Exception {
int fileSizeThreshold = 1000;
DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(fileSizeThreshold)));
TestHttpClient client = new TestHttpClient();
File file = new File("tmp_upload_file.txt");
file.createNewFile();
try {
writeLargeFileContent(file, fileSizeThreshold * 2);
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
entity.addPart("file", new FileBody(file));
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String resp = HttpClientUtils.readResponse(result);
Map<String, String> parsedResponse = parse(resp);
Assert.assertEquals("false", parsedResponse.get("in_memory"));
Assert.assertEquals("tmp_upload_file.txt", parsedResponse.get("file_name"));
Assert.assertEquals(DigestUtils.md5Hex(new FileInputStream(file)), parsedResponse.get("hash"));
} finally {
file.delete();
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.entity.mime.content.FileBody in project stanbol by apache.
the class ReasonersOfflineTest method testPostMultipartConsistency409.
@Test
public void testPostMultipartConsistency409() throws Exception {
FileBody bin = new FileBody(new File(URI.create(inconsistentFileName)));
MultipartEntity incMultiPart = new MultipartEntity();
incMultiPart.addPart(fileParam, bin);
String[] services = { "/owl", "/owlmini" };
// Not consistent
for (String s : services) {
executor.execute(buildMultipartRequest("/reasoners" + s + "/check", incMultiPart)).assertStatus(409);
}
}
use of org.apache.http.entity.mime.content.FileBody in project Talon-for-Twitter by klinker24.
the class TwitPicHelper method uploadToTwitPic.
private TwitPicStatus uploadToTwitPic() {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(POST_URL);
post.addHeader("X-Auth-Service-Provider", SERVICE_PROVIDER);
post.addHeader("X-Verify-Credentials-Authorization", getAuthrityHeader(twitter));
if (file == null) {
// only the input stream was sent, so we need to convert it to a file
Log.v("talon_twitpic", "converting to file from input stream");
String filePath = saveStreamTemp(stream);
file = new File(filePath);
} else {
Log.v("talon_twitpic", "already have the file, going right to send it");
}
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("key", new StringBody(TWITPIC_API_KEY));
entity.addPart("media", new FileBody(file));
entity.addPart("message", new StringBody(message));
Log.v("talon_twitpic", "uploading now");
post.setEntity(entity);
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
String url = "";
StringBuilder builder = new StringBuilder();
while ((line = rd.readLine()) != null) {
Log.v("talon_twitpic", line);
builder.append(line);
}
try {
// there is only going to be one thing returned ever
JSONObject jsonObject = new JSONObject(builder.toString());
url = jsonObject.getString("url");
} catch (Exception e) {
e.printStackTrace();
}
Log.v("talon_twitpic", "url: " + url);
Log.v("talon_twitpic", "message: " + message);
return new TwitPicStatus(message, url);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations