Java tutorial
/** * Copyright (c) 2012 The Wiseserc. All rights reserved. Use of this source code * is governed by a BSD-style license that can be found in the LICENSE file. */ package com.aliyun.android.oss.task; import java.io.IOException; import net.tsz.afinal.http.RetryHandler; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.params.ConnManagerParams; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.xmlpull.v1.XmlPullParserException; import com.aliyun.android.oss.OSSErrorCode; import com.aliyun.android.oss.OSSException; import com.aliyun.android.oss.http.HttpMethod; import com.aliyun.android.oss.http.IHttpHeaders; import com.aliyun.android.oss.http.IHttpParameters; import com.aliyun.android.oss.http.OSSHttpTool; import com.aliyun.android.oss.model.HttpResponseError; import com.aliyun.android.oss.xmlparser.HttpResponseErrorParser; /** * OSS * * @author Michael */ public abstract class Task implements IHttpParameters, IHttpHeaders { public static String OSS_PROTOCOL = "http://"; /** * OSS HOST */ public static String OSS_HOST = "oss.aliyuncs.com"; /** * ?ID???? */ protected String accessId; /** * ?key, ???? */ protected String accessKey; /** * http */ protected HttpMethod httpMethod; /** * Http */ protected OSSHttpTool httpTool; /** * bucket?? */ protected String bucketName; /** * Http? */ protected DefaultHttpClient client; private static int socketTimeout = 10 * 1000; //10 private static int maxRetries = 3;//?RetryHandler /** * * * @param httpMethod * */ public Task(HttpMethod httpMethod) { this(httpMethod, null); } public Task(HttpMethod httpMethod, String bucketName) { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); httpTool = new OSSHttpTool(); this.httpMethod = httpMethod; this.client = new DefaultHttpClient(httpParams); this.client.setHttpRequestRetryHandler(new RetryHandler(maxRetries)); ; this.bucketName = bucketName; } protected String getOSSEndPoint() { if (this.bucketName == null) { return String.format("%s%s", OSS_PROTOCOL, OSS_HOST); } return String.format("%s%s.%s", OSS_PROTOCOL, this.bucketName, OSS_HOST); } protected String getOSSHost() { if (this.bucketName == null) { return OSS_HOST; } return String.format("%s.%s", this.bucketName, OSS_HOST); } /** * ?AccessKey * * @param accessKeyId * @param accessKeySecret */ public void initKey(String accessKeyId, String accessKeySecret) { this.accessId = accessKeyId; this.accessKey = accessKeySecret; } /** * ? ???200???? * ???xml??? {@link OSSException} * * @return ?? * @throws OSSException */ protected HttpResponse execute() throws OSSException { try { checkArguments(); } catch (IllegalArgumentException ie) { OSSException ossException = new OSSException(ie); ossException.setErrorCode(OSSErrorCode.INVALID_ARGUMENTS); throw ossException; } try { HttpResponse r = this.client.execute(this.generateHttpRequest()); if (r.getStatusLine().getStatusCode() / 100 <= 3) { return r; } else { HttpResponseError error = this.getResponseError(r); OSSException osse = new OSSException(error); throw osse; } } catch (OSSException osse) { throw osse; } catch (Exception e) { OSSException ossException = new OSSException(e); ossException.setErrorCode(OSSErrorCode.UNKNOWN_ERROR); throw new OSSException(ossException); } } /** * Http??200??? * * @param response * @return {@link HttpResponseError}{@link HttpResponseErrorParser} * ?? * @throws IllegalStateException * @throws XmlPullParserException * @throws IOException */ protected HttpResponseError getResponseError(HttpResponse response) throws IllegalStateException, XmlPullParserException, IOException { HttpResponseErrorParser parser = new HttpResponseErrorParser(); HttpResponseError error = parser.parse(response.getEntity().getContent()); return error; } /** * ?Http??????Method */ protected abstract HttpUriRequest generateHttpRequest(); /** * ??? */ protected abstract void checkArguments(); /** * HttpClientTask?? */ protected void releaseHttpClient() { this.client.getConnectionManager().shutdown(); } public String getAccessId() { return accessId; } public void setAccessId(String accessId) { this.accessId = accessId; } public String getAccessKey() { return accessKey; } public void setAccessKey(String accessKey) { this.accessKey = accessKey; } }