Search in sources :

Example 1 with SSLSecurityException

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

the class AccountFragment method show_connectivity_error.

public void show_connectivity_error(Exception e) {
    if (e != null) {
        e.printStackTrace();
    }
    if (getActivity() == null) {
        return;
    }
    if (e instanceof OpacErrorException) {
        AccountDataSource adatasource = new AccountDataSource(getActivity());
        adatasource.invalidateCachedAccountData(account);
        dialog_wrong_credentials(e.getMessage());
        return;
    }
    if (getView() != null) {
        final FrameLayout errorView = (FrameLayout) getView().findViewById(R.id.error_view);
        errorView.removeAllViews();
        View connError = getActivity().getLayoutInflater().inflate(R.layout.error_connectivity, errorView);
        TextView tvErrBody = (TextView) connError.findViewById(R.id.tvErrBody);
        Button btnRetry = (Button) connError.findViewById(R.id.btRetry);
        btnRetry.setVisibility(View.VISIBLE);
        if (e != null && e instanceof SSLSecurityException) {
            tvErrBody.setText(R.string.connection_error_detail_security);
        } else if (e != null && e instanceof NotReachableException) {
            tvErrBody.setText(R.string.connection_error_detail_nre);
        } else if (e != null && e instanceof OpacClient.LibraryRemovedException) {
            tvErrBody.setText(R.string.library_removed_error);
            btnRetry.setVisibility(View.GONE);
        }
        btnRetry.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                refresh();
            }
        });
        llLoading.setVisibility(View.GONE);
        swipeRefreshLayout.setVisibility(View.GONE);
        connError.setVisibility(View.VISIBLE);
    }
}
Also used : AccountDataSource(de.geeksfactory.opacclient.storage.AccountDataSource) OpacClient(de.geeksfactory.opacclient.OpacClient) Button(android.widget.Button) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) FrameLayout(android.widget.FrameLayout) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) OnClickListener(android.view.View.OnClickListener) OpacErrorException(de.geeksfactory.opacclient.apis.OpacApi.OpacErrorException) TextView(android.widget.TextView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) RecyclerView(android.support.v7.widget.RecyclerView)

Example 2 with SSLSecurityException

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

the class OkHttpBaseApi method httpPost.

/**
 * Perform a HTTP POST request to a given URL
 *
 * @param url           URL to fetch
 * @param data          POST data to send
 * @param encoding      Expected encoding of the response body
 * @param ignore_errors If true, status codes above 400 do not raise an exception
 * @return Answer content
 * @throws NotReachableException Thrown when server returns a HTTP status code greater or equal
 *                               than 400.
 */
public String httpPost(String url, RequestBody data, String encoding, boolean ignore_errors) throws IOException {
    Request.Builder requestbuilder = new Request.Builder().url(cleanUrl(url)).header("Accept", "*/*").header("User-Agent", getUserAgent());
    if (data.contentType() != null) {
        requestbuilder = requestbuilder.header("Content-Type", data.contentType().toString());
    }
    Request request = requestbuilder.post(data).build();
    try {
        Response response = http_client.newCall(request).execute();
        if (!ignore_errors && response.code() >= 400) {
            throw new NotReachableException(response.message());
        }
        return readBody(response, encoding);
    } 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;
        }
    }
}
Also used : Response(okhttp3.Response) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) Request(okhttp3.Request) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 3 with SSLSecurityException

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

the class SearchResultListFragment method setSearchResult.

public void setSearchResult(final SearchRequestResult searchresult) {
    for (SearchResult result : searchresult.getResults()) {
        result.setPage(searchresult.getPage_index());
    }
    if (searchresult.getTotal_result_count() >= 0) {
        ((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(getResources().getQuantityString(R.plurals.result_number, searchresult.getTotal_result_count(), searchresult.getTotal_result_count()));
    }
    if (searchresult.getResults().size() == 0 && searchresult.getTotal_result_count() <= 0) {
        setEmptyText(getString(R.string.no_results));
    }
    this.searchresult = searchresult;
    OpacApi api = null;
    try {
        api = app.getApi();
    } catch (OpacClient.LibraryRemovedException ignored) {
    }
    adapter = new ResultsAdapterEndless(getActivity(), searchresult, new OnLoadMoreListener() {

        @Override
        public SearchRequestResult onLoadMore(int page) throws Exception {
            SearchRequestResult res = app.getApi().searchGetPage(page);
            setLastLoadedPage(page);
            return res;
        }

        @Override
        public void onError(Exception e) {
            if (getActivity() != null) {
                if (e instanceof OpacErrorException) {
                    showConnectivityError(e.getMessage());
                } else if (e instanceof SSLSecurityException) {
                    showConnectivityError(getResources().getString(R.string.connection_error_detail_security));
                } else if (e instanceof NotReachableException) {
                    showConnectivityError(getResources().getString(R.string.connection_error_detail_nre));
                } else {
                    e.printStackTrace();
                    showConnectivityError();
                }
            }
        }

        @Override
        public void updateResultCount(int resultCount) {
            /*
                         * When IOpac finds more than 200 results, the real
						 * result count is not known until the second page is
						 * loaded
						 */
            if (resultCount >= 0 && getActivity() != null) {
                ((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(getResources().getQuantityString(R.plurals.result_number, resultCount, resultCount));
            }
        }
    }, api);
    setListAdapter(adapter);
    getListView().setTextFilterEnabled(true);
    setListShown(true);
}
Also used : OnLoadMoreListener(de.geeksfactory.opacclient.frontend.ResultsAdapterEndless.OnLoadMoreListener) OpacClient(de.geeksfactory.opacclient.OpacClient) SearchRequestResult(de.geeksfactory.opacclient.objects.SearchRequestResult) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) OpacApi(de.geeksfactory.opacclient.apis.OpacApi) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) SearchResult(de.geeksfactory.opacclient.objects.SearchResult) OpacErrorException(de.geeksfactory.opacclient.apis.OpacApi.OpacErrorException) JSONException(org.json.JSONException) OpacErrorException(de.geeksfactory.opacclient.apis.OpacApi.OpacErrorException) IOException(java.io.IOException) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException)

Example 4 with SSLSecurityException

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

the class ApacheBaseApi method httpPost.

/**
 * Perform a HTTP POST request to a given URL
 *
 * @param url           URL to fetch
 * @param data          POST data to send
 * @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 httpPost(String url, HttpEntity data, String encoding, boolean ignore_errors, CookieStore cookieStore) throws IOException {
    HttpPost httppost = new HttpPost(cleanUrl(url));
    httppost.setEntity(data);
    httppost.setHeader("Accept", "*/*");
    HttpResponse response;
    String html;
    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(httppost, localContext);
        } else {
            response = http_client.execute(httppost);
        }
        if (!ignore_errors && response.getStatusLine().getStatusCode() >= 400) {
            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 : HttpPost(org.apache.http.client.methods.HttpPost) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) 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 5 with SSLSecurityException

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

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