Example usage for java.io IOException getClass

List of usage examples for java.io IOException getClass

Introduction

In this page you can find the example usage for java.io IOException getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:cn.isif.util_plus.http.client.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }/*  w w w .  ja  v a 2 s  .  c o  m*/

    Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = isReqSent == null ? false : (Boolean) isReqSent;

    if (retriedTimes > maxRetries) {
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                LogUtils.e("retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            LogUtils.e("retry error", e);
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_INTERVAL); // sleep a while and retry http request again.
    }

    return retry;
}

From source file:com.xc.framework.https.client.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }//from  w w w .  ja  va  2 s  .c om

    Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = isReqSent == null ? false : (Boolean) isReqSent;

    if (retriedTimes > maxRetries) {
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                Log.e("RetryHandler", "retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            Log.e("retry error", "" + e);
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_INTERVAL); // sleep a while and retry
        // http request again.
    }

    return retry;
}

From source file:com.examination.afinal.http.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // ????????5            retry = false;
    } else if (exceptionBlacklist.contains(exception.getClass())) {
        // ????????
        retry = false;//from   w  ww  .  j a  va2s .co  m
    } else if (exceptionWhitelist.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        retry = currentReq != null && !"POST".equals(currentReq.getMethod());
    }

    if (retry) {
        //?????            SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}

From source file:com.drive.student.xutils.http.client.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }//from  w w  w.  j  a  v  a  2 s  .com

    Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = isReqSent == null ? false : (Boolean) isReqSent;

    if (retriedTimes > maxRetries) {
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                LogUtils.e("retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            LogUtils.e("retry error", e);
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_INTERVAL); // sleep a while and retry
        // http request again.
    }

    return retry;
}

From source file:com.aol.advertising.qiao.injector.file.watcher.QiaoFileManager.java

