Search in sources :

Example 6 with SSLSecurityException

use of de.geeksfactory.opacclient.networking.SSLSecurityException 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 7 with SSLSecurityException

use of de.geeksfactory.opacclient.networking.SSLSecurityException 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)

Aggregations

NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)7 SSLSecurityException (de.geeksfactory.opacclient.networking.SSLSecurityException)7 IOException (java.io.IOException)6 InterruptedIOException (java.io.InterruptedIOException)5 HttpResponse (org.apache.http.HttpResponse)4 UnknownHostException (java.net.UnknownHostException)3 OpacClient (de.geeksfactory.opacclient.OpacClient)2 OpacErrorException (de.geeksfactory.opacclient.apis.OpacApi.OpacErrorException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)2 HttpContext (org.apache.http.protocol.HttpContext)2 Document (org.jsoup.nodes.Document)2 Element (org.jsoup.nodes.Element)2 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 Button (android.widget.Button)1