diff --git a/pom.xml b/pom.xml index 7dc95ab5f..c040c4a28 100644 --- a/pom.xml +++ b/pom.xml @@ -120,7 +120,7 @@ org.yamj api-common - 1.3-SNAPSHOT + 1.3 diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index ba48e5e85..7008ea927 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -19,24 +19,6 @@ */ package com.omertron.themoviedbapi; -import static com.omertron.themoviedbapi.tools.ApiUrl.*; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang3.StringUtils; -import org.apache.http.client.methods.HttpGet; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yamj.api.common.http.CommonHttpClient; -import org.yamj.api.common.http.DigestedResponse; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.omertron.themoviedbapi.model.Account; @@ -67,11 +49,23 @@ import com.omertron.themoviedbapi.model.StatusCode; import com.omertron.themoviedbapi.model.TmdbConfiguration; import com.omertron.themoviedbapi.model.TokenAuthorisation; import com.omertron.themoviedbapi.model.TokenSession; -import com.omertron.themoviedbapi.model.Trailer; import com.omertron.themoviedbapi.model.Translation; +import com.omertron.themoviedbapi.model.Video; import com.omertron.themoviedbapi.results.TmdbResultsList; import com.omertron.themoviedbapi.results.TmdbResultsMap; import com.omertron.themoviedbapi.tools.ApiUrl; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_ADULT; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_COUNTRY; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_END_DATE; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_ID; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_INCLUDE_ALL_MOVIES; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_LANGUAGE; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_PAGE; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_QUERY; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_SESSION; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_START_DATE; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_TOKEN; +import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_YEAR; import com.omertron.themoviedbapi.tools.WebBrowser; import com.omertron.themoviedbapi.wrapper.WrapperAlternativeTitles; import com.omertron.themoviedbapi.wrapper.WrapperChanges; @@ -95,14 +89,32 @@ import com.omertron.themoviedbapi.wrapper.WrapperPersonCredits; import com.omertron.themoviedbapi.wrapper.WrapperPersonList; import com.omertron.themoviedbapi.wrapper.WrapperReleaseInfo; import com.omertron.themoviedbapi.wrapper.WrapperReviews; -import com.omertron.themoviedbapi.wrapper.WrapperTrailers; import com.omertron.themoviedbapi.wrapper.WrapperTranslations; +import com.omertron.themoviedbapi.wrapper.WrapperVideos; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.yamj.api.common.exception.ApiExceptionType; +import org.yamj.api.common.http.DigestedResponse; +import org.yamj.api.common.http.DigestedResponseReader; +import org.yamj.api.common.http.SimpleHttpClientBuilder; /** * The MovieDb API *

- * This is for version 3 of the API as specified here: http://help.themoviedb.org/kb/api/about-3 + * This is for version 3 of the API as specified here: + * http://help.themoviedb.org/kb/api/about-3 * * @author stuart.boston */ @@ -110,8 +122,10 @@ public class TheMovieDbApi { private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApi.class); private String apiKey; - private CommonHttpClient httpClient; + private CloseableHttpClient httpClient; private TmdbConfiguration tmdbConfig; + private static final String DEFAULT_CHARSET = "UTF-8"; + private final Charset charset = Charset.forName(DEFAULT_CHARSET); // API Methods private static final String BASE_MOVIE = "movie/"; private static final String BASE_PERSON = "person/"; @@ -144,7 +158,7 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TheMovieDbApi(String apiKey) throws MovieDbException { - this(apiKey, null); + this(apiKey, new SimpleHttpClientBuilder().build()); } /** @@ -154,7 +168,7 @@ public class TheMovieDbApi { * @param httpClient The httpClient to use for web requests. * @throws MovieDbException */ - public TheMovieDbApi(String apiKey, CommonHttpClient httpClient) throws MovieDbException { + public TheMovieDbApi(String apiKey, CloseableHttpClient httpClient) throws MovieDbException { this.apiKey = apiKey; this.httpClient = httpClient; @@ -208,7 +222,7 @@ public class TheMovieDbApi { throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Unable to proces delete request", url); } - final DigestedResponse response = httpClient.requestContent(httpGet); + final DigestedResponse response = DigestedResponseReader.requestContent(httpClient, httpGet, charset); if (response.getStatusCode() >= HTTP_STATUS_500) { throw new MovieDbException(ApiExceptionType.HTTP_503_ERROR, response.getContent(), response.getStatusCode(), url, null); @@ -229,42 +243,6 @@ public class TheMovieDbApi { return webpage; } - /** - * Set the proxy information - * - * @param host - * @param port - * @param username - * @param password - */ - public void setProxy(String host, int port, String username, String password) { - // should be set in HTTP client already - if (httpClient != null) { - return; - } - - WebBrowser.setProxyHost(host); - WebBrowser.setProxyPort(port); - WebBrowser.setProxyUsername(username); - WebBrowser.setProxyPassword(password); - } - - /** - * Set the connection and read time out values - * - * @param connect - * @param read - */ - public void setTimeout(int connect, int read) { - // should be set in HTTP client already - if (httpClient != null) { - return; - } - - WebBrowser.setWebTimeoutConnect(connect); - WebBrowser.setWebTimeoutRead(read); - } - /** * Compare the MovieDB object with a title & year * @@ -283,7 +261,8 @@ public class TheMovieDbApi { * @param moviedb The moviedb object to compare too * @param title The title of the movie to compare * @param year The year of the movie to compare - * @param maxDistance The Levenshtein Distance between the two titles. 0 = exact match + * @param maxDistance The Levenshtein Distance between the two titles. 0 = + * exact match * @param caseSensitive true if the comparison is to be case sensitive * @return True if there is a match, False otherwise. */ @@ -399,13 +378,16 @@ public class TheMovieDbApi { // /** - * This method is used to generate a valid request token for user based authentication. + * This method is used to generate a valid request token for user based + * authentication. * * A request token is required in order to request a session id. * - * You can generate any number of request tokens but they will expire after 60 minutes. + * You can generate any number of request tokens but they will expire after + * 60 minutes. * - * As soon as a valid session id has been created the token will be destroyed. + * As soon as a valid session id has been created the token will be + * destroyed. * * @return * @throws MovieDbException @@ -425,7 +407,8 @@ public class TheMovieDbApi { } /** - * This method is used to generate a session id for user based authentication. + * This method is used to generate a session id for user based + * authentication. * * A session id is required in order to use any of the write methods. * @@ -456,14 +439,18 @@ public class TheMovieDbApi { /** * This method is used to generate a guest session id. * - * A guest session can be used to rate movies without having a registered TMDb user account. + * A guest session can be used to rate movies without having a registered + * TMDb user account. * - * You should only generate a single guest session per user (or device) as you will be able to attach the ratings to a TMDb user - * account in the future. + * You should only generate a single guest session per user (or device) as + * you will be able to attach the ratings to a TMDb user account in the + * future. * - * There are also IP limits in place so you should always make sure it's the end user doing the guest session actions. + * There are also IP limits in place so you should always make sure it's the + * end user doing the guest session actions. * - * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. + * If a guest session is not used for the first time within 24 hours, it + * will be automatically discarded. * * @return * @throws MovieDbException @@ -483,7 +470,8 @@ public class TheMovieDbApi { } /** - * Get the basic information for an account. You will need to have a valid session id. + * Get the basic information for an account. You will need to have a valid + * session id. * * @param sessionId * @return @@ -595,7 +583,8 @@ public class TheMovieDbApi { * * It will return the single highest rated poster and backdrop. * - * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies + * found. * * @param movieId * @param language @@ -634,7 +623,8 @@ public class TheMovieDbApi { * * It will return the single highest rated poster and backdrop. * - * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies + * found. * * @param imdbId * @param language @@ -669,7 +659,8 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the alternative titles we have for a particular movie. + * This method is used to retrieve all of the alternative titles we have for + * a particular movie. * * @param movieId * @param country @@ -731,7 +722,8 @@ public class TheMovieDbApi { } /** - * This method should be used when you’re wanting to retrieve all of the images for a particular movie. + * This method should be used when you’re wanting to retrieve all of the + * images for a particular movie. * * @param movieId * @param language @@ -764,7 +756,8 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the keywords that have been added to a particular movie. + * This method is used to retrieve all of the keywords that have been added + * to a particular movie. * * Currently, only English keywords exist. * @@ -794,7 +787,8 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the release and certification data we have for a specific movie. + * This method is used to retrieve all of the release and certification data + * we have for a specific movie. * * @param movieId * @param language @@ -824,7 +818,8 @@ public class TheMovieDbApi { } /** - * This method is used to retrieve all of the trailers for a particular movie. + * This method is used to retrieve all of the trailers for a particular + * movie. * * Supported sites are YouTube and QuickTime. * @@ -834,7 +829,7 @@ public class TheMovieDbApi { * @return * @throws MovieDbException */ - public TmdbResultsList getMovieTrailers(int movieId, String language, String... appendToResponse) throws MovieDbException { + public TmdbResultsList