Skip to content

Package: MusicBrainzResponse

MusicBrainzResponse

nameinstructionbranchcomplexitylinemethod
MusicBrainzResponse(Optional, String)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
of(ResponseEntity, Function)
M: 26 C: 32
55%
M: 2 C: 1
33%
M: 2 C: 1
33%
M: 5 C: 6
55%
M: 0 C: 1
100%
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.Optional;
31: import java.util.function.Function;
32: import java.io.StringReader;
33: import jakarta.xml.bind.JAXBContext;
34: import jakarta.xml.bind.JAXBException;
35: import org.springframework.http.ResponseEntity;
36: import org.musicbrainz.ns.mmd_2.Metadata;
37: import it.tidalwave.bluemarine2.rest.RestException;
38: import it.tidalwave.bluemarine2.rest.RestResponse;
39: import lombok.extern.slf4j.Slf4j;
40:
41: /***********************************************************************************************************************
42: *
43: * This class encapsulates a response from the MusicBrainz API, including the requested datum - if available - and some
44: * status codes.
45: *
46: * @author Fabrizio Giudici
47: *
48: **********************************************************************************************************************/
49: @Slf4j
50: class MusicBrainzResponse<T> extends RestResponse<T>
51: {
52: /*******************************************************************************************************************
53: *
54: *
55: ******************************************************************************************************************/
56: public MusicBrainzResponse (@Nonnull final Optional<T> datum, @Nonnull final String responseStatus)
57: {
58: super(datum, responseStatus);
59: }
60:
61: /*******************************************************************************************************************
62: *
63: * Creates a {@code Response} containing a datum out of a {@link ResponseEntity} applying a parser. The parser
64: * receives an XML DOM as the input - it typically uses XPath to extract information.
65: *
66: * @param <X> the type of the datum
67: * @param response the HTTP response
68: * @param parser the parser that produces the datum
69: * @return the {@code Response}
70: *
71: ******************************************************************************************************************/
72: @Nonnull
73: public static <X> MusicBrainzResponse<X> of (@Nonnull final ResponseEntity<String> response,
74: @Nonnull final Function<Metadata, X> function)
75: {
76: final int httpStatus = response.getStatusCodeValue();
77: final String statusCodeAsString = response.getStatusCode().toString();
78:
79:• switch (httpStatus)
80: {
81: case 200: // OK
82: try
83: {
84: final JAXBContext ctx = JAXBContext.newInstance("org.musicbrainz.ns.mmd_2");
85: final Metadata metadata = (Metadata)ctx.createUnmarshaller().unmarshal(new StringReader(response.getBody()));
86: return new MusicBrainzResponse<>(Optional.of(function.apply(metadata)), statusCodeAsString);
87: }
88: catch (JAXBException e)
89: {
90: throw new RuntimeException(e);
91: }
92:
93: case 400: // BAD REQUEST
94: log.warn(">>>> returning HTTP status code: {}", response.getStatusCode());
95: return new MusicBrainzResponse<>(Optional.empty(), statusCodeAsString);
96:
97: default:
98: throw new RestException("Unexpected HTTP status: " + response.getStatusCode(), response.getStatusCode());
99: }
100: }
101: }