use of org.apache.http.entity.mime.MultipartEntityBuilder in project zm-mailbox by Zimbra.
the class TestFileUpload method testAdminUploadWithCsrfInHeader.
@Test
public void testAdminUploadWithCsrfInHeader() 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;
HttpPost post = new HttpPost(Url);
String contentType = "application/x-msdownload";
HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
BasicCookieStore state = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.authTokenCookieName(true), authToken);
cookie.setDomain("localhost");
cookie.setPath("/");
cookie.setSecure(false);
state.addCookie(cookie);
clientBuilder.setDefaultCookieStore(state);
RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
clientBuilder.setDefaultRequestConfig(reqConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody(FILE_NAME, "some file content".getBytes(), ContentType.create(contentType), FILE_NAME);
HttpEntity httpEntity = builder.build();
post.setEntity(httpEntity);
HttpClient client = clientBuilder.build();
post.addHeader(Constants.CSRF_TOKEN, csrfToken);
HttpResponse httpResponse = HttpClientUtil.executeMethod(client, post);
int statusCode = httpResponse.getStatusLine().getStatusCode();
Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
String resp = EntityUtils.toString(httpResponse.getEntity());
Assert.assertNotNull("Response should not be empty", resp);
Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
use of org.apache.http.entity.mime.MultipartEntityBuilder in project zm-mailbox by Zimbra.
the class TestFileUpload method testMissingCsrfAdminUpload.
@Test
public void testMissingCsrfAdminUpload() 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();
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;
HttpPost post = new HttpPost(Url);
String contentType = "application/x-msdownload";
HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
BasicCookieStore state = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.authTokenCookieName(true), authToken);
cookie.setDomain("localhost");
cookie.setPath("/");
cookie.setSecure(false);
state.addCookie(cookie);
clientBuilder.setDefaultCookieStore(state);
RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
clientBuilder.setDefaultRequestConfig(reqConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody(FILE_NAME, "some file content".getBytes(), ContentType.create(contentType), FILE_NAME);
HttpEntity httpEntity = builder.build();
post.setEntity(httpEntity);
HttpClient client = clientBuilder.build();
HttpResponse httpResponse = HttpClientUtil.executeMethod(client, post);
int statusCode = httpResponse.getStatusLine().getStatusCode();
Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
String resp = EntityUtils.toString(httpResponse.getEntity());
Assert.assertNotNull("Response should not be empty", resp);
Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
use of org.apache.http.entity.mime.MultipartEntityBuilder in project ats-framework by Axway.
the class HttpClient method constructRequestBody.
private void constructRequestBody() {
// we have work to do here only when using multipart body
if (requestBodyParts.size() > 0) {
try {
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
for (HttpBodyPart part : requestBodyParts) {
entityBuilder.addPart(part.getName(), part.constructContentBody());
}
requestBody = entityBuilder.build();
} catch (Exception e) {
throw new HttpException("Exception trying to create a multipart message.", e);
}
}
}
use of org.apache.http.entity.mime.MultipartEntityBuilder in project vft-capture by videofirst.
the class DefaultUploadService method getHttpPost.
/**
* Prepare HTTP Post upload call
*/
private HttpPost getHttpPost(Capture capture) {
// Get capture file and data file
File videoFile = validateExists(capture.getVideoFile());
File dataFile = validateExists(capture.getDataFile());
HttpPost httpPost = new HttpPost(uploadConfig.getUrl());
uploadConfig.getHeaders().entrySet().stream().forEach(e -> httpPost.setHeader(e.getKey(), e.getValue()));
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody(PARAM_VIDEO, videoFile, ContentType.DEFAULT_BINARY, videoFile.getName());
builder.addBinaryBody(PARAM_DATA, dataFile, ContentType.DEFAULT_BINARY, dataFile.getName());
HttpEntity multipart = builder.build();
ProgressListener pListener = new VideoUploadProgressListener(captureDao, capture, DAO_UPDATE_INTERVAL_MILLIS);
httpPost.setEntity(new ProgressEntityWrapper(multipart, pListener));
return httpPost;
}
use of org.apache.http.entity.mime.MultipartEntityBuilder in project vft-capture by videofirst.
the class MockUploadController method main.
/**
* Manually hit an end-point.
*/
public static void main(String[] args) throws IOException {
String dir = "src/test/resources/videos/acme/moon-rocket/ui/bob-feature/dave-scenario/2018-02-15_12-14-02_n3jwzb/";
File videoFile = new File(dir + "2018-02-15_12-14-02_n3jwzb.avi");
File dataFile = new File(dir + "2018-02-15_12-14-02_n3jwzb.json");
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:1357/mock-upload");
httpPost.setHeader("Authorization", "Basic dGVzdDpwYXNzd29yZA==");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("video", videoFile, ContentType.DEFAULT_BINARY, videoFile.getName());
builder.addBinaryBody("data", dataFile, ContentType.DEFAULT_BINARY, dataFile.getName());
HttpEntity multipart = builder.build();
ProgressListener pListener = (transferredBytes, totalBytes) -> System.out.println("transferredBytes [ " + transferredBytes + " ] , totalBytes [ " + totalBytes + " ] ");
httpPost.setEntity(new ProgressEntityWrapper(multipart, pListener));
System.out.println("Posting");
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
client.close();
System.out.println("Done");
}
Aggregations