Package com.sun.jersey.api.client

Examples of com.sun.jersey.api.client.Client


        client().resource(uri).type(format(APPLICATION_TYPE, type.name().toLowerCase()))
                .put(ClientResponse.class, entity);
    }

    private Client client() {
        Client client = Client.create();
        if (username != null) {
            client.addFilter(new HTTPBasicAuthFilter(username, password));
        }
        return client;
    }
View Full Code Here


            throw new ApiException(500, e.getMessage());
        }
    }

    public InputStream getBinary(String host, String path, Map<String, String> queryParams, Map<String, String> headerParams) throws ApiException {
        Client client = getClient(host);

        StringBuilder b = new StringBuilder();

        for (String key : queryParams.keySet()) {
            String value = queryParams.get(key);
            if (value != null) {
                if (b.toString().length() == 0)
                    b.append("?");
                else
                    b.append("&");
                b.append(escapeString(key)).append("=").append(escapeString(value));
            }
        }
        String querystring = b.toString();

        Builder builder = client.resource(host + path + querystring).accept("application/json");
        for (String key : headerParams.keySet()) {
            builder.header(key, headerParams.get(key));
        }

        for (String key : defaultHeaderMap.keySet()) {
View Full Code Here

                    response.getEntity(String.class));
        }
    }

    public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType) throws ApiException {
        Client client = getClient(host);

        StringBuilder b = new StringBuilder();

        for (String key : queryParams.keySet()) {
            String value = queryParams.get(key);
            if (value != null) {
                if (b.toString().length() == 0)
                    b.append("?");
                else
                    b.append("&");
                b.append(escapeString(key)).append("=").append(escapeString(value));
            }
        }
        String querystring = b.toString();

        Builder builder = client.resource(host + path + querystring).accept("application/json");
        for (String key : headerParams.keySet()) {
            builder.header(key, headerParams.get(key));
        }

        for (String key : defaultHeaderMap.keySet()) {
View Full Code Here

    private Client getClient(String host) {
        if (!hostMap.containsKey(host)) {
            ClientConfig clientConfig = new DefaultClientConfig();
            clientConfig.getClasses().add(MultiPartWriter.class);
            Client client = Client.create(clientConfig);
            if (isDebug) {
                client.addFilter(new LoggingFilter());
            }
            hostMap.put(host, client);
        }
        return hostMap.get(host);
    }
View Full Code Here

          verifyAppStateXML(response, RMAppState.KILLING, RMAppState.ACCEPTED);
        }

        String locationHeaderValue =
            response.getHeaders().getFirst(HttpHeaders.LOCATION);
        Client c = Client.create();
        WebResource tmp = c.resource(locationHeaderValue);
        if (isAuthenticationEnabled()) {
          tmp = tmp.queryParam("user.name", webserviceUserName);
        }
        response = tmp.get(ClientResponse.class);
        assertEquals(Status.OK, response.getClientResponseStatus());
View Full Code Here

      throw new IllegalStateException("missing parameter 'musicbrainz.acoustid.servicekey' please set acoustid service url un socialmusicdiscovery.properties or via -Dmusicbrainz.acoustid.servicekey");
    }
  }
 
  protected void lookup(int duration, String fingerprint) {
    Client client = Client.create();
    client.addFilter(new LoggingFilter(System.out));
    client.addFilter(new GZIPContentEncodingFilter(true));
    WebResource webResource = client.resource(this.ACOUSTID_SERVICE_URL);
   
// DONE: convert this (parameter in url) to form encoded post
//    JSONObject object = webResource.queryParam("client", SERVICE_KEY)
//          .queryParam("duration", "341")
//          .queryParam("meta", "recordings releasegroups")
View Full Code Here

        final String SERVICE_URL = "http://" + squeezeboxServerHost + ":" + squeezeboxServerPort + "/jsonrpc.js";
        long offset = 0;

        try {
            JSONObject request = createRequest(offset, CHUNK_SIZE);
            Client c = Client.create();
            if (squeezeboxServerUsername != null && squeezeboxServerUsername.length() > 0) {
                if (squeezeboxServerPassword != null && squeezeboxServerPassword.length() > 0) {
                    c.addFilter(new HTTPBasicAuthFilter(squeezeboxServerUsername, squeezeboxServerPassword));
                } else if (squeezeboxServerPasswordHash != null && squeezeboxServerPasswordHash.length() > 0) {
                    c.addFilter(new HTTPBasicAuthFilter(squeezeboxServerUsername, squeezeboxServerPasswordHash));
                }
            }
            JSONObject response = null;
            if (squeezeboxServerPasswordHash != null && squeezeboxServerPasswordHash.length() > 0) {
                response = c.resource(SERVICE_URL).accept("application/json").header("X-Scanner", 1).post(JSONObject.class, request);
            } else {
                response = c.resource(SERVICE_URL).accept("application/json").post(JSONObject.class, request);
            }
            ObjectMapper mapper = new ObjectMapper();
            trackList = mapper.readValue(response.getString("result"), TrackListData.class);

            if (trackList != null) {
                while (trackList != null && !isAborted()) {
                    long i = 0;
                    entityManager.getTransaction().begin();
                    for (TrackData track : trackList.getTracks()) {
                        progressHandler.progress(getId(), track.getFile(), trackList.getOffset() + i + 1, trackList.getCount());
                        try {
                            importNewPlayableElement(track);
                        } catch (ConstraintViolationException e) {
                            //TODO: Change this so it uses the logging framework
                            System.err.println("ERROR when importing: " + track.getFile() + ": ");
                            for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
                                System.err.println("- " + violation.getLeafBean().getClass().getSimpleName() + "." + violation.getPropertyPath().toString() + ": " + violation.getMessage());
                            }
                        }
                        i++;
                    }
                    entityManager.flush();
                    entityManager.clear();
                    entityManager.getTransaction().commit();
                    if (offset + trackList.getTracks().size() < trackList.getCount()) {
                        offset = offset + trackList.getTracks().size();
                        request = createRequest(offset, CHUNK_SIZE);
                        if (squeezeboxServerPasswordHash != null && squeezeboxServerPasswordHash.length() > 0) {
                            response = c.resource(SERVICE_URL).accept("application/json").header("X-Scanner", 1).post(JSONObject.class, request);
                        } else {
                            response = c.resource(SERVICE_URL).accept("application/json").post(JSONObject.class, request);
                        }
                        trackList = mapper.readValue(response.getString("result"), TrackListData.class);
                    } else {
                        trackList = null;
                    }
View Full Code Here

        final String SERVICE_URL = "http://" + spotifyDaemonHost + ":" + spotifyDaemonPort;

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        Client c = Client.create();

        List<String> playlistUris = new ArrayList<String>();
        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")) {
View Full Code Here

    }

    private void importRelease(Map<String, List<String>> result, Map<String, String> artistCache, Map<String, String> personCache, Map<String, String> labelCache, Map<String, String> genreCache, Map<String, String> styleCache, String discogsReleaseId) throws ParserConfigurationException, IOException, SAXException {
//        String data = Client.create().resource("http://www.discogs.com/release/" + discogsReleaseId + "?f=xml&api_key=" + API_KEY).accept(MediaType.APPLICATION_XML).header("Accept-Encoding", "gzip").header("User-Agent",USER_AGENT).get(String.class);
// Logging version
      Client client = Client.create();
    client.addFilter(new LoggingFilter(System.out));
    WebResource webResource = client.resource("http://www.discogs.com/release/" + discogsReleaseId + "?f=xml&api_key=" + API_KEY);
    String data = webResource.accept(MediaType.APPLICATION_XML).header("Accept-Encoding", "gzip").header("User-Agent",USER_AGENT).get(String.class);
   
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(data.getBytes()));
        NodeList releases = doc.getElementsByTagName("release");
        for (int i = 0; i < releases.getLength(); i++) {
View Full Code Here

  }

  @Test
  public void testRegistration() throws JSONException, Exception {
    RegistrationResponse response;
    Client client = createTestClient();
    WebResource webResource = client.resource(AGENT_URL + "test/register");
    response = webResource.type(MediaType.APPLICATION_JSON)
        .post(RegistrationResponse.class, createDummyJSONRegister());
    Assert.assertEquals(RegistrationStatus.OK, response.getResponseStatus());
  }
View Full Code Here

TOP

Related Classes of com.sun.jersey.api.client.Client

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.