Skip to content

Package: DefaultMusicBrainzMetadataProvider

DefaultMusicBrainzMetadataProvider

nameinstructionbranchcomplexitylinemethod
DefaultMusicBrainzMetadataProvider()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
findReleaseGroupByTitle(String, String[])
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
findReleaseListByToc(String, String[])
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getHost()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getResource(MusicBrainzMetadataProvider.ResourceType, String, String[])
M: 0 C: 49
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
includesToString(String, String[])
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setHost(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.metadata.musicbrainz.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.List;
31: import java.util.stream.Stream;
32: import java.io.IOException;
33: import org.springframework.http.ResponseEntity;
34: import org.musicbrainz.ns.mmd_2.Metadata;
35: import org.musicbrainz.ns.mmd_2.ReleaseGroupList;
36: import org.musicbrainz.ns.mmd_2.ReleaseList;
37: import it.tidalwave.bluemarine2.metadata.musicbrainz.MusicBrainzMetadataProvider;
38: import it.tidalwave.bluemarine2.rest.CachingRestClientSupport;
39: import it.tidalwave.bluemarine2.rest.RestResponse;
40: import lombok.Getter;
41: import lombok.Setter;
42: import lombok.extern.slf4j.Slf4j;
43: import static java.util.stream.Collectors.*;
44: import static it.tidalwave.bluemarine2.metadata.cddb.impl.MusicBrainzUtilities.escape;
45:
46: /***********************************************************************************************************************
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @Slf4j
52: public class DefaultMusicBrainzMetadataProvider extends CachingRestClientSupport implements MusicBrainzMetadataProvider
53: {
54: private static final String URL_RELEASE_GROUP = "http://%s/ws/2/release-group/?query=release:%s";
55:
56: private static final String URL_DISCID = "http://%s/ws/2/discid/?toc=%s%s";
57:
58: private static final String URL_RESOURCE = "http://%s/ws/2/%s/%s%s";
59:
60: @Getter @Setter
61: private String host = "musicbrainz.org";
62:
63: /*******************************************************************************************************************
64: *
65: *
66: *
67: ******************************************************************************************************************/
68: public DefaultMusicBrainzMetadataProvider()
69: {
70: // sometimes MusicBrainz returns HTTP 500, but it's usually transitory
71: setRetryStatusCodes(List.of(500, 503));
72: setMaxRetry(10);
73: setThrottleLimit(1500);
74: }
75:
76: /*******************************************************************************************************************
77: *
78: * {@inheritDoc}
79: *
80: ******************************************************************************************************************/
81: @Override @Nonnull
82: public RestResponse<ReleaseGroupList> findReleaseGroupByTitle (@Nonnull final String title,
83: @Nonnull final String ... includes)
84: throws IOException, InterruptedException
85: {
86: log.debug("findReleaseGroupByTitle({})", title);
87: final ResponseEntity<String> response = request(String.format(URL_RELEASE_GROUP, host, escape(title)));
88: return MusicBrainzResponse.of(response, Metadata::getReleaseGroupList);
89: }
90:
91: /*******************************************************************************************************************
92: *
93: * {@inheritDoc}
94: *
95: ******************************************************************************************************************/
96: @Override @Nonnull
97: public RestResponse<ReleaseList> findReleaseListByToc (@Nonnull final String toc,
98: @Nonnull final String ... includes)
99: throws IOException, InterruptedException
100: {
101: log.debug("findReleaseListByToc({}, {})", toc, includes);
102: final ResponseEntity<String> response = request(String.format(URL_DISCID, host, toc, includesToString("&", includes)));
103: return MusicBrainzResponse.of(response, Metadata::getReleaseList);
104: }
105:
106: /*******************************************************************************************************************
107: *
108: * {@inheritDoc}
109: *
110: ******************************************************************************************************************/
111: @Override @Nonnull
112: public <T> RestResponse<T> getResource (@Nonnull final ResourceType<T> resourceType,
113: @Nonnull final String id,
114: @Nonnull final String ... includes)
115: throws IOException, InterruptedException
116: {
117: log.debug("getResource({}. {}, {})", resourceType, id, includes);
118: final String url = String.format(URL_RESOURCE, host, resourceType.getName(), id, includesToString("?", includes));
119: return MusicBrainzResponse.of(request(url), resourceType.getResultProvider());
120: }
121:
122: /*******************************************************************************************************************
123: *
124: *
125: ******************************************************************************************************************/
126: @Nonnull
127: private static String includesToString (@Nonnull final String separator, @Nonnull final String ... includes)
128: {
129: return Stream.of(includes).collect(joining("%2b", separator + "inc=", ""));
130: }
131: }