Search in sources :

Example 6 with CredentialInvalidException

use of me.vertretungsplan.exception.CredentialInvalidException 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)

Aggregations

CredentialInvalidException (me.vertretungsplan.exception.CredentialInvalidException)6 IOException (java.io.IOException)5 Document (org.jsoup.nodes.Document)4 PasswordCredential (me.vertretungsplan.objects.credential.PasswordCredential)3 UserPasswordCredential (me.vertretungsplan.objects.credential.UserPasswordCredential)3 HttpResponseException (org.apache.http.client.HttpResponseException)3 JSONObject (org.json.JSONObject)3 Element (org.jsoup.nodes.Element)3 ArrayList (java.util.ArrayList)2 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)2 NameValuePair (org.apache.http.NameValuePair)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 JSONException (org.json.JSONException)2 Claims (io.jsonwebtoken.Claims)1 SignatureException (io.jsonwebtoken.SignatureException)1 BigInteger (java.math.BigInteger)1 URL (java.net.URL)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1