com.fihmi.tools.minet.MiHttpRequest.java Source code

Java tutorial

Introduction

Here is the source code for com.fihmi.tools.minet.MiHttpRequest.java

Source

/*
Android Asynchronous Http Client
Copyright (c) 2011 James Smith <james@loopj.com>
http://loopj.com
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.fihmi.tools.minet;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.protocol.HttpContext;

class MiHttpRequest implements Runnable {
    private final AbstractHttpClient client;
    private final HttpContext context;
    private final HttpUriRequest request;
    private final MiHttpResponseHandler responseHandler;
    private final int request_tag;
    private int executionCount;

    public MiHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request,
            MiHttpResponseHandler responseHandler, final int request_tag) {
        this.client = client;
        this.context = context;
        this.request = request;
        this.responseHandler = responseHandler;
        this.request_tag = request_tag;
    }

    @Override
    public void run() {
        try {
            if (responseHandler != null) {
                responseHandler.sendStartMessage(request_tag);
            }
            makeRequestWithRetries();
            if (responseHandler != null) {
                responseHandler.sendFinishMessage(request_tag);
            }
        } catch (IOException e) {
            if (responseHandler != null) {
                responseHandler.sendFinishMessage(request_tag);
                responseHandler.sendFailureMessage(e, (String) null, request_tag);
            }
        }
    }

    private void makeRequest() throws IOException {
        if (!Thread.currentThread().isInterrupted()) {
            try {
                HttpResponse response = client.execute(request, context);
                if (!Thread.currentThread().isInterrupted()) {
                    if (responseHandler != null) {
                        responseHandler.sendResponseMessage(response, request_tag);
                    }
                } else {
                }
            } catch (IOException e) {
                if (!Thread.currentThread().isInterrupted()) {
                    throw e;
                }
            }
        }
    }

    private void makeRequestWithRetries() throws ConnectException {
        boolean retry = true;
        IOException cause = null;
        HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
        while (retry) {
            try {
                makeRequest();
                return;
            } catch (UnknownHostException e) {
                if (responseHandler != null) {
                    responseHandler.sendFailureMessage(e, "?", request_tag);
                }
                return;
            } catch (SocketException e) {
                // Added to detect host unreachable
                if (responseHandler != null) {
                    responseHandler.sendFailureMessage(e, "?", request_tag);
                }
                return;
            } catch (SocketTimeoutException e) {
                if (responseHandler != null) {
                    responseHandler.sendFailureMessage(e, "", request_tag);
                }
                return;
            } catch (IOException e) {
                cause = e;
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            } catch (NullPointerException e) {
                cause = new IOException("NPE in HttpClient" + e.getMessage());
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            } catch (Exception e) {
                if (responseHandler != null) {
                    responseHandler.sendFailureMessage(e, "?url?", request_tag);
                }
                return;
            }
        }

        // no retries left, crap out with exception
        ConnectException ex = new ConnectException();
        ex.initCause(cause);
        throw ex;
    }
}