Added exception information
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.moviejukebox.themoviedb;
|
||||
|
||||
public class MovieDbException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = -8952129102483143278L;
|
||||
|
||||
public enum MovieDbExceptionType {
|
||||
|
||||
UNKNOWN_CAUSE, INVALID_URL, HTTP_404_ERROR, MOVIE_ID_NOT_FOUND, MAPPING_FAILED, CONNECTION_ERROR;
|
||||
}
|
||||
private final MovieDbExceptionType exceptionType;
|
||||
private final String response;
|
||||
|
||||
public MovieDbException(final MovieDbExceptionType exceptionType,
|
||||
final String response) {
|
||||
|
||||
super();
|
||||
this.exceptionType = exceptionType;
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public MovieDbExceptionType getExceptionType() {
|
||||
|
||||
return exceptionType;
|
||||
}
|
||||
|
||||
public String getResponse() {
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
*/
|
||||
package com.moviejukebox.themoviedb;
|
||||
|
||||
import com.moviejukebox.themoviedb.MovieDbException.MovieDbExceptionType;
|
||||
import com.moviejukebox.themoviedb.model.*;
|
||||
import com.moviejukebox.themoviedb.tools.ApiUrl;
|
||||
import com.moviejukebox.themoviedb.tools.FilteringLayout;
|
||||
@@ -71,13 +72,18 @@ public class TheMovieDb {
|
||||
* @param apiKey
|
||||
* @throws IOException
|
||||
*/
|
||||
public TheMovieDb(String apiKey) throws IOException {
|
||||
public TheMovieDb(String apiKey) throws MovieDbException {
|
||||
this.apiKey = apiKey;
|
||||
URL configUrl = tmdbConfigUrl.getQueryUrl("");
|
||||
String webPage = WebBrowser.request(configUrl);
|
||||
WrapperConfig wc = mapper.readValue(webPage, WrapperConfig.class);
|
||||
tmdbConfig = wc.getTmdbConfiguration();
|
||||
FilteringLayout.addApiKey(apiKey);
|
||||
|
||||
try {
|
||||
WrapperConfig wc = mapper.readValue(webPage, WrapperConfig.class);
|
||||
tmdbConfig = wc.getTmdbConfiguration();
|
||||
} catch (IOException error) {
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, "Failed to read configuration");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,11 +96,12 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the proxy information
|
||||
* Set the proxy information
|
||||
*
|
||||
* @param host
|
||||
* @param port
|
||||
* @param username
|
||||
* @param password
|
||||
* @param password
|
||||
*/
|
||||
public void setProxy(String host, String port, String username, String password) {
|
||||
WebBrowser.setProxyHost(host);
|
||||
@@ -105,8 +112,9 @@ public class TheMovieDb {
|
||||
|
||||
/**
|
||||
* Set the connection and read time out values
|
||||
*
|
||||
* @param connect
|
||||
* @param read
|
||||
* @param read
|
||||
*/
|
||||
public void setTimeout(int connect, int read) {
|
||||
WebBrowser.setWebTimeoutConnect(connect);
|
||||
@@ -119,15 +127,16 @@ public class TheMovieDb {
|
||||
* through movies quickly. http://help.themoviedb.org/kb/api/search-movies
|
||||
* TODO: Make the allResults work
|
||||
*/
|
||||
public List<MovieDb> searchMovie(String movieName, String language, boolean allResults) {
|
||||
public List<MovieDb> searchMovie(String movieName, String language, boolean allResults) throws MovieDbException {
|
||||
|
||||
URL url = tmdbSearchMovie.getQueryUrl(movieName, language, 1);
|
||||
String webPage = WebBrowser.request(url);
|
||||
try {
|
||||
URL url = tmdbSearchMovie.getQueryUrl(movieName, language, 1);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperResultList resultList = mapper.readValue(webPage, WrapperResultList.class);
|
||||
return resultList.getResults();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to find movie: " + ex.getMessage());
|
||||
return new ArrayList<MovieDb>();
|
||||
} catch (IOException error) {
|
||||
LOGGER.warn("Failed to find movie: " + error.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,15 +148,16 @@ public class TheMovieDb {
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
public MovieDb getMovieInfo(int movieId, String language) {
|
||||
public MovieDb getMovieInfo(int movieId, String language) throws MovieDbException {
|
||||
|
||||
URL url = tmdbMovieInfo.getIdUrl(movieId, language);
|
||||
String webPage = WebBrowser.request(url);
|
||||
try {
|
||||
URL url = tmdbMovieInfo.getIdUrl(movieId, language);
|
||||
String webPage = WebBrowser.request(url);
|
||||
return mapper.readValue(webPage, MovieDb.class);
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie info: " + ex.getMessage());
|
||||
} catch (IOException error) {
|
||||
LOGGER.warn("Failed to get movie info: " + error.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return new MovieDb();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,15 +168,16 @@ public class TheMovieDb {
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
public MovieDb getMovieInfoImdb(String imdbId, String language) {
|
||||
public MovieDb getMovieInfoImdb(String imdbId, String language) throws MovieDbException {
|
||||
|
||||
URL url = tmdbMovieInfo.getIdUrl(imdbId, language);
|
||||
String webPage = WebBrowser.request(url);
|
||||
try {
|
||||
URL url = tmdbMovieInfo.getIdUrl(imdbId, language);
|
||||
String webPage = WebBrowser.request(url);
|
||||
return mapper.readValue(webPage, MovieDb.class);
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie info: " + ex.getMessage());
|
||||
} catch (IOException error) {
|
||||
LOGGER.warn("Failed to get movie info: " + error.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return new MovieDb();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,16 +188,17 @@ public class TheMovieDb {
|
||||
* @param country
|
||||
* @return
|
||||
*/
|
||||
public List<AlternativeTitle> getMovieAlternativeTitles(int movieId, String country) {
|
||||
public List<AlternativeTitle> getMovieAlternativeTitles(int movieId, String country) throws MovieDbException {
|
||||
|
||||
URL url = tmdbMovieAltTitles.getIdUrl(movieId, country);
|
||||
String webPage = WebBrowser.request(url);
|
||||
try {
|
||||
URL url = tmdbMovieAltTitles.getIdUrl(movieId, country);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperAlternativeTitles at = mapper.readValue(webPage, WrapperAlternativeTitles.class);
|
||||
return at.getTitles();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie alternative titles: " + ex.getMessage());
|
||||
} catch (IOException error) {
|
||||
LOGGER.warn("Failed to get movie alternative titles: " + error.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return new ArrayList<AlternativeTitle>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,12 +208,13 @@ public class TheMovieDb {
|
||||
* @param movieId
|
||||
* @return
|
||||
*/
|
||||
public List<Person> getMovieCasts(int movieId) {
|
||||
public List<Person> getMovieCasts(int movieId) throws MovieDbException {
|
||||
|
||||
List<Person> people = new ArrayList<Person>();
|
||||
|
||||
URL url = tmdbMovieCasts.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
try {
|
||||
URL url = tmdbMovieCasts.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperMovieCasts mc = mapper.readValue(webPage, WrapperMovieCasts.class);
|
||||
|
||||
// Add a cast member
|
||||
@@ -219,10 +232,10 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
return people;
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie casts: " + ex.getMessage());
|
||||
} catch (IOException error) {
|
||||
LOGGER.warn("Failed to get movie casts: " + error.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return people;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,11 +246,12 @@ public class TheMovieDb {
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
public List<Artwork> getMovieImages(int movieId, String language) {
|
||||
public List<Artwork> getMovieImages(int movieId, String language) throws MovieDbException {
|
||||
|
||||
List<Artwork> artwork = new ArrayList<Artwork>();
|
||||
URL url = tmdbMovieImages.getIdUrl(movieId, language);
|
||||
String webPage = WebBrowser.request(url);
|
||||
try {
|
||||
URL url = tmdbMovieImages.getIdUrl(movieId, language);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperImages mi = mapper.readValue(webPage, WrapperImages.class);
|
||||
|
||||
// Add all the posters to the list
|
||||
@@ -253,10 +267,10 @@ public class TheMovieDb {
|
||||
}
|
||||
|
||||
return artwork;
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie images: " + ex.getMessage());
|
||||
} catch (IOException error) {
|
||||
LOGGER.warn("Failed to get movie images: " + error.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return artwork;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,16 +280,18 @@ public class TheMovieDb {
|
||||
* @param movieId
|
||||
* @return
|
||||
*/
|
||||
public List<Keyword> getMovieKeywords(int movieId) {
|
||||
public List<Keyword> getMovieKeywords(int movieId) throws MovieDbException {
|
||||
|
||||
URL url = tmdbMovieKeywords.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbMovieKeywords.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperMovieKeywords mk = mapper.readValue(webPage, WrapperMovieKeywords.class);
|
||||
return mk.getKeywords();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie keywords: " + ex.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return new ArrayList<Keyword>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -286,16 +302,18 @@ public class TheMovieDb {
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
public List<ReleaseInfo> getMovieReleaseInfo(int movieId, String language) {
|
||||
public List<ReleaseInfo> getMovieReleaseInfo(int movieId, String language) throws MovieDbException {
|
||||
|
||||
URL url = tmdbMovieReleaseInfo.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbMovieReleaseInfo.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperReleaseInfo ri = mapper.readValue(webPage, WrapperReleaseInfo.class);
|
||||
return ri.getCountries();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie release information: " + ex.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return new ArrayList<ReleaseInfo>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,11 +324,13 @@ public class TheMovieDb {
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
public List<Trailer> getMovieTrailers(int movieId, String language) {
|
||||
public List<Trailer> getMovieTrailers(int movieId, String language) throws MovieDbException {
|
||||
|
||||
List<Trailer> trailers = new ArrayList<Trailer>();
|
||||
URL url = tmdbMovieTrailers.getIdUrl(movieId, language);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbMovieTrailers.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperTrailers wt = mapper.readValue(webPage, WrapperTrailers.class);
|
||||
|
||||
// Add the trailer to the return list along with it's source
|
||||
@@ -318,7 +338,6 @@ public class TheMovieDb {
|
||||
trailer.setWebsite(Trailer.WEBSITE_QUICKTIME);
|
||||
trailers.add(trailer);
|
||||
}
|
||||
|
||||
// Add the trailer to the return list along with it's source
|
||||
for (Trailer trailer : wt.getYoutube()) {
|
||||
trailer.setWebsite(Trailer.WEBSITE_YOUTUBE);
|
||||
@@ -327,8 +346,8 @@ public class TheMovieDb {
|
||||
return trailers;
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie trailers: " + ex.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return trailers;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -338,16 +357,18 @@ public class TheMovieDb {
|
||||
* @param movieId
|
||||
* @return
|
||||
*/
|
||||
public List<Translation> getMovieTranslations(int movieId) {
|
||||
public List<Translation> getMovieTranslations(int movieId) throws MovieDbException {
|
||||
|
||||
URL url = tmdbMovieTranslations.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbMovieTranslations.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperTranslations wt = mapper.readValue(webPage, WrapperTranslations.class);
|
||||
return wt.getTranslations();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie tranlations: " + ex.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
return new ArrayList<Translation>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -359,13 +380,16 @@ public class TheMovieDb {
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
public CollectionInfo getCollectionInfo(int movieId, String language) {
|
||||
public CollectionInfo getCollectionInfo(int movieId, String language) throws MovieDbException {
|
||||
|
||||
URL url = tmdbCollectionInfo.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbCollectionInfo.getIdUrl(movieId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
return mapper.readValue(webPage, CollectionInfo.class);
|
||||
} catch (IOException ex) {
|
||||
return new CollectionInfo();
|
||||
LOGGER.warn("Failed to get movie tranlations: " + ex.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,7 +409,7 @@ public class TheMovieDb {
|
||||
* @param requiredSize
|
||||
* @return
|
||||
*/
|
||||
public URL createImageUrl(String imagePath, String requiredSize) {
|
||||
public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException {
|
||||
URL returnUrl = null;
|
||||
StringBuilder sb;
|
||||
|
||||
@@ -400,12 +424,11 @@ public class TheMovieDb {
|
||||
sb = new StringBuilder(tmdbConfig.getBaseUrl());
|
||||
sb.append(requiredSize);
|
||||
sb.append(imagePath);
|
||||
returnUrl = new URL(sb.toString());
|
||||
return (new URL(sb.toString()));
|
||||
} catch (MalformedURLException ex) {
|
||||
LOGGER.warn("Failed to create image URL: " + ex.getMessage());
|
||||
throw new MovieDbException(MovieDbExceptionType.INVALID_URL, returnUrl.toString());
|
||||
}
|
||||
|
||||
return returnUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -413,16 +436,17 @@ public class TheMovieDb {
|
||||
* is to be a quick and light method so you can iterate through people
|
||||
* quickly. TODO: Fix allResults
|
||||
*/
|
||||
public List<Person> searchPeople(String personName, boolean allResults) {
|
||||
public List<Person> searchPeople(String personName, boolean allResults) throws MovieDbException {
|
||||
|
||||
URL url = tmdbSearchPeople.getQueryUrl(personName, "", 1);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbSearchPeople.getQueryUrl(personName, "", 1);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperPerson resultList = mapper.readValue(webPage, WrapperPerson.class);
|
||||
return resultList.getResults();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to find person: " + ex.getMessage());
|
||||
return new ArrayList<Person>();
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,14 +457,16 @@ public class TheMovieDb {
|
||||
* @param personId
|
||||
* @return
|
||||
*/
|
||||
public Person getPersonInfo(int personId) {
|
||||
public Person getPersonInfo(int personId) throws MovieDbException {
|
||||
|
||||
URL url = tmdbPersonInfo.getIdUrl(personId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbPersonInfo.getIdUrl(personId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
return mapper.readValue(webPage, Person.class);
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get movie info: " + ex.getMessage());
|
||||
return new Person();
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,12 +478,14 @@ public class TheMovieDb {
|
||||
* @param personId
|
||||
* @return
|
||||
*/
|
||||
public List<PersonCredit> getPersonCredits(int personId) {
|
||||
public List<PersonCredit> getPersonCredits(int personId) throws MovieDbException {
|
||||
|
||||
List<PersonCredit> personCredits = new ArrayList<PersonCredit>();
|
||||
|
||||
URL url = tmdbPersonCredits.getIdUrl(personId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbPersonCredits.getIdUrl(personId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperPersonCredits pc = mapper.readValue(webPage, WrapperPersonCredits.class);
|
||||
|
||||
// Add a cast member
|
||||
@@ -465,17 +493,15 @@ public class TheMovieDb {
|
||||
cast.setPersonType(PersonType.CAST);
|
||||
personCredits.add(cast);
|
||||
}
|
||||
|
||||
// Add a crew member
|
||||
for (PersonCredit crew : pc.getCrew()) {
|
||||
crew.setPersonType(PersonType.CREW);
|
||||
personCredits.add(crew);
|
||||
}
|
||||
|
||||
return personCredits;
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get person credits: " + ex.getMessage());
|
||||
return personCredits;
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -485,12 +511,14 @@ public class TheMovieDb {
|
||||
* @param personId
|
||||
* @return
|
||||
*/
|
||||
public List<Artwork> getPersonImages(int personId) {
|
||||
public List<Artwork> getPersonImages(int personId) throws MovieDbException {
|
||||
|
||||
List<Artwork> personImages = new ArrayList<Artwork>();
|
||||
|
||||
URL url = tmdbPersonImages.getIdUrl(personId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbPersonImages.getIdUrl(personId);
|
||||
String webPage = WebBrowser.request(url);
|
||||
WrapperImages images = mapper.readValue(webPage, WrapperImages.class);
|
||||
|
||||
// Update the image type
|
||||
@@ -498,11 +526,10 @@ public class TheMovieDb {
|
||||
artwork.setArtworkType(ArtworkType.PROFILE);
|
||||
personImages.add(artwork);
|
||||
}
|
||||
|
||||
return personImages;
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get person images: " + ex.getMessage());
|
||||
return personImages;
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,14 +538,16 @@ public class TheMovieDb {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public MovieDb getLatestMovie() {
|
||||
public MovieDb getLatestMovie() throws MovieDbException {
|
||||
|
||||
URL url = tmdbLatestMovie.getIdUrl("");
|
||||
String webPage = WebBrowser.request(url);
|
||||
|
||||
try {
|
||||
URL url = tmdbLatestMovie.getIdUrl("");
|
||||
String webPage = WebBrowser.request(url);
|
||||
return mapper.readValue(webPage, MovieDb.class);
|
||||
} catch (IOException ex) {
|
||||
LOGGER.warn("Failed to get latest movie: " + ex.getMessage());
|
||||
return new MovieDb();
|
||||
throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,11 +12,13 @@
|
||||
*/
|
||||
package com.moviejukebox.themoviedb.tools;
|
||||
|
||||
import com.moviejukebox.themoviedb.MovieDbException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -27,12 +29,14 @@ import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* Web browser with simple cookies support
|
||||
*/
|
||||
public final class WebBrowser {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(WebBrowser.class);
|
||||
private static Map<String, String> browserProperties = new HashMap<String, String>();
|
||||
private static Map<String, Map<String, String>> cookies = new HashMap<String, Map<String, String>>();
|
||||
private static String proxyHost = null;
|
||||
@@ -58,27 +62,35 @@ public final class WebBrowser {
|
||||
}
|
||||
}
|
||||
|
||||
public static String request(String url) throws IOException {
|
||||
return request(new URL(url));
|
||||
public static String request(String url) throws MovieDbException {
|
||||
try {
|
||||
return request(new URL(url));
|
||||
} catch (MalformedURLException ex) {
|
||||
throw new MovieDbException(MovieDbException.MovieDbExceptionType.INVALID_URL, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static URLConnection openProxiedConnection(URL url) throws IOException {
|
||||
if (proxyHost != null) {
|
||||
System.getProperties().put("proxySet", "true");
|
||||
System.getProperties().put("proxyHost", proxyHost);
|
||||
System.getProperties().put("proxyPort", proxyPort);
|
||||
public static URLConnection openProxiedConnection(URL url) throws MovieDbException {
|
||||
try {
|
||||
if (proxyHost != null) {
|
||||
System.getProperties().put("proxySet", "true");
|
||||
System.getProperties().put("proxyHost", proxyHost);
|
||||
System.getProperties().put("proxyPort", proxyPort);
|
||||
}
|
||||
|
||||
URLConnection cnx = url.openConnection();
|
||||
|
||||
if (proxyUsername != null) {
|
||||
cnx.setRequestProperty("Proxy-Authorization", proxyEncodedPassword);
|
||||
}
|
||||
|
||||
return cnx;
|
||||
} catch (IOException ex) {
|
||||
throw new MovieDbException(MovieDbException.MovieDbExceptionType.INVALID_URL, null);
|
||||
}
|
||||
|
||||
URLConnection cnx = url.openConnection();
|
||||
|
||||
if (proxyUsername != null) {
|
||||
cnx.setRequestProperty("Proxy-Authorization", proxyEncodedPassword);
|
||||
}
|
||||
|
||||
return cnx;
|
||||
}
|
||||
|
||||
public static String request(URL url) throws IOException {
|
||||
public static String request(URL url) throws MovieDbException {
|
||||
StringWriter content = null;
|
||||
|
||||
try {
|
||||
@@ -106,9 +118,15 @@ public final class WebBrowser {
|
||||
}
|
||||
}
|
||||
return content.toString();
|
||||
} catch (IOException error) {
|
||||
throw new MovieDbException(MovieDbException.MovieDbExceptionType.CONNECTION_ERROR, null);
|
||||
} finally {
|
||||
if (content != null) {
|
||||
content.close();
|
||||
try {
|
||||
content.close();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.debug("Failed to close connection: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ package com.moviejukebox.themoviedb;
|
||||
|
||||
import com.moviejukebox.themoviedb.model.*;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.List;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
@@ -38,7 +37,7 @@ public class TheMovieDbTest {
|
||||
private static final int ID_STAR_WARS_COLLECTION = 10;
|
||||
private static final int ID_BRUCE_WILLIS = 62;
|
||||
|
||||
public TheMovieDbTest() throws IOException {
|
||||
public TheMovieDbTest() throws MovieDbException {
|
||||
tmdb = new TheMovieDb(API_KEY);
|
||||
}
|
||||
|
||||
@@ -78,7 +77,7 @@ public class TheMovieDbTest {
|
||||
* Test of searchMovie method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testSearchMovie() throws UnsupportedEncodingException {
|
||||
public void testSearchMovie() throws MovieDbException {
|
||||
LOGGER.info("searchMovie");
|
||||
|
||||
// Try a movie with less than 1 page of results
|
||||
@@ -98,7 +97,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieInfo method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieInfo() {
|
||||
public void testGetMovieInfo() throws MovieDbException {
|
||||
LOGGER.info("getMovieInfo");
|
||||
String language = "en";
|
||||
MovieDb result = tmdb.getMovieInfo(ID_BLADE_RUNNER, language);
|
||||
@@ -109,7 +108,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieAlternativeTitles method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieAlternativeTitles() {
|
||||
public void testGetMovieAlternativeTitles() throws MovieDbException {
|
||||
LOGGER.info("getMovieAlternativeTitles");
|
||||
String country = "";
|
||||
List<AlternativeTitle> results = tmdb.getMovieAlternativeTitles(ID_BLADE_RUNNER, country);
|
||||
@@ -125,7 +124,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieCasts method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieCasts() {
|
||||
public void testGetMovieCasts() throws MovieDbException {
|
||||
LOGGER.info("getMovieCasts");
|
||||
List<Person> people = tmdb.getMovieCasts(ID_BLADE_RUNNER);
|
||||
assertTrue("No cast information", people.size() > 0);
|
||||
@@ -152,7 +151,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieImages method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieImages() {
|
||||
public void testGetMovieImages() throws MovieDbException {
|
||||
LOGGER.info("getMovieImages");
|
||||
String language = "";
|
||||
List<Artwork> result = tmdb.getMovieImages(ID_BLADE_RUNNER, language);
|
||||
@@ -163,7 +162,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieKeywords method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieKeywords() {
|
||||
public void testGetMovieKeywords() throws MovieDbException {
|
||||
LOGGER.info("getMovieKeywords");
|
||||
List<Keyword> result = tmdb.getMovieKeywords(ID_BLADE_RUNNER);
|
||||
assertFalse("No keywords found", result.isEmpty());
|
||||
@@ -173,7 +172,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieReleaseInfo method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieReleaseInfo() {
|
||||
public void testGetMovieReleaseInfo() throws MovieDbException {
|
||||
LOGGER.info("getMovieReleaseInfo");
|
||||
List<ReleaseInfo> result = tmdb.getMovieReleaseInfo(ID_BLADE_RUNNER, "");
|
||||
assertFalse("Release information missing", result.isEmpty());
|
||||
@@ -183,7 +182,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieTrailers method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieTrailers() {
|
||||
public void testGetMovieTrailers() throws MovieDbException {
|
||||
LOGGER.info("getMovieTrailers");
|
||||
List<Trailer> result = tmdb.getMovieTrailers(ID_BLADE_RUNNER, "");
|
||||
assertFalse("Movie trailers missing", result.isEmpty());
|
||||
@@ -193,7 +192,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieTranslations method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieTranslations() {
|
||||
public void testGetMovieTranslations() throws MovieDbException {
|
||||
LOGGER.info("getMovieTranslations");
|
||||
List<Translation> result = tmdb.getMovieTranslations(ID_BLADE_RUNNER);
|
||||
assertFalse("No translations found", result.isEmpty());
|
||||
@@ -203,7 +202,7 @@ public class TheMovieDbTest {
|
||||
* Test of getCollectionInfo method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCollectionInfo() {
|
||||
public void testGetCollectionInfo() throws MovieDbException {
|
||||
LOGGER.info("getCollectionInfo");
|
||||
String language = "";
|
||||
CollectionInfo result = tmdb.getCollectionInfo(ID_STAR_WARS_COLLECTION, language);
|
||||
@@ -211,7 +210,7 @@ public class TheMovieDbTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateImageUrl() {
|
||||
public void testCreateImageUrl() throws MovieDbException {
|
||||
LOGGER.info("createImageUrl");
|
||||
MovieDb movie = tmdb.getMovieInfo(ID_BLADE_RUNNER, "");
|
||||
String result = tmdb.createImageUrl(movie.getPosterPath(), "original").toString();
|
||||
@@ -222,7 +221,7 @@ public class TheMovieDbTest {
|
||||
* Test of getMovieInfoImdb method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMovieInfoImdb() {
|
||||
public void testGetMovieInfoImdb() throws MovieDbException {
|
||||
LOGGER.info("getMovieInfoImdb");
|
||||
MovieDb result = tmdb.getMovieInfoImdb("tt0076759", "en-US");
|
||||
assertTrue("Error getting the movie from IMDB ID", result.getId() == 11);
|
||||
@@ -256,7 +255,7 @@ public class TheMovieDbTest {
|
||||
* Test of searchPeople method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testSearchPeople() {
|
||||
public void testSearchPeople() throws MovieDbException {
|
||||
LOGGER.info("searchPeople");
|
||||
String personName = "Bruce Willis";
|
||||
boolean allResults = false;
|
||||
@@ -268,7 +267,7 @@ public class TheMovieDbTest {
|
||||
* Test of getPersonInfo method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetPersonInfo() {
|
||||
public void testGetPersonInfo() throws MovieDbException {
|
||||
LOGGER.info("getPersonInfo");
|
||||
Person result = tmdb.getPersonInfo(ID_BRUCE_WILLIS);
|
||||
assertTrue("Wrong actor returned", result.getId() == ID_BRUCE_WILLIS);
|
||||
@@ -278,7 +277,7 @@ public class TheMovieDbTest {
|
||||
* Test of getPersonCredits method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetPersonCredits() {
|
||||
public void testGetPersonCredits() throws MovieDbException {
|
||||
LOGGER.info("getPersonCredits");
|
||||
|
||||
List<PersonCredit> people = tmdb.getPersonCredits(ID_BRUCE_WILLIS);
|
||||
@@ -289,7 +288,7 @@ public class TheMovieDbTest {
|
||||
* Test of getPersonImages method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetPersonImages() {
|
||||
public void testGetPersonImages() throws MovieDbException {
|
||||
LOGGER.info("getPersonImages");
|
||||
|
||||
List<Artwork> artwork = tmdb.getPersonImages(ID_BRUCE_WILLIS);
|
||||
@@ -300,7 +299,7 @@ public class TheMovieDbTest {
|
||||
* Test of getLatestMovie method, of class TheMovieDb.
|
||||
*/
|
||||
@Test
|
||||
public void testGetLatestMovie() {
|
||||
public void testGetLatestMovie() throws MovieDbException {
|
||||
LOGGER.info("getLatestMovie");
|
||||
MovieDb result = tmdb.getLatestMovie();
|
||||
LOGGER.info(result.toString());
|
||||
|
||||
Reference in New Issue
Block a user