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

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

Introduction

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

Prototype

public PostMethod(String paramString) 

Source Link

Usage

From source file:fr.msch.wissl.server.TestEdition.java

public void test() throws Exception {
    HttpClient client = new HttpClient();
    RuntimeStats rt = new RuntimeStats();
    rt.songCount.set(15);//from   w ww  .  j ava  2s  .c o m
    rt.albumCount.set(5);
    rt.artistCount.set(2);
    rt.userCount.set(2);
    rt.playlistCount.set(0);
    rt.playtime.set(15);
    rt.downloaded.set(0);
    this.addMusicFolder("src/test/resources/data", rt);

    // 401: requires admin
    PostMethod post = new PostMethod(URL + "edit/artist");
    post.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(post);
    assertEquals(401, post.getStatusCode());
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(post);
    assertEquals(401, post.getStatusCode());
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(post);
    assertEquals(401, post.getStatusCode());

    // 404: unknown param
    post = new PostMethod(URL + "edit/artist");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("artist_ids[]", "99999");
    client.executeMethod(post);
    assertEquals(404, post.getStatusCode());
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("album_ids[]", "99999");
    client.executeMethod(post);
    assertEquals(404, post.getStatusCode());
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("song_ids[]", "99999");
    client.executeMethod(post);
    assertEquals(404, post.getStatusCode());

    // get artist id for 'Foo'
    GetMethod get = new GetMethod(URL + "search/foo");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    JSONObject obj = new JSONObject(get.getResponseBodyAsString());
    int foo_id = obj.getJSONArray("artists").getJSONObject(0).getInt("id");
    int bob_id = -1;

    // rename artist 'Foo' to 'Glouglou'
    post = new PostMethod(URL + "edit/artist");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("artist_ids[]", "" + foo_id);
    post.addParameter("artist_name", "Glouglou");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());
    // wait for indexer
    checkStats(rt);

    // list artists: 'Bob' and 'Glouglou'
    get = new GetMethod(URL + "artists");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    JSONArray arr = obj.getJSONArray("artists");
    assertEquals(2, arr.length());
    for (int i = 0; i < 2; i++) {
        obj = arr.getJSONObject(i).getJSONObject("artist");
        String name = obj.getString("name");
        assertTrue(name.equals("Glouglou") || name.equals("Bob"));
        if (name.equals("Bob")) {
            bob_id = obj.getInt("id");
        } else {
            foo_id = obj.getInt("id");
        }
    }

    // rename both artists to 'Abc- 12$'
    post = new PostMethod(URL + "edit/artist");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("artist_ids[]", "" + foo_id);
    post.addParameter("artist_ids[]", "" + bob_id);
    post.addParameter("artist_name", "Abc- 12$");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // wait for indexer
    rt.artistCount.set(1);
    checkStats(rt);

    // list artists: 'Abc- 12$'
    get = new GetMethod(URL + "artists");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    arr = obj.getJSONArray("artists");
    assertEquals(1, arr.length());
    obj = arr.getJSONObject(0).getJSONObject("artist");
    String name = obj.getString("name");
    assertTrue(name.equals("Abc- 12$"));

    // list albums
    get = new GetMethod(URL + "albums/" + obj.getInt("id"));
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    arr = obj.getJSONArray("albums");

    // revert data as it was before using album edition
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("artist_name", "Bob");
    for (int i = 0; i < arr.length(); i++) {
        obj = arr.getJSONObject(i);
        int id = obj.getInt("id");
        String n = obj.getString("name");
        if (n.equals("Gni") || n.equals("Ok") || n.equals("Qux")) {
            post.addParameter("album_ids[]", "" + id);
        }
    }
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("artist_name", "Foo");
    for (int i = 0; i < arr.length(); i++) {
        obj = arr.getJSONObject(i);
        String n = obj.getString("name");
        int id = obj.getInt("id");
        if (n.equals("Bar") || n.equals("Baz")) {
            post.addParameter("album_ids[]", "" + id);
        }
    }
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    rt.songCount.set(15);
    rt.albumCount.set(5);
    rt.artistCount.set(2);
    rt.userCount.set(2);
    rt.playlistCount.set(0);
    rt.playtime.set(15);
    rt.downloaded.set(0);
    checkStats(rt);

    // get album id of 'Gni'
    get = new GetMethod(URL + "search/Gni");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    int gni_id = obj.getJSONArray("albums").getJSONObject(0).getInt("id");

    // rename album 'Gni' to 'Pwet', set date to '1664', genre to 'zouk'
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("album_ids[]", "" + gni_id);
    post.addParameter("genre", "zouk");
    post.addParameter("date", "1664");
    post.addParameter("album_name", "Pwet");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // wait for indexer
    checkStats(rt);

    // check album Pwet
    get = new GetMethod(URL + "search/Pwet");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    obj = obj.getJSONArray("albums").getJSONObject(0);
    int pwet_id = obj.getInt("id");
    assertEquals("Pwet", obj.getString("name"));
    assertEquals("zouk", obj.getString("genre"));
    assertEquals("1664", obj.getString("date"));

    // get ids for 'Qux'
    get = new GetMethod(URL + "search/Qux");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    obj = obj.getJSONArray("albums").getJSONObject(0);
    int qux_id = obj.getInt("id");

    // change genre of albums Pwet and Qux to 'hardcore testing'
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("album_ids[]", "" + pwet_id);
    post.addParameter("album_ids[]", "" + qux_id);
    post.addParameter("genre", "hardcore testing");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // wait for indexer
    checkStats(rt);

    // checking
    get = new GetMethod(URL + "search/Qux");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    obj = obj.getJSONArray("albums").getJSONObject(0);
    qux_id = obj.getInt("id");
    assertEquals("hardcore testing", obj.getString("genre"));
    get = new GetMethod(URL + "search/Pwet");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    obj = obj.getJSONArray("albums").getJSONObject(0);
    pwet_id = obj.getInt("id");
    assertEquals("hardcore testing", obj.getString("genre"));

    // reset album 'Gni'
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("album_ids[]", "" + pwet_id);
    post.addParameter("album_name", "Gni");
    post.addParameter("genre", "aggressive raggae");
    post.addParameter("date", "2009");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // reset album 'Qux'
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("album_ids[]", "" + qux_id);
    post.addParameter("genre", "death jazz");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // find song 'Thirteen'
    get = new GetMethod(URL + "search/thirteen");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    obj = obj.getJSONArray("songs").getJSONObject(0);
    int id_13 = obj.getInt("id");

    // song 'Thirteen' : set position to 14, disc number to 1, name to '13'
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_13);
    post.addParameter("song_title", "13");
    post.addParameter("position", "14");
    post.addParameter("disc_no", "1");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // wait for indexer
    checkStats(rt);

    // check song 13
    get = new GetMethod(URL + "search/13");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    obj = obj.getJSONArray("songs").getJSONObject(0);
    id_13 = obj.getInt("id");
    assertEquals("13", obj.getString("title"));
    assertEquals(14, obj.getInt("position"));
    assertEquals(1, obj.getInt("disc_no"));

    // restore song 13
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_13);
    post.addParameter("song_title", "Thirteen");
    post.addParameter("position", "1");
    post.addParameter("disc_no", "2");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // find songs 'Four' and 'Fourteen'
    get = new GetMethod(URL + "search/four");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    arr = obj.getJSONArray("songs");
    assertEquals(2, arr.length());
    int id_4 = -1, id_14 = -1;
    for (int i = 0; i < 2; i++) {
        obj = arr.getJSONObject(i);
        int id = obj.getInt("id");
        String title = obj.getString("title");
        if (title.equals("Four")) {
            id_4 = id;
        } else if (title.equals("Fourteen")) {
            id_14 = id;
        }
    }

    // move songs 'Four' to album 'chaleur tournante' by 'bosch'
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_4);
    post.addParameter("album_name", "chaleur tournante");
    post.addParameter("artist_name", "bosch");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // move songs 'Fourteen' to album 'chaleur tournante' by 'bosch'
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_14);
    post.addParameter("album_name", "chaleur tournante");
    post.addParameter("artist_name", "bosch");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // wait for indexer
    rt.artistCount.set(3);
    rt.albumCount.set(6);
    checkStats(rt);

    // check songs
    // find songs 'Four' and 'Fourteen'
    get = new GetMethod(URL + "search/four");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    arr = obj.getJSONArray("songs");
    assertEquals(2, arr.length());
    for (int i = 0; i < 2; i++) {
        obj = arr.getJSONObject(i);
        int id = obj.getInt("id");
        String title = obj.getString("title");
        assertEquals("bosch", obj.getString("artist_name"));
        assertEquals("chaleur tournante", obj.getString("album_name"));
        assertTrue(title.equals("Four") || title.equals("Fourteen"));
        if (title.equals("Four")) {
            id_4 = id;
        } else if (title.equals("Fourteen")) {
            id_14 = id;
        }
    }

    // revert both songs
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_4);
    post.addParameter("album_name", "Bar");
    post.addParameter("artist_name", "Foo");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_14);
    post.addParameter("album_name", "Gni");
    post.addParameter("artist_name", "Bob");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    get = new GetMethod(URL + "search/Bob");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    bob_id = obj.getJSONArray("artists").getJSONObject(0).getInt("id");

    // find albums 'Ok' and 'Qux'
    get = new GetMethod(URL + "albums/" + bob_id);
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    arr = obj.getJSONArray("albums");
    assertEquals(3, arr.length());
    int ok_id = -1;
    qux_id = -1;
    for (int i = 0; i < 3; i++) {
        obj = arr.getJSONObject(i);
        int id = obj.getInt("id");
        name = obj.getString("name");
        if (name.equals("Ok")) {
            ok_id = id;
        } else if (name.equals("Qux")) {
            qux_id = id;
        }
    }

    // merge albums 'Ok' and 'Qux' to album 'okux'
    post = new PostMethod(URL + "edit/album");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("album_ids[]", "" + ok_id);
    post.addParameter("album_ids[]", "" + qux_id);
    post.addParameter("album_name", "okux");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // check new album 'okux'
    get = new GetMethod(URL + "albums/" + bob_id);
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    arr = obj.getJSONArray("albums");

    assertEquals(2, arr.length());
    int okux_id = -1;
    for (int i = 0; i < 2; i++) {
        obj = arr.getJSONObject(i);
        int id = obj.getInt("id");
        name = obj.getString("name");
        if (name.equals("okux")) {
            okux_id = id;
        }
    }

    get = new GetMethod(URL + "songs/" + okux_id);
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    arr = obj.getJSONArray("songs");
    obj = obj.getJSONObject("album");

    assertEquals(3, arr.length());
    assertEquals(3, obj.getInt("songs"));
    int id_8 = -1, id_9 = -1, id_15 = -1;
    for (int i = 0; i < 3; i++) {
        obj = arr.getJSONObject(i);
        int id = obj.getInt("id");
        String title = obj.getString("title");
        if (title.equals("Eight")) {
            id_8 = id;
        } else if (title.equals("Nine")) {
            id_9 = id;
        } else if (title.equals("Fifteen")) {
            id_15 = id;
        }
    }

    // revert data
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_8);
    post.addParameter("song_ids[]", "" + id_9);
    post.addParameter("album_name", "Qux");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());
    post = new PostMethod(URL + "edit/song");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + id_15);
    post.addParameter("album_name", "Ok");
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());
}

