Example usage for org.apache.commons.httpclient.methods PostMethod setRequestEntity

List of usage examples for org.apache.commons.httpclient.methods PostMethod setRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod setRequestEntity.

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation10Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation10Xml";
    PostMethod method = new PostMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String xmlResponse = new String(responseBody);
    assertTrue(xmlResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1);
    assertTrue(xmlResponse.indexOf("<title>My Track 1</title>") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation10Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation10Json";
    PostMethod method = new PostMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"My Track 1\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation11Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation11Xml/21?page=31";
    PostMethod method = new PostMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String xmlResponse = new String(responseBody);
    assertTrue(xmlResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1);
    assertTrue(xmlResponse.indexOf("<title>postOperation11-21-31</title>") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation11Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation11Json/21?page=31";
    PostMethod method = new PostMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"postOperation11-21-31\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation12Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation12Xml";
    PostMethod method = new PostMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String xmlResponse = new String(responseBody);
    assertTrue(xmlResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1);
    assertTrue(xmlResponse.indexOf("<title>My Track 1</title>") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation13Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation13Json/artists/madonna/tracks/4";
    PostMethod method = new PostMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"madonna-4\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation12Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation12Json";
    PostMethod method = new PostMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"newRelease\":true") > -1);
    assertTrue(jsonResponse.indexOf("\"title\":\"My Track 1\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation2() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation2";
    PostMethod method = new PostMethod(url);
    String param = "this is string parameter!";
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);//  ww w.jav a 2s  .  c om
}

From source file:com.aimluck.eip.services.social.ALSocialApplicationHandler.java

public Map<String, ALGadgetSpec> getMetaData(List<String> specUrls, String view, boolean isDetail,
        boolean nocache) {
    Map<String, ALGadgetSpec> maps = new HashMap<String, ALGadgetSpec>();
    if (specUrls == null || specUrls.size() == 0) {
        return maps;
    }/*from w w  w . j  a  v a 2s . c o m*/
    try {

        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter("http.connection.timeout", 10000);
        httpClient.getParams().setParameter("http.socket.timeout", 10000);
        PostMethod postMethod = new PostMethod(getMetaDataUrl());
        postMethod.addRequestHeader("Content-Type", "application/javascript");
        postMethod.addParameter("st", "default:st");
        postMethod.addParameter("req", "1");
        postMethod.addParameter("callback", "1");
        JSONObject jsonObject = new JSONObject();
        JSONObject context = new JSONObject();
        context.put("country", "JP");
        context.put("language", "ja");
        context.put("view", view == null ? "home" : view);
        context.put("container", "default");
        context.put("nocache", nocache ? 1 : 0);
        if (isDetail) {
            context.put("withDescription", "1");
            context.put("withOAuthService", "1");
        }
        JSONArray gadgets = new JSONArray();
        for (String specUrl : specUrls) {
            JSONObject gadget = new JSONObject();
            gadget.put("url", specUrl);
            gadget.put("moduleId", 1);
            gadgets.add(gadget);
        }
        jsonObject.put("context", context);
        jsonObject.put("gadgets", gadgets);
        postMethod.setRequestEntity(
                new StringRequestEntity(jsonObject.toString(), "application/javascript", "UTF-8"));
        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        JSONObject fromObject = JSONObject.fromObject(result);
        JSONArray jsonArray = (JSONArray) fromObject.get("gadgets");
        Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
        classMap.put("oauthService", ALOAuthService.class);
        classMap.put("userPrefs", HashMap.class);

        if (jsonArray == null) {
            return maps;
        }

        int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            try {
                JSONObject object = jsonArray.getJSONObject(i);
                Object e = object.get("errors");
                if (e == null) {
                    ALGadgetSpec spec = (ALGadgetSpec) JSONObject.toBean(object, ALGadgetSpec.class, classMap);
                    maps.put(spec.getUrl(), spec);
                }
            } catch (Throwable t) {
                logger.warn("[ALSocialApplicationHandler]", t);
            }
        }
    } catch (Throwable t) {
        logger.warn("[ALSocialApplicationHandler]", t);
    }
    return maps;
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPostOperation4() throws Exception {
    String url = URL_RESOURCE1 + "/pathPostOperation4";
    PostMethod method = new PostMethod(url);
    String param = "this is string parameter!";
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String actualResponse = new String(responseBody);
    String expectedResponse = "this is string parameter!";
    assertEquals(expectedResponse, actualResponse);
}