com.aliyun.android.oss.task.DeleteMultipleObjectTask.java Source code

Java tutorial

Introduction

Here is the source code for com.aliyun.android.oss.task.DeleteMultipleObjectTask.java

Source

/**
 * 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.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;

import android.util.Log;

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.xmlparser.DeleteMultipleObjectXmlParser;
import com.aliyun.android.oss.xmlserializer.DeleteMultipleObjectXmlSerializer;
import com.aliyun.android.util.Base64;
import com.aliyun.android.util.Helper;
import com.aliyun.android.util.MD5Util;

/**
 * HTTP?BucketObject??1000Object
 * ????(verbose)???(quiet)?
 * <ul>
 *   <li>?:OSS???Object</li>
 *   <li>??:OSS???Object???</li>
 * </ul>
 * 
 * @author luoruici 
 *
 */
public class DeleteMultipleObjectTask extends Task {
    /**
     * ObjectKey List
     */
    private List<String> keyList;

    /**
     * ????quiet?
     * truequietquiet??false
     */
    private boolean quiet;

    /**
     * ?DeleteMultipleObjectTaskquitefalse??
     *
     * @param bucketName
     * @param keyList
     */
    public DeleteMultipleObjectTask(String bucketName, List<String> keyList) {
        this(bucketName, keyList, false);
    }

    public DeleteMultipleObjectTask(String bucketName, List<String> keyList, boolean quiet) {
        super(HttpMethod.POST, bucketName);
        this.keyList = keyList;
        this.quiet = quiet;
    }

    private String generateHttpEntityStr() throws UnsupportedEncodingException {
        DeleteMultipleObjectXmlSerializer serializer = new DeleteMultipleObjectXmlSerializer("Delete");
        String xml = serializer.serialize(this.keyList, this.quiet);
        return xml;
    }

    /**
     * key???????
     * ???0List?
     * 
     * @return key
     * @throws OSSException
     */
    public List<String> getResult() throws OSSException {
        try {
            HttpResponse r = this.execute();
            return new DeleteMultipleObjectXmlParser().parse(r.getEntity().getContent());
        } catch (OSSException osse) {
            throw osse;
        } catch (Exception e) {
            throw new OSSException(e);
        } finally {
            this.releaseHttpClient();
        }
    }

    @Override
    protected HttpUriRequest generateHttpRequest() {
        String requestUri = this.getOSSEndPoint() + httpTool.generateCanonicalizedResource("/?delete");

        HttpPost httpPost = new HttpPost(requestUri);

        String md5 = "";
        try {
            String entity = generateHttpEntityStr();
            byte[] mds = MD5Util.getMD5(entity.getBytes("UTF-8"));
            md5 = Base64.encode(mds);
            httpPost.setHeader("Content-MD5", md5);
            httpPost.setEntity(new StringEntity(entity, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            Log.e("exception occurs when deleting mutiple object", e.getMessage());
        }

        String resource = httpTool.generateCanonicalizedResource("/" + bucketName + "/?delete");
        String dateStr = Helper.getGMTDate();
        String authorization = OSSHttpTool.generateAuthorization(accessId, accessKey, httpMethod.toString(), md5,
                "text/plain; charsert=UTF-8", dateStr, "", resource);

        httpPost.setHeader(CONTENT_TYPE, "text/plain; charsert=UTF-8");
        httpPost.setHeader(AUTHORIZATION, authorization);
        httpPost.setHeader(DATE, dateStr);

        return httpPost;
    }

    @Override
    protected void checkArguments() {
        if (Helper.isEmptyString(bucketName) || keyList == null || keyList.size() == 0) {
            throw new IllegalArgumentException("bucketName or objectKeyList not set");
        }
    }

}