Search in sources :

Example 31 with NoRouteToHostException

use of java.net.NoRouteToHostException in project android_frameworks_base by DirtyUnicorns.

the class MediaHTTPConnection method seekTo.

private void seekTo(long offset) throws IOException {
    teardownConnection();
    try {
        int response;
        int redirectCount = 0;
        URL url = mURL;
        // do not use any proxy for localhost (127.0.0.1)
        boolean noProxy = isLocalHost(url);
        while (true) {
            if (noProxy) {
                mConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
            } else {
                mConnection = (HttpURLConnection) url.openConnection();
            }
            mConnection.setConnectTimeout(CONNECT_TIMEOUT_MS);
            // handle redirects ourselves if we do not allow cross-domain redirect
            mConnection.setInstanceFollowRedirects(mAllowCrossDomainRedirect);
            if (mHeaders != null) {
                for (Map.Entry<String, String> entry : mHeaders.entrySet()) {
                    mConnection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            if (offset > 0) {
                mConnection.setRequestProperty("Range", "bytes=" + offset + "-");
            }
            response = mConnection.getResponseCode();
            if (response != HttpURLConnection.HTTP_MULT_CHOICE && response != HttpURLConnection.HTTP_MOVED_PERM && response != HttpURLConnection.HTTP_MOVED_TEMP && response != HttpURLConnection.HTTP_SEE_OTHER && response != HTTP_TEMP_REDIRECT) {
                // not a redirect, or redirect handled by HttpURLConnection
                break;
            }
            if (++redirectCount > MAX_REDIRECTS) {
                throw new NoRouteToHostException("Too many redirects: " + redirectCount);
            }
            String method = mConnection.getRequestMethod();
            if (response == HTTP_TEMP_REDIRECT && !method.equals("GET") && !method.equals("HEAD")) {
                // automatically redirect the request"
                throw new NoRouteToHostException("Invalid redirect");
            }
            String location = mConnection.getHeaderField("Location");
            if (location == null) {
                throw new NoRouteToHostException("Invalid redirect");
            }
            url = new URL(mURL, /* TRICKY: don't use url! */
            location);
            if (!url.getProtocol().equals("https") && !url.getProtocol().equals("http")) {
                throw new NoRouteToHostException("Unsupported protocol redirect");
            }
            boolean sameProtocol = mURL.getProtocol().equals(url.getProtocol());
            if (!mAllowCrossProtocolRedirect && !sameProtocol) {
                throw new NoRouteToHostException("Cross-protocol redirects are disallowed");
            }
            boolean sameHost = mURL.getHost().equals(url.getHost());
            if (!mAllowCrossDomainRedirect && !sameHost) {
                throw new NoRouteToHostException("Cross-domain redirects are disallowed");
            }
            if (response != HTTP_TEMP_REDIRECT) {
                // update effective URL, unless it is a Temporary Redirect
                mURL = url;
            }
        }
        if (mAllowCrossDomainRedirect) {
            // remember the current, potentially redirected URL if redirects
            // were handled by HttpURLConnection
            mURL = mConnection.getURL();
        }
        if (response == HttpURLConnection.HTTP_PARTIAL) {
            // Partial content, we cannot just use getContentLength
            // because what we want is not just the length of the range
            // returned but the size of the full content if available.
            String contentRange = mConnection.getHeaderField("Content-Range");
            mTotalSize = -1;
            if (contentRange != null) {
                // format is "bytes xxx-yyy/zzz
                // where "zzz" is the total number of bytes of the
                // content or '*' if unknown.
                int lastSlashPos = contentRange.lastIndexOf('/');
                if (lastSlashPos >= 0) {
                    String total = contentRange.substring(lastSlashPos + 1);
                    try {
                        mTotalSize = Long.parseLong(total);
                    } catch (NumberFormatException e) {
                    }
                }
            }
        } else if (response != HttpURLConnection.HTTP_OK) {
            throw new IOException();
        } else {
            mTotalSize = mConnection.getContentLength();
        }
        if (offset > 0 && response != HttpURLConnection.HTTP_PARTIAL) {
            // data from the start of the content.
            throw new ProtocolException();
        }
        mInputStream = new BufferedInputStream(mConnection.getInputStream());
        mCurrentOffset = offset;
    } catch (IOException e) {
        mTotalSize = -1;
        mInputStream = null;
        mConnection = null;
        mCurrentOffset = -1;
        throw e;
    }
}
Also used : ProtocolException(java.net.ProtocolException) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) NoRouteToHostException(java.net.NoRouteToHostException) URL(java.net.URL)

Aggregations

NoRouteToHostException (java.net.NoRouteToHostException)31 IOException (java.io.IOException)26 InterruptedIOException (java.io.InterruptedIOException)19 ConnectException (java.net.ConnectException)19 InetSocketAddress (java.net.InetSocketAddress)14 Socket (java.net.Socket)14 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)13 InetAddress (java.net.InetAddress)12 PollStatus (org.opennms.netmgt.poller.PollStatus)11 InputStreamReader (java.io.InputStreamReader)7 BufferedInputStream (java.io.BufferedInputStream)6 BufferedReader (java.io.BufferedReader)6 URL (java.net.URL)6 ProtocolException (java.net.ProtocolException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Test (org.junit.Test)4 DatagramSocket (java.net.DatagramSocket)3 SocketTimeoutException (java.net.SocketTimeoutException)3 OutputStream (java.io.OutputStream)2