use of org.apache.commons.httpclient.methods.multipart.FilePart in project zm-mailbox by Zimbra.
the class ZMailbox method uploadAttachments.
/* ------------------------------------------------- */
/**
* Uploads files to <tt>FileUploadServlet</tt>.
* @return the attachment id
*/
public String uploadAttachments(File[] files, int msTimeout) throws ServiceException {
Part[] parts = new Part[files.length];
for (int i = 0; i < files.length; i++) {
File file = files[i];
String contentType = URLConnection.getFileNameMap().getContentTypeFor(file.getName());
try {
parts[i] = new FilePart(file.getName(), file, contentType, "UTF-8");
} catch (IOException e) {
throw ZClientException.IO_ERROR(e.getMessage(), e);
}
}
return uploadAttachments(parts, msTimeout);
}
use of org.apache.commons.httpclient.methods.multipart.FilePart in project spatial-portal by AtlasOfLivingAustralia.
the class SandboxPasteController method upload.
private boolean upload(byte[] bytes, String filename, String contentType) throws Exception {
//create tmp file
File tmp = File.createTempFile("pointsUpload", "_" + filename);
FileUtils.writeByteArrayToFile(tmp, bytes);
String url = CommonData.getSettings().getProperty("sandbox.url") + "upload/uploadFile";
HttpClient httpClient = new HttpClient();
PostMethod filePost = new PostMethod(url);
Part[] parts = { new FilePart("myFile", filename, tmp, contentType, null) };
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
int status = httpClient.executeMethod(filePost);
if (status == 302) {
String responseText = filePost.getResponseHeader("Location").getValue();
uploadId = responseText.substring(responseText.indexOf("preview/") + "preview/".length(), responseText.lastIndexOf('?'));
uploadFn = responseText.substring(responseText.indexOf('?') + 4);
System.out.println(responseText);
return true;
}
return false;
}
use of org.apache.commons.httpclient.methods.multipart.FilePart in project zm-mailbox by Zimbra.
the class TestDeployZimlet method adminUpload.
public String adminUpload(String authToken, String fileName, String filePath) throws Exception {
PostMethod post = new PostMethod(ADMIN_UPLOAD_URL);
FilePart part = new FilePart(fileName, new FilePartSource(new File(filePath)));
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(localServer.getServiceHostname(), ZimbraCookie.authTokenCookieName(true), authToken, "/", null, false));
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.setState(state);
post.setRequestEntity(new MultipartRequestEntity(new Part[] { part }, post.getParams()));
int statusCode = HttpClientUtil.executeMethod(client, post);
assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
String resp = post.getResponseBodyAsString();
assertNotNull("Response should not be empty", resp);
ZimbraLog.test.debug("Upload response " + resp);
String[] responseParts = resp.split(",", 3);
String aid = null;
if (responseParts.length == 3) {
aid = responseParts[2].trim();
if (aid.startsWith("'") || aid.startsWith("\"")) {
aid = aid.substring(1);
}
if (aid.endsWith("'") || aid.endsWith("\"")) {
aid = aid.substring(0, aid.length() - 1);
}
}
return aid;
}
use of org.apache.commons.httpclient.methods.multipart.FilePart 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;
PostMethod post = new PostMethod(Url);
FilePart part = new FilePart(FILE_NAME, new ByteArrayPartSource(FILE_NAME, "some file content".getBytes()));
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 }, post.getParams()));
post.addRequestHeader(Constants.CSRF_TOKEN, csrfToken);
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));
}
use of org.apache.commons.httpclient.methods.multipart.FilePart 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;
PostMethod post = new PostMethod(Url);
FilePart part = new FilePart(FILE_NAME, new ByteArrayPartSource(FILE_NAME, "some file content".getBytes()));
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 }, 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