Search in sources :

Example 11 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project Talon-for-Twitter by klinker24.

the class TweetMarkerHelper method getLastStatus.

public boolean getLastStatus(String collection, final Context context) {
    long currentId = sharedPrefs.getLong("current_position_" + currentAccount, 0l);
    boolean updated = false;
    try {
        long startTime = Calendar.getInstance().getTimeInMillis();
        HttpGet get = new HttpGet(postURL + "&" + collection);
        get.addHeader("X-Auth-Service-Provider", SERVICE_PROVIDER);
        get.addHeader("X-Verify-Credentials-Authorization", getAuthrityHeader(twitter));
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        Log.v("talon_tweetmarker", "getting id response code: " + response.getStatusLine().getStatusCode() + " for " + screenname);
        long endTime = Calendar.getInstance().getTimeInMillis();
        StatusLine statusLine = response.getStatusLine();
        final long responseTime = endTime - startTime;
        if (endTime - startTime > 15000 && statusLine.getStatusCode() == 200) {
            ((Activity) context).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (sharedPrefs.getBoolean("show_tweetmarker_length", true)) {
                            new AlertDialog.Builder(context).setTitle("Slow TweetMarker Fetch").setMessage("TweetMarker successfully fetched it's position, but seemed to take quite a bit of time. " + "They may be experiencing issues at the moment, you may want to try again in a little while! \n\n" + "Server Response Time: " + (responseTime / 1000) + " seconds").setPositiveButton("Turn Off TM", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    sharedPrefs.edit().putString("tweetmarker_options", "0").commit();
                                    AppSettings.invalidate();
                                }
                            }).setNeutralButton(R.string.dont_show_again, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    sharedPrefs.edit().putBoolean("show_tweetmarker_length", false).commit();
                                    dialog.dismiss();
                                }
                            }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).create().show();
                        }
                    } catch (Exception e) {
                    }
                }
            });
        }
        if (statusLine.getStatusCode() == 500 || statusLine.getStatusCode() == 503) {
            // common tweetmarker failure codes
            final StatusLine s = statusLine;
            ((Activity) context).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        new AlertDialog.Builder(context).setTitle("TweetMarker Failure").setMessage("Error: " + s.getStatusCode() + "(" + s.getReasonPhrase() + ")" + "\n\n" + "TweetMarker has been experiencing some issues on their end lately with some apps. They seem intermittent, random, and are causing incredibly slow load times." + "I have been in contact with them, but I would recommend turning off this feature until these issues are resolved.").setPositiveButton("Turn Off TM", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                sharedPrefs.edit().putString("tweetmarker_options", "0").commit();
                                AppSettings.invalidate();
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create().show();
                    } catch (Exception e) {
                    }
                }
            });
            updated = false;
        } else if (statusLine.getStatusCode() == 200) {
            // request ok
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = rd.readLine()) != null) {
                builder.append(line);
            }
            JSONObject jsonObject = new JSONObject(builder.toString());
            JSONObject timeline = jsonObject.getJSONObject(collection);
            if (timeline != null) {
                currentId = Long.parseLong(timeline.getString("id"));
                int version = Integer.parseInt(timeline.getString("version"));
                Log.v("talon_tweetmarker", "getting tweetmarker," + " version: " + version + " id: " + currentId + " screename: " + screenname);
                int lastVer = sharedPrefs.getInt("last_version_account_" + currentAccount, 0);
                if (lastVer != version) {
                    updated = true;
                }
                sharedPrefs.edit().putInt("last_version_account_" + currentAccount, version).commit();
            }
        } else {
            // wait first
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
            }
            response = client.execute(get);
            statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == 200) {
                // request ok
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line;
                StringBuilder builder = new StringBuilder();
                while ((line = rd.readLine()) != null) {
                    builder.append(line);
                }
                JSONObject jsonObject = new JSONObject(builder.toString());
                JSONObject timeline = jsonObject.getJSONObject(collection);
                if (timeline != null) {
                    currentId = Long.parseLong(timeline.getString("id"));
                    int version = Integer.parseInt(timeline.getString("version"));
                    Log.v("talon_tweetmarker", "getting tweetmarker," + " version: " + version + " id: " + currentId + " screename: " + screenname);
                    int lastVer = sharedPrefs.getInt("last_version_account_" + currentAccount, 0);
                    if (lastVer != version) {
                        updated = true;
                    }
                    sharedPrefs.edit().putInt("last_version_account_" + currentAccount, version).commit();
                }
            }
        }
    } catch (HttpHostConnectException e) {
        // timeout when connecting to host.
        ((Activity) context).runOnUiThread(new Runnable() {

            @Override
            public void run() {
                try {
                    new AlertDialog.Builder(context).setTitle("TweetMarker Failure").setMessage("Timeout connecting to TweetMarker." + "\n\n" + "TweetMarker has been experiencing some issues on their end lately with some apps. They seem intermittent, random, and are causing incredibly slow load times." + "I have been in contact with them, but I would recommend turning off this feature until these issues are resolved.").setPositiveButton("Turn Off TM", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            sharedPrefs.edit().putString("tweetmarker_options", "0").commit();
                            AppSettings.invalidate();
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create().show();
                } catch (Exception e) {
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
    }
    Log.v("talon_launcher_stuff", "writing " + currentId + " to shared prefs");
    sharedPrefs.edit().putLong("current_position_" + currentAccount, currentId).commit();
    if (contentProvider) {
        HomeContentProvider.updateCurrent(currentAccount, context, currentId);
    } else {
        try {
            HomeDataSource.getInstance(context).markPosition(currentAccount, currentId);
        } catch (Exception e) {
        }
    }
    return updated;
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) HttpGet(org.apache.http.client.methods.HttpGet) Activity(android.app.Activity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) InputStreamReader(java.io.InputStreamReader) HttpResponse(org.apache.http.HttpResponse) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) StatusLine(org.apache.http.StatusLine) JSONObject(org.json.JSONObject) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader)

Example 12 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project XPrivacy by M66B.

the class Util method bug.

public static void bug(XHook hook, Throwable ex) {
    if (ex instanceof InvocationTargetException) {
        InvocationTargetException exex = (InvocationTargetException) ex;
        if (exex.getTargetException() != null)
            ex = exex.getTargetException();
    }
    int priority;
    if (ex instanceof ActivityShare.AbortException)
        priority = Log.WARN;
    else if (ex instanceof ActivityShare.ServerException)
        priority = Log.WARN;
    else if (ex instanceof ConnectTimeoutException)
        priority = Log.WARN;
    else if (ex instanceof FileNotFoundException)
        priority = Log.WARN;
    else if (ex instanceof HttpHostConnectException)
        priority = Log.WARN;
    else if (ex instanceof NameNotFoundException)
        priority = Log.WARN;
    else if (ex instanceof NoClassDefFoundError)
        priority = Log.WARN;
    else if (ex instanceof OutOfMemoryError)
        priority = Log.WARN;
    else if (ex instanceof RuntimeException)
        priority = Log.WARN;
    else if (ex instanceof SecurityException)
        priority = Log.WARN;
    else if (ex instanceof SocketTimeoutException)
        priority = Log.WARN;
    else if (ex instanceof SSLPeerUnverifiedException)
        priority = Log.WARN;
    else if (ex instanceof StackOverflowError)
        priority = Log.WARN;
    else if (ex instanceof TransactionTooLargeException)
        priority = Log.WARN;
    else if (ex instanceof UnknownHostException)
        priority = Log.WARN;
    else if (ex instanceof UnsatisfiedLinkError)
        priority = Log.WARN;
    else
        priority = Log.ERROR;
    boolean xprivacy = false;
    for (StackTraceElement frame : ex.getStackTrace()) if (frame.getClassName() != null && frame.getClassName().startsWith("biz.bokhorst.xprivacy")) {
        xprivacy = true;
        break;
    }
    if (!xprivacy)
        priority = Log.WARN;
    log(hook, priority, ex.toString() + " uid=" + Process.myUid() + "\n" + Log.getStackTraceString(ex));
}
Also used : UnknownHostException(java.net.UnknownHostException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SuppressLint(android.annotation.SuppressLint) RuntimeException(java.lang.RuntimeException) SocketTimeoutException(java.net.SocketTimeoutException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) TransactionTooLargeException(android.os.TransactionTooLargeException) StackOverflowError(java.lang.StackOverflowError) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 13 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project XobotOS by xamarin.

the class DefaultClientConnectionOperator method openConnection.

// non-javadoc, see interface ClientConnectionOperator
public void openConnection(OperatedClientConnection conn, HttpHost target, InetAddress local, HttpContext context, HttpParams params) throws IOException {
    if (conn == null) {
        throw new IllegalArgumentException("Connection must not be null.");
    }
    if (target == null) {
        throw new IllegalArgumentException("Target host must not be null.");
    }
    //@@@ is context allowed to be null?
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (conn.isOpen()) {
        throw new IllegalArgumentException("Connection must not be open.");
    }
    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    final SocketFactory sf = schm.getSocketFactory();
    final SocketFactory plain_sf;
    final LayeredSocketFactory layered_sf;
    if (sf instanceof LayeredSocketFactory) {
        plain_sf = staticPlainSocketFactory;
        layered_sf = (LayeredSocketFactory) sf;
    } else {
        plain_sf = sf;
        layered_sf = null;
    }
    InetAddress[] addresses = InetAddress.getAllByName(target.getHostName());
    for (int i = 0; i < addresses.length; ++i) {
        Socket sock = plain_sf.createSocket();
        conn.opening(sock, target);
        try {
            Socket connsock = plain_sf.connectSocket(sock, addresses[i].getHostAddress(), schm.resolvePort(target.getPort()), local, 0, params);
            if (sock != connsock) {
                sock = connsock;
                conn.opening(sock, target);
            }
            /*
                 * prepareSocket is called on the just connected
                 * socket before the creation of the layered socket to
                 * ensure that desired socket options such as
                 * TCP_NODELAY, SO_RCVTIMEO, SO_LINGER will be set
                 * before any I/O is performed on the socket. This
                 * happens in the common case as
                 * SSLSocketFactory.createSocket performs hostname
                 * verification which requires that SSL handshaking be
                 * performed.
                 */
            prepareSocket(sock, context, params);
            if (layered_sf != null) {
                Socket layeredsock = layered_sf.createSocket(sock, target.getHostName(), schm.resolvePort(target.getPort()), true);
                if (layeredsock != sock) {
                    conn.opening(layeredsock, target);
                }
                conn.openCompleted(sf.isSecure(layeredsock), params);
            } else {
                conn.openCompleted(sf.isSecure(sock), params);
            }
            break;
        // BEGIN android-changed
        //       catch SocketException to cover any kind of connect failure
        } catch (SocketException ex) {
            if (i == addresses.length - 1) {
                ConnectException cause = ex instanceof ConnectException ? (ConnectException) ex : new ConnectException(ex.getMessage(), ex);
                throw new HttpHostConnectException(target, cause);
            }
        // END android-changed
        } catch (ConnectTimeoutException ex) {
            if (i == addresses.length - 1) {
                throw ex;
            }
        }
    }
}
Also used : SocketException(java.net.SocketException) Scheme(org.apache.http.conn.scheme.Scheme) PlainSocketFactory(org.apache.http.conn.scheme.PlainSocketFactory) LayeredSocketFactory(org.apache.http.conn.scheme.LayeredSocketFactory) SocketFactory(org.apache.http.conn.scheme.SocketFactory) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) LayeredSocketFactory(org.apache.http.conn.scheme.LayeredSocketFactory) InetAddress(java.net.InetAddress) Socket(java.net.Socket) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 14 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project XobotOS by xamarin.

the class DefaultClientConnectionOperator method updateSecureConnection.

// openConnection
// non-javadoc, see interface ClientConnectionOperator
public void updateSecureConnection(OperatedClientConnection conn, HttpHost target, HttpContext context, HttpParams params) throws IOException {
    if (conn == null) {
        throw new IllegalArgumentException("Connection must not be null.");
    }
    if (target == null) {
        throw new IllegalArgumentException("Target host must not be null.");
    }
    //@@@ is context allowed to be null?
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (!conn.isOpen()) {
        throw new IllegalArgumentException("Connection must be open.");
    }
    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) {
        throw new IllegalArgumentException("Target scheme (" + schm.getName() + ") must have layered socket factory.");
    }
    final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory();
    final Socket sock;
    try {
        sock = lsf.createSocket(conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), true);
    } catch (ConnectException ex) {
        throw new HttpHostConnectException(target, ex);
    }
    prepareSocket(sock, context, params);
    conn.update(sock, target, lsf.isSecure(sock), params);
//@@@ error handling: close the layered socket in case of exception?
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) LayeredSocketFactory(org.apache.http.conn.scheme.LayeredSocketFactory) Socket(java.net.Socket) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException)

