Search in sources :

Example 11 with NotReachableException

use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.

the class ApacheBaseApi method httpGet.

/**
 * Perform a HTTP GET request to a given URL
 *
 * @param url           URL to fetch
 * @param encoding      Expected encoding of the response body
 * @param ignore_errors If true, status codes above 400 do not raise an exception
 * @param cookieStore   If set, the given cookieStore is used instead of the built-in one.
 * @return Answer content
 * @throws NotReachableException Thrown when server returns a HTTP status code greater or equal
 *                               than 400.
 */
public String httpGet(String url, String encoding, boolean ignore_errors, CookieStore cookieStore) throws IOException {
    HttpGet httpget = new HttpGet(cleanUrl(url));
    HttpResponse response;
    String html;
    httpget.setHeader("Accept", "*/*");
    try {
        if (cookieStore != null) {
            // Create local HTTP context
            HttpContext localContext = new BasicHttpContext();
            // Bind custom cookie store to the local context
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
            response = http_client.execute(httpget, localContext);
        } else {
            response = http_client.execute(httpget);
        }
        if (!ignore_errors && response.getStatusLine().getStatusCode() >= 400) {
            HttpUtils.consume(response.getEntity());
            throw new NotReachableException(response.getStatusLine().getReasonPhrase());
        }
        html = convertStreamToString(response.getEntity().getContent(), encoding);
        HttpUtils.consume(response.getEntity());
    } catch (javax.net.ssl.SSLPeerUnverifiedException e) {
        logHttpError(e);
        throw new SSLSecurityException(e.getMessage());
    } catch (javax.net.ssl.SSLException e) {
        // aborted/interrupted handshake/connection
        if (e.getMessage().contains("timed out") || e.getMessage().contains("reset by")) {
            logHttpError(e);
            throw new NotReachableException(e.getMessage());
        } else {
            logHttpError(e);
            throw new SSLSecurityException(e.getMessage());
        }
    } catch (InterruptedIOException e) {
        logHttpError(e);
        throw new NotReachableException(e.getMessage());
    } catch (UnknownHostException e) {
        throw new NotReachableException(e.getMessage());
    } catch (IOException e) {
        if (e.getMessage() != null && e.getMessage().contains("Request aborted")) {
            logHttpError(e);
            throw new NotReachableException(e.getMessage());
        } else {
            throw e;
        }
    }
    return html;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) HttpResponse(org.apache.http.HttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 12 with NotReachableException

use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.

the class Bibliotheca method parse_account.

public static AccountData parse_account(Account acc, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_lent, JSONObject headers_reservations) throws JSONException, NotReachableException {
    if (doc.select(".kontozeile_center table").size() == 0) {
        throw new NotReachableException();
    }
    Map<String, Integer> copymap = new HashMap<>();
    Elements headerCells = doc.select(".kontozeile_center table").get(0).select("tr.exemplarmenubar").get(0).children();
    JSONArray headersList = new JSONArray();
    JSONArray unknownHeaders = new JSONArray();
    int i = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        headersList.put(header);
        if (headers_lent.has(header)) {
            if (!headers_lent.isNull(header))
                copymap.put(headers_lent.getString(header), i);
        } else {
            unknownHeaders.put(header);
        }
        i++;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - lent", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject accounttable = data.getJSONObject("accounttable");
        copymap = jsonToMap(accounttable);
    }
    List<LentItem> media = new ArrayList<>();
    Elements exemplartrs = doc.select(".kontozeile_center table").get(0).select("tr.tabKonto");
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    DateTimeFormatter fmt2 = DateTimeFormat.forPattern("d/M/yyyy").withLocale(Locale.GERMAN);
    for (Element tr : exemplartrs) {
        LentItem item = new LentItem();
        for (Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            if (key.equals("prolongurl")) {
                if (tr.child(index).children().size() > 0) {
                    item.setProlongData(tr.child(index).child(0).attr("href"));
                    item.setRenewable(!tr.child(index).child(0).attr("href").contains("vermsg"));
                }
            } else if (key.equals("returndate")) {
                try {
                    item.setDeadline(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setDeadline(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        e2.printStackTrace();
                    }
                }
            } else {
                item.set(key, tr.child(index).text());
            }
        }
        media.add(item);
    }
    copymap = new HashMap<>();
    headerCells = doc.select(".kontozeile_center table").get(1).select("tr.exemplarmenubar").get(0).children();
    headersList = new JSONArray();
    unknownHeaders = new JSONArray();
    i = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        headersList.put(header);
        if (headers_reservations.has(header)) {
            if (!headers_reservations.isNull(header)) {
                copymap.put(headers_reservations.getString(header), i);
            }
        } else {
            unknownHeaders.put(header);
        }
        i++;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - reservations", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject reservationtable = data.getJSONObject("reservationtable");
        copymap = jsonToMap(reservationtable);
    }
    List<ReservedItem> reservations = new ArrayList<>();
    exemplartrs = doc.select(".kontozeile_center table").get(1).select("tr.tabKonto");
    for (Element tr : exemplartrs) {
        ReservedItem item = new ReservedItem();
        for (Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            if (key.equals("cancelurl")) {
                if (tr.child(index).children().size() > 0) {
                    item.setCancelData(tr.child(index).child(0).attr("href"));
                }
            } else if (key.equals("availability")) {
                try {
                    item.setReadyDate(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setReadyDate(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        e2.printStackTrace();
                    }
                }
            } else if (key.equals("expirationdate")) {
                try {
                    item.setExpirationDate(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setExpirationDate(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        item.setStatus(tr.child(index).text());
                    }
                }
            } else {
                item.set(key, tr.child(index).text());
            }
        }
        reservations.add(item);
    }
    AccountData res = new AccountData(acc.getId());
    for (Element row : doc.select(".kontozeile_center, div[align=center]")) {
        String text = row.text().trim();
        if (text.matches(".*Ausstehende Geb.+hren:[^0-9]+([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*")) {
            text = text.replaceAll(".*Ausstehende Geb.+hren:[^0-9]+([0-9.," + "]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*", "$1 $2");
            res.setPendingFees(text);
        }
        if (text.matches("Ihr Ausweis ist g.ltig bis:.*")) {
            text = text.replaceAll("Ihr Ausweis ist g.ltig bis:[^A-Za-z0-9]+", "");
            res.setValidUntil(text);
        } else if (text.matches("Ausweis g.ltig bis:.*")) {
            text = text.replaceAll("Ausweis g.ltig bis:[^A-Za-z0-9]+", "");
            res.setValidUntil(text);
        }
    }
    res.setLent(media);
    res.setReservations(reservations);
    return res;
}
Also used : NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) HashMap(java.util.HashMap) Report(de.geeksfactory.opacclient.reporting.Report) Element(org.jsoup.nodes.Element) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements) JSONObject(org.json.JSONObject) AccountData(de.geeksfactory.opacclient.objects.AccountData) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 13 with NotReachableException

