use of org.apache.commons.httpclient.methods.multipart.FilePart in project camel by apache.
the class HttpBridgeMultipartRouteTest method testHttpClient.
@Test
public void testHttpClient() throws Exception {
File jpg = new File("src/test/resources/java.jpg");
String body = "TEST";
Part[] parts = new Part[] { new StringPart("body", body), new FilePart(jpg.getName(), jpg) };
PostMethod method = new PostMethod("http://localhost:" + port2 + "/test/hello");
MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams());
method.setRequestEntity(requestEntity);
HttpClient client = new HttpClient();
client.executeMethod(method);
String responseBody = method.getResponseBodyAsString();
assertEquals(body, responseBody);
String numAttachments = method.getResponseHeader("numAttachments").getValue();
assertEquals(numAttachments, "2");
}
use of org.apache.commons.httpclient.methods.multipart.FilePart in project h2o-2 by h2oai.
the class WebAPI method importModel.
/**
* Imports a model from a JSON file.
*/
public static void importModel() throws Exception {
// Upload file to H2O
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(URL + "/Upload.json?key=" + JSON_FILE.getName());
Part[] parts = { new FilePart(JSON_FILE.getName(), JSON_FILE) };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
if (200 != client.executeMethod(post))
throw new RuntimeException("Request failed: " + post.getStatusLine());
post.releaseConnection();
// Parse the key into a model
GetMethod get = new GetMethod(//
URL + "/2/ImportModel.json?" + //
"destination_key=MyImportedNeuralNet&" + //
"type=NeuralNetModel&" + "json=" + JSON_FILE.getName());
if (200 != client.executeMethod(get))
throw new RuntimeException("Request failed: " + get.getStatusLine());
get.releaseConnection();
}
use of org.apache.commons.httpclient.methods.multipart.FilePart in project pinot by linkedin.
the class ServerSegmentCompletionProtocolHandler method segmentCommit.
public SegmentCompletionProtocol.Response segmentCommit(long offset, final String segmentName, final File segmentTarFile) throws FileNotFoundException {
SegmentCompletionProtocol.Request.Params params = new SegmentCompletionProtocol.Request.Params();
params.withInstanceId(_instance).withOffset(offset).withSegmentName(segmentName);
SegmentCompletionProtocol.SegmentCommitRequest request = new SegmentCompletionProtocol.SegmentCommitRequest(params);
final InputStream inputStream = new FileInputStream(segmentTarFile);
Part[] parts = { new FilePart(segmentName, new PartSource() {
@Override
public long getLength() {
return segmentTarFile.length();
}
@Override
public String getFileName() {
return "fileName";
}
@Override
public InputStream createInputStream() throws IOException {
return new BufferedInputStream(inputStream);
}
}) };
return doHttp(request, parts);
}
use of org.apache.commons.httpclient.methods.multipart.FilePart in project twitter-2-weibo by rjyo.
the class HttpClient method multPartURL.
public Response multPartURL(String fileParamName, String url, PostParameter[] params, File file, boolean authenticated) throws WeiboException {
PostMethod postMethod = new PostMethod(url);
try {
Part[] parts = null;
if (params == null) {
parts = new Part[1];
} else {
parts = new Part[params.length + 1];
}
if (params != null) {
int i = 0;
for (PostParameter entry : params) {
parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
}
}
FilePart filePart = new FilePart(fileParamName, file.getName(), file, new MimetypesFileTypeMap().getContentType(file), "UTF-8");
filePart.setTransferEncoding("binary");
parts[parts.length - 1] = filePart;
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
return httpRequest(postMethod);
} catch (Exception ex) {
throw new WeiboException(ex.getMessage(), ex, -1);
}
}
use of org.apache.commons.httpclient.methods.multipart.FilePart in project zm-mailbox by Zimbra.
the class TestFileUpload method testAdminUploadWithCsrfInFormField.
@Test
public void testAdminUploadWithCsrfInFormField() throws Exception {
SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
req.setCsrfSupported(true);
Element response = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
String authToken = authResp.getAuthToken();
String csrfToken = authResp.getCsrfToken();
int port = 7071;
try {
port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
} catch (ServiceException e) {
ZimbraLog.test.error("Unable to get admin SOAP port", e);
}
String Url = "https://localhost:" + port + ADMIN_UPLOAD_URL;
PostMethod post = new PostMethod(Url);
FilePart part = new FilePart(FILE_NAME, new ByteArrayPartSource(FILE_NAME, "some file content".getBytes()));
Part csrfPart = new StringPart("csrfToken", csrfToken);
String contentType = "application/x-msdownload";
part.setContentType(contentType);
HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
HttpState state = new HttpState();
state.addCookie(new org.apache.commons.httpclient.Cookie("localhost", ZimbraCookie.authTokenCookieName(true), authToken, "/", null, false));
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.setState(state);
post.setRequestEntity(new MultipartRequestEntity(new Part[] { part, csrfPart }, post.getParams()));
int statusCode = HttpClientUtil.executeMethod(client, post);
Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
String resp = post.getResponseBodyAsString();
Assert.assertNotNull("Response should not be empty", resp);
Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
Aggregations