Example 15 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project jackrabbit-oak by apache.

the class TomcatIT method testTomcat.

public void testTomcat() throws Exception {
    HtmlPage page = null;
    try {
        page = client.getPage(url);
    } catch (HttpHostConnectException e) {
        // sometimes on jenkins there are connections exceptions.
        // ignoring the rest of the test in this case
        LOG.error("Failed connecting to tomcat", e);
        return;
    }
    assertEquals("Content Repository Setup", page.getTitleText());
    page = submitNewRepositoryForm(page);
    assertEquals("Content Repository Ready", page.getTitleText());
    page = page.getAnchorByText("home").click();
    assertEquals("Apache Jackrabbit JCR Server", page.getTitleText());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException)

Aggregations

HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)17 ConnectException (java.net.ConnectException)6 Socket (java.net.Socket)6 LayeredSocketFactory (org.apache.http.conn.scheme.LayeredSocketFactory)6 Scheme (org.apache.http.conn.scheme.Scheme)6 HttpResponse (org.apache.http.HttpResponse)4 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)4 InputStream (java.io.InputStream)3 InetAddress (java.net.InetAddress)3 SocketException (java.net.SocketException)3 HttpGet (org.apache.http.client.methods.HttpGet)3 PlainSocketFactory (org.apache.http.conn.scheme.PlainSocketFactory)3 SocketFactory (org.apache.http.conn.scheme.SocketFactory)3 IOException (java.io.IOException)2 URI (java.net.URI)2 HttpEntity (org.apache.http.HttpEntity)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 Executor (org.apache.http.client.fluent.Executor)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2