use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.

the class Adis method htmlPost.

public Document htmlPost(String url, List<NameValuePair> data) throws IOException {
    HttpPost httppost = new HttpPost(cleanUrl(url));
    boolean rcf = false;
    for (NameValuePair nv : data) {
        if (nv.getName().equals("requestCount")) {
            rcf = true;
            break;
        }
    }
    if (!rcf) {
        data.add(new BasicNameValuePair("requestCount", s_requestCount + ""));
    }
    httppost.setEntity(new UrlEncodedFormEntity(data, getDefaultEncoding()));
    HttpResponse response;
    try {
        response = http_client.execute(httppost);
    } catch (javax.net.ssl.SSLPeerUnverifiedException e) {
        throw new SSLSecurityException(e.getMessage());
    } catch (javax.net.ssl.SSLException e) {
        // aborted/interrupted handshake/connection
        if (e.getMessage().contains("timed out") || e.getMessage().contains("reset by")) {
            e.printStackTrace();
            throw new NotReachableException(e.getMessage());
        } else {
            throw new SSLSecurityException(e.getMessage());
        }
    } catch (InterruptedIOException e) {
        e.printStackTrace();
        throw new NotReachableException(e.getMessage());
    } catch (IOException e) {
        if (e.getMessage() != null && e.getMessage().contains("Request aborted")) {
            e.printStackTrace();
            throw new NotReachableException(e.getMessage());
        } else {
            throw e;
        }
    }
    if (response.getStatusLine().getStatusCode() >= 400) {
        throw new NotReachableException(response.getStatusLine().getReasonPhrase());
    }
    String html = convertStreamToString(response.getEntity().getContent(), getDefaultEncoding());
    HttpUtils.consume(response.getEntity());
    Document doc = Jsoup.parse(html);
    Pattern patRequestCount = Pattern.compile(".*requestCount=([0-9]+)[^0-9].*");
    for (Element a : doc.select("a")) {
        Matcher objid_matcher = patRequestCount.matcher(a.attr("href"));
        if (objid_matcher.matches()) {
            s_requestCount = Integer.parseInt(objid_matcher.group(1));
        }
    }
    doc.setBaseUri(url);
    return doc;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) InterruptedIOException(java.io.InterruptedIOException) Pattern(java.util.regex.Pattern) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 14 with NotReachableException

