Search in sources :

Example 46 with HttpResponseException

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;
}
Also used : DefaultCoverArtArchiveClient(fm.last.musicbrainz.coverart.impl.DefaultCoverArtArchiveClient) CoverArtImage(fm.last.musicbrainz.coverart.CoverArtImage) InputStream(java.io.InputStream) CoverArtArchiveResult(net.pms.database.TableCoverArtArchive.CoverArtArchiveResult) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) CoverArt(fm.last.musicbrainz.coverart.CoverArt) CoverArtException(fm.last.musicbrainz.coverart.CoverArtException)

Example 47 with HttpResponseException

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;
}
Also used : SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) ArrayList(java.util.ArrayList) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) UserPasswordCredential(me.vertretungsplan.objects.credential.UserPasswordCredential) Document(org.jsoup.nodes.Document) LocalDate(org.joda.time.LocalDate)

Example 48 with HttpResponseException

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();
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 49 with HttpResponseException

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("&nbsp;", "");
        } 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);
            }
        }
    }
}
Also used : Element(org.jsoup.nodes.Element) HttpResponseException(org.apache.http.client.HttpResponseException) CredentialInvalidException(me.vertretungsplan.exception.CredentialInvalidException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Example 50 with HttpResponseException

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;
}
Also used : SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) Element(org.jsoup.nodes.Element) HttpResponseException(org.apache.http.client.HttpResponseException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Aggregations

HttpResponseException (org.apache.http.client.HttpResponseException)82 IOException (java.io.IOException)32 StatusLine (org.apache.http.StatusLine)30 HttpEntity (org.apache.http.HttpEntity)25 HttpGet (org.apache.http.client.methods.HttpGet)14 HttpResponse (org.apache.http.HttpResponse)13 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)11 Header (org.apache.http.Header)9 ClientProtocolException (org.apache.http.client.ClientProtocolException)9 URISyntaxException (java.net.URISyntaxException)8 ArrayList (java.util.ArrayList)8 URI (java.net.URI)7 Document (org.jsoup.nodes.Document)7 Test (org.junit.Test)7 InputStream (java.io.InputStream)6 Expectations (mockit.Expectations)5 HttpClient (org.apache.http.client.HttpClient)5 Element (org.jsoup.nodes.Element)5 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)4 HttpPost (org.apache.http.client.methods.HttpPost)4