Changeset 661

Show
Ignore:
Timestamp:
05/21/07 13:56:13 (2 years ago)
Author:
yuanying
Message:

HttpClientConnection? supports Follow Redirects.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • org.jalcedo.client/trunk/org.jalcedo.client.rest/src/org/jalcedo/client/rest/connect/HttpClientConnection.java

    r656 r661  
    1414import java.io.OutputStream; 
    1515import java.util.ArrayList; 
     16import java.util.HashMap; 
    1617import java.util.Iterator; 
    1718import java.util.List; 
    1819import java.util.Map; 
    1920 
     21import org.apache.commons.httpclient.Header; 
    2022import org.apache.commons.httpclient.HttpClient; 
    2123import org.apache.commons.httpclient.HttpException; 
    2224import org.apache.commons.httpclient.HttpMethod; 
     25import org.apache.commons.httpclient.HttpStatus; 
    2326import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 
    2427import org.apache.commons.httpclient.NameValuePair; 
     28import org.apache.commons.httpclient.URI; 
     29import org.apache.commons.httpclient.URIException; 
    2530import org.apache.commons.httpclient.methods.DeleteMethod; 
    2631import org.apache.commons.httpclient.methods.EntityEnclosingMethod; 
     
    4247 
    4348    protected final Log logger = LogFactory.getLog(this.getClass()); 
     49     
     50    private boolean followRedirects = true; 
     51 
     52    /** 
     53     *  
     54     * @return 
     55     */ 
     56    public synchronized boolean isFollowRedirects() { 
     57        return followRedirects; 
     58    } 
     59 
     60    /** 
     61     *  
     62     * @param followRedirects 
     63     */ 
     64    public synchronized void setFollowRedirects(boolean followRedirects) { 
     65        this.followRedirects = followRedirects; 
     66    } 
    4467 
    4568    /* 
     
    7093        try { 
    7194            GetMethod method = new GetMethod(url); 
     95            if (this.isFollowRedirects()) { 
     96                method.setFollowRedirects(true); 
     97            } 
    7298            return doMethod(queryParams, method); 
    7399        } catch (IOException e) { 
     
    90116        this.execute(method); 
    91117        this.checkHttpResponse(method); 
     118        if (this.isFollowRedirects() && this.needRedirect(method)) { 
     119            URI redirectUri = this.extractRedirectURI(method); 
     120            return this.get(redirectUri.toString(), new HashMap<String, String>()); 
     121        } 
    92122        return method.getResponseBodyAsString(); 
     123    } 
     124     
     125    /** 
     126     *  
     127     * @param method 
     128     * @return 
     129     */ 
     130    private URI extractRedirectURI(HttpMethod method) { 
     131        URI redirectUri = null; 
     132        Header header = method.getRequestHeader("location"); 
     133        try { 
     134            redirectUri = new URI(header.getValue(), true); 
     135            if (!redirectUri.isRelativeURI()) { 
     136                redirectUri = new URI(method.getURI(), redirectUri); 
     137            } 
     138        } catch (URIException e) { 
     139            // TODO Auto-generated catch block 
     140            throw new RuntimeException(e); 
     141        } catch (NullPointerException e) { 
     142            // TODO Auto-generated catch block 
     143            throw new RuntimeException(e); 
     144        } 
     145        return redirectUri; 
     146    } 
     147 
     148    /** 
     149     * this http method needs redirect. 
     150     * @param method 
     151     * @return 
     152     */ 
     153    private boolean needRedirect(HttpMethod method) { 
     154        switch (method.getStatusCode()) { 
     155        case HttpStatus.SC_MOVED_PERMANENTLY: 
     156        case HttpStatus.SC_MOVED_TEMPORARILY: 
     157        case HttpStatus.SC_SEE_OTHER: 
     158        case HttpStatus.SC_TEMPORARY_REDIRECT: 
     159            return true; 
     160        default: 
     161            return false; 
     162        } 
    93163    } 
    94164