From source file:info.jtrac.mylyn.JtracClient.java

private String doPost(String url, String message) throws Exception {
    PostMethod post = new PostMethod(url);
    post.setRequestEntity(new StringRequestEntity(message, "text/xml", "UTF-8"));
    String response = null;//from  w  w w  .j ava 2s  . c om
    int code;
    try {
        code = httpClient.executeMethod(post);
        if (code != HttpURLConnection.HTTP_OK) {
            throw new HttpException("HTTP Response Code: " + code);
        }
        response = post.getResponseBodyAsString();
    } finally {
        post.releaseConnection();
    }
    return response;
}

From source file:hydrograph.server.service.HydrographServiceClient.java

public void chcekConnectionStatus() throws IOException {

    HttpClient httpClient = new HttpClient();
    //TODO : add connection details while testing only,remove it once done
    String teradatajson = "{\"username\":\"\",\"password\":\"\",\"hostname\":\"\",\"database\":\"\",\"dbtype\":\"\",\"port\":\"\"}";
    PostMethod postMethod = new PostMethod("http://" + HOST_NAME + ":" + PORT + "/getConnectionStatus");

    //postMethod.addParameter("request_parameters", redshiftjson);
    postMethod.addParameter("request_parameters", teradatajson);

    int response = httpClient.executeMethod(postMethod);
    InputStream inputStream = postMethod.getResponseBodyAsStream();

    byte[] buffer = new byte[1024 * 1024 * 5];
    String path = null;/*ww w  . j av  a2 s.co  m*/
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        path = new String(buffer);
    }
    System.out.println("Response of service: " + path);
    System.out.println("==================");
}

