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;
}
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;
}
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;
}
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));
}
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);
}
Aggregations