Search in sources :

Example 86 with SocketTimeoutException

use of java.net.SocketTimeoutException in project Rutgers-Course-Tracker by tevjef.

the class CourseFragment method showError.

@Override
public void showError(Throwable t) {
    String message;
    Resources resources = getContext().getResources();
    if (t instanceof UnknownHostException) {
        message = resources.getString(R.string.no_internet);
    } else if (t instanceof JsonParseException || t instanceof RutgersServerIOException) {
        message = resources.getString(R.string.server_down);
    } else if (t instanceof SocketTimeoutException) {
        message = resources.getString(R.string.timed_out);
    } else {
        message = t.getMessage();
    }
    //error message finalized, now save it.
    mViewState.errorMessage = message;
    // Redirects the message that would usually be in the snackbar, to error layout.
    if (!adapterHasItems()) {
        showLayout(LayoutType.ERROR);
        TextView textViewMessage = ButterKnife.findById(mErrorView, R.id.text);
        textViewMessage.setText(message);
    } else {
        showSnackBar(message);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) RutgersServerIOException(com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersServerIOException) TextView(android.widget.TextView) Resources(android.content.res.Resources) JsonParseException(com.google.gson.JsonParseException)

Example 87 with SocketTimeoutException

use of java.net.SocketTimeoutException in project Rutgers-Course-Tracker by tevjef.

the class TrackedSectionsFragment method showError.

@Override
public void showError(Throwable t) {
    String message;
    Resources resources = getContext().getResources();
    if (t instanceof UnknownHostException) {
        message = resources.getString(R.string.no_internet);
    } else if (t instanceof JsonParseException || t instanceof RutgersServerIOException) {
        message = resources.getString(R.string.server_down);
    } else if (t instanceof SocketTimeoutException) {
        message = resources.getString(R.string.timed_out);
    } else {
        message = t.getMessage();
    }
    // Save current error message ahead of config change.
    mViewState.errorMessage = message;
    // https://www.google.com/design/spec/patterns/errors.html#errors-app-errors
    if (!adapterHasItems()) {
        showLayout(LayoutType.ERROR);
        TextView textViewMessage = ButterKnife.findById(mErrorView, R.id.text);
        textViewMessage.setText(message);
    } else {
        showSnackBar(message);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) RutgersServerIOException(com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersServerIOException) TextView(android.widget.TextView) Resources(android.content.res.Resources) JsonParseException(com.google.gson.JsonParseException)

Example 88 with SocketTimeoutException

use of java.net.SocketTimeoutException in project bazel by bazelbuild.

the class RetryingInputStreamTest method maxRetries_givesUp.

@Test
@SuppressWarnings("unchecked")
public void maxRetries_givesUp() throws Exception {
    when(delegate.read()).thenReturn(1).thenThrow(new IOException()).thenThrow(new IOException()).thenThrow(new IOException()).thenThrow(new SocketTimeoutException());
    when(reconnector.connect(any(Throwable.class), any(ImmutableMap.class))).thenReturn(connection);
    when(connection.getInputStream()).thenReturn(delegate);
    when(connection.getHeaderField("Content-Range")).thenReturn("bytes 1-42/42");
    stream.read();
    try {
        stream.read();
        fail("Expected SocketTimeoutException");
    } catch (SocketTimeoutException e) {
        assertThat(e.getSuppressed()).hasLength(3);
        verify(reconnector, times(3)).connect(any(Throwable.class), eq(ImmutableMap.of("Range", "bytes 1-")));
        verify(delegate, times(5)).read();
        verify(delegate, times(3)).close();
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 89 with SocketTimeoutException

use of java.net.SocketTimeoutException in project bazel by bazelbuild.

the class RetryingInputStreamTest method failureWhenNoBytesAreRead_doesntUseRange.

@Test
@SuppressWarnings("unchecked")
public void failureWhenNoBytesAreRead_doesntUseRange() throws Exception {
    when(delegate.read()).thenThrow(new SocketTimeoutException());
    when(newDelegate.read()).thenReturn(1);
    when(reconnector.connect(any(Throwable.class), any(ImmutableMap.class))).thenReturn(connection);
    when(connection.getInputStream()).thenReturn(newDelegate);
    assertThat(stream.read()).isEqualTo(1);
    verify(reconnector).connect(any(Throwable.class), eq(ImmutableMap.<String, String>of()));
    verify(delegate).read();
    verify(delegate).close();
    verify(newDelegate).read();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 90 with SocketTimeoutException

use of java.net.SocketTimeoutException in project Conversations by siacs.

the class DNSHelper method queryDNS.

public static Bundle queryDNS(String host, InetAddress dnsServer) {
    Bundle bundle = new Bundle();
    try {
        client.setTimeout(Config.SOCKET_TIMEOUT * 1000);
        final String qname = "_xmpp-client._tcp." + host.toLowerCase(Locale.US);
        final String tlsQname = "_xmpps-client._tcp." + host.toLowerCase(Locale.US);
        Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host);
        final Map<Integer, List<TlsSrv>> priorities = new TreeMap<>();
        final Map<String, List<String>> ips4 = new TreeMap<>();
        final Map<String, List<String>> ips6 = new TreeMap<>();
        fillSrvMaps(qname, dnsServer, priorities, ips4, ips6, false);
        fillSrvMaps(tlsQname, dnsServer, priorities, ips4, ips6, true);
        final List<TlsSrv> result = new ArrayList<>();
        for (final List<TlsSrv> s : priorities.values()) {
            result.addAll(s);
        }
        final ArrayList<Bundle> values = new ArrayList<>();
        if (result.size() == 0) {
            DNSMessage response;
            try {
                response = client.query(host, TYPE.A, CLASS.IN, dnsServer.getHostAddress());
                for (int i = 0; i < response.getAnswers().length; ++i) {
                    values.add(createNamePortBundle(host, 5222, response.getAnswers()[i].getPayload(), false));
                }
            } catch (SocketTimeoutException e) {
                Log.d(Config.LOGTAG, "ignoring timeout exception when querying A record on " + dnsServer.getHostAddress());
            }
            try {
                response = client.query(host, TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
                for (int i = 0; i < response.getAnswers().length; ++i) {
                    values.add(createNamePortBundle(host, 5222, response.getAnswers()[i].getPayload(), false));
                }
            } catch (SocketTimeoutException e) {
                Log.d(Config.LOGTAG, "ignoring timeout exception when querying AAAA record on " + dnsServer.getHostAddress());
            }
            values.add(createNamePortBundle(host, 5222, false));
            bundle.putParcelableArrayList("values", values);
            return bundle;
        }
        for (final TlsSrv tlsSrv : result) {
            final SRV srv = tlsSrv.srv;
            final String name = srv.getName() != null ? srv.getName().toLowerCase(Locale.US) : null;
            if (ips6.containsKey(name)) {
                values.add(createNamePortBundle(name, srv.getPort(), ips6, tlsSrv.tls));
            } else {
                try {
                    DNSMessage response = client.query(name, TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
                    for (int i = 0; i < response.getAnswers().length; ++i) {
                        values.add(createNamePortBundle(name, srv.getPort(), response.getAnswers()[i].getPayload(), tlsSrv.tls));
                    }
                } catch (SocketTimeoutException e) {
                    Log.d(Config.LOGTAG, "ignoring timeout exception when querying AAAA record on " + dnsServer.getHostAddress());
                }
            }
            if (ips4.containsKey(name)) {
                values.add(createNamePortBundle(name, srv.getPort(), ips4, tlsSrv.tls));
            } else {
                DNSMessage response = client.query(name, TYPE.A, CLASS.IN, dnsServer.getHostAddress());
                for (int i = 0; i < response.getAnswers().length; ++i) {
                    values.add(createNamePortBundle(name, srv.getPort(), response.getAnswers()[i].getPayload(), tlsSrv.tls));
                }
            }
            values.add(createNamePortBundle(name, srv.getPort(), tlsSrv.tls));
        }
        bundle.putParcelableArrayList("values", values);
    } catch (SocketTimeoutException e) {
        bundle.putString("error", "timeout");
    } catch (Exception e) {
        bundle.putString("error", "unhandled");
    }
    return bundle;
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketTimeoutException(java.net.SocketTimeoutException) SRV(de.measite.minidns.record.SRV) ArrayList(java.util.ArrayList) List(java.util.List) DNSMessage(de.measite.minidns.DNSMessage)

Aggregations

SocketTimeoutException (java.net.SocketTimeoutException)721 IOException (java.io.IOException)420 Test (org.junit.Test)139 Socket (java.net.Socket)103 SocketException (java.net.SocketException)99 ServerSocket (java.net.ServerSocket)79 InputStream (java.io.InputStream)77 ConnectException (java.net.ConnectException)70 UnknownHostException (java.net.UnknownHostException)64 InetSocketAddress (java.net.InetSocketAddress)60 URL (java.net.URL)51 EOFException (java.io.EOFException)48 HttpURLConnection (java.net.HttpURLConnection)47 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)42 InterruptedIOException (java.io.InterruptedIOException)40 ArrayList (java.util.ArrayList)40 MalformedURLException (java.net.MalformedURLException)38 InputStreamReader (java.io.InputStreamReader)37 HashMap (java.util.HashMap)36 OutputStream (java.io.OutputStream)35