From source file:net.mojodna.searchable.solr.SolrIndexer.java

/**
 * Commit pending documents to the index.
 * @throws IOException//  w  ww.j a  v a 2 s  . c om
 */
protected void commit() throws IOException {
    final PostMethod post = new PostMethod(solrPath);
    post.setRequestEntity(new StringRequestEntity("<commit waitFlush=\"false\" waitSearcher=\"false\"/>",
            "text/xml", "UTF-8"));
    log.debug("Committing.");
    getHttpClient().executeMethod(post);
}

From source file:com.owncloud.android.lib.resources.files.StoreMetadataOperation.java

/**
 * @param client Client object//from  www  .  ja v a2 s  .  co m
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    PostMethod postMethod = null;
    RemoteOperationResult result;

    try {
        // remote request
        postMethod = new PostMethod(client.getBaseUri() + METADATA_URL + fileId + JSON_FORMAT);
        postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
        postMethod.setParameter(METADATA, encryptedMetadataJson);

        int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

        if (status == HttpStatus.SC_OK) {
            String response = postMethod.getResponseBodyAsString();

            // Parse the response
            JSONObject respJSON = new JSONObject(response);
            String metadata = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA)
                    .get(NODE_META_DATA);

            result = new RemoteOperationResult(true, postMethod);
            ArrayList<Object> keys = new ArrayList<>();
            keys.add(metadata);
            result.setData(keys);
        } else {
            result = new RemoteOperationResult(false, postMethod);
            client.exhaustResponse(postMethod.getResponseBodyAsStream());
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Storing of metadata for folder " + fileId + " failed: " + result.getLogMessage(),
                result.getException());
    } finally {
        if (postMethod != null)
            postMethod.releaseConnection();
    }
    return result;
}

From source file:com.googlecode.fascinator.indexer.SolrSearcher.java

public InputStream get(String query, Map<String, Set<String>> extras, boolean escape) throws IOException {
    if (query == null) {
        query = "*:*";
    } else if (!QUERY_ALL.equals(query) && escape) {
        query = query.replaceAll(":", "\\\\:");
    }//from   w  w  w.  j av a 2  s .co  m
    String selectUrl = baseUrl + "/select";
    NameValuePair[] postData = getPostData(query, extras);
    log.debug("URL:{}, POSTDATA:{}", selectUrl, postData);
    PostMethod method = new PostMethod(selectUrl);
    method.addRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    method.setRequestBody(postData);
    int status = client.executeMethod(method, true);
    if (status == HttpStatus.SC_OK) {
        return method.getResponseBodyAsStream();
    }
    return null;
}

From source file:com.cloudmaster.cmp.util.AlarmSystem.transfer.HttpSender.java

public ResponseObject send(TransportObject object) throws Exception {
    ResponseObject rs = new ResponseObject();
    ByteArrayOutputStream bOs = null;
    DataOutputStream dOs = null;/*from  w w w  .  java 2s .  co m*/
    DataInputStream dIs = null;
    HttpClient client;
    PostMethod meth = null;
    byte[] rawData;
    try {
        bOs = new ByteArrayOutputStream();
        dOs = new DataOutputStream(bOs);
        object.toStream(dOs);
        bOs.flush();
        rawData = bOs.toByteArray();

        client = new HttpClient();
        client.setConnectionTimeout(this.timeout);
        client.setTimeout(this.datatimeout);
        client.setHttpConnectionFactoryTimeout(this.timeout);

        meth = new PostMethod(object.getValue(SERVER_URL));
        // meth = new UTF8PostMethod(url);
        meth.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODING);
        // meth.addParameter(SERVER_ARGS, new String(rawData,"UTF-8"));

        // meth.setRequestBody(new String(rawData));
        // meth.setRequestBody(new String(rawData,"UTF-8"));

        byte[] base64Array = Base64.encode(rawData).getBytes();
        meth.setRequestBody(new String(base64Array));

        // System.out.println(new String(rawData));

        client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(1, false));
        client.executeMethod(meth);

        dIs = new DataInputStream(meth.getResponseBodyAsStream());

        if (meth.getStatusCode() == HttpStatus.SC_OK) {

            Header errHeader = meth.getResponseHeader(HDR_ERROR);

            if (errHeader != null) {
                rs.setError(meth.getResponseBodyAsString());
                return rs;
            }

            rs = ResponseObject.fromStream(dIs);

            return rs;
        } else {
            meth.releaseConnection();
            throw new IOException("Connection failure: " + meth.getStatusLine().toString());
        }
    } finally {
        if (meth != null) {
            meth.releaseConnection();
        }
        if (bOs != null) {
            bOs.close();
        }
        if (dOs != null) {
            dOs.close();
        }
        if (dIs != null) {
            dIs.close();
        }
    }
}

