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

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

Introduction

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

Prototype

public void addParameter(String paramString1, String paramString2) throws IllegalArgumentException 

Source Link

Usage

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * DiamondServerDataID//  w w  w  .j a  v a 2 s  .c  o m
 * 
 * @param timeout
 * @return
 */
Set<String> checkUpdateDataIds(long timeout) {
    if (!isRun) {
        throw new RuntimeException("DiamondSubscriberDataID");
    }
    // ==============================================
    if (MockServer.isTestMode()) {
        return testData();
    }
    // ==========================================================
    long waitTime = 0;

    // Set<String> localModifySet = getLocalUpdateDataIds();
    String probeUpdateString = getProbeUpdateString();
    if (StringUtils.isBlank(probeUpdateString)) {
        return null;
    }

    while (0 == timeout || timeout > waitTime) {
        // 
        long onceTimeOut = getOnceTimeOut(waitTime, timeout);
        waitTime += onceTimeOut;

        PostMethod postMethod = new PostMethod(Constants.HTTP_URI_FILE);

        postMethod.addParameter(Constants.PROBE_MODIFY_REQUEST, probeUpdateString);

        // HttpMethod
        HttpMethodParams params = new HttpMethodParams();
        params.setSoTimeout((int) onceTimeOut);
        // ///////////////////////
        postMethod.setParams(params);

        try {
            httpClient.getHostConfiguration().setHost(
                    diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
                    this.diamondConfigure.getPort());

            int httpStatus = httpClient.executeMethod(postMethod);

            switch (httpStatus) {
            case SC_OK: {
                Set<String> result = getUpdateDataIds(postMethod);
                return result;
            }

            case SC_SERVICE_UNAVAILABLE: {
                rotateToNextDomain();
            }
                break;

            default: {
                log.warn("DataIDHTTP State: " + httpStatus);
                rotateToNextDomain();
            }
            }
        } catch (HttpException e) {
            log.error("Http", e);
            rotateToNextDomain();
        } catch (IOException e) {
            log.error("IO", e);
            rotateToNextDomain();
        } catch (Exception e) {
            log.error("", e);
            rotateToNextDomain();
        } finally {
            postMethod.releaseConnection();
        }
    }
    throw new RuntimeException("DataID "
            + diamondConfigure.getDomainNameList().get(this.domainNamePos.get()) + ", " + timeout);
}

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * DiamondServer??DataID/*from w w  w  .ja va2  s . c om*/
 * 
 * @param timeout
 * @return
 */
Set<String> checkUpdateDataIds(long timeout) {
    if (!isRun) {
        throw new RuntimeException(
                "DiamondSubscriber????DataID");
    }
    // =======================?=======================
    if (MockServer.isTestMode()) {
        return testData();
    }
    // ==========================================================
    long waitTime = 0;

    // Set<String> localModifySet = getLocalUpdateDataIds();
    String probeUpdateString = getProbeUpdateString();
    if (StringUtils.isBlank(probeUpdateString)) {
        return null;
    }

    while (0 == timeout || timeout > waitTime) {
        // 
        long onceTimeOut = getOnceTimeOut(waitTime, timeout);
        waitTime += onceTimeOut;

        PostMethod postMethod = new PostMethod(Constants.HTTP_URI_FILE);

        postMethod.addParameter(Constants.PROBE_MODIFY_REQUEST, probeUpdateString);

        // HttpMethod?
        HttpMethodParams params = new HttpMethodParams();
        params.setSoTimeout((int) onceTimeOut);
        // ///////////////////////
        postMethod.setParams(params);

        try {
            httpClient.getHostConfiguration().setHost(
                    diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
                    this.diamondConfigure.getPort());

            int httpStatus = httpClient.executeMethod(postMethod);

            log.info(httpStatus);

            switch (httpStatus) {
            case SC_OK: {
                Set<String> result = getUpdateDataIds(postMethod);
                return result;
            }

            case SC_SERVICE_UNAVAILABLE: {
                rotateToNextDomain();
            }
                break;

            default: {
                log.warn("?DataIDHTTP State: " + httpStatus);
                rotateToNextDomain();
            }
            }
        } catch (HttpException e) {
            log.error("???Http", e);
            rotateToNextDomain();
        } catch (IOException e) {
            log.error("???IO", e);
            rotateToNextDomain();
        } catch (Exception e) {
            log.error("", e);
            rotateToNextDomain();
        } finally {
            postMethod.releaseConnection();
        }
    }
    throw new RuntimeException("?DataID "
            + diamondConfigure.getDomainNameList().get(this.domainNamePos.get()) + ", "
            + timeout);
}

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

