use of org.apache.http.client.HttpResponseException in project UniversalMediaServer by UniversalMediaServer.
the class CoverArtArchiveUtil method doGetThumbnail.
@Override
protected byte[] doGetThumbnail(Tag tag, boolean externalNetwork) {
String mBID = getMBID(tag, externalNetwork);
if (mBID != null) {
// Secure exclusive access to search for this tag
CoverArtArchiveCoverLatch latch = reserveCoverLatch(mBID);
if (latch == null) {
// Couldn't reserve exclusive access, giving up
return null;
}
try {
// Check if it's cached first
CoverArtArchiveResult result = TableCoverArtArchive.findMBID(mBID);
if (result.found) {
if (result.cover != null) {
return result.cover;
} else if (System.currentTimeMillis() - result.modified.getTime() < EXPIRATION_TIME) {
// return null. Do another lookup after expireTime has passed
return null;
}
}
if (!externalNetwork) {
LOGGER.warn("Can't download cover from Cover Art Archive since external network is disabled");
LOGGER.info("Either enable external network or disable cover download");
return null;
}
DefaultCoverArtArchiveClient client = new DefaultCoverArtArchiveClient();
CoverArt coverArt;
try {
coverArt = client.getByMbid(UUID.fromString(mBID));
} catch (CoverArtException e) {
LOGGER.debug("Could not get cover with MBID \"{}\": {}", mBID, e.getMessage());
LOGGER.trace("", e);
return null;
}
if (coverArt == null || coverArt.getImages().isEmpty()) {
LOGGER.debug("MBID \"{}\" has no cover at CoverArtArchive", mBID);
TableCoverArtArchive.writeMBID(mBID, null);
return null;
}
CoverArtImage image = coverArt.getFrontImage();
if (image == null) {
image = coverArt.getImages().get(0);
}
byte[] cover = null;
try {
try (InputStream is = image.getLargeThumbnail()) {
cover = IOUtils.toByteArray(is);
} catch (HttpResponseException e) {
// Use the default image if the large thumbnail is not available
try (InputStream is = image.getImage()) {
cover = IOUtils.toByteArray(is);
}
}
TableCoverArtArchive.writeMBID(mBID, cover);
return cover;
} catch (HttpResponseException e) {
if (e.getStatusCode() == 404) {
LOGGER.debug("Cover for MBID \"{}\" was not found at CoverArtArchive", mBID);
TableCoverArtArchive.writeMBID(mBID, null);
return null;
}
LOGGER.warn("Got HTTP response {} while trying to download cover for MBID \"{}\" from CoverArtArchive: {}", e.getStatusCode(), mBID, e.getMessage());
} catch (IOException e) {
LOGGER.error("An error occurred while downloading cover for MBID \"{}\": {}", mBID, e.getMessage());
LOGGER.trace("", e);
return null;
}
} finally {
releaseCoverLatch(latch);
}
}
return null;
}
use of org.apache.http.client.HttpResponseException in project substitution-schedule-parser by vertretungsplanme.
the class IndiwareStundenplan24Parser method getSubstitutionSchedule.
@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
String baseurl;
if (data.has("schoolNumber")) {
baseurl = "https://www.stundenplan24.de/" + data.getString("schoolNumber") + "/vplan/";
if (credential == null || !(credential instanceof UserPasswordCredential)) {
throw new IOException("no login");
}
String login = ((UserPasswordCredential) credential).getUsername();
String password = ((UserPasswordCredential) credential).getPassword();
executor.auth(login, password);
} else {
baseurl = data.getString("baseurl") + "/";
new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
}
List<Document> docs = new ArrayList<>();
for (int i = 0; i < MAX_DAYS; i++) {
LocalDate date = LocalDate.now().plusDays(i);
String dateStr = DateTimeFormat.forPattern("yyyyMMdd").print(date);
String url = baseurl + "vdaten/VplanKl" + dateStr + ".xml?_=" + System.currentTimeMillis();
try {
String xml = httpGet(url, ENCODING);
Document doc = Jsoup.parse(xml, url, Parser.xmlParser());
if (doc.select("kopf datei").text().equals("VplanKl" + dateStr + ".xml")) {
docs.add(doc);
}
} catch (HttpResponseException e) {
if (e.getStatusCode() != 404 && e.getStatusCode() != 300)
throw e;
}
}
SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
for (Document doc : docs) {
v.addDay(parseIndiwareDay(doc, false));
}
v.setWebsite(baseurl);
v.setClasses(getAllClasses());
v.setTeachers(getAllTeachers());
return v;
}
use of org.apache.http.client.HttpResponseException in project substitution-schedule-parser by vertretungsplanme.
the class IphisParser method getJSONArray.
private JSONArray getJSONArray(String url) throws IOException, CredentialInvalidException {
try {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + authToken);
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
final String httpResponse = httpGet(url, "UTF-8", headers);
return new JSONArray(httpResponse);
} catch (HttpResponseException httpResponseException) {
if (httpResponseException.getStatusCode() == 404) {
return null;
}
throw httpResponseException;
} catch (JSONException e) {
return new JSONArray();
}
}
use of org.apache.http.client.HttpResponseException in project substitution-schedule-parser by vertretungsplanme.
the class UntisMonitorParser method loadUrl.
private void loadUrl(String url, String encoding, boolean following, List<Document> docs, String startUrl, int recursionDepth) throws IOException, CredentialInvalidException {
String html;
if (url.equals(VALUE_URL_LOGIN_RESPONSE)) {
html = loginResponse;
} else {
try {
html = httpGet(url, encoding).replace(" ", "");
} catch (HttpResponseException e) {
if (docs.size() == 0) {
throw e;
} else {
// ignore if first page was loaded and redirect didn't work
return;
}
}
}
Document doc = Jsoup.parse(html);
doc.setBaseUri(url);
if (doc.select(".mon_title").size() == 0) {
// inside a frame?
if (doc.select("frameset frame[name").size() > 0) {
for (Element frame : doc.select("frameset frame")) {
if (frame.attr("src").matches(".*subst_\\d\\d\\d.html?") && recursionDepth < MAX_RECURSION_DEPTH) {
String frameUrl = frame.absUrl("src");
loadUrl(frame.absUrl("src"), encoding, following, docs, frameUrl, recursionDepth + 1);
}
}
} else if (doc.text().contains("registriert")) {
throw new CredentialInvalidException();
} else {
if (docs.size() == 0) {
// ignore if first page was loaded and redirect didn't work
throw new IOException("Could not find .mon-title, seems like there is no Untis " + "schedule here");
}
}
} else {
findSubDocs(docs, html, doc);
if (following && doc.select("meta[http-equiv=refresh]").size() > 0) {
Element meta = doc.select("meta[http-equiv=refresh]").first();
String attr = meta.attr("content").toLowerCase();
String redirectUrl = url.substring(0, url.lastIndexOf("/") + 1) + attr.substring(attr.indexOf("url=") + 4);
if (!redirectUrl.equals(startUrl) && recursionDepth < MAX_RECURSION_DEPTH) {
loadUrl(redirectUrl, encoding, true, docs, startUrl, recursionDepth + 1);
}
}
}
}
use of org.apache.http.client.HttpResponseException in project substitution-schedule-parser by vertretungsplanme.
the class UntisSubstitutionParser method getSubstitutionSchedule.
@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
String encoding = data.optString(PARAM_ENCODING, null);
SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
int successfulSchedules = 0;
HttpResponseException lastExceptionSchedule = null;
for (String baseUrl : ParserUtils.handleUrlsWithDateFormat(urls)) {
try {
Document doc = Jsoup.parse(this.httpGet(baseUrl, encoding));
Elements classes = doc.select("td a");
String lastChange = doc.select("td[align=right]:not(:has(b))").text();
int successfulClasses = 0;
HttpResponseException lastExceptionClass = null;
for (Element klasse : classes) {
try {
Document classDoc = Jsoup.parse(httpGet(baseUrl.substring(0, baseUrl.lastIndexOf("/")) + "/" + klasse.attr("href"), encoding));
parseSubstitutionTable(v, lastChange, classDoc, klasse.text());
successfulClasses++;
} catch (HttpResponseException e) {
lastExceptionClass = e;
}
}
if (successfulClasses == 0 && lastExceptionClass != null) {
throw lastExceptionClass;
}
successfulSchedules++;
} catch (HttpResponseException e) {
lastExceptionSchedule = e;
}
}
if (successfulSchedules == 0 && lastExceptionSchedule != null) {
throw lastExceptionSchedule;
}
if (data.has(PARAM_WEBSITE)) {
v.setWebsite(data.getString(PARAM_WEBSITE));
} else {
v.setWebsite(urls.get(0));
}
v.setClasses(getAllClasses());
v.setTeachers(getAllTeachers());
return v;
}
Aggregations