Search in sources :

Example 1 with HttpMethod

use of com.aliyun.oss.HttpMethod in project aliyun-oss-java-sdk by aliyun.

the class ChunkedEncodingTest method testPutObjectChunked2.

@Ignore
public void testPutObjectChunked2() {
    try {
        Date expiration = DateUtil.parseRfc822Date("Wed, 12 Mar 2015 03:15:00 GMT");
        HttpMethod method = HttpMethod.PUT;
        URL signedUrl = client.generatePresignedUrl(bucketName, key, expiration, method);
        File f = new File(filePath);
        FileInputStream fin = new FileInputStream(f);
        Map<String, String> customHeaders = new HashMap<String, String>();
        customHeaders.put("x-oss-meta-author", "aliy");
        customHeaders.put("x-oss-tag", "byurl");
        // Using url signature & chunked encoding to upload specified inputstream.
        PutObjectResult result = client.putObject(signedUrl, fin, f.length(), customHeaders, true);
        fin = new FileInputStream(f);
        byte[] binaryData = IOUtils.readStreamAsByteArray(fin);
        String expectedETag = BinaryUtil.encodeMD5(binaryData);
        String actualETag = result.getETag();
        Assert.assertEquals(expectedETag, actualETag);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) PutObjectResult(com.aliyun.oss.model.PutObjectResult) File(java.io.File) Date(java.util.Date) HttpMethod(com.aliyun.oss.HttpMethod) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Ignore(org.junit.Ignore)

Example 2 with HttpMethod

use of com.aliyun.oss.HttpMethod in project aliyun-oss-java-sdk by aliyun.

the class HttpRequestFactory method createHttpRequest.

public HttpRequestBase createHttpRequest(ServiceClient.Request request, ExecutionContext context) {
    String uri = request.getUri();
    HttpRequestBase httpRequest;
    HttpMethod method = request.getMethod();
    if (method == HttpMethod.POST) {
        HttpPost postMethod = new HttpPost(uri);
        if (request.getContent() != null) {
            postMethod.setEntity(new RepeatableInputStreamEntity(request));
        }
        httpRequest = postMethod;
    } else if (method == HttpMethod.PUT) {
        HttpPut putMethod = new HttpPut(uri);
        if (request.getContent() != null) {
            if (request.isUseChunkEncoding()) {
                putMethod.setEntity(buildChunkedInputStreamEntity(request));
            } else {
                putMethod.setEntity(new RepeatableInputStreamEntity(request));
            }
        }
        httpRequest = putMethod;
    } else if (method == HttpMethod.GET) {
        httpRequest = new HttpGet(uri);
    } else if (method == HttpMethod.DELETE) {
        httpRequest = new HttpDelete(uri);
    } else if (method == HttpMethod.HEAD) {
        httpRequest = new HttpHead(uri);
    } else if (method == HttpMethod.OPTIONS) {
        httpRequest = new HttpOptions(uri);
    } else {
        throw new ClientException("Unknown HTTP method name: " + method.toString());
    }
    configureRequestHeaders(request, context, httpRequest);
    return httpRequest;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) ClientException(com.aliyun.oss.ClientException) HttpMethod(com.aliyun.oss.HttpMethod) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpMethod (com.aliyun.oss.HttpMethod)2 ClientException (com.aliyun.oss.ClientException)1 PutObjectResult (com.aliyun.oss.model.PutObjectResult)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 URL (java.net.URL)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpHead (org.apache.http.client.methods.HttpHead)1 HttpOptions (org.apache.http.client.methods.HttpOptions)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 Ignore (org.junit.Ignore)1