Search in sources :

Example 26 with ProtocolException

use of java.net.ProtocolException in project robovm by robovm.

the class URLConnectionTest method assertInvalidRequestMethod.

private void assertInvalidRequestMethod(String requestMethod) throws Exception {
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    try {
        connection.setRequestMethod(requestMethod);
        fail();
    } catch (ProtocolException expected) {
    }
}
Also used : ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection)

Example 27 with ProtocolException

use of java.net.ProtocolException in project platform_frameworks_base by android.

the class NetworkStatsCollection method readLegacyUid.

@Deprecated
public void readLegacyUid(File file, boolean onlyTags) throws IOException {
    final AtomicFile inputFile = new AtomicFile(file);
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
        // verify file magic header intact
        final int magic = in.readInt();
        if (magic != FILE_MAGIC) {
            throw new ProtocolException("unexpected magic: " + magic);
        }
        final int version = in.readInt();
        switch(version) {
            case VERSION_UID_INIT:
                {
                    // mapping into NetworkIdentitySet.
                    break;
                }
            case VERSION_UID_WITH_IDENT:
                {
                    // for a short time.
                    break;
                }
            case VERSION_UID_WITH_TAG:
            case VERSION_UID_WITH_SET:
                {
                    // uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
                    final int identSize = in.readInt();
                    for (int i = 0; i < identSize; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final int size = in.readInt();
                        for (int j = 0; j < size; j++) {
                            final int uid = in.readInt();
                            final int set = (version >= VERSION_UID_WITH_SET) ? in.readInt() : SET_DEFAULT;
                            final int tag = in.readInt();
                            final Key key = new Key(ident, uid, set, tag);
                            final NetworkStatsHistory history = new NetworkStatsHistory(in);
                            if ((tag == TAG_NONE) != onlyTags) {
                                recordHistory(key, history);
                            }
                        }
                    }
                    break;
                }
            default:
                {
                    throw new ProtocolException("unexpected version: " + version);
                }
        }
    } catch (FileNotFoundException e) {
    // missing stats is okay, probably first boot
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : ProtocolException(java.net.ProtocolException) AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) NetworkStatsHistory(android.net.NetworkStatsHistory) DataInputStream(java.io.DataInputStream)

Example 28 with ProtocolException

use of java.net.ProtocolException in project platform_frameworks_base by android.

the class NetworkStatsCollection method readLegacyNetwork.

@Deprecated
public void readLegacyNetwork(File file) throws IOException {
    final AtomicFile inputFile = new AtomicFile(file);
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
        // verify file magic header intact
        final int magic = in.readInt();
        if (magic != FILE_MAGIC) {
            throw new ProtocolException("unexpected magic: " + magic);
        }
        final int version = in.readInt();
        switch(version) {
            case VERSION_NETWORK_INIT:
                {
                    // network := size *(NetworkIdentitySet NetworkStatsHistory)
                    final int size = in.readInt();
                    for (int i = 0; i < size; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final NetworkStatsHistory history = new NetworkStatsHistory(in);
                        final Key key = new Key(ident, UID_ALL, SET_ALL, TAG_NONE);
                        recordHistory(key, history);
                    }
                    break;
                }
            default:
                {
                    throw new ProtocolException("unexpected version: " + version);
                }
        }
    } catch (FileNotFoundException e) {
    // missing stats is okay, probably first boot
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : ProtocolException(java.net.ProtocolException) AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) NetworkStatsHistory(android.net.NetworkStatsHistory) DataInputStream(java.io.DataInputStream)

Example 29 with ProtocolException

use of java.net.ProtocolException in project platform_frameworks_base by android.

the class OSUCache method putResult.

private void putResult(ScanResult scanResult, AnqpInformationElement[] elements) {
    for (AnqpInformationElement ie : elements) {
        if (ie.getElementId() == AnqpInformationElement.HS_OSU_PROVIDERS && ie.getVendorId() == AnqpInformationElement.HOTSPOT20_VENDOR_ID) {
            try {
                HSOsuProvidersElement providers = new HSOsuProvidersElement(Constants.ANQPElementType.HSOSUProviders, ByteBuffer.wrap(ie.getPayload()).order(ByteOrder.LITTLE_ENDIAN));
                putProviders(scanResult, providers);
            } catch (ProtocolException pe) {
                Log.w(OSUManager.TAG, "Failed to parse OSU element: " + pe);
            }
        }
    }
}
Also used : ProtocolException(java.net.ProtocolException) AnqpInformationElement(android.net.wifi.AnqpInformationElement) HSOsuProvidersElement(com.android.anqp.HSOsuProvidersElement)

Example 30 with ProtocolException

use of java.net.ProtocolException in project platform_frameworks_base by android.

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

ProtocolException (java.net.ProtocolException)214 IOException (java.io.IOException)78 URL (java.net.URL)62 HttpURLConnection (java.net.HttpURLConnection)60 MalformedURLException (java.net.MalformedURLException)44 InputStreamReader (java.io.InputStreamReader)32 BufferedReader (java.io.BufferedReader)31 InputStream (java.io.InputStream)21 BufferedInputStream (java.io.BufferedInputStream)20 FileInputStream (java.io.FileInputStream)20 NetworkStats (android.net.NetworkStats)18 NetworkStatsHistory (android.net.NetworkStatsHistory)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 FileNotFoundException (java.io.FileNotFoundException)18 OutputStream (java.io.OutputStream)18 Test (org.junit.Test)16 DataInputStream (java.io.DataInputStream)14 Map (java.util.Map)13 AtomicFile (android.util.AtomicFile)12