For this class exercise, you will write a Java program to consume data from the MusicBrainz Web Service, which provides a RESTful web service interface to the MusicBrainz Databse. The database can be searched by recording and artist as well as many other fields.
The example program with which you have been provided is hard coded to search for recordings with titles that that match the query "Carry on Wayward Son". When you run it, it will print out a very large string of XML, the beginning of which you can see in the example below.
java MBSimpleRequestAPIExample <?xml version="1.0" encoding="UTF-8" standalone="yes"?><metadata created="2020-01-26T18:33:51.293Z" xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ns2="http://musicbrainz.org/ns/ext#-2.0"><recording-list count="86" offset="0"><recording id="34608c81-c15f-4f97-9146-cf7f25789ff9" ns2:score="100"><title>Carry on Wayward Son</title><length>259000</length><artist-credit><name-credit><name>Tufts University Amalgamates</name><artist id="47f61709-da5d-437c-ac0f-366332a66fa9"><name>Tufts University Amalgamates</name><sort-name>Tufts University Amalgamates</sort-name></artist></name-credit></artist-credit><release-list><release id="f2bc1557-94b2-44a8-850a-e41f37669000"><title>Hands Off The Mannequin!</title><status>Official</status><release-group id="f9edb063-44ea-4bf0-829e-2bbb67e4696f" type="Album" type-id="f529b476-6e62-324f-b0aa-1f3e33d313fc"><title>Hands Off The Mannequin!</title><primary-type id="f529b476-6e62-324f-b0aa-1f3e33d313fc">Album</primary-type></release-group><date>2011</date><country>US</country><release-event-list><release-event><date>2011</date><area id="489ce91b-6658-3307-9877-795b68554c98"><name>United States</name><sort-name>United States</sort-name><iso-3166-1-code-list><iso-3166-1-code>US</iso-3166-1-code></iso-3166-1-code-list></area></release-event></release-event-list><medium-list count="1"><track-count>13</track-count><medium><position>1</position><track-list count="13" offset="9"><track id="f9d76101-67bd-4f9a-94e3-37fe6a251d51"><number>10</number><title>Carry on Wayward Son</title><length>259000</length></track></track-list></medium></medium-list></release></release-list></recording>...
You will modify the program to make a number of changes:
When finished, your output should look similar to the example below.
java MBSimpleRequestAPIExample smells like teen spirit 1) Smells Like Teen Spirit by Le Bang Bang 2) Smells Like Teen Spirit by Tori Amos 3) Smells Like Teen Spirit by The Arrogant Sons of Bitches 4) Smells Like Teen Spirit by Tori Amos 5) Smells Like Teen Spirit by Cássia Eller 6) Smells Like Teen Spirit by Hammerschmitt 7) Smells Like Teen Spirit by René Amesz Baggi Begovic 8) Smells Like Teen Spirit by Kevin Devine 9) Smells Like Teen Spirit by Tori Amos 10) Smells Like Teen Spirit by Mountain Men 11) Smells Like Teen Spirit by Tori Amos 12) Smells Like Teen Spirit by MAN WITH A MISSION 13) Smells Like Teen Spirit by Tori Amos 14) Smells Like Teen Spirit by Deep Schrott 15) Smells Like Teen Spirit by Ben Wild and the Wild Band 16) Smells Like Teen Spirit by Young Widows 17) Smells Like Teen Spirit by Ben Williams 18) Smells Like Teen Spirit by Robert Glasper 19) Smells Like Teen Spirit by ONE OK ROCK 20) Smells Like Teen Spirit by DJ MAGIC DRAGON 21) Smells Like Teen Spirit by The International Classic Rock Orchestra 22) Smells Like Teen Spirit by Phish 23) Smells Like Teen Spirit by Tori Amos 24) Smells Like Teen Spirit by Nirvana 25) Smells Like Teen Spirit by Nirvana
Your request to the webservice is based on a URL. For the MusicBrainz
Web Service, the URL is "https://musicbrainz.org/ws/2/recording?query="{query string}"
.
You specify the desired response format using an &fmt={value}
attribute at the end of the URL with a value of json
or
xml
.
The example program shows how
to use Java's HTTP support to open an HttpURLConncection
to
the URL for the web service. Readers are wrapped around the connection to
facilitate access to the response text.
The first design decision that you have to make is to choose to work with either JSON or XML responses. You may make that choice based on the approach you want to use for parsing the response string. In no particular order, you can consider doing:
BufferedReader
wrapped around the
URL connection directly in the fromJson()
call. You will
have to create classes that have a similar structure to the JSON
message. There are many Gson tutorial websites available. One of them
is
here.
[]
in the JSON response indicates an array
which you will have to capture as a List
even if
there is only one array element.
{}
in the JSON response indicates a class
which you will have to capture as a class that you create with
attributes for the elements within that {}
structure.
java.util.regex.Pattern
operation can
capture the values for all the elements that you are interested
in.
StringMatcher
, or String
substring
operations to extract the values that you want.
RecordingsSearch
. When you are ready to parse the JSON,
you simply need to pass your class as a parameter, e.g.
RecordingSearch search = gson.fromJson(json, RecordingSearch.class);
artist-credit
attribute.
In these cases (or any other, really) you may add a
@SerializedName
annotation to the field, to help Gson
find it, e.g.@SerializedName("artist-credit") private List<ArtistCredit> credits;
Once you have decided on the approach that you will use for parsing the input, start with MBSimpleRequestAPIExample.java as the initial base.
Submit a ZIP file (.zip file extension) that includes your Java source files and example of the output generated by the program when it runs.