Skip to content

Commit 020e74b

Browse files
committed
support bucket policy.
1 parent 6ec4028 commit 020e74b

File tree

11 files changed

+461
-4
lines changed

11 files changed

+461
-4
lines changed

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,83 @@ public ServerSideEncryptionConfiguration getBucketEncryption(GenericRequest gene
25382538
*/
25392539
public void deleteBucketEncryption(GenericRequest genericRequest) throws OSSException, ClientException;
25402540

2541+
/**
2542+
* Sets the policy on the {@link Bucket} instance.
2543+
*
2544+
* @param bucketName
2545+
* Bucket name.
2546+
* @param policyText
2547+
* Policy JSON text, please refer to the policy writing rules of Aliyun
2548+
* @throws OSSException
2549+
* OSS Server side exception.
2550+
* @throws ClientException
2551+
* OSS Client side exception.
2552+
*/
2553+
public void setBucketPolicy(String bucketName, String policyText) throws OSSException, ClientException;
2554+
2555+
/**
2556+
* Sets the policy on the {@link Bucket} instance.
2557+
*
2558+
* @param SetBucketPolicyRequest
2559+
* {@link SetBucketPolicyRequest} instance that has bucket
2560+
* information as well as policy information.
2561+
* @throws OSSException
2562+
* OSS Server side exception.
2563+
* @throws ClientException
2564+
* OSS Client side exception.
2565+
*/
2566+
public void setBucketPolicy(SetBucketPolicyRequest setBucketPolicyRequest) throws OSSException, ClientException;
2567+
2568+
/**
2569+
* Gets policy text of the {@link Bucket} instance.
2570+
*
2571+
* @param genericRequest
2572+
* {@link GenericRequest} instance that has the bucket name.
2573+
* @return The policy's content in {@link InputStream}.
2574+
* @throws OSSException
2575+
* OSS Server side exception.
2576+
* @throws ClientException
2577+
* OSS Client side exception.
2578+
*/
2579+
public GetBucketPolicyResult getBucketPolicy(GenericRequest genericRequest) throws OSSException, ClientException;
2580+
2581+
/**
2582+
* Gets policy text of the {@link Bucket} instance.
2583+
*
2584+
* @param bucketName
2585+
* Bucket name
2586+
* @return The policy's content in {@link InputStream}.
2587+
* @throws OSSException
2588+
* OSS Server side exception.
2589+
* @throws ClientException
2590+
* OSS Client side exception.
2591+
*/
2592+
public GetBucketPolicyResult getBucketPolicy(String bucketName) throws OSSException, ClientException;
2593+
2594+
/**
2595+
* Delete policy of the {@link Bucket} instance.
2596+
*
2597+
* @param genericRequest
2598+
* {@link GenericRequest} instance that has the bucket name.
2599+
* @throws OSSException
2600+
* OSS Server side exception.
2601+
* @throws ClientException
2602+
* OSS Client side exception.
2603+
*/
2604+
public void deleteBucketPolicy(GenericRequest genericRequest) throws OSSException, ClientException;
2605+
2606+
/**
2607+
* Delete policy of the {@link Bucket} instance.
2608+
*
2609+
* @param bucketName
2610+
* Bucket name
2611+
* @throws OSSException
2612+
* OSS Server side exception.
2613+
* @throws ClientException
2614+
* OSS Client side exception.
2615+
*/
2616+
public void deleteBucketPolicy(String bucketName) throws OSSException, ClientException;
2617+
25412618
/**
25422619
* File upload
25432620
*

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,36 @@ public void deleteBucketEncryption(GenericRequest genericRequest) throws OSSExce
12341234
this.bucketOperation.deleteBucketEncryption(genericRequest);
12351235
}
12361236

1237+
@Override
1238+
public void setBucketPolicy(String bucketName, String policyText) throws OSSException, ClientException{
1239+
this.bucketOperation.setBucketPolicy(new SetBucketPolicyRequest(bucketName, policyText));
1240+
}
1241+
1242+
@Override
1243+
public void setBucketPolicy(SetBucketPolicyRequest setBucketPolicyRequest) throws OSSException, ClientException{
1244+
this.bucketOperation.setBucketPolicy(setBucketPolicyRequest);
1245+
}
1246+
1247+
@Override
1248+
public GetBucketPolicyResult getBucketPolicy(GenericRequest genericRequest) throws OSSException, ClientException{
1249+
return bucketOperation.getBucketPolicy(genericRequest);
1250+
}
1251+
1252+
@Override
1253+
public GetBucketPolicyResult getBucketPolicy(String bucketName) throws OSSException, ClientException{
1254+
return bucketOperation.getBucketPolicy(new GenericRequest(bucketName));
1255+
}
1256+
1257+
@Override
1258+
public void deleteBucketPolicy(GenericRequest genericRequest) throws OSSException, ClientException{
1259+
bucketOperation.deleteBucketPolicy(genericRequest);
1260+
}
1261+
1262+
@Override
1263+
public void deleteBucketPolicy(String bucketName) throws OSSException, ClientException{
1264+
bucketOperation.deleteBucketPolicy(new GenericRequest(bucketName));
1265+
}
1266+
12371267
@Override
12381268
public UploadFileResult uploadFile(UploadFileRequest uploadFileRequest) throws Throwable {
12391269
return this.uploadOperation.uploadFile(uploadFileRequest);

src/main/java/com/aliyun/oss/OSSErrorCode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,14 @@ public interface OSSErrorCode {
256256
* The archive file is not restored before usage.
257257
*/
258258
static final String INVALID_OBJECT_STATE = "InvalidObjectState";
259+
260+
/**
261+
* The policy text is illegal.
262+
*/
263+
static final String INVALID_POLICY_DOCUMENT = "InvalidPolicyDocument";
264+
265+
/**
266+
* The exsiting bucket without policy
267+
*/
268+
static final String NO_SUCH_BUCKET_POLICY = "NoSuchBucketPolicy";
259269
}

