Search in sources :

Example 16 with ApplicationException

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

the class AbstractMusicBrainzClient method executeWSRequest.

protected String executeWSRequest(WebserviceInvocation invocation, String path, List<NameValuePair> params) throws ApplicationException {
    String response = null;
    HttpGet httpGet = new HttpGet(getURI(path, params));
    httpGet.setHeader(USER_AGENT, CLIENT_INFO);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        long elapsedMs = -currentTimeMillis();
        response = httpClient.execute(httpGet, responseHandler);
        elapsedMs += currentTimeMillis();
        sleep(Math.max(INTERVAL_MS - elapsedMs, 0));
    } catch (HttpResponseException e) {
        LOG.warn(format("MusicBrainz internal error: %d, %s", e.getStatusCode(), e.getMessage()));
        throw new ApplicationException("MusicBrainz internal error!", e);
    } catch (IOException e) {
        throw new ApplicationException("MusicBrainz communication failed!", e);
    } catch (InterruptedException e) {
        LOG.warn("MusicBrainz sleep interrupted!", e);
    }
    webserviceHistoryService.logWebserviceInvocation(invocation);
    return response;
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 17 with ApplicationException

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

the class AbstractMusicBrainzClient method getURI.

protected URI getURI(String path, 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 MusicBrainz 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 18 with ApplicationException

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

the class MusicBrainzServiceTest method handlesArtistFailureDuringUpdate.

@Test
public void handlesArtistFailureDuringUpdate() throws ApplicationException {
    final String revName = reverse(artistName);
    submitFile(additionDao, getFile(revName, albumName, trackName));
    Mockito.when(service.artistQueryClient.get(revName)).thenThrow(new ApplicationException("Fail", new HttpResponseException(503, "Overloaded")));
    service.updateDiscography();
    List<MBAlbum> albums = service.getMissingAlbums(artistName, asList(TYPE_EP), null, -1, 0);
    Assert.assertEquals(2, albums.size());
    assertEquals("Switchblade / Cult of Luna", albums.get(0).getTitle());
    assertEquals("Bodies / Recluse", albums.get(1).getTitle());
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpResponseException(org.apache.http.client.HttpResponseException) MBAlbum(com.github.hakko.musiccabinet.domain.model.music.MBAlbum) Test(org.junit.Test)

Example 19 with ApplicationException

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

the class LibraryScannerService method update.

public void update(Set<String> paths, boolean isRootPaths) throws ApplicationException {
    isLibraryBeingScanned = true;
    try {
        clearImport();
        startReceivingServices();
        Set<String> rootPaths = getRootPaths(paths);
        for (String path : rootPaths) {
            Files.walkFileTree(Paths.get(path), new LibraryScanner(libraryPresenceChannel));
        }
        if (isRootPaths) {
            libraryPresenceChannel.send(msg(null, rootPaths, new HashSet<File>()));
        }
        libraryPresenceChannel.send(FINISHED_MESSAGE);
        workerThreads.await();
        updateLibrary();
    } catch (IOException | InterruptedException e) {
        throw new ApplicationException("Scanning aborted due to error!", e);
    }
    isLibraryBeingScanned = false;
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) LibraryScanner(com.github.hakko.musiccabinet.io.LibraryScanner) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 20 with ApplicationException

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

the class AbstractWSClient method authenticateParameterList.

protected void authenticateParameterList(List<NameValuePair> params) throws ApplicationException {
    Collections.sort(params, paramComparator);
    StringBuilder sb = new StringBuilder();
    for (NameValuePair param : params) {
        sb.append(param.getName()).append(param.getValue());
    }
    sb.append(API_SEC);
    try {
        MessageDigest md = MessageDigest.getInstance("md5");
        params.add(new BasicNameValuePair(PARAM_API_SIG, new String(encodeHex(md.digest(sb.toString().getBytes(UTF8))))));
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        throw new ApplicationException("Can not make authenticated call!", e);
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

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