Package org.codehaus.jettison.json

Examples of org.codehaus.jettison.json.JSONObject


        playlistUris.add("inbox");
        playlistUris.add("starred");

        try {
            System.out.println("Making call to: " + SERVICE_URL + "/playlists.json");
            JSONObject response = c.resource(SERVICE_URL + "/playlists.json").accept("application/json").get(JSONObject.class);
            try {
                JSONArray playlistArray = response.getJSONArray("playlists");
                for (int i = 0; i < playlistArray.length(); i++) {
                    try {
                        playlistUris.add(playlistArray.getJSONObject(i).getString("uri"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        //TODO: Error handling ?
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                //TODO: Error handling ?
            }

            int noOfTracks = 0;
            for (String playlistUri : playlistUris) {
                response = c.resource(SERVICE_URL + "/" + playlistUri + "/playlists.json").accept("application/json").get(JSONObject.class);

                try {
                    SpotifyPlaylistData playlistData = mapper.readValue(response.toString(), SpotifyPlaylistData.class);
                    playlists.add(playlistData);
                    noOfTracks += playlistData.getTracks().size();
                } catch (JsonMappingException e) {
                    e.printStackTrace();
                    //TODO: Error handling ?
                } catch (JsonParseException e) {
                    e.printStackTrace();
                    //TODO: Error handling ?
                } catch (IOException e) {
                    e.printStackTrace();
                    //TODO: Error handling ?
                }
            }


            long i = 0;
            long offset = 0;
            final long CHUNK_SIZE = 10;
            for (SpotifyPlaylistData playlist : playlists) {
                for (SpotifyPlaylistTrackData trackData : playlist.getTracks()) {
                    if (!isAborted()) {
                        if (i == 0) {
                            entityManager.getTransaction().begin();
                        }

                        TrackData track = new TrackData();
                        track.setFile(trackData.getUri());
                        track.setFormat("spotify");
                        track.setSmdID(trackData.getUri());
                        track.setUrl(trackData.getUri());
                        track.setTags(new ArrayList<TagData>());
                        track.getTags().add(new TagData(TagData.ALBUM, trackData.getAlbum()));
                        track.getTags().add(new TagData(TagData.TITLE, trackData.getName()));
                        track.getTags().add(new TagData(TagData.SPOTIFY_TRACK_ID, trackData.getUri()));
                        track.getTags().add(new TagData("SPOTIFYIMAGE", trackData.getCover()));
                        track.getTags().add(new TagData("DURATION", "" + trackData.getDuration()));

                        if (trackData.getIndex() != null) {
                            if (trackData.getIndex() != null && trackData.getIndex() != 0) {
                                track.getTags().add(new TagData(TagData.TRACKNUM, "" + trackData.getIndex()));
                            }
                            if (trackData.getDisc() != null && trackData.getDisc() != 0) {
                                track.getTags().add(new TagData(TagData.DISC, "" + trackData.getDisc()));
                            }
                            if (trackData.getAlbumuri() != null) {
                                track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, trackData.getAlbumuri()));
                            }
                            if (trackData.getArtists() != null) {
                                for (SpotifyArtistData artistData : trackData.getArtists()) {
                                    track.getTags().add(new TagData(TagData.ARTIST, artistData.getName()));
                                    track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistData.getUri()));
                                }
                            }
                        } else {
                            //TODO: Remove this as soon as 2.2.7 of spotifyd has been officially released
                            response = c.resource(SERVICE_URL + "/" + trackData.getUri() + "/browse.json").accept("application/json").get(JSONObject.class);
                            try {
                                if (response.has("index") && !response.getString("index").equals("0")) {
                                    track.getTags().add(new TagData(TagData.TRACKNUM, response.getString("index")));
                                }
                                if (response.has("disc") && !response.getString("disc").equals("0")) {
                                    track.getTags().add(new TagData(TagData.DISC, response.getString("disc")));
                                }
                                if (response.has("albumuri") && response.getString("albumuri").length() > 0) {
                                    track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, response.getString("albumuri")));
                                }
                                if (response.has("artists")) {
                                    JSONArray artistsArray = response.getJSONArray("artists");
                                    for (int j = 0; j < artistsArray.length(); j++) {
                                        JSONObject artistObject = artistsArray.getJSONObject(j);
                                        track.getTags().add(new TagData(TagData.ARTIST, artistObject.getString("name")));
                                        track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistObject.getString("uri")));
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                //TODO: Implement error handling
View Full Code Here


            assert id!=null;
            Long count = ((JSONObject) resultItems.get(i)).optLong("count");
            assert count!=null;
            assert count>0;

            JSONObject result = Client.create().resource(HOSTURL + "/browse/"+id+"?criteria="+release+"&itemInfo=true&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
            assert result.getLong("size") == count;

            JSONArray childResultItems = result.getJSONArray("items");
            for (int j = 0; j < childResultItems.length(); j++) {
                JSONObject item = ((JSONObject) childResultItems.get(j)).getJSONObject("item");
                String childId = item.getString("id");
                JSONArray childItems = ((JSONObject) childResultItems.get(j)).optJSONArray("childItems");
                assert childItems != null;

                Map<String,Long> childMap = new HashMap<String,Long>();
                for (int k=0;k<childItems.length();k++) {
                    childMap.put(((JSONObject)childItems.get(k)).getString("id"),((JSONObject)childItems.get(k)).getLong("count"));
                }

                JSONArray childCounters = Client.create().resource(HOSTURL + "/browse?criteria="+release+"&criteria="+id+":"+childId+"&counters=true").accept(MediaType.APPLICATION_JSON).get(JSONArray.class);
                for (int v = 0; v < childCounters.length(); v++) {
                    String childCounterId = ((JSONObject) childCounters.get(v)).getString("id");
                    assert childCounterId!=null;
                    Long childCounter = ((JSONObject) childCounters.get(v)).getLong("count");
                    assert childCounter!=null;
                    assert childMap.containsKey(childCounterId);
                    if(!childMap.get(childCounterId).equals(childCounter)) {
                        JSONObject testing = Client.create().resource(HOSTURL + "/browse/"+childCounterId+"?criteria="+release+"&criteria="+id+":"+childId).accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
                        testing.toString();
                    }
                    assert childMap.get(childCounterId).equals(childCounter);
                }
            }
        }
View Full Code Here

        }
    }

    @Test
    public void testBrowseArtists() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Artist?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 50;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 50;
        JSONArray resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Artist artist = jsonProvider.fromJson(item.toString(), Artist.class);
            assert artist != null;
            assert artist.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
        }

        result = Client.create().resource(HOSTURL + "/browse/Artist?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 50;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Artist artist = jsonProvider.fromJson(item.toString(), Artist.class);
            assert artist != null;
            assert artist.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
View Full Code Here

        }
    }

    @Test
    public void testBrowseReleases() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Release?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 5;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 5;
        JSONArray resultItems = result.getJSONArray("items");

        int images = 0;
        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Release release = jsonProvider.fromJson(item.toString(), Release.class);
            assert release != null;
            assert release.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
            if(resultItems.getJSONObject(i).optJSONObject("image")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("providerId")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("url")!=null) {
                images++;
            }
        }
        assert images==4;

        result = Client.create().resource(HOSTURL + "/browse/Release?itemInfo=true&offset=2&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 3;
        assert result.getLong("offset") == 2;
        assert result.getLong("totalSize") == 5;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Release release = jsonProvider.fromJson(item.toString(), Release.class);
            assert release != null;
            assert release.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
View Full Code Here

        }
    }

    @Test
    public void testBrowseTracks() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Track?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 79;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 79;
        JSONArray resultItems = result.getJSONArray("items");

        boolean foundWithImage = false;
        boolean foundWithoutImage = false;
        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Track track = jsonProvider.fromJson(item.toString(), Track.class);
            assert track != null;
            assert track.getNumber() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
            if(resultItems.getJSONObject(i).optJSONObject("image")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("providerId")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("url")!=null) {
                foundWithImage = true;
            }else if(resultItems.getJSONObject(i).optJSONObject("image")==null) {
                foundWithoutImage = true;
            }

        }
        assert foundWithImage;
        assert foundWithoutImage;

        result = Client.create().resource(HOSTURL + "/browse/Track?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 79;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Track track = jsonProvider.fromJson(item.toString(), Track.class);
            assert track != null;
            assert track.getNumber() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
View Full Code Here

        }
    }

    @Test
    public void testBrowseWorks() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Work?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 79;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 79;
        JSONArray resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Work work = jsonProvider.fromJson(item.toString(), Work.class);
            assert work != null;
            assert work.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
        }

        result = Client.create().resource(HOSTURL + "/browse/Work?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 79;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Work work = jsonProvider.fromJson(item.toString(), Work.class);
            assert work != null;
            assert work.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
View Full Code Here

        }
    }

    @Test
    public void testBrowseClassifications() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Classification?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 17;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 17;
        JSONArray resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Classification classification = jsonProvider.fromJson(item.toString(), Classification.class);
            assert classification != null;
            assert classification.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
        }

        result = Client.create().resource(HOSTURL + "/browse/Classification?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 17;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Classification classification = jsonProvider.fromJson(item.toString(), Classification.class);
            assert classification != null;
            assert classification.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
View Full Code Here

    injector = Guice.createInjector(new MockModule());
    injector.injectMembers(handler);
  }

  private JSONObject createDummyJSONRegister() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("responseId", -1);
    json.put("timestamp", System.currentTimeMillis());
    json.put("hostname", "dummyHost");
    return json;
  }
View Full Code Here

    json.put("hostname", "dummyHost");
    return json;
  }

  private JSONObject createDummyHeartBeat() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("responseId", -1);
    json.put("timestamp", System.currentTimeMillis());
    json.put("hostname", "dummyHost");
    return json;
  }
View Full Code Here

    json.put("hostname", "dummyHost");
    return json;
  }

  private JSONObject createDummyHeartBeatWithAgentEnv() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("responseId", -1);
    json.put("timestamp", System.currentTimeMillis());
    json.put("hostname", "dummyHost");

    JSONObject agentEnv = new JSONObject();
    json.put("agentEnv", agentEnv);
    return json;
  }
View Full Code Here

TOP

Related Classes of org.codehaus.jettison.json.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.