public void test() throws Exception {
    HttpClient client = new HttpClient();

    RuntimeStats rt = new RuntimeStats();
    rt.songCount.set(15);//from  www. ja  va2 s. c  o m
    rt.albumCount.set(5);
    rt.artistCount.set(2);
    rt.playlistCount.set(0);
    rt.userCount.set(2);
    rt.playtime.set(15);
    rt.downloaded.set(0);
    this.addMusicFolder(new File("src/test/resources/data").getAbsolutePath(), rt);

    rt.songCount.set(24);
    rt.albumCount.set(6);
    rt.artistCount.set(3);
    rt.playtime.set(24);
    this.addMusicFolder(new File("src/test/resources/data2").getAbsolutePath(), rt);

    // create playlist with no name: 400
    PostMethod post = new PostMethod(URL + "playlist/create");
    post.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(post);
    assertEquals(400, post.getStatusCode());

    // create playlist 'foo'
    post = new PostMethod(URL + "playlist/create");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("name", "foo");
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    JSONObject obj = new JSONObject(post.getResponseBodyAsString());
    Playlist foo = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals("foo", foo.name);
    assertEquals(0, foo.songs);
    assertEquals(0, foo.playtime);

    // create playlist 'foo' again: returns the same playlist
    post = new PostMethod(URL + "playlist/create");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("name", "foo");
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(foo, new Playlist(obj.getJSONObject("playlist").toString()));

    // create playlist 'foo' as admin: creates another playlist
    post = new PostMethod(URL + "playlist/create");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("name", "foo");
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    Playlist pl = new Playlist(obj.getJSONObject("playlist").toString());
    assertNotSame(foo, pl);

    // playlists for user: 'foo'
    GetMethod get = new GetMethod(URL + "playlists");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals(1, obj.getJSONArray("playlists").length());
    assertEquals(foo, new Playlist(obj.getJSONArray("playlists").get(0).toString()));

    // playlists/id for user: 'foo'
    get = new GetMethod(URL + "playlists/" + user_userId);
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals(1, obj.getJSONArray("playlists").length());
    assertEquals(foo, new Playlist(obj.getJSONArray("playlists").get(0).toString()));

    // remove playlist without argument: 400
    post = new PostMethod(URL + "playlists/remove");
    post.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(post);
    assertEquals(400, post.getStatusCode());

    // remove admin 'foo' playlist as user: 403
    post = new PostMethod(URL + "playlists/remove");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("playlist_ids[]", "" + pl.id);
    client.executeMethod(post);
    assertEquals(403, post.getStatusCode());

    // remove admin 'foo'
    post = new PostMethod(URL + "playlists/remove");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("playlist_ids[]", "" + pl.id);
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // playlists for admin: 'none'
    get = new GetMethod(URL + "playlists");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals(0, obj.getJSONArray("playlists").length());

    // playlists/id for admin: 'none'
    get = new GetMethod(URL + "playlists/" + admin_userId);
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals(0, obj.getJSONArray("playlists").length());

    // playlist/create-add with no name : 400
    post = new PostMethod(URL + "playlist/create-add");
    post.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(post);
    assertEquals(400, post.getStatusCode());

    // playlist/create-add 'bar' with no songs
    post = new PostMethod(URL + "playlist/create-add");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("name", "bar");
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(0, obj.getInt("added"));
    Playlist bar = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals("bar", bar.name);
    assertEquals(0, bar.playtime);
    assertEquals(0, bar.songs);

    int[] song_ids = new int[4];
    int album_id;

    // search for 'o': 4 songs, 1 album with 1 song
    get = new GetMethod(URL + "search/o");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    JSONArray songs = obj.getJSONArray("songs");
    for (int i = 0; i < 4; i++) {
        song_ids[i] = songs.getJSONObject(i).getInt("id");
    }
    JSONObject ok = obj.getJSONArray("albums").getJSONObject(0);
    album_id = ok.getInt("id");

    // playlist/create-add 'bar' with songs
    post = new PostMethod(URL + "playlist/create-add");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("name", "bar");
    post.addParameter("song_ids[]", "" + song_ids[0]);
    post.addParameter("song_ids[]", "" + song_ids[1]);
    post.addParameter("song_ids[]", "" + song_ids[2]);
    post.addParameter("song_ids[]", "" + song_ids[3]);
    post.addParameter("album_ids[]", "" + album_id);
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(5, obj.getInt("added"));
    bar = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals("bar", bar.name);
    assertEquals(5, bar.playtime);
    assertEquals(5, bar.songs);

    // check song list in 'bar'
    get = new GetMethod(URL + "playlist/" + bar.id + "/songs");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals("bar", obj.getString("name"));
    JSONArray arr = obj.getJSONArray("playlist");
    assertEquals(5, arr.length());
    for (int i = 0; i < 4; i++) {
        assertEquals(new Song(songs.getJSONObject(i).toString()), new Song(arr.getJSONObject(i).toString()));
    }
    Song s5 = new Song(arr.getJSONObject(4).toString());
    assertEquals("Ok", s5.album_name);

    // playlist/remove song as wrong user
    post = new PostMethod(URL + "playlist/" + bar.id + "/remove");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("song_ids[]", "" + song_ids[0]);
    client.executeMethod(post);
    assertEquals(403, post.getStatusCode());

    // playlist/remove 2 songs
    post = new PostMethod(URL + "playlist/" + bar.id + "/remove");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("song_ids[]", "" + song_ids[1]);
    post.addParameter("song_ids[]", "" + song_ids[2]);
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // check song list
    get = new GetMethod(URL + "playlist/" + bar.id + "/songs");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals("bar", obj.getString("name"));
    arr = obj.getJSONArray("playlist");
    assertEquals(3, arr.length());
    assertEquals(new Song(songs.getJSONObject(0).toString()), new Song(arr.getJSONObject(0).toString()));
    assertEquals(new Song(songs.getJSONObject(3).toString()), new Song(arr.getJSONObject(1).toString()));
    assertEquals(s5, new Song(arr.getJSONObject(2).toString()));

    // re-check with song/id/pos
    get = new GetMethod(URL + "playlist/" + bar.id + "/song/0");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString()).getJSONObject("song");
    assertEquals(new Song(songs.getJSONObject(0).toString()), new Song(obj.toString()));

    // 2nd song
    get = new GetMethod(URL + "playlist/" + bar.id + "/song/1");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString()).getJSONObject("song");
    assertEquals(new Song(songs.getJSONObject(3).toString()), new Song(obj.toString()));

    // 3rd song
    get = new GetMethod(URL + "playlist/" + bar.id + "/song/2");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString()).getJSONObject("song");
    assertEquals(s5, new Song(obj.toString()));

    // no more song in playlist
    get = new GetMethod(URL + "playlist/" + bar.id + "/song/3");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(404, get.getStatusCode());

    // playlist/create-add 'bar' with other songs and clear
    post = new PostMethod(URL + "playlist/create-add");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("name", "bar");
    post.addParameter("clear", "true");
    post.addParameter("album_ids[]", "" + album_id);
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(1, obj.getInt("added"));
    bar = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals("bar", bar.name);
    assertEquals(1, bar.playtime);
    assertEquals(1, bar.songs);

    // playlist/add as admin: 403
    post = new PostMethod(URL + "playlist/" + bar.id + "/add");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("clear", "true");
    post.addParameter("song_ids", "" + s5.id);
    client.executeMethod(post);
    assertEquals(403, post.getStatusCode());

    // playlist/add with duplicate songs: 400
    post = new PostMethod(URL + "playlist/" + bar.id + "/add");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("clear", "true");
    post.addParameter("song_ids[]", "" + song_ids[0]);
    post.addParameter("song_ids[]", "" + song_ids[0]);
    post.addParameter("album_ids[]", "" + album_id);
    client.executeMethod(post);
    assertEquals(500, post.getStatusCode());

    // playlist/add a couple songs w/ clear
    post = new PostMethod(URL + "playlist/" + bar.id + "/add");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("clear", "true");
    post.addParameter("song_ids[]", "" + song_ids[0]);
    post.addParameter("song_ids[]", "" + song_ids[2]);
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(2, obj.getInt("added"));
    pl = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals(2, pl.playtime);
    assertEquals(2, pl.songs);
    assertEquals("bar", pl.name);

    // check song list
    get = new GetMethod(URL + "playlist/" + pl.id + "/songs");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals("bar", obj.getString("name"));
    arr = obj.getJSONArray("playlist");
    assertEquals(2, arr.length());
    assertEquals(new Song(songs.getJSONObject(0).toString()), new Song(arr.getJSONObject(0).toString()));
    assertEquals(new Song(songs.getJSONObject(2).toString()), new Song(arr.getJSONObject(1).toString()));

    // re-check with song/id/pos
    get = new GetMethod(URL + "playlist/" + pl.id + "/song/0");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString()).getJSONObject("song");
    assertEquals(new Song(songs.getJSONObject(0).toString()), new Song(obj.toString()));

    // 2nd song
    get = new GetMethod(URL + "playlist/" + pl.id + "/song/1");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString()).getJSONObject("song");
    assertEquals(new Song(songs.getJSONObject(2).toString()), new Song(obj.toString()));

    // no more song in playlist
    get = new GetMethod(URL + "playlist/" + pl.id + "/song/2");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(404, get.getStatusCode());

    // random playlist with no name : 400
    post = new PostMethod(URL + "playlist/random");
    post.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(post);
    assertEquals(400, post.getStatusCode());

    // random playlist with no song number : 400
    post = new PostMethod(URL + "playlist/random");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("name", "toto");
    client.executeMethod(post);
    assertEquals(400, post.getStatusCode());

    // random playlist with too many songs: 400
    post = new PostMethod(URL + "playlist/random");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("number", "60");
    post.addParameter("name", "toto");
    client.executeMethod(post);
    assertEquals(400, post.getStatusCode());

    // random playlist 'toto'
    post = new PostMethod(URL + "playlist/random");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("number", "15");
    post.addParameter("name", "toto");
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(15, obj.getInt("added"));
    Playlist rand = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals(15, rand.playtime);
    assertEquals(15, rand.songs);
    assertEquals("toto", rand.name);

    // get first song
    get = new GetMethod(URL + "playlist/" + rand.id + "/song/0");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    Song s = new Song(new JSONObject(get.getResponseBodyAsString()).getJSONObject("song").toString());
    assertEquals(s.id, obj.getInt("first_song"));

    // all random songs should fit in this hashset
    HashSet<Song> randSet = new HashSet<Song>(15);
    get = new GetMethod(URL + "playlist/" + rand.id + "/songs");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals("toto", obj.get("name"));
    arr = obj.getJSONArray("playlist");
    assertEquals(15, arr.length());
    for (int i = 0; i < arr.length(); i++) {
        Song ss = new Song(arr.getJSONObject(i).toString());
        assertFalse(randSet.contains(ss));
        randSet.add(ss);
    }

    // there are 24 songs total in library, try to create a 30 songs playlist
    post = new PostMethod(URL + "playlist/random");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("number", "30");
    post.addParameter("name", "titi");
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(24, obj.getInt("added"));
    rand = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals(24, rand.playtime);
    assertEquals(24, rand.songs);
    assertEquals("titi", rand.name);

    // all 24 random songs should fit in this hashset
    randSet = new HashSet<Song>(24);
    get = new GetMethod(URL + "playlist/" + rand.id + "/songs");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals("titi", obj.get("name"));
    arr = obj.getJSONArray("playlist");
    assertEquals(24, arr.length());
    for (int i = 0; i < arr.length(); i++) {
        Song ss = new Song(arr.getJSONObject(i).toString());
        assertFalse(randSet.contains(ss));
        randSet.add(ss);
    }

    // re-create 'titi' with 10 songs
    post = new PostMethod(URL + "playlist/random");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("number", "10");
    post.addParameter("name", "titi");
    client.executeMethod(post);
    assertEquals(200, post.getStatusCode());
    obj = new JSONObject(post.getResponseBodyAsString());
    assertEquals(10, obj.getInt("added"));
    rand = new Playlist(obj.getJSONObject("playlist").toString());
    assertEquals(10, rand.playtime);
    assertEquals(10, rand.songs);
    assertEquals("titi", rand.name);

    // playlists for user: 'foo', 'bar', 'toto', 'titi'
    get = new GetMethod(URL + "playlists");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals(4, obj.getJSONArray("playlists").length());
    foo = new Playlist(obj.getJSONArray("playlists").get(0).toString());
    bar = new Playlist(obj.getJSONArray("playlists").get(1).toString());
    Playlist toto = new Playlist(obj.getJSONArray("playlists").get(2).toString());
    Playlist titi = new Playlist(obj.getJSONArray("playlists").get(3).toString());
    assertEquals("foo", foo.name);
    assertEquals(0, foo.songs);
    assertEquals("bar", bar.name);
    assertEquals(2, bar.songs);
    assertEquals("toto", toto.name);
    assertEquals(15, toto.songs);
    assertEquals("titi", titi.name);
    assertEquals(10, titi.songs);

    // remove all 4 user playlists
    post = new PostMethod(URL + "playlists/remove");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("playlist_ids[]", "" + foo.id);
    post.addParameter("playlist_ids[]", "" + bar.id);
    post.addParameter("playlist_ids[]", "" + toto.id);
    post.addParameter("playlist_ids[]", "" + titi.id);
    client.executeMethod(post);
    assertEquals(204, post.getStatusCode());

    // check there are no more user playlists
    get = new GetMethod(URL + "playlists");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    assertEquals(0, obj.getJSONArray("playlists").length());

}

