Search in sources :

Example 96 with ProtocolException

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

the class NetworkStatsFactory method javaReadNetworkStatsDetail.

/**
     * Parse and return {@link NetworkStats} with UID-level details. Values are
     * expected to monotonically increase since device boot.
     */
@VisibleForTesting
public static NetworkStats javaReadNetworkStatsDetail(File detailPath, int limitUid, String[] limitIfaces, int limitTag) throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    int idx = 1;
    int lastIdx = 1;
    ProcFileReader reader = null;
    try {
        // open and consume header line
        reader = new ProcFileReader(new FileInputStream(detailPath));
        reader.finishLine();
        while (reader.hasMoreData()) {
            idx = reader.nextInt();
            if (idx != lastIdx + 1) {
                throw new ProtocolException("inconsistent idx=" + idx + " after lastIdx=" + lastIdx);
            }
            lastIdx = idx;
            entry.iface = reader.nextString();
            entry.tag = kernelToTag(reader.nextString());
            entry.uid = reader.nextInt();
            entry.set = reader.nextInt();
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            if ((limitIfaces == null || ArrayUtils.contains(limitIfaces, entry.iface)) && (limitUid == UID_ALL || limitUid == entry.uid) && (limitTag == TAG_ALL || limitTag == entry.tag)) {
                stats.addValues(entry);
            }
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 97 with ProtocolException

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

the class NetworkStatsFactory method readNetworkStatsSummaryDev.

/**
     * Parse and return interface-level summary {@link NetworkStats} measured
     * using {@code /proc/net/dev} style hooks, which may include non IP layer
     * traffic. Values monotonically increase since device boot, and may include
     * details about inactive interfaces.
     *
     * @throws IllegalStateException when problem parsing stats.
     */
public NetworkStats readNetworkStatsSummaryDev() throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    ProcFileReader reader = null;
    try {
        reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceAll));
        while (reader.hasMoreData()) {
            entry.iface = reader.nextString();
            entry.uid = UID_ALL;
            entry.set = SET_ALL;
            entry.tag = TAG_NONE;
            final boolean active = reader.nextInt() != 0;
            // always include snapshot values
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            // fold in active numbers, but only when active
            if (active) {
                entry.rxBytes += reader.nextLong();
                entry.rxPackets += reader.nextLong();
                entry.txBytes += reader.nextLong();
                entry.txPackets += reader.nextLong();
            }
            stats.addValues(entry);
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing stats", e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing stats", e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream)

Example 98 with ProtocolException

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

the class NetworkStatsFactory method readNetworkStatsSummaryXt.

/**
     * Parse and return interface-level summary {@link NetworkStats}. Designed
     * to return only IP layer traffic. Values monotonically increase since
     * device boot, and may include details about inactive interfaces.
     *
     * @throws IllegalStateException when problem parsing stats.
     */
public NetworkStats readNetworkStatsSummaryXt() throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    // return null when kernel doesn't support
    if (!mStatsXtIfaceFmt.exists())
        return null;
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    ProcFileReader reader = null;
    try {
        // open and consume header line
        reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt));
        reader.finishLine();
        while (reader.hasMoreData()) {
            entry.iface = reader.nextString();
            entry.uid = UID_ALL;
            entry.set = SET_ALL;
            entry.tag = TAG_NONE;
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            stats.addValues(entry);
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing stats", e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing stats", e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream)

Example 99 with ProtocolException

use of java.net.ProtocolException in project java-apns by notnoop.

the class TlsTunnelBuilder method build.

public Socket build(SSLSocketFactory factory, Proxy proxy, String proxyUsername, String proxyPassword, String host, int port) throws IOException {
    boolean success = false;
    Socket proxySocket = null;
    try {
        logger.debug("Attempting to use proxy : " + proxy.toString());
        InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
        proxySocket = makeTunnel(host, port, proxyUsername, proxyPassword, proxyAddress);
        // Handshake with the origin server.
        if (proxySocket == null) {
            throw new ProtocolException("Unable to create tunnel through proxy server.");
        }
        Socket socket = factory.createSocket(proxySocket, host, port, true);
        success = true;
        return socket;
    } finally {
        if (!success) {
            Utilities.close(proxySocket);
        }
    }
}
Also used : ProtocolException(java.net.ProtocolException) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 100 with ProtocolException

use of java.net.ProtocolException in project java-apns by notnoop.

the class TlsTunnelBuilder method AuthenticateProxy.

private Socket AuthenticateProxy(ConnectMethod method, ProxyClient client, String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) throws IOException {
    if ("ntlm".equalsIgnoreCase(method.getProxyAuthState().getAuthScheme().getSchemeName())) {
        // If Auth scheme is NTLM, set NT credentials with blank host and domain name
        client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, "", ""));
    } else {
        // If Auth scheme is Basic/Digest, set regular Credentials
        client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
    }
    ProxyClient.ConnectResponse response = client.connect();
    Socket socket = response.getSocket();
    if (socket == null) {
        method = response.getConnectMethod();
        throw new ProtocolException("Proxy Authentication failed. Socket not created: " + method.getStatusLine());
    }
    return socket;
}
Also used : ProtocolException(java.net.ProtocolException) ProxyClient(org.apache.commons.httpclient.ProxyClient) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Socket(java.net.Socket) NTCredentials(org.apache.commons.httpclient.NTCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

ProtocolException (java.net.ProtocolException)153 IOException (java.io.IOException)41 HttpURLConnection (java.net.HttpURLConnection)32 URL (java.net.URL)28 FileInputStream (java.io.FileInputStream)19 NetworkStats (android.net.NetworkStats)18 NetworkStatsHistory (android.net.NetworkStatsHistory)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 BufferedInputStream (java.io.BufferedInputStream)17 FileNotFoundException (java.io.FileNotFoundException)17 MalformedURLException (java.net.MalformedURLException)15 InputStream (java.io.InputStream)13 OutputStream (java.io.OutputStream)13 AtomicFile (android.util.AtomicFile)12 DataInputStream (java.io.DataInputStream)12 Map (java.util.Map)12 Test (org.junit.Test)12 BufferedReader (java.io.BufferedReader)11 InputStreamReader (java.io.InputStreamReader)11