Search in sources :

Example 21 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class AbstractWSGetClient method getURI.

/*
	 * Assemble URI for the Last.fm web service.
	 */
protected URI getURI(List<NameValuePair> params) throws ApplicationException {
    URI uri = null;
    try {
        URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.setScheme(HTTP);
        uriBuilder.setHost(HOST);
        uriBuilder.setPath(PATH);
        for (NameValuePair param : params) {
            uriBuilder.addParameter(param.getName(), param.getValue());
        }
        uri = uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new ApplicationException("Could not create Last.fm URI!", e);
    }
    return uri;
}
Also used : NameValuePair(org.apache.http.NameValuePair) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 22 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class AbstractWSPostClient method getURI.

/*
	 * Assemble URI for the Last.fm web service.
	 */
protected URI getURI(List<NameValuePair> params) throws ApplicationException {
    URI uri = null;
    try {
        URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.setScheme(HTTP);
        uriBuilder.setHost(HOST);
        uriBuilder.setPath(PATH);
        uri = uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new ApplicationException("Could not create Last.fm URI!", e);
    }
    return uri;
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 23 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class AbstractWSPostClient method executeWSRequest.

/*
	 * Executes a request to a Last.fm web service.
	 * 
	 * When adding support for a new web service, a class extending this should be
	 * implemented. The web service can then be invoked by calling this method, using
	 * relevant parameters.
	 * 
	 * The parameter api_key, which is identical for all web service invocations, is
	 * automatically included.
	 * 
	 * The response is bundled in a WSResponse object, with eventual error code/message.
	 * 
	 * Note: For non US-ASCII characters, Last.fm distinguishes between upper and lower
	 * case. Make sure to use proper capitalization.
	 */
protected WSResponse executeWSRequest(List<NameValuePair> params) throws ApplicationException {
    authenticateParameterList(params);
    WSResponse wsResponse;
    HttpClient httpClient = getHttpClient();
    try {
        HttpPost httpPost = new HttpPost(getURI(params));
        httpPost.setEntity(new UrlEncodedFormEntity(params, CharSet.UTF8));
        HttpResponse response = httpClient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity responseEntity = response.getEntity();
        String responseBody = EntityUtils.toString(responseEntity);
        EntityUtils.consume(responseEntity);
        LOG.debug("post responseBody: " + responseBody);
        wsResponse = (statusCode == 200) ? new WSResponse(responseBody) : new WSResponse(isHttpRecoverable(statusCode), statusCode, responseBody);
    } catch (ClientProtocolException e) {
        throw new ApplicationException("The request to post data to Last.fm could not be completed!", e);
    } catch (IOException e) {
        LOG.warn("Could not post data to Last.fm!", e);
        wsResponse = new WSResponse(true, -1, "Call failed due to " + e.getMessage());
    }
    return wsResponse;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 24 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class TrackUnLoveClientTest method validateParameters.

@Test
public void validateParameters() throws ApplicationException {
    final String method = TrackUnLoveClient.METHOD;
    final String lastFmUser = "arnathalon";
    final String sessionKey = "sessionkey";
    final String artist = "artist";
    final String track = "track";
    new TrackUnLoveClient() {

        @Override
        protected WSResponse executeWSRequest(List<NameValuePair> params) throws ApplicationException {
            assertHasParameter(params, PARAM_METHOD, method);
            assertHasParameter(params, PARAM_TRACK, track);
            assertHasParameter(params, PARAM_ARTIST, artist);
            assertHasParameter(params, PARAM_SK, sessionKey);
            return null;
        }
    }.unlove(new Track(artist, track), new LastFmUser(lastFmUser, sessionKey));
}
Also used : NameValuePair(org.apache.http.NameValuePair) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) Track(com.github.hakko.musiccabinet.domain.model.music.Track) Test(org.junit.Test)

Example 25 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class UpdateNowPlayingClientTest method validateParameters.

@Test
public void validateParameters() throws ApplicationException {
    final Track track = browserDao.getTracks(browserDao.getRandomTrackIds(1)).get(0);
    final LastFmUser user = new LastFmUser("lastFmUser", "sessionKey");
    final Scrobble scrobble = new Scrobble(user, track, false);
    final String method = UpdateNowPlayingClient.METHOD;
    new UpdateNowPlayingClient() {

        @Override
        protected WSResponse executeWSRequest(List<NameValuePair> params) throws ApplicationException {
            assertHasParameter(params, PARAM_METHOD, method);
            assertHasParameter(params, PARAM_ARTIST, track.getArtist().getName());
            assertHasParameter(params, PARAM_ALBUM, track.getMetaData().getAlbum());
            assertHasParameter(params, PARAM_TRACK, track.getName());
            assertHasParameter(params, PARAM_DURATION, "" + track.getMetaData().getDuration());
            assertHasParameter(params, PARAM_SK, user.getSessionKey());
            return null;
        }
    }.updateNowPlaying(scrobble);
}
Also used : NameValuePair(org.apache.http.NameValuePair) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble) Track(com.github.hakko.musiccabinet.domain.model.music.Track) Test(org.junit.Test)

Aggregations

ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)43 Test (org.junit.Test)20 NameValuePair (org.apache.http.NameValuePair)18 StringUtil (com.github.hakko.musiccabinet.util.StringUtil)14 WebserviceInvocation (com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation)11 WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)11 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)10 WebserviceHistoryService (com.github.hakko.musiccabinet.service.lastfm.WebserviceHistoryService)10 ArrayList (java.util.ArrayList)8 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)7 IOException (java.io.IOException)6 Track (com.github.hakko.musiccabinet.domain.model.music.Track)4 ArtistUserTag (com.github.hakko.musiccabinet.domain.model.aggr.ArtistUserTag)2 Scrobble (com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)2 LastFmGroup (com.github.hakko.musiccabinet.domain.model.library.LastFmGroup)2 Period (com.github.hakko.musiccabinet.domain.model.library.Period)2 Album (com.github.hakko.musiccabinet.domain.model.music.Album)2 MBArtist (com.github.hakko.musiccabinet.domain.model.music.MBArtist)2 Tag (com.github.hakko.musiccabinet.domain.model.music.Tag)2 ArtistInfoParserImpl (com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParserImpl)2