use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.

the class Adis method htmlGet.

public Document htmlGet(String url) throws IOException {
    if (!url.contains("requestCount") && s_requestCount >= 0) {
        url = url + (url.contains("?") ? "&" : "?") + "requestCount=" + s_requestCount;
    }
    HttpGet httpget = new HttpGet(cleanUrl(url));
    HttpResponse response;
    try {
        response = http_client.execute(httpget);
    } catch (javax.net.ssl.SSLPeerUnverifiedException e) {
        throw new SSLSecurityException(e.getMessage());
    } catch (javax.net.ssl.SSLException e) {
        // aborted/interrupted handshake/connection
        if (e.getMessage().contains("timed out") || e.getMessage().contains("reset by")) {
            e.printStackTrace();
            throw new NotReachableException(e.getMessage());
        } else {
            throw new SSLSecurityException(e.getMessage());
        }
    } catch (InterruptedIOException e) {
        e.printStackTrace();
        throw new NotReachableException(e.getMessage());
    } catch (IOException e) {
        if (e.getMessage() != null && e.getMessage().contains("Request aborted")) {
            e.printStackTrace();
            throw new NotReachableException(e.getMessage());
        } else {
            throw e;
        }
    }
    if (response.getStatusLine().getStatusCode() >= 400) {
        throw new NotReachableException(response.getStatusLine().getReasonPhrase());
    }
    String html = convertStreamToString(response.getEntity().getContent(), getDefaultEncoding());
    HttpUtils.consume(response.getEntity());
    Document doc = Jsoup.parse(html);
    Pattern patRequestCount = Pattern.compile("requestCount=([0-9]+)");
    for (Element a : doc.select("a")) {
        Matcher objid_matcher = patRequestCount.matcher(a.attr("href"));
        if (objid_matcher.matches()) {
            s_requestCount = Integer.parseInt(objid_matcher.group(1));
        }
    }
    doc.setBaseUri(url);
    return doc;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) Pattern(java.util.regex.Pattern) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) Matcher(java.util.regex.Matcher) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.jsoup.nodes.Element) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) HttpResponse(org.apache.http.HttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Example 15 with NotReachableException

use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.

the class IOpac method cancel.

@Override
public CancelResult cancel(String media, Account account, int useraction, String selection) throws IOException, OpacErrorException {
    String html = httpGet(opac_url + "/" + media, getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    try {
        Element form = doc.select("form[name=form1]").first();
        String sessionid = form.select("input[name=sessionid]").attr("value");
        String kndnr = form.select("input[name=kndnr]").attr("value");
        String mednr = form.select("input[name=mednr]").attr("value");
        httpGet(opac_url + "/cgi-bin/di.exe?mode=9&kndnr=" + kndnr + "&mednr=" + mednr + "&sessionid=" + sessionid + "&psh100=Stornieren", getDefaultEncoding());
        return new CancelResult(MultiStepResult.Status.OK);
    } catch (Throwable e) {
        e.printStackTrace();
        throw new NotReachableException(e.getMessage());
    }
}
Also used : NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document)

Aggregations

NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)18 Element (org.jsoup.nodes.Element)11 IOException (java.io.IOException)10 Document (org.jsoup.nodes.Document)10 ArrayList (java.util.ArrayList)8 SSLSecurityException (de.geeksfactory.opacclient.networking.SSLSecurityException)7 Matcher (java.util.regex.Matcher)7 Pattern (java.util.regex.Pattern)7 InterruptedIOException (java.io.InterruptedIOException)6 Elements (org.jsoup.select.Elements)6 HttpResponse (org.apache.http.HttpResponse)5 AccountData (de.geeksfactory.opacclient.objects.AccountData)3 LentItem (de.geeksfactory.opacclient.objects.LentItem)3 ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)3 SearchRequestResult (de.geeksfactory.opacclient.objects.SearchRequestResult)3 SearchResult (de.geeksfactory.opacclient.objects.SearchResult)3 DropdownSearchField (de.geeksfactory.opacclient.searchfields.DropdownSearchField)3 SearchField (de.geeksfactory.opacclient.searchfields.SearchField)3 TextSearchField (de.geeksfactory.opacclient.searchfields.TextSearchField)3 UnknownHostException (java.net.UnknownHostException)3