From source file:eu.learnpad.core.impl.mt.XwikiBridgeInterfaceRestResource.java

@Override
public InputStream transform(ModelSetType type, InputStream model) throws LpRestException {
    HttpClient httpClient = this.getAnonymousClient();
    String uri = String.format("%s/learnpad/mt/bridge/transform", this.restPrefix);
    PostMethod postMethod = new PostMethod(uri);
    postMethod.addRequestHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
    postMethod.addRequestHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM);
    NameValuePair[] queryString = new NameValuePair[1];
    queryString[0] = new NameValuePair("type", type.toString());
    postMethod.setQueryString(queryString);

    RequestEntity modelEntity = new InputStreamRequestEntity(model);
    postMethod.setRequestEntity(modelEntity);
    try {// w  w  w . j  ava 2 s .  c  o m
        httpClient.executeMethod(postMethod);
    } catch (IOException e) {
        String message = String.format("Error in sending POST to Model Transformer component [type: %s]", type);
        throw new LpRestExceptionXWikiImpl(message, e);
    }
    try {
        return postMethod.getResponseBodyAsStream();
    } catch (IOException e) {
        String message = String.format(
                "Model Transformer component failed to return result of transformation [type: %s]", type);
        throw new LpRestExceptionXWikiImpl(message, e);
    }
}

