Added exception information

master
Omertron 14 years ago
parent c61d718b45
commit 6667b4679a

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

Loading…
Cancel
Save