Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// TODO 1: Complete this class, look for the TODO's below. package edu.cuny.brooklyn.web; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URISyntaxException; import java.net.URL;

// TODO 1: Complete this class, look for the TODO's below.
package edu.cuny.brooklyn.web;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NasaApodService {
private final static Logger LOGGER = LoggerFactory.getLogger(NasaApodService.class);
/*
* Query Parameters
* Parameter Type Default Description
* date YYYY-MM-DD today The date of the APOD image to retrieve
* hd bool False Retrieve the URL for the high resolution image
* api_key string DEMO_KEY api.nasa.gov key for expanded usage (to obtain at https://api.nasa.gov/index.html#apply-for-an-api-key)
*
* Example query
* https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY
*/
private final static String API_END_POINT = "https://api.nasa.gov/planetary/apod";
private String apiKey;
public NasaApodService(String apiKey) {
this.apiKey = apiKey;
}
public NasaApod getApod(String date, boolean highResolution) throws IOException, URISyntaxException {
// TODO 1(a): use the RequestParameterBuilder to build the query String,
// i.e. the query variable should references the value of the
// query string.
String query = "";
HttpURLConnection conn = sendHttpRequest(API_END_POINT, query);
// check the status
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
logErrorMessage(conn);
throw new IllegalStateException("Server responses with " + status);
}
// get the object
NasaApod nasaApod = readNasaApod(conn);
// disconnect to release resource
conn.disconnect();
LOGGER.info("Got the NASA APOD from the service.");
return nasaApod;
}
private void logErrorMessage(HttpURLConnection conn) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()))) {
String line;
while ((line = reader.readLine()) != null) {
LOGGER.error("Line: " + line);
}
}
}
private HttpURLConnection sendHttpRequest(String apiWebResource, String query) throws IOException {
URL url = new URL(apiWebResource + "?" + query);
LOGGER.info("url: " + url.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
return conn;
}
private NasaApod readNasaApod(HttpURLConnection conn) throws IOException, URISyntaxException {
// TODO 1(b): construct a NasaApod object from returned JSON object/array
NasaApod nasaApod = null;
return nasaApod;
}

}

// TODO 2: complete the fromJsonObject method below
package edu.cuny.brooklyn.web;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javax.json.JsonObject;
public class NasaApod {
/* Example:
* {
* "date": "2018-05-07",
* "explanation": "Why is there a large boulder near the center of Tycho's peak? Tycho crater on the Moon is one of the easiest features to see, visible even to the unaided eye (inset, lower right). But at the center of Tycho (inset, upper left) is a something unusual -- a 120-meter boulder. This boulder was imaged at very high resolution at sunrise, over the past decade, by the Moon-circling Lunar Reconnaissance Orbiter (LRO). The leading origin hypothesis is that that the boulder was thrown during the tremendous collision that formed Tycho crater about 110 million years ago, and by chance came back down right near the center of the newly-formed central mountain. Over the next billion years meteor impacts and moonquakes should slowly degrade Tycho's center, likely causing the central boulder to tumble 2000 meters down to the crater floor and disintegrate.",
* "hdurl": "https://apod.nasa.gov/apod/image/1805/TychoBoulder2_LRO_960.jpg",
* "media_type": "image",
* "service_version": "v1",
* "title": "The Unusual Boulder at Tycho's Peak",
* "url": "https://apod.nasa.gov/apod/image/1805/TychoBoulder2_LRO_960.jpg"
* }
*/
private final static String DATE_KEY = "date";
private final static String TITLE_KEY = "title";
private final static String EXPLANATION_KEY = "explanation";
private final static String MEDIA_TYPE_KEY = "media_type";
private final static String URL_KEY = "url";
private final static String HDURL_KEY = "hdurl";
public final static DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private LocalDate date;
private String explanantion;
private String title;
private URI hdPictureURI;
private URI mediaURI;
private String mediaType;
public NasaApod(LocalDate date, String title, String explanation, String mediaType, URI mediaURI,
URI hdPictureURI) {
this.date = date;
this.explanantion = explanation;
this.title = title;
this.mediaType = mediaType;
this.mediaURI = mediaURI;
this.hdPictureURI = hdPictureURI;
}
public LocalDate getDate() {
return date;
}
public String getDateAsString() {
return date.format(DATETIME_FORMATTER);
}
public String getExplanantion() {
return explanantion;
}
public String getTitle() {
return title;
}
public URI getHdPictureURI() {
return hdPictureURI;
}
public URI getMediaURI() {
return mediaURI;
}
public String getMediaType() {
return mediaType;
}
public static NasaApod fromJsonObject(JsonObject obj) throws URISyntaxException {
// TODO 2(a): parsing the JsonObject obj to obtain a NasaApod object. Note
// that this method is expected to be called in the readNasaApod method
// in the NasaApodService class
return null;
}
public static String formatDate(LocalDate date) {
return date.format(DATETIME_FORMATTER);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

Solve Problem x - 2 2(x - 5)

Answered: 1 week ago

Question

The amount of work I am asked to do is reasonable.

Answered: 1 week ago

Question

What is conservative approach ?

Answered: 1 week ago