From source file:com.zimbra.cs.store.http.HttpStoreManager.java

@Override
public String writeStreamToStore(InputStream in, long actualSize, Mailbox mbox)
        throws IOException, ServiceException {
    MessageDigest digest;//from   w  w  w .  j a v  a2  s  . c  o m
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw ServiceException.FAILURE("SHA-256 digest not found", e);
    }
    ByteUtil.PositionInputStream pin = new ByteUtil.PositionInputStream(new DigestInputStream(in, digest));

    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    PostMethod post = new PostMethod(getPostUrl(mbox));
    try {
        HttpClientUtil.addInputStreamToHttpMethod(post, pin, actualSize, "application/octet-stream");
        int statusCode = HttpClientUtil.executeMethod(client, post);
        if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED
                || statusCode == HttpStatus.SC_NO_CONTENT) {
            return getLocator(post, ByteUtil.encodeFSSafeBase64(digest.digest()), pin.getPosition(), mbox);
        } else {
            throw ServiceException.FAILURE("error POSTing blob: " + post.getStatusText(), null);
        }
    } finally {
        post.releaseConnection();
    }
}

From source file:de.bermuda.arquillian.example.ContentTypeProxyServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String localPathInfo = req.getPathInfo() == null ? "" : req.getPathInfo();
    String queryString = req.getQueryString();
    log.info("Setting URL to: " + serviceURL.toExternalForm() + localPathInfo);
    PostMethod post = new PostMethod(serviceURL.toExternalForm() + localPathInfo);
    post.setQueryString(queryString);//  w  w w .  j av a 2 s . c  o m

    copyRequestHeaders(req, post);

    copyRequestBody(req, post);

    try {
        int statusCode = client.executeMethod(post);
        if (statusCode != HttpStatus.SC_OK) {
            log.warn("Could not post request to i.Serve: " + statusCode);
        }

        migrateResponseHeader(resp, post);

        copyResponseContent(resp, post);
    } catch (HttpException e) {
        log.error("Catched HTTPException: ", e);
    } catch (IOException e) {
        log.error("Catched IOException: ", e);
    }
}