@Override
public void onCreate(Path file) {
    if (Files.notExists(file)) {
        logger.info("file " + file + " does not exist");
        return;/*from  w w w  .jav  a 2  s. c  o  m*/
    }

    if (pathMatcher.matches(file)) {
        try {
            Path new_path = renameToTmpFilePath(file);

            long checksum = CommonUtils.checksumOptionalylUseFileLength(new_path.toFile(), checksumByteLength);

            Path target_path = file;
            if (!nameContainsChecksum(file, checksum)) {
                target_path = resolveNewFilePath(file, checksum);
            }

            FileOperationEvent event = new FileOperationEvent(EVENT_TYPE.RENAME_FILE, new_path, checksum,
                    target_path);
            this.addToQueue(event);

        } catch (IOException e) {
            logger.error(e.getClass().getName() + ": " + e.getMessage());

        } catch (InterruptedException e) {
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestSuppressTransientExceptions.java

@Test
public void TestSuppressTransientExceptions() throws IOException {
    try {//from   ww w .ja va 2s. c o m
        final Logger logger = Logger.getRootLogger();
        Logger.getLogger(RequestHandler.class).setLevel(Level.ALL);
        Logger.getLogger(TestSuppressTransientExceptions.class).setLevel(Level.ALL);

        final int MAX_RETRY = 2;
        Configuration conf = new HdfsConfiguration();
        RequestHandler.setRetryCount(MAX_RETRY);
        HdfsStorageFactory.resetDALInitialized();
        HdfsStorageFactory.setConfiguration(conf);
        HdfsStorageFactory.formatStorage();

        //not all transient exceptions are suppressed only
        //the dealock, dbextent and lock upgrade exceptions are suppressed

        LOG.info("******* Test 0 ********");
        final LogVerificationAppender appender = new LogVerificationAppender();
        logger.addAppender(appender);
        IOException e = new TransientStorageException("TransientStorageException");
        try {
            runWithErrors(3, e, HDFSOperationType.TEST);
            fail("Expecting Exception");
        } catch (TransientStorageException te) {
            assertTrue(getExceptionCount(appender.getLog(), e.getClass()) == 3);
        }

        LOG.info("******* Test 1 ********");
        final LogVerificationAppender appender1 = new LogVerificationAppender();
        logger.addAppender(appender1);
        e = new TransientStorageException("TransientStorageException");
        runWithErrors(2, e, HDFSOperationType.TEST);
        assertTrue(getExceptionCount(appender1.getLog(), e.getClass()) == 2);

        LOG.info("******* Test 2 ********");
        final LogVerificationAppender appender2 = new LogVerificationAppender();
        logger.addAppender(appender2);
        e = new TransientDeadLockException("TransientDeadLockException");
        runWithErrors(2, e, HDFSOperationType.TEST);
        assertTrue(getExceptionCount(appender2.getLog(), e.getClass()) == 0);

        LOG.info("******* Test 3 ********");
        final LogVerificationAppender appender3 = new LogVerificationAppender();
        logger.addAppender(appender3);
        e = new TransientDeadLockException("TransientDeadLockException");
        try {
            runWithErrors(3, e, HDFSOperationType.TEST);
            fail("Expecting Exception");
        } catch (TransientDeadLockException de) {
            assertTrue(getExceptionCount(appender3.getLog(), e.getClass()) == 3);
        }

        //Testing nontransient exceptions, i.e. OutOfDBExtentsException, LockUpgradeException
        LOG.info("******* Test 4 ********");
        int failures = 100;
        final LogVerificationAppender appender4 = new LogVerificationAppender();
        logger.addAppender(appender4);
        try {
            e = new OutOfDBExtentsException("OutOfDBExtentsException");
            runWithErrors(failures, e, HDFSOperationType.TEST);
            fail();
        } catch (OutOfDBExtentsException ex) {
        }
        assertTrue(getExceptionCount(appender4.getLog(), e.getClass()) == 1); // for
        // HDFSOperationType.TEST the exception will not be suppressed

        LOG.info("******* Test 5 ********");
        failures = 100;
        final LogVerificationAppender appender5 = new LogVerificationAppender();
        logger.addAppender(appender5);
        try {
            e = new OutOfDBExtentsException("OutOfDBExtentsException");
            runWithErrors(failures, e, HDFSOperationType.COMPLETE_FILE);
            fail();
        } catch (OutOfDBExtentsException ex) {
        }
        assertTrue(getExceptionCount(appender5.getLog(), e.getClass()) == 0);

        LOG.info("******* Test 6 ********");
        failures = 100;
        final LogVerificationAppender appender6 = new LogVerificationAppender();
        logger.addAppender(appender6);
        try {
            e = new LockUpgradeException("LockUpgradeException");
            runWithErrors(failures, e, HDFSOperationType.TEST);
            fail();
        } catch (LockUpgradeException ex) {
        }
        assertTrue(getExceptionCount(appender6.getLog(), e.getClass()) == 1); // for
        // HDFSOperationType.TEST the exception will not be suppressed

        LOG.info("******* Test 7 ********");
        failures = 100;
        final LogVerificationAppender appender7 = new LogVerificationAppender();
        logger.addAppender(appender7);
        try {
            e = new LockUpgradeException("LockUpgradeException");
            runWithErrors(failures, e, HDFSOperationType.GET_BLOCK_LOCATIONS);
            fail();
        } catch (LockUpgradeException ex) {
        }
        assertTrue(getExceptionCount(appender7.getLog(), e.getClass()) == 0);

    } finally {
    }
}

From source file:com.vdisk.net.session.RetryHandler.java

public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Log.d("Test", "retry count->" + executionCount);

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;//from   w  ww.  jav  a2 s  .  co m
    } else if (exceptionBlacklist.contains(exception.getClass())) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (exceptionWhitelist.contains(exception.getClass())) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

        Log.d("Test", "HttpUriRequest:" + currentReq);
        if (currentReq != null) {
            String requestType = currentReq.getMethod();
            retry = !requestType.equals("POST") && !requestType.equals("PUT");
        }

    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}

From source file:cz.incad.kramerius.audio.servlets.ServletAudioHttpRequestForwarder.java

private void forwardData(InputStream input, ServletOutputStream output) {
    try {//ww w.ja  va2  s . com
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesForwarded;
        while ((bytesForwarded = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesForwarded);
        }
    } catch (IOException ex) {
        if (ex.getCause() != null && ex.getCause() instanceof SocketException
                && (ex.getCause().getMessage().equals(CONNECTION_RESET)
                        || ex.getCause().getMessage().equals(BROKEN_PIPE))) {
            LOGGER.warning("Connection reset probably by client (or by repository)");
        } else {
            if (!"ClientAbortException".equals(ex.getClass().getSimpleName())) {
                LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
            }
        }
    } finally {
        try {
            LOGGER.fine("closing connection to repository");
            input.close();
            //output stream should not be closed here because it is closed by container. 
            //If closed here, for example filters won't be able to write to stream anymore
        } catch (IOException ex1) {
            LOGGER.log(Level.SEVERE, "Failed to close connection to repository", ex1);
        }
    }
}

From source file:com.adavr.http.Client.java

public Client() {
    httpClient = new DefaultHttpClient(new BasicClientConnectionManager());
    localContext = new BasicHttpContext();
    byteStream = new ByteArrayOutputStream();

    CookieStore cookieStore = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override//from   w w w .  j  a  v  a2 s. com
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, DEFAULT_USER_AGENT);
    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= DEFAULT_RETRY) {
                // Do not retry if over max retry count
                return false;
            }
            System.out.println(exception.getClass().getName());
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (exception instanceof SSLHandshakeException) {
                // Do not retry on SSL handshake exception
                return false;
            }
            HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // Retry if the request is considered idempotent
                return true;
            }
            return false;
        }
    };

    httpClient.setHttpRequestRetryHandler(myRetryHandler);
}

