Search in sources :

Example 1 with PasswordAuthenticationData

use of me.vertretungsplan.objects.authentication.PasswordAuthenticationData in project substitution-schedule-parser by vertretungsplanme.

the class ESchoolParser method getSubstitutionSchedule.

@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    if (!(scheduleData.getAuthenticationData() instanceof NoAuthenticationData) && (credential == null || !(credential instanceof PasswordCredential) || ((PasswordCredential) credential).getPassword() == null || ((PasswordCredential) credential).getPassword().isEmpty())) {
        throw new IOException("no login");
    }
    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("wp", scheduleData.getData().getString(PARAM_ID)));
    nvps.add(new BasicNameValuePair("go", "vplan"));
    nvps.add(new BasicNameValuePair("content", "x14"));
    nvps.add(new BasicNameValuePair("sortby", "S"));
    String url = BASE_URL + "?" + URLEncodedUtils.format(nvps, "UTF-8");
    Document doc = Jsoup.parse(httpGet(url, ENCODING));
    if (doc.select("form[name=loginform]").size() > 0 && scheduleData.getAuthenticationData() instanceof PasswordAuthenticationData) {
        // Login required
        List<NameValuePair> formParams = new ArrayList<>();
        formParams.add(new BasicNameValuePair("password", ((PasswordCredential) credential).getPassword()));
        formParams.add(new BasicNameValuePair("login", ""));
        doc = Jsoup.parse(httpPost(url, ENCODING, formParams));
        if (doc.select("font[color=red]").text().contains("fehlgeschlagen")) {
            throw new CredentialInvalidException();
        }
    }
    SubstitutionSchedule schedule = parseESchoolSchedule(doc);
    return schedule;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) NoAuthenticationData(me.vertretungsplan.objects.authentication.NoAuthenticationData) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) PasswordCredential(me.vertretungsplan.objects.credential.PasswordCredential) ArrayList(java.util.ArrayList) PasswordAuthenticationData(me.vertretungsplan.objects.authentication.PasswordAuthenticationData) CredentialInvalidException(me.vertretungsplan.exception.CredentialInvalidException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CredentialInvalidException (me.vertretungsplan.exception.CredentialInvalidException)1 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)1 NoAuthenticationData (me.vertretungsplan.objects.authentication.NoAuthenticationData)1 PasswordAuthenticationData (me.vertretungsplan.objects.authentication.PasswordAuthenticationData)1 PasswordCredential (me.vertretungsplan.objects.credential.PasswordCredential)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 Document (org.jsoup.nodes.Document)1