* make sure unexpected JSON response data does not make us throw up NPEs

This commit is contained in:
Reinhard Pointner 2014-08-28 11:42:41 +00:00
parent 09332e8aca
commit aa16398590
2 changed files with 86 additions and 58 deletions

View File

@ -253,7 +253,7 @@ public class MediaBindingBean {
@Define("imdbid")
public String getImdbId() throws Exception {
int imdbid = getMovie().getImdbId();
Integer imdbid = getMovie().getImdbId();
if (imdbid <= 0) {
if (getMovie().getTmdbId() <= 0) {
@ -264,7 +264,7 @@ public class MediaBindingBean {
imdbid = WebServices.TheMovieDB.getMovieInfo(getMovie(), Locale.ENGLISH, false).getImdbId();
}
return String.format("tt%07d", imdbid);
return imdbid != null ? String.format("tt%07d", imdbid) : null;
}
@Define("vc")

View File

@ -208,38 +208,60 @@ public class TMDbClient implements MovieIdentificationService {
JSONObject collection = (JSONObject) response.get("belongs_to_collection");
fields.put(MovieProperty.collection, (String) collection.get("name"));
} catch (Exception e) {
// ignore
// movie doesn't belong to any collection
}
List<String> genres = new ArrayList<String>();
try {
for (JSONObject it : jsonList(response.get("genres"))) {
String name = (String) it.get("name");
if (name != null && name.length() > 0) {
genres.add(name);
}
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal genres data: " + response);
}
List<String> spokenLanguages = new ArrayList<String>();
try {
for (JSONObject it : jsonList(response.get("spoken_languages"))) {
spokenLanguages.add((String) it.get("iso_639_1"));
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal spoken_languages data: " + response);
}
List<String> alternativeTitles = new ArrayList<String>();
try {
JSONObject titles = (JSONObject) response.get("alternative_titles");
if (titles != null) {
for (JSONObject it : jsonList(titles.get("titles"))) {
alternativeTitles.add((String) it.get("title"));
}
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal alternative_titles data: " + response);
}
try {
String countryCode = locale.getCountry().isEmpty() ? "US" : locale.getCountry();
JSONObject releases = (JSONObject) response.get("releases");
if (releases != null) {
for (JSONObject it : jsonList(releases.get("countries"))) {
if (countryCode.equals(it.get("iso_3166_1"))) {
fields.put(MovieProperty.certification, (String) it.get("certification"));
}
}
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal releases data: " + response);
}
List<Person> cast = new ArrayList<Person>();
try {
JSONObject castResponse = (JSONObject) response.get("casts");
if (castResponse != null) {
for (String section : new String[] { "cast", "crew" }) {
for (JSONObject it : jsonList(castResponse.get(section))) {
Map<PersonProperty, String> person = new EnumMap<PersonProperty, String>(PersonProperty.class);
@ -252,10 +274,15 @@ public class TMDbClient implements MovieIdentificationService {
cast.add(new Person(person));
}
}
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal casts data: " + response);
}
List<Trailer> trailers = new ArrayList<Trailer>();
JSONObject trailerResponse = (JSONObject) response.get("trailers");
try {
JSONObject trailerResponse = (JSONObject) response.get("trailers");
if (trailerResponse != null) {
for (String section : new String[] { "quicktime", "youtube" }) {
for (JSONObject it : jsonList(trailerResponse.get(section))) {
Map<String, String> sources = new LinkedHashMap<String, String>();
@ -269,8 +296,9 @@ public class TMDbClient implements MovieIdentificationService {
trailers.add(new Trailer(section, it.get("name").toString(), sources));
}
}
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal trailer data: " + trailerResponse);
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal trailers data: " + response);
}
return new MovieInfo(fields, alternativeTitles, genres, spokenLanguages, cast, trailers);