|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.ozone.s3.endpoint; |
| 19 | + |
| 20 | +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.newError; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import javax.ws.rs.core.Response; |
| 25 | +import org.apache.hadoop.ozone.audit.S3GAction; |
| 26 | +import org.apache.hadoop.ozone.client.OzoneBucket; |
| 27 | +import org.apache.hadoop.ozone.om.exceptions.OMException; |
| 28 | +import org.apache.hadoop.ozone.s3.exception.OS3Exception; |
| 29 | +import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; |
| 30 | +import org.apache.hadoop.ozone.s3.util.S3Consts.QueryParams; |
| 31 | +import org.apache.hadoop.util.Time; |
| 32 | +import org.apache.http.HttpStatus; |
| 33 | + |
| 34 | +/** |
| 35 | + * Handler for default bucket CRUD operations. |
| 36 | + * Implements PUT (create bucket) and DELETE operations when no |
| 37 | + * subresource query parameters are present. |
| 38 | + * |
| 39 | + * This handler processes bucket-level requests that do not target |
| 40 | + * specific subresources (such as {@code ?acl}, {@code ?uploads}, |
| 41 | + * or {@code ?delete}), which are handled by dedicated handlers. |
| 42 | + * |
| 43 | + * This handler extends EndpointBase to inherit all required functionality |
| 44 | + * (configuration, headers, request context, audit logging, metrics, etc.). |
| 45 | + */ |
| 46 | +public class BucketCrudHandler extends EndpointBase implements BucketOperationHandler { |
| 47 | + |
| 48 | + /** |
| 49 | + * Handle only plain PUT bucket (create bucket), not subresources. |
| 50 | + */ |
| 51 | + private boolean shouldHandle() { |
| 52 | + return queryParams().get(QueryParams.ACL) == null |
| 53 | + && queryParams().get(QueryParams.UPLOADS) == null |
| 54 | + && queryParams().get(QueryParams.DELETE) == null; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Handle PUT /{bucket} for bucket creation. |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public Response handlePutRequest(String bucketName, InputStream body) |
| 62 | + throws IOException, OS3Exception { |
| 63 | + |
| 64 | + if (!shouldHandle()) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + long startNanos = Time.monotonicNowNanos(); |
| 69 | + S3GAction s3GAction = S3GAction.CREATE_BUCKET; |
| 70 | + |
| 71 | + try { |
| 72 | + String location = createS3Bucket(bucketName); |
| 73 | + auditWriteSuccess(s3GAction); |
| 74 | + getMetrics().updateCreateBucketSuccessStats(startNanos); |
| 75 | + return Response.status(HttpStatus.SC_OK).header("Location", location) |
| 76 | + .build(); |
| 77 | + } catch (OMException exception) { |
| 78 | + auditWriteFailure(s3GAction, exception); |
| 79 | + getMetrics().updateCreateBucketFailureStats(startNanos); |
| 80 | + if (exception.getResult() == OMException.ResultCodes.INVALID_BUCKET_NAME) { |
| 81 | + throw newError(S3ErrorTable.INVALID_BUCKET_NAME, bucketName, exception); |
| 82 | + } |
| 83 | + throw exception; |
| 84 | + } catch (Exception ex) { |
| 85 | + auditWriteFailure(s3GAction, ex); |
| 86 | + throw ex; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Handle DELETE /{bucket} for bucket deletion. |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public Response handleDeleteRequest(String bucketName) |
| 95 | + throws IOException, OS3Exception { |
| 96 | + |
| 97 | + if (!shouldHandle()) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + long startNanos = Time.monotonicNowNanos(); |
| 102 | + S3GAction s3GAction = S3GAction.DELETE_BUCKET; |
| 103 | + |
| 104 | + try { |
| 105 | + if (S3Owner.hasBucketOwnershipVerificationConditions(getHeaders())) { |
| 106 | + OzoneBucket bucket = getBucket(bucketName); |
| 107 | + S3Owner.verifyBucketOwnerCondition(getHeaders(), bucketName, bucket.getOwner()); |
| 108 | + } |
| 109 | + deleteS3Bucket(bucketName); |
| 110 | + } catch (OMException ex) { |
| 111 | + auditWriteFailure(s3GAction, ex); |
| 112 | + getMetrics().updateDeleteBucketFailureStats(startNanos); |
| 113 | + if (ex.getResult() == OMException.ResultCodes.BUCKET_NOT_EMPTY) { |
| 114 | + throw newError(S3ErrorTable.BUCKET_NOT_EMPTY, bucketName, ex); |
| 115 | + } else if (ex.getResult() == OMException.ResultCodes.BUCKET_NOT_FOUND) { |
| 116 | + throw newError(S3ErrorTable.NO_SUCH_BUCKET, bucketName, ex); |
| 117 | + } else if (isAccessDenied(ex)) { |
| 118 | + throw newError(S3ErrorTable.ACCESS_DENIED, bucketName, ex); |
| 119 | + } else { |
| 120 | + throw ex; |
| 121 | + } |
| 122 | + } catch (Exception ex) { |
| 123 | + auditWriteFailure(s3GAction, ex); |
| 124 | + throw ex; |
| 125 | + } |
| 126 | + |
| 127 | + auditWriteSuccess(s3GAction); |
| 128 | + getMetrics().updateDeleteBucketSuccessStats(startNanos); |
| 129 | + return Response |
| 130 | + .status(HttpStatus.SC_NO_CONTENT) |
| 131 | + .build(); |
| 132 | + } |
| 133 | +} |
0 commit comments