From source file:com.mengka.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * DiamondServer??DataID/*from  w  ww .j  a v  a2 s.c  o m*/
 * 
 * @param timeout
 * @return
 */
Set<String> checkUpdateDataIds(long timeout) {
    if (!isRun) {
        throw new RuntimeException(
                "DiamondSubscriber????DataID");
    }
    // =======================?=======================
    if (MockServer.isTestMode()) {
        return testData();
    }
    // ==========================================================
    long waitTime = 0;

    // Set<String> localModifySet = getLocalUpdateDataIds();
    String probeUpdateString = getProbeUpdateString();
    if (StringUtils.isBlank(probeUpdateString)) {
        return null;
    }

    while (0 == timeout || timeout > waitTime) {
        // 
        long onceTimeOut = getOnceTimeOut(waitTime, timeout);
        waitTime += onceTimeOut;

        PostMethod postMethod = new PostMethod(Constants.CONFIG_HTTP_URI_FILE);

        postMethod.addParameter(Constants.PROBE_MODIFY_REQUEST, probeUpdateString);

        // HttpMethod?
        HttpMethodParams params = new HttpMethodParams();
        params.setSoTimeout((int) onceTimeOut);
        // ///////////////////////
        postMethod.setParams(params);

        try {
            httpClient.getHostConfiguration().setHost(
                    diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
                    this.diamondConfigure.getPort());

            int httpStatus = httpClient.executeMethod(postMethod);

            switch (httpStatus) {
            case SC_OK: {
                Set<String> result = getUpdateDataIds(postMethod);
                return result;
            }

            case SC_SERVICE_UNAVAILABLE: {
                rotateToNextDomain();
            }
                break;

            default: {
                log.warn("?DataIDHTTP State: " + httpStatus);
                rotateToNextDomain();
            }
            }
        } catch (HttpException e) {
            log.error("???Http", e);
            rotateToNextDomain();
        } catch (IOException e) {
            log.error("???IO", e);
            rotateToNextDomain();
        } catch (Exception e) {
            log.error("", e);
            rotateToNextDomain();
        } finally {
            postMethod.releaseConnection();
        }
    }
    throw new RuntimeException("?DataID "
            + diamondConfigure.getDomainNameList().get(this.domainNamePos.get()) + ", "
            + timeout);
}

