Search in sources :

Example 71 with PostMethod

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;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)

Example 72 with PostMethod

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));
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HeaderElement(org.apache.commons.httpclient.HeaderElement) Element(com.zimbra.common.soap.Element) HttpState(org.apache.commons.httpclient.HttpState) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) ServiceException(com.zimbra.common.service.ServiceException) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Example 73 with PostMethod

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;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 74 with PostMethod

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);
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 75 with PostMethod

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();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) IOException(java.io.IOException)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11