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 java.io.UnsupportedEncodingException; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.StringEntity; import org.xmlpull.v1.XmlPullParserException; import android.util.Log; 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.OSSHttpTool; import com.aliyun.android.oss.model.OSSObjectSummary; import com.aliyun.android.oss.model.ObjectGroup; import com.aliyun.android.oss.model.ObjectGroupMetaData; import com.aliyun.android.oss.xmlparser.PostObjectGroupXmlParser; import com.aliyun.android.oss.xmlserializer.PartsXmlSerializer; import com.aliyun.android.util.Helper; /** * Post Object Group????Object?OSS?Object Group * ?Object GroupObject Name,E-tag?ObjectGroup? Part * IDXML??OSS * * @author Harttle */ public class PostObjectGroupTask extends Task { /** * ?ObjectGroup */ private ObjectGroup objectGroup; /** * ?ObjectGroupMeta */ private ObjectGroupMetaData objectGroupMetaData; /** * @param httpMethod */ public PostObjectGroupTask(ObjectGroup objectGroup) { super(HttpMethod.POST); this.objectGroup = objectGroup; super.httpTool.setGroup(true); } /** * FileGroupXml * @return xml */ private String generateHttpEntity() { PartsXmlSerializer serializer = new PartsXmlSerializer("CreateFileGroup"); return serializer.serialize(objectGroup.getParts()); } /* (non-Javadoc) * @see com.aliyun.android.oss.task.Task#checkArguments() */ @Override protected void checkArguments() { if (Helper.isEmptyString(objectGroup.getBucketName()) || Helper.isEmptyString(objectGroup.getName())) { throw new IllegalArgumentException("bucketName or objectKey not set"); } if (objectGroup.getParts() == null || objectGroup.getParts().isEmpty()) { throw new IllegalArgumentException("partList not set"); } } /** * HttpPut */ protected HttpUriRequest generateHttpRequest() { // ?Http String requestUri = this.getOSSEndPoint() + httpTool.generateCanonicalizedResource("/" + objectGroup.getName()); ; HttpPut httpPut = new HttpPut(requestUri); // HttpPut String resource = httpTool .generateCanonicalizedResource("/" + objectGroup.getBucketName() + "/" + objectGroup.getName()); String authorization = OSSHttpTool.generateAuthorization(accessId, accessKey, httpMethod.toString(), "", objectGroupMetaData.getContentType(), Helper.getGMTDate(), OSSHttpTool.generateCanonicalizedHeader(objectGroupMetaData.getAttrs()), resource); httpPut.setHeader(AUTHORIZATION, authorization); httpPut.setHeader(DATE, Helper.getGMTDate()); httpPut.setHeader(HOST, OSS_HOST); try { httpPut.setEntity(new StringEntity(generateHttpEntity())); } catch (UnsupportedEncodingException e) { Log.e(this.getClass().getName(), e.getMessage()); } return httpPut; } /** * ?Post??{@link OSSObjectSummary} ?? * <ul> * <li>Bucket: ObjectBucket??</li> * <li>Key: ObjectKey</li> * <li>Size: Object?</li> * <li>ETag: ObjectETag</li> * </ul> * * @return * @throws OSSException */ public OSSObjectSummary getResult() throws OSSException { try { HttpResponse r = this.execute(); return new PostObjectGroupXmlParser().parse(r.getEntity().getContent()); } catch (OSSException osse) { throw osse; } catch (XmlPullParserException xe) { OSSException ossException = new OSSException(xe); ossException.setErrorCode(OSSErrorCode.PARSE_XML_ERROR); throw ossException; } catch (IOException ioe) { OSSException ossException = new OSSException(ioe); ossException.setErrorCode(OSSErrorCode.GET_ENTITY_CONTENT_ERROR); throw ossException; // } catch (Exception e) { // throw new OSSException(e); } finally { this.releaseHttpClient(); } } }