use of org.apache.commons.httpclient.methods.PostMethod in project zm-mailbox by Zimbra.
the class TestFileUpload method postAndVerify.
private String postAndVerify(ZMailbox mbox, URI uri, boolean clearCookies, String requestId, String attContent) throws IOException {
HttpClient client = mbox.getHttpClient(uri);
if (clearCookies) {
client.getState().clearCookies();
}
List<Part> parts = new ArrayList<Part>();
parts.add(new StringPart("requestId", requestId));
if (attContent != null) {
parts.add(mbox.createAttachmentPart("test.txt", attContent.getBytes()));
}
PostMethod post = new PostMethod(uri.toString());
post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
int status = HttpClientUtil.executeMethod(client, post);
Assert.assertEquals(200, status);
String contentType = getHeaderValue(post, "Content-Type");
Assert.assertTrue(contentType, contentType.startsWith("text/html"));
String content = post.getResponseBodyAsString();
post.releaseConnection();
return content;
}
use of org.apache.commons.httpclient.methods.PostMethod 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));
}
use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class ClusterServiceServletImpl method ping.
@Override
public boolean ping(final String callingPeer) throws RemoteException {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Ping at " + _serviceUrl);
}
final HttpClient client = getHttpClient();
final PostMethod method = new PostMethod(_serviceUrl);
method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_PING));
method.addParameter("callingPeer", callingPeer);
final String returnVal = executePostMethod(client, method);
if ("true".equalsIgnoreCase(returnVal)) {
return true;
}
return false;
}
use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class ClusterServiceServletImpl method execute.
@Override
public String execute(final ClusterServicePdu pdu) throws RemoteException {
final HttpClient client = getHttpClient();
final PostMethod method = new PostMethod(_serviceUrl);
method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_DELIVER_PDU));
method.addParameter("sourcePeer", pdu.getSourcePeer());
method.addParameter("destPeer", pdu.getDestPeer());
method.addParameter("pduSeq", Long.toString(pdu.getSequenceId()));
method.addParameter("pduAckSeq", Long.toString(pdu.getAckSequenceId()));
method.addParameter("agentId", Long.toString(pdu.getAgentId()));
method.addParameter("gsonPackage", pdu.getJsonPackage());
method.addParameter("stopOnError", pdu.isStopOnError() ? "1" : "0");
method.addParameter("pduType", Integer.toString(pdu.getPduType()));
return executePostMethod(client, method);
}
use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class Action method executePost.
protected String executePost(final String uri, final StringRequestEntity entity) throws NeutronRestApiException {
try {
validateCredentials();
} catch (NeutronInvalidCredentialsException e) {
throw new NeutronRestApiException("Invalid credentials!", e);
}
NeutronRestFactory factory = NeutronRestFactory.getInstance();
NeutronRestApi neutronRestApi = factory.getNeutronApi(PostMethod.class);
PostMethod postMethod = (PostMethod) neutronRestApi.createMethod(url, uri);
try {
postMethod.setRequestHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);
postMethod.setRequestEntity(entity);
String encodedCredentials = encodeCredentials();
postMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
neutronRestApi.executeMethod(postMethod);
if (postMethod.getStatusCode() != HttpStatus.SC_CREATED) {
String errorMessage = responseToErrorMessage(postMethod);
postMethod.releaseConnection();
s_logger.error("Failed to create object : " + errorMessage);
throw new NeutronRestApiException("Failed to create object : " + errorMessage);
}
return postMethod.getResponseBodyAsString();
} catch (NeutronRestApiException e) {
s_logger.error("NeutronRestApiException caught while trying to execute HTTP Method on the Neutron Controller", e);
throw new NeutronRestApiException("API call to Neutron Controller Failed", e);
} catch (IOException e) {
throw new NeutronRestApiException("Failed to load json response body", e);
} finally {
postMethod.releaseConnection();
}
}
Aggregations