From source file:co.paralleluniverse.fibers.httpclient.FiberHttpClient.java

@Override
@Suspendable//from   ww w. j  a v  a  2 s.co m
protected final CloseableHttpResponse doExecute(final HttpHost target, final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException {
    try {
        for (int executionCount = 0;; executionCount++) {
            try {
                final HttpResponse response = new AsyncHttpReq() {
                    @Override
                    protected void requestAsync() {
                        client.execute(target, request, context, this);
                    }
                }.run();
                return new CloseableHttpResponseWrapper(response);
            } catch (IOException ex) {
                if (httpRequestRetryHandler != null
                        && httpRequestRetryHandler.retryRequest(ex, executionCount, context)) {
                    if (this.log.isInfoEnabled()) {
                        this.log.info("I/O exception (" + ex.getClass().getName()
                                + ") caught when processing request: " + ex.getMessage());
                    }
                    if (this.log.isDebugEnabled()) {
                        this.log.debug(ex.getMessage(), ex);
                    }
                    this.log.info("Retrying request");
                } else
                    throw ex;
            }
        }
    } catch (SuspendExecution e) {
        throw new AssertionError();
    } catch (IllegalStateException ise) {
        if (ioreactor != null) {
            final List<ExceptionEvent> events = ioreactor.getAuditLog();
            if (events != null) {
                for (ExceptionEvent event : events) {
                    final StringBuilder msg = new StringBuilder();
                    msg.append("Apache Async HTTP Client I/O Reactor exception timestamp: ");
                    msg.append(event.getTimestamp());
                    if (event.getCause() != null) {
                        msg.append(", cause stacktrace:\n");
                        final StringWriter sw = new StringWriter();
                        final PrintWriter pw = new PrintWriter(sw);
                        ise.getCause().printStackTrace(pw);
                        msg.append(sw.toString());
                    }
                    this.log.fatal(msg.toString());
                }
            }
        }
        throw ise;
    }
}