From source file:com.amazonaws.elasticmapreduce.AmazonElasticMapReduceClient.java

/**
 * Add authentication related and version parameter and set request body
 * with all of the parameters//from   w w  w. j  a v a2  s .  c o m
 */
private void addRequiredParametersToRequest(PostMethod method, Map<String, String> parameters)
        throws SignatureException {
    parameters.put("Version", config.getServiceVersion());
    parameters.put("SignatureVersion", config.getSignatureVersion());
    parameters.put("Timestamp", getFormattedTimestamp());
    parameters.put("AWSAccessKeyId", this.awsAccessKeyId);
    parameters.put("Signature", signParameters(parameters, this.awsSecretAccessKey));
    for (Entry<String, String> entry : parameters.entrySet()) {
        method.addParameter(entry.getKey(), entry.getValue());
    }
}

From source file:com.liferay.portal.util.HttpImpl.java

protected void processPostMethod(PostMethod postMethod, List<Http.FilePart> fileParts,
        Map<String, String> parts) {

    if ((fileParts == null) || fileParts.isEmpty()) {
        if (parts != null) {
            for (Map.Entry<String, String> entry : parts.entrySet()) {
                String value = entry.getValue();

                if (Validator.isNotNull(value)) {
                    postMethod.addParameter(entry.getKey(), value);
                }/*from www  . j  a  v  a 2  s .  c o m*/
            }
        }
    } else {
        List<Part> partsList = new ArrayList<Part>();

        if (parts != null) {
            for (Map.Entry<String, String> entry : parts.entrySet()) {
                String value = entry.getValue();

                if (Validator.isNotNull(value)) {
                    StringPart stringPart = new StringPart(entry.getKey(), value);

                    partsList.add(stringPart);
                }
            }
        }

        for (Http.FilePart filePart : fileParts) {
            partsList.add(toCommonsFilePart(filePart));
        }

        MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(
                partsList.toArray(new Part[0]), postMethod.getParams());

        postMethod.setRequestEntity(multipartRequestEntity);
    }
}