src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public final class RequestMarshallers {
7777
public static final ProcessObjectRequestMarshaller processObjectRequestMarshaller = new ProcessObjectRequestMarshaller();
7878
public static final SetBucketVersioningRequestMarshaller setBucketVersioningRequestMarshaller = new SetBucketVersioningRequestMarshaller();
7979
public static final SetBucketEncryptionRequestMarshaller setBucketEncryptionRequestMarshaller = new SetBucketEncryptionRequestMarshaller();
80-
80+
public static final SetBucketPolicyRequestMarshaller setBucketPolicyRequestMarshaller = new SetBucketPolicyRequestMarshaller();
81+
8182
public static final CreateSelectObjectMetadataRequestMarshaller createSelectObjectMetadataRequestMarshaller = new CreateSelectObjectMetadataRequestMarshaller();
8283
public static final SelectObjectRequestMarshaller selectObjectRequestMarshaller = new SelectObjectRequestMarshaller();
8384

@@ -896,7 +897,24 @@ public byte[] marshall(SetBucketEncryptionRequest setBucketEncryptionRequest) {
896897
}
897898

898899
}
899-
900+
901+
public static final class SetBucketPolicyRequestMarshaller
902+
implements RequestMarshaller2<SetBucketPolicyRequest> {
903+
904+
@Override
905+
public byte[] marshall(SetBucketPolicyRequest setBucketPolicyRequest) {
906+
907+
byte[] rawData = null;
908+
try {
909+
rawData = setBucketPolicyRequest.getPolicyText().getBytes(DEFAULT_CHARSET_NAME);
910+
} catch (UnsupportedEncodingException e) {
911+
throw new ClientException("Unsupported encoding " + e.getMessage(), e);
912+
}
913+
return rawData;
914+
}
915+
916+
}
917+
900918
public static final class CreateLiveChannelRequestMarshaller
901919
implements RequestMarshaller2<CreateLiveChannelRequest> {
902920

src/main/java/com/aliyun/oss/internal/OSSBucketOperation.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static com.aliyun.oss.common.parser.RequestMarshallers.bucketImageProcessConfMarshaller;
3636
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketVersioningRequestMarshaller;
3737
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketEncryptionRequestMarshaller;
38+
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketPolicyRequestMarshaller;
3839
import static com.aliyun.oss.common.utils.CodingUtils.assertParameterNotNull;
3940
import static com.aliyun.oss.internal.OSSUtils.OSS_RESOURCE_MANAGER;
4041
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
@@ -63,6 +64,7 @@
6364
import static com.aliyun.oss.internal.ResponseParsers.listImageStyleResponseParser;
6465
import static com.aliyun.oss.internal.ResponseParsers.getBucketImageProcessConfResponseParser;
6566
import static com.aliyun.oss.internal.ResponseParsers.getBucketEncryptionResponseParser;
67+
import static com.aliyun.oss.internal.ResponseParsers.getBucketPolicyResponseParser;
6668

6769
import java.io.ByteArrayInputStream;
6870
import java.util.ArrayList;
@@ -127,6 +129,8 @@
127129
import com.aliyun.oss.model.SetBucketTaggingRequest;
128130
import com.aliyun.oss.model.SetBucketVersioningRequest;
129131
import com.aliyun.oss.model.SetBucketWebsiteRequest;
132+
import com.aliyun.oss.model.SetBucketPolicyRequest;
133+
import com.aliyun.oss.model.GetBucketPolicyResult;
130134
import com.aliyun.oss.model.TagSet;
131135
import com.aliyun.oss.model.Style;
132136
import com.aliyun.oss.model.UserQos;
@@ -570,7 +574,6 @@ public List<Style> listImageStyle(String bucketName, GenericRequest genericReque
570574
ensureBucketNameValid(bucketName);
571575

572576
Map<String, String> params = new HashMap<String, String>();
573-
;
574577
params.put(SUBRESOURCE_STYLE, null);
575578

576579
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
@@ -1263,6 +1266,60 @@ public void deleteBucketEncryption(GenericRequest genericRequest) throws OSSExce
12631266
doOperation(request, emptyResponseParser, bucketName, null);
12641267
}
12651268

1269+
public void setBucketPolicy(SetBucketPolicyRequest setBucketPolicyRequest) throws OSSException, ClientException {
1270+
1271+
assertParameterNotNull(setBucketPolicyRequest, "setBucketPolicyRequest");
1272+
1273+
String bucketName = setBucketPolicyRequest.getBucketName();
1274+
assertParameterNotNull(bucketName, "bucketName");
1275+
ensureBucketNameValid(bucketName);
1276+
Map<String, String> params = new HashMap<String, String>();
1277+
params.put(SUBRESOURCE_POLICY, null);
1278+
1279+
byte[] rawContent = setBucketPolicyRequestMarshaller.marshall(setBucketPolicyRequest);
1280+
Map<String, String> headers = new HashMap<String, String>();
1281+
addRequestRequiredHeaders(headers, rawContent);
1282+
1283+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
1284+
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params)
1285+
.setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent))
1286+
.setOriginalRequest(setBucketPolicyRequest).build();
1287+
1288+
doOperation(request, emptyResponseParser, bucketName, null);
1289+
}
1290+
1291+
public GetBucketPolicyResult getBucketPolicy(GenericRequest genericRequest) throws OSSException, ClientException {
1292+
assertParameterNotNull(genericRequest, "genericRequest");
1293+
1294+
String bucketName = genericRequest.getBucketName();
1295+
assertParameterNotNull(bucketName, "bucketName");
1296+
ensureBucketNameValid(bucketName);
1297+
Map<String, String> params = new HashMap<String, String>();
1298+
params.put(SUBRESOURCE_POLICY, null);
1299+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
1300+
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
1301+
.setOriginalRequest(genericRequest).build();
1302+
return doOperation(request, getBucketPolicyResponseParser, bucketName, null, true);
1303+
}
1304+
1305+
public void deleteBucketPolicy(GenericRequest genericRequest) throws OSSException, ClientException {
1306+
1307+
assertParameterNotNull(genericRequest, "genericRequest");
1308+
1309+
String bucketName = genericRequest.getBucketName();
1310+
assertParameterNotNull(bucketName, "bucketName");
1311+
ensureBucketNameValid(bucketName);
1312+
1313+
Map<String, String> params = new HashMap<String, String>();
1314+
params.put(SUBRESOURCE_POLICY, null);
1315+
1316+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
1317+
.setMethod(HttpMethod.DELETE).setBucket(bucketName).setParameters(params)
1318+
.setOriginalRequest(genericRequest).build();
1319+
1320+
doOperation(request, emptyResponseParser, bucketName, null);
1321+
}
1322+
12661323
private static void populateListObjectsRequestParameters(ListObjectsRequest listObjectsRequest,
12671324
Map<String, String> params) {
12681325

src/main/java/com/aliyun/oss/internal/RequestParameters.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public final class RequestParameters {
6161
public static final String SUBRESOURCE_VRESIONS = "versions";
6262
public static final String SUBRESOURCE_VRESIONING = "versioning";
6363
public static final String SUBRESOURCE_VRESION_ID = "versionId";
64+
public static final String SUBRESOURCE_POLICY = "policy";
6465

6566
public static final String SUBRESOURCE_UDF = "udf";
6667
public static final String SUBRESOURCE_UDF_NAME = "udfName";

src/main/java/com/aliyun/oss/internal/ResponseParsers.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import static com.aliyun.oss.internal.OSSUtils.trimQuotes;
2525

2626
import java.io.InputStream;
27+
import java.io.BufferedReader;
28+
import java.io.InputStreamReader;
2729
import java.math.BigInteger;
2830
import java.text.ParseException;
2931
import java.util.ArrayList;
@@ -120,6 +122,7 @@
120122
import com.aliyun.oss.model.UploadPartCopyResult;
121123
import com.aliyun.oss.model.UserQos;
122124
import com.aliyun.oss.model.VersionListing;
125+
import com.aliyun.oss.model.GetBucketPolicyResult;
123126

124127
/*
125128
* A collection of parsers that parse HTTP reponses into corresponding human-readable results.
@@ -149,6 +152,7 @@ public final class ResponseParsers {
149152
public static final GetBucketQosResponseParser getBucketQosResponseParser = new GetBucketQosResponseParser();
150153
public static final GetBucketVersioningResponseParser getBucketVersioningResponseParser = new GetBucketVersioningResponseParser();
151154
public static final GetBucketEncryptionResponseParser getBucketEncryptionResponseParser = new GetBucketEncryptionResponseParser();
155+
public static final GetBucketPolicyResponseParser getBucketPolicyResponseParser = new GetBucketPolicyResponseParser();
152156

153157
public static final ListObjectsReponseParser listObjectsReponseParser = new ListObjectsReponseParser();
154158
public static final ListVersionsReponseParser listVersionsReponseParser = new ListVersionsReponseParser();
@@ -446,6 +450,21 @@ public ServerSideEncryptionConfiguration parse(ResponseMessage response) throws
446450

447451
}
448452

453+
public static final class GetBucketPolicyResponseParser implements ResponseParser<GetBucketPolicyResult> {
454+
455+
@Override
456+
public GetBucketPolicyResult parse(ResponseMessage response) throws ResponseParseException {
457+
try {
458+
GetBucketPolicyResult result = parseGetBucketPolicy(response.getContent());
459+
result.setRequestId(response.getRequestId());
460+
return result;
461+
}finally {
462+
safeCloseResponse(response);
463+
}
464+
}
465+
466+
}
467+
449468
public static final class CreateLiveChannelResponseParser implements ResponseParser<CreateLiveChannelResult> {
450469

451470
@Override
@@ -2798,6 +2817,29 @@ public static ServerSideEncryptionConfiguration parseGetBucketEncryption(InputSt
27982817

27992818
}
28002819

2820+
/**
2821+
* Unmarshall get bucket policy response body .
2822+
*/
2823+
public static GetBucketPolicyResult parseGetBucketPolicy(InputStream responseBody) throws ResponseParseException {
2824+
2825+
try {
2826+
GetBucketPolicyResult result = new GetBucketPolicyResult();
2827+
2828+
BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody));
2829+
StringBuilder sb = new StringBuilder();
2830+
2831+
String s ;
2832+
while (( s = reader.readLine()) != null)
2833+
sb.append(s);
2834+
2835+
result.setPolicy(sb.toString());
2836+
2837+
return result;
2838+
} catch (Exception e) {
2839+
throw new ResponseParseException(e.getMessage(), e);
2840+
}
2841+
}
2842+
28012843
/**
28022844
* Unmarshall get bucket lifecycle response body to lifecycle rules.
28032845
*/

src/main/java/com/aliyun/oss/internal/SignParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SignParameters {
3737
SUBRESOURCE_PROCESS_CONF, SUBRESOURCE_SYMLINK, SUBRESOURCE_STAT, SUBRESOURCE_UDF, SUBRESOURCE_UDF_NAME,
3838
SUBRESOURCE_UDF_IMAGE, SUBRESOURCE_UDF_IMAGE_DESC, SUBRESOURCE_UDF_APPLICATION, SUBRESOURCE_UDF_LOG,
3939
SUBRESOURCE_RESTORE, SUBRESOURCE_VRESIONS, SUBRESOURCE_VRESIONING, SUBRESOURCE_VRESION_ID,
40-
SUBRESOURCE_ENCRYPTION});
40+
SUBRESOURCE_ENCRYPTION, SUBRESOURCE_POLICY});
4141

4242

4343
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.model;
21+
22+
public class GetBucketPolicyResult extends GenericResult {
23+
24+
public String getPolicyText(){
25+
return policyText;
26+
}
27+
28+
public void setPolicy(String policyText){
29+
this.policyText = policyText;
30+
}
31+
32+
private String policyText;
33+
}

0 commit comments

Comments
 (0)