Example usage for java.net SocketTimeoutException getCause

List of usage examples for java.net SocketTimeoutException getCause

Introduction

In this page you can find the example usage for java.net SocketTimeoutException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.intuit.elves.network.CustomNetwork.java

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        HttpResponse httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = Collections.emptyMap();
        try {//  w ww.j av a2  s .  c  o  m
            // Gather headers.
            Map<String, String> headers = new HashMap<String, String>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            responseHeaders = convertHeaders(httpResponse.getAllHeaders());
            // Handle cache validation.
            if (statusCode == HttpStatus.SC_NOT_MODIFIED) {

                Entry entry = request.getCacheEntry();
                if (entry == null) {
                    return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null, responseHeaders, true);
                }

                // A HTTP 304 response does not have all header fields. We
                // have to use the header fields from the cache entry plus
                // the new ones from the response.
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
                entry.responseHeaders.putAll(responseHeaders);
                return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data, entry.responseHeaders, true);
            }

            // if the request is slow, log it.
            long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
            logSlowRequests(requestLifetime, request, statusLine);

            if (statusCode < 200 || statusCode > 299) {
                // Dump the response in debug mode
                if (CustomLog.mDebugLogEnabled && httpResponse.getEntity() != null
                        && request instanceof CustomRequest) {
                    NetworkUtil.getResponsePayloadAsString(httpResponse.getEntity());
                }
                throw new IOException();
            }
            // Some responses such as 204s do not have content.  We must check.
            if (httpResponse.getEntity() != null) {
                return new CustomNetworkResponse(statusCode, httpResponse.getEntity(), responseHeaders, false);
            } else {
                // Add 0 byte response as a way of honestly representing a
                // no-content request.
                responseContents = new byte[0];
            }
            return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket", request, new TimeoutError());
        } catch (ConnectTimeoutException e) {
            attemptRetryOnException("connection", request, new TimeoutError());
        } catch (UnknownHostException e) {
            attemptRetryOnException("connection", request, new ServerError(e));
        } catch (ClientProtocolException e) {
            attemptRetryOnException("connection", request, new ServerError(e));
        } catch (MalformedURLException e) {
            throw new RuntimeException("Bad URL " + request.getUrl(), e);
        } catch (IOException e) {
            int statusCode = 0;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } else {
                if (e.getCause() instanceof CustomError) {
                    throw (CustomError) e.getCause();
                }
                throw new NoConnectionError(e);
            }
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            if (httpResponse.getEntity() != null) {
                networkResponse = new CustomNetworkResponse(statusCode, httpResponse.getEntity(),
                        responseHeaders, false);
                if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
                } else if (statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR
                        || statusCode == HttpStatus.SC_BAD_GATEWAY) {
                    attemptRetryOnException("server", request, new ServerError(networkResponse));
                } else {
                    throw new ServerError(networkResponse);
                }
            } else {
                throw new NetworkError();
            }
        }
    }
}

From source file:uk.sipperfly.utils.FTPUtil.java

/**
 * Upload a single file to the FTP server.
 *
 * @param ftpClient      an instance of org.apache.commons.net.ftp.FTPClient class.
 * @param localFilePath  Path of the file on local computer
 * @param remoteFilePath Path of the file on remote the server
 * @return true if the file was uploaded successfully, false otherwise
 * @throws IOException if any network or IO error occurred.
 *//*from   w  ww  .  j a va 2 s . c  o m*/
public static boolean uploadSingleFile(FTPClient ftpClient, String localFilePath, String remoteFilePath)
        throws FileNotFoundException, IOException {
    File localFile = new File(localFilePath);
    try (InputStream inputStream = new FileInputStream(localFile)) {
        try {
            return ftpClient.storeFile(remoteFilePath, inputStream);
        } catch (SocketTimeoutException e) {
            Logger.getLogger(GACOM).log(Level.SEVERE, "upload Single File: ", e.getCause());
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
                Logger.getLogger(GACOM).log(Level.SEVERE, "upload Single File: ", e.getCause());
            }
            return false;
        }
    }
}