From source file:com.amazonaws.a2s.AmazonA2SClient.java

/**
 * Add  parameters and set request body// w w w .  j  ava  2s.  c  om
 * with all of the parameters
 */
private void addRequiredParametersToRequest(PostMethod method, Map<String, String> parameters) {
    parameters.put("Version", config.getServiceVersion());
    parameters.put("AWSAccessKeyId", this.awsAccessKeyId);
    parameters.put("AssociateTag", this.associateTag);
    Iterator it = parameters.keySet().iterator();
    while (it.hasNext()) {
        String parameterName = (String) it.next();
        method.addParameter(parameterName, parameters.get(parameterName));
    }
}

From source file:com.starit.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * DiamondServer??DataID//from w  w w.  j  ava  2 s .  c  om
 * 
 * @param timeout
 * @return
 */
Set<String> checkUpdateDataIds(long timeout) {
    if (!isRun) {
        throw new RuntimeException(
                "DiamondSubscriber????DataID");
    }
    // =======================?=======================
    if (MockServer.isTestMode()) {
        return testData();
    }
    // ==========================================================
    long waitTime = 0;

    String probeUpdateString = getProbeUpdateString();
    if (StringUtils.isBlank(probeUpdateString)) {
        return null;
    }

    while (0 == timeout || timeout > waitTime) {
        // 
        long onceTimeOut = getOnceTimeOut(waitTime, timeout);
        waitTime += onceTimeOut;

        PostMethod postMethod = new PostMethod(Constants.CONFIG_HTTP_URI_FILE);

        postMethod.addParameter(Constants.PROBE_MODIFY_REQUEST, probeUpdateString);

        if (null != this.appName) {
            postMethod.addRequestHeader(Constants.APPNAME, this.appName);
        }
        postMethod.addRequestHeader(Constants.CLIENT_VERSION_HEADER, Constants.CLIENT_VERSION);
        // HttpMethod?
        HttpMethodParams params = new HttpMethodParams();
        params.setSoTimeout((int) onceTimeOut);
        // ///////////////////////
        postMethod.setParams(params);

        try {
            httpClient.getHostConfiguration().setHost(
                    diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
                    this.diamondConfigure.getPort());

            int httpStatus = httpClient.executeMethod(postMethod);

            switch (httpStatus) {
            case SC_OK: {

                Set<String> result = getUpdateDataIds(postMethod);
                return result;
            }

            case SC_SERVICE_UNAVAILABLE: {
                rotateToNextDomain();
            }
                break;

            default: {
                log.warn("?DataIDHTTP State: " + httpStatus);
                rotateToNextDomain();
            }
            }
        } catch (HttpException e) {
            log.error("???Http" + e);
            rotateToNextDomain();
        } catch (IOException e) {
            log.error("???IO" + e);
            rotateToNextDomain();
        } catch (Exception e) {
            log.error("", e);
            rotateToNextDomain();
        } finally {
            postMethod.releaseConnection();
        }
    }
    throw new RuntimeException("?DataID "
            + diamondConfigure.getDomainNameList().get(this.domainNamePos.get()) + ", "
            + timeout);
}

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);//w ww.  j  a v a  2  s  .  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:JiraWebClient.java

private void addCustomField(JiraClient client, PostMethod post, CustomField customField) {
    for (String value : customField.getValues()) {
        String key = customField.getKey();
        if (includeCustomField(key, value)) {
            if (value != null && (JiraFieldType.DATE.getKey().equals(key)
                    || JiraFieldType.DATETIME.getKey().equals(key))) {
                try {
                    Date date = JiraRssHandler.getDateTimeFormat().parse(value);
                    DateFormat format;
                    if (JiraFieldType.DATE.getKey().equals(key)) {
                        format = client.getLocalConfiguration().getDateFormat();
                    } else {
                        format = client.getLocalConfiguration().getDateTimeFormat();
                    }/*  w  w  w.j  av  a  2  s. co m*/
                    value = format.format(date);
                } catch (ParseException e) {
                    // XXX ignore
                }
            }
            post.addParameter(customField.getId(), value == null ? "" : value); //$NON-NLS-1$
        }
    }
}