com.huangyifei.etag.HttpClientWrapper.java Source code

Java tutorial

Introduction

Here is the source code for com.huangyifei.etag.HttpClientWrapper.java

Source

/**
 * @(#)HttpClientWrapper.java, 20141210. 
 * 
 * Copyright 2014 netease, Inc. All rights reserved.
 * Netease PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.huangyifei.etag;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import android.text.TextUtils;

import com.huangyifei.etag.disc.L;

/**
 *
 * @author huangyifei
 *
 */
public class HttpClientWrapper implements HttpWrapperInterface<HttpResponse> {
    private HttpClient client;

    private HttpUriRequest request;

    private HttpResponse response;

    public HttpClientWrapper(HttpClient client, HttpUriRequest request) {
        this.client = client;
        this.request = request;
    }

    /*
     * (non-Javadoc)
     * @see com.huangyifei.etag.HttpWrapperBase#getKeySeed()
     */
    @Override
    public String getRawKey() {
        String url = request.getURI().toString();
        String method = request.getMethod();
        Header[] headers = request.getAllHeaders();
        HttpEntity entity = null;
        String entityString = null;
        if (request instanceof HttpEntityEnclosingRequestBase) {
            entity = ((HttpEntityEnclosingRequestBase) request).getEntity();
        }
        if (entity != null) {
            if (!(entity instanceof UrlEncodedFormEntity)) {
                return "";
            }
            if (!entity.isRepeatable()) {
                return "";
            }
            try {
                entityString = EntityUtils.toString(entity);
            } catch (ParseException e) {
                L.d("generateKey", e);
            } catch (IOException e) {
                L.d("generateKey", e);
            }
        }
        StringBuilder source = new StringBuilder(method);
        source.append('-');

        source.append(url);
        source.append('-');

        if (headers != null) {
            for (Header header : headers) {
                source.append(header.getName()).append(':').append(header.getValue());
            }
            source.append('-');
        }

        if (!TextUtils.isEmpty(entityString)) {
            source.append(entityString);
        }
        return source.toString();
    }

    /*
     * (non-Javadoc)
     * @see com.huangyifei.etag.HttpWrapperBase#addEtagHeader(java.lang.String)
     */
    @Override
    public void addEtagHeader(String etag) {
        request.addHeader(EtagCache.ETAG_REQUEST_HEADER, etag);
    }

    /*
     * (non-Javadoc)
     * @see com.huangyifei.etag.HttpWrapperBase#excute()
     */
    @Override
    public HttpResult excute() {
        HttpResult result = null;
        try {
            response = client.execute(request);
            result = new HttpResult();
            result.statusCode = response.getStatusLine().getStatusCode();
            if (response.getEntity() != null) {
                result.body = EntityUtils.toString(response.getEntity());
            }
            Header header = response.getFirstHeader(EtagCache.ETAG_RESPONSE_HEADER);
            if (header != null) {
                result.etag = header.getValue();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.huangyifei.etag.HttpWrapperBase#useCacheBody(java.lang.String)
     */
    @Override
    public void useCacheBody(String body, boolean isFromCache) {
        try {
            response.setEntity(new StringEntity(body, "UTF-8"));
            if (isFromCache) {
                response.setStatusCode(HttpStatus.SC_OK);
            }
        } catch (UnsupportedEncodingException e) {
            L.d("useCacheBody", e);
        }
    }

    /*
     * (non-Javadoc)
     * @see com.huangyifei.etag.HttpWrapperBase#getFullResponse()
     */
    @Override
    public HttpResponse getFullResponse() {
        return response;
    }

}