http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java new file mode 100644 index 0000000..d32bc9f --- /dev/null +++ b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java @@ -0,0 +1,222 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.HEAD; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; +import javax.ws.rs.core.Response.Status; +import javax.ws.rs.core.StreamingOutput; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdds.client.ReplicationFactor; +import org.apache.hadoop.hdds.client.ReplicationType; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.scm.ScmConfigKeys; +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneKeyDetails; +import org.apache.hadoop.ozone.client.io.OzoneInputStream; +import org.apache.hadoop.ozone.client.io.OzoneOutputStream; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; +import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Key level rest endpoints. + */ +@Path("/{bucket}/{path:.+}") +public class ObjectEndpoint extends EndpointBase { + + private static final Logger LOG = + LoggerFactory.getLogger(ObjectEndpoint.class); + + private List<String> customizableGetHeaders = new ArrayList<>(); + + public ObjectEndpoint() { + customizableGetHeaders.add("Content-Type"); + customizableGetHeaders.add("Content-Language"); + customizableGetHeaders.add("Expires"); + customizableGetHeaders.add("Cache-Control"); + customizableGetHeaders.add("Content-Disposition"); + customizableGetHeaders.add("Content-Encoding"); + } + + /** + * Rest endpoint to upload object to a bucket. + * <p> + * See: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html for + * more details. + */ + @PUT + public Response put( + @Context HttpHeaders headers, + @PathParam("bucket") String bucketName, + @PathParam("path") String keyPath, + @DefaultValue("STAND_ALONE") @QueryParam("replicationType") + ReplicationType replicationType, + @DefaultValue("ONE") @QueryParam("replicationFactor") + ReplicationFactor replicationFactor, + @DefaultValue("32 * 1024 * 1024") @QueryParam("chunkSize") + String chunkSize, + @HeaderParam("Content-Length") long length, + InputStream body) throws IOException, OS3Exception { + + try { + Configuration config = new OzoneConfiguration(); + config.set(ScmConfigKeys.OZONE_SCM_CHUNK_SIZE_KEY, chunkSize); + + OzoneBucket bucket = getBucket(bucketName); + OzoneOutputStream output = bucket + .createKey(keyPath, length, replicationType, replicationFactor); + + IOUtils.copy(body, output); + output.close(); + + return Response.ok().status(HttpStatus.SC_OK) + .build(); + } catch (IOException ex) { + LOG.error("Exception occurred in PutObject", ex); + throw ex; + } + } + + /** + * Rest endpoint to download object from a bucket. + * <p> + * See: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html for + * more details. + */ + @GET + public Response get( + @Context HttpHeaders headers, + @PathParam("bucket") String bucketName, + @PathParam("path") String keyPath, + InputStream body) throws IOException, OS3Exception { + + try { + OzoneBucket bucket = getBucket(bucketName); + + OzoneInputStream key = bucket + .readKey(keyPath); + + StreamingOutput output = dest -> IOUtils.copy(key, dest); + ResponseBuilder responseBuilder = Response.ok(output); + + for (String responseHeader : customizableGetHeaders) { + String headerValue = headers.getHeaderString(responseHeader); + if (headerValue != null) { + responseBuilder.header(responseHeader, headerValue); + } + } + + return responseBuilder.build(); + } catch (IOException ex) { + if (ex.getMessage().contains("NOT_FOUND")) { + OS3Exception os3Exception = S3ErrorTable.newError(S3ErrorTable + .NO_SUCH_OBJECT, S3ErrorTable.Resource.OBJECT); + throw os3Exception; + } else { + throw ex; + } + } + } + + /** + * Rest endpoint to check existence of an object in a bucket. + * <p> + * See: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectHEAD.html + * for more details. + */ + @HEAD + public Response head( + @PathParam("bucket") String bucketName, + @PathParam("path") String keyPath) throws Exception { + OzoneKeyDetails key; + + try { + key = getBucket(bucketName).getKey(keyPath); + // TODO: return the specified range bytes of this object. + } catch (IOException ex) { + LOG.error("Exception occurred in HeadObject", ex); + if (ex.getMessage().contains("KEY_NOT_FOUND")) { + OS3Exception os3Exception = S3ErrorTable.newError(S3ErrorTable + .NO_SUCH_OBJECT, S3ErrorTable.Resource.OBJECT); + throw os3Exception; + } else { + throw ex; + } + } + + return Response.ok().status(HttpStatus.SC_OK) + .header("Last-Modified", key.getModificationTime()) + .header("ETag", "" + key.getModificationTime()) + .header("Content-Length", key.getDataSize()) + .header("Content-Type", "binary/octet-stream") + .build(); + } + + /** + * Delete a specific object from a bucket. + * <p> + * See: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html + * for more details. + */ + @DELETE + public Response delete( + @PathParam("bucket") String bucketName, + @PathParam("path") String keyPath) throws IOException, OS3Exception { + + try { + OzoneBucket bucket = getBucket(bucketName); + bucket.getKey(keyPath); + bucket.deleteKey(keyPath); + } catch (IOException ex) { + if (ex.getMessage().contains("BUCKET_NOT_FOUND")) { + throw S3ErrorTable.newError(S3ErrorTable + .NO_SUCH_BUCKET, S3ErrorTable.Resource.BUCKET); + } else if (!ex.getMessage().contains("NOT_FOUND")) { + throw ex; + } + //NOT_FOUND is not a problem, AWS doesn't throw exception for missing + // keys. Just return 204. + } + return Response + .status(Status.NO_CONTENT) + .build(); + + } + +}
http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/RootEndpoint.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/RootEndpoint.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/RootEndpoint.java new file mode 100644 index 0000000..4de26f3 --- /dev/null +++ b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/RootEndpoint.java @@ -0,0 +1,82 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.GET; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.Path; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import java.io.IOException; +import java.time.Instant; +import java.util.Iterator; + +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneVolume; +import org.apache.hadoop.ozone.s3.commontypes.BucketMetadata; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Top level rest endpoint. + */ +@Path("/") +public class RootEndpoint extends EndpointBase { + + private static final Logger LOG = + LoggerFactory.getLogger(RootEndpoint.class); + + /** + * Rest endpoint to list all the buckets of the current user. + * + * See https://docs.aws.amazon.com/AmazonS3/latest/API/RESTServiceGET.html + * for more details. + */ + @GET + public ListBucketResponse get(@Context HttpHeaders headers) + throws OS3Exception, IOException { + OzoneVolume volume; + ListBucketResponse response = new ListBucketResponse(); + + String volumeName = "s3" + parseUsername(headers).toLowerCase(); + try { + //TODO: we need a specific s3bucketlist endpoint instead + // of reimplement the naming convention here + volume = getVolume(volumeName); + } catch (NotFoundException ex) { + return response; + } catch (IOException e) { + throw e; + } + + Iterator<? extends OzoneBucket> volABucketIter = volume.listBuckets(null); + + while (volABucketIter.hasNext()) { + OzoneBucket next = volABucketIter.next(); + BucketMetadata bucketMetadata = new BucketMetadata(); + bucketMetadata.setName(next.getName()); + bucketMetadata.setCreationDate( + Instant.ofEpochMilli(next.getCreationTime())); + response.addBucket(bucketMetadata); + } + + return response; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java new file mode 100644 index 0000000..c55cdf4 --- /dev/null +++ b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Rest endpoint implementation for the s3 gateway. + */ [email protected]( + namespace = "http://s3.amazonaws" + + ".com/doc/2006-03-01/", elementFormDefault = + javax.xml.bind.annotation.XmlNsForm.QUALIFIED, + xmlns = { + @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://s3.amazonaws" + + ".com/doc/2006-03-01/", prefix = "")}) + +package org.apache.hadoop.ozone.s3.endpoint; http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/DeleteObject.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/DeleteObject.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/DeleteObject.java deleted file mode 100644 index d5ef70e..0000000 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/DeleteObject.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.ozone.s3.object; - -import javax.ws.rs.DELETE; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.IOException; - -import org.apache.hadoop.ozone.client.OzoneBucket; -import org.apache.hadoop.ozone.s3.EndpointBase; - -/** - * Delete Object rest endpoint. - */ -@Path("/{volume}/{bucket}/{path:.+}") -public class DeleteObject extends EndpointBase { - - @DELETE - @Produces(MediaType.APPLICATION_XML) - public Response delete( - @PathParam("volume") String volumeName, - @PathParam("bucket") String bucketName, - @PathParam("path") String keyPath) throws IOException { - - OzoneBucket bucket = getBucket(volumeName, bucketName); - bucket.deleteKey(keyPath); - return Response. - ok() - .build(); - - } -} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/HeadObject.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/HeadObject.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/HeadObject.java deleted file mode 100644 index 8bbdf76..0000000 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/HeadObject.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.ozone.s3.object; - -import javax.ws.rs.HEAD; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.IOException; - -import org.apache.hadoop.ozone.client.OzoneKeyDetails; -import org.apache.hadoop.ozone.s3.EndpointBase; -import org.apache.hadoop.ozone.s3.exception.OS3Exception; -import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; - -import org.apache.http.HttpStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Get object info rest endpoint. - */ -@Path("/{volume}/{bucket}/{path:.+}") -public class HeadObject extends EndpointBase { - private static final Logger LOG = - LoggerFactory.getLogger(HeadObject.class); - - @HEAD - @Produces(MediaType.APPLICATION_XML) - public Response head( - @PathParam("volume") String volumeName, - @PathParam("bucket") String bucketName, - @PathParam("path") String keyPath) throws Exception { - OzoneKeyDetails key; - - try { - key = getVolume(volumeName).getBucket(bucketName).getKey(keyPath); - // TODO: return the specified range bytes of this object. - } catch (IOException ex) { - LOG.error("Exception occurred in HeadObject", ex); - if (ex.getMessage().contains("KEY_NOT_FOUND")) { - OS3Exception os3Exception = S3ErrorTable.newError(S3ErrorTable - .NO_SUCH_OBJECT, S3ErrorTable.Resource.OBJECT); - throw os3Exception; - } else { - throw ex; - } - } - - return Response.ok().status(HttpStatus.SC_OK) - .header("Last-Modified", key.getModificationTime()) - .header("ETag", "" + key.getModificationTime()) - .header("Content-Length", key.getDataSize()) - .header("Content-Type", "binary/octet-stream") - .build(); - } -} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObject.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObject.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObject.java deleted file mode 100644 index a7bd7ad..0000000 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObject.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.ozone.s3.object; - -import javax.ws.rs.DefaultValue; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; -import java.io.IOException; -import java.time.Instant; -import java.util.Iterator; - -import org.apache.hadoop.ozone.client.OzoneBucket; -import org.apache.hadoop.ozone.client.OzoneKey; -import org.apache.hadoop.ozone.client.OzoneVolume; -import org.apache.hadoop.ozone.s3.EndpointBase; -import org.apache.hadoop.ozone.s3.commontypes.KeyMetadata; - -import org.apache.commons.lang3.StringUtils; -import org.apache.hadoop.ozone.s3.exception.OS3Exception; - -/** - * List Object Rest endpoint. - */ -@Path("/{volume}/{bucket}") -public class ListObject extends EndpointBase { - - - @GET - @Produces(MediaType.APPLICATION_XML) - public ListObjectResponse get( - @PathParam("volume") String volumeName, - @PathParam("bucket") String bucketName, - @QueryParam("delimiter") String delimiter, - @QueryParam("encoding-type") String encodingType, - @QueryParam("marker") String marker, - @DefaultValue("1000") @QueryParam("max-keys") int maxKeys, - @QueryParam("prefix") String prefix, - @Context HttpHeaders hh) throws OS3Exception, IOException { - - if (delimiter == null) { - delimiter = "/"; - } - if (prefix == null) { - prefix = ""; - } - - OzoneVolume volume = getVolume(volumeName); - OzoneBucket bucket = getBucket(volume, bucketName); - - Iterator<? extends OzoneKey> ozoneKeyIterator = bucket.listKeys(prefix); - - ListObjectResponse response = new ListObjectResponse(); - response.setDelimiter(delimiter); - response.setName(bucketName); - response.setPrefix(prefix); - response.setMarker(""); - response.setMaxKeys(1000); - response.setEncodingType("url"); - response.setTruncated(false); - - String prevDir = null; - while (ozoneKeyIterator.hasNext()) { - OzoneKey next = ozoneKeyIterator.next(); - String relativeKeyName = next.getName().substring(prefix.length()); - - int depth = - StringUtils.countMatches(relativeKeyName, delimiter); - - if (prefix.length() > 0 && !prefix.endsWith(delimiter) - && relativeKeyName.length() > 0) { - response.addPrefix(prefix + "/"); - break; - } - if (depth > 0) { - String dirName = relativeKeyName - .substring(0, relativeKeyName.indexOf(delimiter)); - if (!dirName.equals(prevDir)) { - response.addPrefix( - prefix + dirName + delimiter); - prevDir = dirName; - } - } else if (relativeKeyName.endsWith(delimiter)) { - response.addPrefix(relativeKeyName); - } else if (relativeKeyName.length() > 0) { - KeyMetadata keyMetadata = new KeyMetadata(); - keyMetadata.setKey(next.getName()); - keyMetadata.setSize(next.getDataSize()); - keyMetadata.setETag("" + next.getModificationTime()); - keyMetadata.setStorageClass("STANDARD"); - keyMetadata - .setLastModified(Instant.ofEpochMilli(next.getModificationTime())); - response.addKey(keyMetadata); - } - } - return response; - } - -} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObjectResponse.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObjectResponse.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObjectResponse.java deleted file mode 100644 index a32fb93..0000000 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/ListObjectResponse.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.ozone.s3.object; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import java.util.ArrayList; -import java.util.List; - -import org.apache.hadoop.ozone.s3.commontypes.CommonPrefix; -import org.apache.hadoop.ozone.s3.commontypes.KeyMetadata; - -/** - * Response from the ListObject RPC Call. - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlRootElement(name = "ListBucketResult", namespace = "http://s3.amazonaws" - + ".com/doc/2006-03-01/") -public class ListObjectResponse { - - @XmlElement(name = "Name") - private String name; - - @XmlElement(name = "Prefix") - private String prefix; - - @XmlElement(name = "Marker") - private String marker; - - @XmlElement(name = "MaxKeys") - private int maxKeys; - - @XmlElement(name = "Delimiter") - private String delimiter = "/"; - - @XmlElement(name = "EncodingType") - private String encodingType = "url"; - - @XmlElement(name = "IsTruncated") - private boolean isTruncated; - - @XmlElement(name = "Contents") - private List<KeyMetadata> contents = new ArrayList<>(); - - @XmlElement(name = "CommonPrefixes") - private List<CommonPrefix> commonPrefixes = new ArrayList<>(); - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getPrefix() { - return prefix; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - public String getMarker() { - return marker; - } - - public void setMarker(String marker) { - this.marker = marker; - } - - public int getMaxKeys() { - return maxKeys; - } - - public void setMaxKeys(int maxKeys) { - this.maxKeys = maxKeys; - } - - public String getDelimiter() { - return delimiter; - } - - public void setDelimiter(String delimiter) { - this.delimiter = delimiter; - } - - public String getEncodingType() { - return encodingType; - } - - public void setEncodingType(String encodingType) { - this.encodingType = encodingType; - } - - public boolean isTruncated() { - return isTruncated; - } - - public void setTruncated(boolean truncated) { - isTruncated = truncated; - } - - public List<KeyMetadata> getContents() { - return contents; - } - - public void setContents( - List<KeyMetadata> contents) { - this.contents = contents; - } - - public List<CommonPrefix> getCommonPrefixes() { - return commonPrefixes; - } - - public void setCommonPrefixes( - List<CommonPrefix> commonPrefixes) { - this.commonPrefixes = commonPrefixes; - } - - public void addKey(KeyMetadata keyMetadata) { - contents.add(keyMetadata); - } - - public void addPrefix(String relativeKeyName) { - commonPrefixes.add(new CommonPrefix(relativeKeyName)); - } -} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/PutObject.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/PutObject.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/PutObject.java deleted file mode 100644 index 84c25eb..0000000 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/PutObject.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.ozone.s3.object; - -import javax.ws.rs.DefaultValue; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.IOException; -import java.io.InputStream; - -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hdds.client.ReplicationFactor; -import org.apache.hadoop.hdds.client.ReplicationType; -import org.apache.hadoop.hdds.conf.OzoneConfiguration; -import org.apache.hadoop.hdds.scm.ScmConfigKeys; -import org.apache.hadoop.ozone.client.OzoneBucket; -import org.apache.hadoop.ozone.client.io.OzoneOutputStream; -import org.apache.hadoop.ozone.s3.EndpointBase; - -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * File upload. - */ -@Path("/{bucket}/{path:.+}") -public class PutObject extends EndpointBase { - - private static final Logger LOG = - LoggerFactory.getLogger(PutObject.class); - - @PUT - @Produces(MediaType.APPLICATION_XML) - public Response put( - @Context HttpHeaders headers, - @PathParam("bucket") String bucketName, - @PathParam("path") String keyPath, - @DefaultValue("STAND_ALONE" ) @QueryParam("replicationType") - ReplicationType replicationType, - @DefaultValue("ONE") @QueryParam("replicationFactor") - ReplicationFactor replicationFactor, - @DefaultValue("32 * 1024 * 1024") @QueryParam("chunkSize") - String chunkSize, - @HeaderParam("Content-Length") long length, - InputStream body) throws IOException { - - try { - Configuration config = new OzoneConfiguration(); - config.set(ScmConfigKeys.OZONE_SCM_CHUNK_SIZE_KEY, chunkSize); - - OzoneBucket bucket = getVolume(getOzoneVolumeName(bucketName)) - .getBucket(bucketName); - OzoneOutputStream output = bucket - .createKey(keyPath, length, replicationType, replicationFactor); - - IOUtils.copy(body, output); - output.close(); - - return Response.ok().status(HttpStatus.SC_OK) - .header("Content-Length", length) - .build(); - } catch (IOException ex) { - LOG.error("Exception occurred in PutObject", ex); - throw ex; - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/package-info.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/package-info.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/package-info.java deleted file mode 100644 index e255991..0000000 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/object/package-info.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Rest endpoint implementation for the Object specific methods. - */ [email protected]( - namespace = "http://s3.amazonaws" - + ".com/doc/2006-03-01/", elementFormDefault = - javax.xml.bind.annotation.XmlNsForm.QUALIFIED, - xmlns = { - @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://s3.amazonaws" - + ".com/doc/2006-03-01/", prefix = "")}) -package org.apache.hadoop.ozone.s3.object; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/client/OzoneVolumeStub.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/client/OzoneVolumeStub.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/client/OzoneVolumeStub.java index c0c5e11..dd8701f 100644 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/client/OzoneVolumeStub.java +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/client/OzoneVolumeStub.java @@ -82,7 +82,8 @@ public class OzoneVolumeStub extends OzoneVolume { return bucket.getName().startsWith(bucketPrefix); } else { return true; - }}) + } + }) .collect(Collectors.toList()) .iterator(); } http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestBucketResponse.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestBucketResponse.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestBucketResponse.java deleted file mode 100644 index efc69be..0000000 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestBucketResponse.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.hadoop.ozone.s3.bucket; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; - -import org.apache.hadoop.ozone.s3.object.ListObjectResponse; - -import org.junit.Test; - -/** - * Testing JAXB serialization. - */ -public class TestBucketResponse { - - @Test - public void serialize() throws JAXBException { - JAXBContext context = JAXBContext.newInstance(ListObjectResponse.class); - context.createMarshaller().marshal(new ListObjectResponse(), System.out); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestDeleteBucket.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestDeleteBucket.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestDeleteBucket.java deleted file mode 100644 index 513c33e..0000000 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestDeleteBucket.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.hadoop.ozone.s3.bucket; - -import org.apache.hadoop.ozone.client.ObjectStore; -import org.apache.hadoop.ozone.client.ObjectStoreStub; -import org.apache.hadoop.ozone.client.OzoneClientStub; -import org.apache.hadoop.ozone.s3.exception.OS3Exception; -import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; -import org.apache.http.HttpStatus; -import org.junit.Before; -import org.junit.Test; - -import javax.ws.rs.core.Response; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * This class tests delete bucket functionality. - */ -public class TestDeleteBucket { - private String bucketName = "myBucket"; - private OzoneClientStub clientStub; - private ObjectStore objectStoreStub; - private DeleteBucket deleteBucket; - - @Before - public void setup() throws Exception { - - //Create client stub and object store stub. - clientStub = new OzoneClientStub(); - objectStoreStub = clientStub.getObjectStore(); - - objectStoreStub.createS3Bucket("ozone", bucketName); - - // Create HeadBucket and setClient to OzoneClientStub - deleteBucket = new DeleteBucket(); - deleteBucket.setClient(clientStub); - - - } - - @Test - public void testDeleteBucket() throws Exception { - Response response = deleteBucket.delete(bucketName); - assertEquals(response.getStatus(), HttpStatus.SC_NO_CONTENT); - - } - - @Test - public void testDeleteWithNoSuchBucket() throws Exception { - try { - deleteBucket.delete("unknownbucket"); - } catch (OS3Exception ex) { - assertEquals(S3ErrorTable.NO_SUCH_BUCKET.getCode(), ex.getCode()); - assertEquals(S3ErrorTable.NO_SUCH_BUCKET.getErrorMessage(), - ex.getErrorMessage()); - return; - } - fail("testDeleteWithNoSuchBucket failed"); - } - - - @Test - public void testDeleteWithBucketNotEmpty() throws Exception { - try { - String bucket = "nonemptybucket"; - objectStoreStub.createS3Bucket("ozone1", bucket); - ObjectStoreStub stub = (ObjectStoreStub) objectStoreStub; - stub.setBucketEmptyStatus(bucket, false); - deleteBucket.delete(bucket); - } catch (OS3Exception ex) { - assertEquals(S3ErrorTable.BUCKET_NOT_EMPTY.getCode(), ex.getCode()); - assertEquals(S3ErrorTable.BUCKET_NOT_EMPTY.getErrorMessage(), - ex.getErrorMessage()); - return; - } - fail("testDeleteWithBucketNotEmpty failed"); - } -} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestGetBucket.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestGetBucket.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestGetBucket.java deleted file mode 100644 index 123dd79..0000000 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestGetBucket.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.hadoop.ozone.s3.bucket; - -import java.io.IOException; - -import org.apache.hadoop.ozone.client.OzoneBucket; -import org.apache.hadoop.ozone.client.OzoneClient; -import org.apache.hadoop.ozone.client.OzoneClientStub; -import org.apache.hadoop.ozone.s3.exception.OS3Exception; -import org.apache.hadoop.ozone.s3.object.ListObject; -import org.apache.hadoop.ozone.s3.object.ListObjectResponse; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Testing basic object list browsing. - */ -public class TestGetBucket { - - @Test - public void listRoot() throws OS3Exception, IOException { - - ListObject getBucket = new ListObject(); - - OzoneClient client = createClientWithKeys("file1", "dir1/file2"); - - getBucket.setClient(client); - - ListObjectResponse getBucketResponse = - getBucket.get("vol1", "b1", "/", null, null, 100, "", null); - - Assert.assertEquals(1, getBucketResponse.getCommonPrefixes().size()); - Assert.assertEquals("dir1/", - getBucketResponse.getCommonPrefixes().get(0).getPrefix()); - - Assert.assertEquals(1, getBucketResponse.getContents().size()); - Assert.assertEquals("file1", - getBucketResponse.getContents().get(0).getKey()); - - } - - @Test - public void listDir() throws OS3Exception, IOException { - - ListObject getBucket = new ListObject(); - - OzoneClient client = createClientWithKeys("dir1/file2", "dir1/dir2/file2"); - - getBucket.setClient(client); - - ListObjectResponse getBucketResponse = - getBucket.get("vol1", "b1", "/", null, null, 100, "dir1", null); - - Assert.assertEquals(1, getBucketResponse.getCommonPrefixes().size()); - Assert.assertEquals("dir1/", - getBucketResponse.getCommonPrefixes().get(0).getPrefix()); - - Assert.assertEquals(0, getBucketResponse.getContents().size()); - - } - - @Test - public void listSubDir() throws OS3Exception, IOException { - - ListObject getBucket = new ListObject(); - OzoneClient ozoneClient = - createClientWithKeys("dir1/file2", "dir1/dir2/file2"); - - getBucket.setClient(ozoneClient); - - ListObjectResponse getBucketResponse = - getBucket.get("vol1", "b1", "/", null, null, 100, "dir1/", null); - - Assert.assertEquals(1, getBucketResponse.getCommonPrefixes().size()); - Assert.assertEquals("dir1/dir2/", - getBucketResponse.getCommonPrefixes().get(0).getPrefix()); - - Assert.assertEquals(1, getBucketResponse.getContents().size()); - Assert.assertEquals("dir1/file2", - getBucketResponse.getContents().get(0).getKey()); - - } - - private OzoneClient createClientWithKeys(String... keys) throws IOException { - OzoneClient client = new OzoneClientStub(); - client.getObjectStore().createVolume("vol1"); - client.getObjectStore().getVolume("vol1").createBucket("b1"); - OzoneBucket bucket = - client.getObjectStore().getVolume("vol1").getBucket("b1"); - for (String key : keys) { - bucket.createKey(key, 0).close(); - } - return client; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestHeadBucket.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestHeadBucket.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestHeadBucket.java deleted file mode 100644 index c392ac0..0000000 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestHeadBucket.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.hadoop.ozone.s3.bucket; - -import org.apache.hadoop.ozone.client.ObjectStore; -import org.apache.hadoop.ozone.client.OzoneClientStub; -import org.apache.hadoop.ozone.s3.exception.OS3Exception; -import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; -import org.junit.Before; -import org.junit.Test; - -import javax.ws.rs.core.Response; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * This class test HeadBucket functionality. - */ -public class TestHeadBucket { - - private String bucketName = "myBucket"; - private String userName = "ozone"; - private OzoneClientStub clientStub; - private ObjectStore objectStoreStub; - private HeadBucket headBucket; - - @Before - public void setup() throws Exception { - - //Create client stub and object store stub. - clientStub = new OzoneClientStub(); - objectStoreStub = clientStub.getObjectStore(); - - objectStoreStub.createS3Bucket(userName, bucketName); - - // Create HeadBucket and setClient to OzoneClientStub - headBucket = new HeadBucket(); - headBucket.setClient(clientStub); - } - - @Test - public void testHeadBucket() throws Exception { - - Response response = headBucket.head(bucketName); - assertEquals(200, response.getStatus()); - - } - - @Test - public void testHeadFail() { - try { - headBucket.head("unknownbucket"); - } catch (Exception ex) { - if (ex instanceof OS3Exception) { - assertEquals(S3ErrorTable.NO_SUCH_BUCKET.getCode(), - ((OS3Exception) ex).getCode()); - assertEquals(S3ErrorTable.NO_SUCH_BUCKET.getErrorMessage(), ( - (OS3Exception) ex).getErrorMessage()); - } else { - fail("testHeadFail failed"); - } - return; - } - fail("testHeadFail failed"); - } -} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestListBucket.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestListBucket.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestListBucket.java deleted file mode 100644 index d0db815..0000000 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/TestListBucket.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.hadoop.ozone.s3.bucket; - -import org.apache.commons.lang3.RandomStringUtils; -import org.apache.hadoop.ozone.client.ObjectStore; -import org.apache.hadoop.ozone.client.OzoneClientStub; -import org.apache.hadoop.ozone.client.OzoneVolume; -import org.apache.hadoop.ozone.s3.exception.OS3Exception; -import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; -import org.junit.Before; -import org.junit.Test; - -import javax.ws.rs.core.Response; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * This class test HeadBucket functionality. - */ -public class TestListBucket { - - private String volumeName = "vol1"; - private OzoneClientStub clientStub; - private ObjectStore objectStoreStub; - OzoneVolume volumeStub; - private ListBucket listBucket; - - @Before - public void setup() throws Exception { - - //Create client stub and object store stub. - clientStub = new OzoneClientStub(); - objectStoreStub = clientStub.getObjectStore(); - - // Create volume and bucket - objectStoreStub.createVolume(volumeName); - - volumeStub = objectStoreStub.getVolume(volumeName); - //volumeStub.createBucket(bucketName); - - // Create HeadBucket and setClient to OzoneClientStub - listBucket = new ListBucket(); - listBucket.setClient(clientStub); - } - - @Test - public void testListBucket() throws Exception { - // List operation should success even there is no bucket. - ListBucketResponse response = listBucket.get(volumeName); - assertEquals(0, response.getBucketsNum()); - - String bucketBaseName = "bucket-"; - for(int i = 0; i < 10; i++) { - volumeStub.createBucket( - bucketBaseName + RandomStringUtils.randomNumeric(3)); - } - response = listBucket.get(volumeName); - assertEquals(10, response.getBucketsNum()); - } - - @Test - public void testListBucketFail() { - try { - listBucket.get("badVolumeName"); - } catch (Exception ex) { - if (ex instanceof OS3Exception) { - assertEquals(S3ErrorTable.NO_SUCH_VOLUME.getCode(), - ((OS3Exception) ex).getCode()); - assertEquals(S3ErrorTable.NO_SUCH_VOLUME.getErrorMessage(), ( - (OS3Exception) ex).getErrorMessage()); - } else { - fail("testHeadFail failed"); - } - return; - } - } -} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/package-info.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/package-info.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/package-info.java deleted file mode 100644 index de09dae..0000000 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/bucket/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * Unit tests for the bucket related rest endpoints. - */ -package org.apache.hadoop.ozone.s3.bucket; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketDelete.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketDelete.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketDelete.java new file mode 100644 index 0000000..5114a47 --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketDelete.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.core.Response; + +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.ObjectStoreStub; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; +import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; + +import org.apache.http.HttpStatus; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import org.junit.Before; +import org.junit.Test; + +/** + * This class tests delete bucket functionality. + */ +public class TestBucketDelete { + + private String bucketName = "myBucket"; + private OzoneClientStub clientStub; + private ObjectStore objectStoreStub; + private BucketEndpoint bucketEndpoint; + + @Before + public void setup() throws Exception { + + //Create client stub and object store stub. + clientStub = new OzoneClientStub(); + objectStoreStub = clientStub.getObjectStore(); + + objectStoreStub.createS3Bucket("ozone", bucketName); + + // Create HeadBucket and setClient to OzoneClientStub + bucketEndpoint = new BucketEndpoint(); + bucketEndpoint.setClient(clientStub); + + + } + + @Test + public void testBucketEndpoint() throws Exception { + Response response = bucketEndpoint.delete(bucketName); + assertEquals(response.getStatus(), HttpStatus.SC_NO_CONTENT); + + } + + @Test + public void testDeleteWithNoSuchBucket() throws Exception { + try { + bucketEndpoint.delete("unknownbucket"); + } catch (OS3Exception ex) { + assertEquals(S3ErrorTable.NO_SUCH_BUCKET.getCode(), ex.getCode()); + assertEquals(S3ErrorTable.NO_SUCH_BUCKET.getErrorMessage(), + ex.getErrorMessage()); + return; + } + fail("testDeleteWithNoSuchBucket failed"); + } + + + @Test + public void testDeleteWithBucketNotEmpty() throws Exception { + try { + String bucket = "nonemptybucket"; + objectStoreStub.createS3Bucket("ozone1", bucket); + ObjectStoreStub stub = (ObjectStoreStub) objectStoreStub; + stub.setBucketEmptyStatus(bucket, false); + bucketEndpoint.delete(bucket); + } catch (OS3Exception ex) { + assertEquals(S3ErrorTable.BUCKET_NOT_EMPTY.getCode(), ex.getCode()); + assertEquals(S3ErrorTable.BUCKET_NOT_EMPTY.getErrorMessage(), + ex.getErrorMessage()); + return; + } + fail("testDeleteWithBucketNotEmpty failed"); + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketGet.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketGet.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketGet.java new file mode 100644 index 0000000..41778b2 --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketGet.java @@ -0,0 +1,115 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import java.io.IOException; + +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Testing basic object list browsing. + */ +public class TestBucketGet { + + @Test + public void listRoot() throws OS3Exception, IOException { + + BucketEndpoint getBucket = new BucketEndpoint(); + + OzoneClient client = createClientWithKeys("file1", "dir1/file2"); + + getBucket.setClient(client); + + ListObjectResponse getBucketResponse = + getBucket.list("b1", "/", null, null, 100, "", null); + + Assert.assertEquals(1, getBucketResponse.getCommonPrefixes().size()); + Assert.assertEquals("dir1/", + getBucketResponse.getCommonPrefixes().get(0).getPrefix()); + + Assert.assertEquals(1, getBucketResponse.getContents().size()); + Assert.assertEquals("file1", + getBucketResponse.getContents().get(0).getKey()); + + } + + @Test + public void listDir() throws OS3Exception, IOException { + + BucketEndpoint getBucket = new BucketEndpoint(); + + OzoneClient client = createClientWithKeys("dir1/file2", "dir1/dir2/file2"); + + getBucket.setClient(client); + + ListObjectResponse getBucketResponse = + getBucket.list("b1", "/", null, null, 100, "dir1", null); + + Assert.assertEquals(1, getBucketResponse.getCommonPrefixes().size()); + Assert.assertEquals("dir1/", + getBucketResponse.getCommonPrefixes().get(0).getPrefix()); + + Assert.assertEquals(0, getBucketResponse.getContents().size()); + + } + + @Test + public void listSubDir() throws OS3Exception, IOException { + + BucketEndpoint getBucket = new BucketEndpoint(); + + OzoneClient ozoneClient = + createClientWithKeys("dir1/file2", "dir1/dir2/file2"); + + getBucket.setClient(ozoneClient); + + ListObjectResponse getBucketResponse = + getBucket.list("b1", "/", null, null, 100, "dir1/", null); + + Assert.assertEquals(1, getBucketResponse.getCommonPrefixes().size()); + Assert.assertEquals("dir1/dir2/", + getBucketResponse.getCommonPrefixes().get(0).getPrefix()); + + Assert.assertEquals(1, getBucketResponse.getContents().size()); + Assert.assertEquals("dir1/file2", + getBucketResponse.getContents().get(0).getKey()); + + } + + private OzoneClient createClientWithKeys(String... keys) throws IOException { + OzoneClient client = new OzoneClientStub(); + + client.getObjectStore().createS3Bucket("bilbo", "b1"); + String volume = client.getObjectStore().getOzoneVolumeName("b1"); + client.getObjectStore().getVolume(volume).createBucket("b1"); + OzoneBucket bucket = + client.getObjectStore().getVolume(volume).getBucket("b1"); + for (String key : keys) { + bucket.createKey(key, 0).close(); + } + return client; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketHead.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketHead.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketHead.java new file mode 100644 index 0000000..f06da70 --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketHead.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.core.Response; + +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.OzoneClientStub; + +import org.junit.Assert; +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Test; + +/** + * This class test HeadBucket functionality. + */ +public class TestBucketHead { + + private String bucketName = "myBucket"; + private String userName = "ozone"; + private OzoneClientStub clientStub; + private ObjectStore objectStoreStub; + private BucketEndpoint bucketEndpoint; + + @Before + public void setup() throws Exception { + + //Create client stub and object store stub. + clientStub = new OzoneClientStub(); + objectStoreStub = clientStub.getObjectStore(); + + objectStoreStub.createS3Bucket(userName, bucketName); + + // Create HeadBucket and setClient to OzoneClientStub + bucketEndpoint = new BucketEndpoint(); + bucketEndpoint.setClient(clientStub); + } + + @Test + public void testHeadBucket() throws Exception { + + Response response = bucketEndpoint.head(bucketName); + assertEquals(200, response.getStatus()); + + } + + @Test + public void testHeadFail() throws Exception { + Response response = bucketEndpoint.head("unknownbucket"); + Assert.assertEquals(400, response.getStatus()); + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketResponse.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketResponse.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketResponse.java new file mode 100644 index 0000000..7c5bfad --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketResponse.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; + +import org.junit.Test; + +/** + * Testing JAXB serialization. + */ +public class TestBucketResponse { + + @Test + public void serialize() throws JAXBException { + JAXBContext context = JAXBContext.newInstance(ListObjectResponse.class); + context.createMarshaller().marshal(new ListObjectResponse(), System.out); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectDelete.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectDelete.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectDelete.java new file mode 100644 index 0000000..395aceb --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectDelete.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import java.io.IOException; + +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test delete object. + */ +public class TestObjectDelete { + + @Test + public void delete() throws IOException, OS3Exception { + //GIVEN + OzoneClient client = new OzoneClientStub(); + client.getObjectStore().createS3Bucket("bilbo", "b1"); + + String volumeName = client.getObjectStore().getOzoneVolumeName("b1"); + + OzoneBucket bucket = + client.getObjectStore().getVolume(volumeName).getBucket("b1"); + + bucket.createKey("key1", 0).close(); + + ObjectEndpoint rest = new ObjectEndpoint(); + rest.setClient(client); + + //WHEN + rest.delete("b1", "key1"); + + //THEN + Assert.assertFalse("Bucket Should not contain any key after delete", + bucket.listKeys("").hasNext()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectGet.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectGet.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectGet.java new file mode 100644 index 0000000..65abb1d --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectGet.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.core.HttpHeaders; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.Charset; + +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.client.OzoneVolume; +import org.apache.hadoop.ozone.client.io.OzoneInputStream; +import org.apache.hadoop.ozone.client.io.OzoneOutputStream; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; + +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +/** + * Test get object. + */ +public class TestObjectGet { + + public static final String CONTENT = "0123456789"; + + @Test + public void get() throws IOException, OS3Exception { + //GIVEN + OzoneClientStub client = new OzoneClientStub(); + client.getObjectStore().createS3Bucket("bilbo", "b1"); + String volumeName = client.getObjectStore().getOzoneVolumeName("b1"); + OzoneVolume volume = client.getObjectStore().getVolume(volumeName); + volume.createBucket("b1"); + OzoneBucket bucket = + volume.getBucket("b1"); + OzoneOutputStream keyStream = + bucket.createKey("key1", CONTENT.getBytes().length); + keyStream.write(CONTENT.getBytes()); + keyStream.close(); + + ObjectEndpoint rest = new ObjectEndpoint(); + rest.setClient(client); + HttpHeaders headers = Mockito.mock(HttpHeaders.class); + + ByteArrayInputStream body = new ByteArrayInputStream(CONTENT.getBytes()); + + //WHEN + rest.get(headers, "b1", "key1", body); + + //THEN + OzoneInputStream ozoneInputStream = + volume.getBucket("b1") + .readKey("key1"); + String keyContent = + IOUtils.toString(ozoneInputStream, Charset.forName("UTF-8")); + + Assert.assertEquals(CONTENT, keyContent); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectHead.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectHead.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectHead.java new file mode 100644 index 0000000..446c2c9 --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectHead.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.core.Response; +import java.io.IOException; + +import org.apache.hadoop.hdds.client.ReplicationFactor; +import org.apache.hadoop.hdds.client.ReplicationType; +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.client.io.OzoneOutputStream; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; + +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Test head object. + */ +public class TestObjectHead { + private String bucketName = "b1"; + private OzoneClientStub clientStub; + private ObjectStore objectStoreStub; + private ObjectEndpoint keyEndpoint; + private OzoneBucket bucket; + + @Before + public void setup() throws IOException { + //Create client stub and object store stub. + clientStub = new OzoneClientStub(); + objectStoreStub = clientStub.getObjectStore(); + + // Create volume and bucket + objectStoreStub.createS3Bucket("bilbo", bucketName); + String volName = objectStoreStub.getOzoneVolumeName(bucketName); + + bucket = objectStoreStub.getVolume(volName).getBucket(bucketName); + + // Create HeadBucket and setClient to OzoneClientStub + keyEndpoint = new ObjectEndpoint(); + keyEndpoint.setClient(clientStub); + } + + @Test + public void testHeadObject() throws Exception { + //GIVEN + String value = RandomStringUtils.randomAlphanumeric(32); + OzoneOutputStream out = bucket.createKey("key1", + value.getBytes().length, ReplicationType.STAND_ALONE, + ReplicationFactor.ONE); + out.write(value.getBytes()); + out.close(); + + //WHEN + Response response = keyEndpoint.head(bucketName, "key1"); + + //THEN + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals(value.getBytes().length, + Long.parseLong(response.getHeaderString("Content-Length"))); + } + + @Test + public void testHeadFailByBadName() throws Exception { + //Head an object that doesn't exist. + try { + keyEndpoint.head(bucketName, "badKeyName"); + } catch (OS3Exception ex) { + Assert.assertTrue(ex.getCode().contains("NoSuchObject")); + Assert.assertTrue(ex.getErrorMessage().contains("object does not exist")); + Assert.assertEquals(HTTP_NOT_FOUND, ex.getHttpCode()); + } + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestPutObject.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestPutObject.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestPutObject.java new file mode 100644 index 0000000..c3607da --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestPutObject.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.Charset; + +import org.apache.hadoop.hdds.client.ReplicationFactor; +import org.apache.hadoop.hdds.client.ReplicationType; +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.client.io.OzoneInputStream; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; + +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +/** + * Test put object. + */ +public class TestPutObject { + public static final String CONTENT = "0123456789"; + private String userName = "ozone"; + private String bucketName = "b1"; + private String keyName = "key1"; + private OzoneClientStub clientStub; + private ObjectStore objectStoreStub; + private ObjectEndpoint objectEndpoint; + + @Before + public void setup() throws IOException { + //Create client stub and object store stub. + clientStub = new OzoneClientStub(); + objectStoreStub = clientStub.getObjectStore(); + + // Create bucket + objectStoreStub.createS3Bucket(userName, bucketName); + + // Create PutObject and setClient to OzoneClientStub + objectEndpoint = new ObjectEndpoint(); + objectEndpoint.setClient(clientStub); + } + + @Test + public void testPutObject() throws IOException, OS3Exception { + //GIVEN + HttpHeaders headers = Mockito.mock(HttpHeaders.class); + ByteArrayInputStream body = new ByteArrayInputStream(CONTENT.getBytes()); + + //WHEN + Response response = objectEndpoint.put(headers, bucketName, keyName, + ReplicationType.STAND_ALONE, ReplicationFactor.ONE, "32 * 1024 * 1024", + CONTENT.length(), body); + + //THEN + String volumeName = clientStub.getObjectStore() + .getOzoneVolumeName(bucketName); + OzoneInputStream ozoneInputStream = + clientStub.getObjectStore().getVolume(volumeName).getBucket(bucketName) + .readKey(keyName); + String keyContent = + IOUtils.toString(ozoneInputStream, Charset.forName("UTF-8")); + + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals(CONTENT, keyContent); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestRootList.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestRootList.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestRootList.java new file mode 100644 index 0000000..65fdf4e --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestRootList.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.hadoop.ozone.s3.endpoint; + +import javax.ws.rs.core.HttpHeaders; + +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.client.OzoneVolume; + +import org.apache.commons.lang3.RandomStringUtils; +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import static org.mockito.Mockito.when; + +/** + * This class test HeadBucket functionality. + */ +public class TestRootList { + + private String volumeName = "vol1"; + private OzoneClientStub clientStub; + private ObjectStore objectStoreStub; + private OzoneVolume volumeStub; + private RootEndpoint rootEndpoint; + + @Before + public void setup() throws Exception { + + //Create client stub and object store stub. + clientStub = new OzoneClientStub(); + objectStoreStub = clientStub.getObjectStore(); + objectStoreStub.createVolume("s3key"); + volumeStub = objectStoreStub.getVolume("s3key"); + + // Create HeadBucket and setClient to OzoneClientStub + rootEndpoint = new RootEndpoint(); + rootEndpoint.setClient(clientStub); + } + + @Test + public void testListBucket() throws Exception { + HttpHeaders headers = Mockito.mock(HttpHeaders.class); + when(headers.getHeaderString("Authorization")).thenReturn("AWS key:secret"); + + // List operation should success even there is no bucket. + ListBucketResponse response = rootEndpoint.get(headers); + assertEquals(0, response.getBucketsNum()); + + String bucketBaseName = "bucket-"; + for(int i = 0; i < 10; i++) { + volumeStub.createBucket( + bucketBaseName + RandomStringUtils.randomNumeric(3)); + } + response = rootEndpoint.get(headers); + assertEquals(10, response.getBucketsNum()); + } + +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java new file mode 100644 index 0000000..d320041 --- /dev/null +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/package-info.java @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Unit tests for the rest endpoint implementations. + */ +package org.apache.hadoop.ozone.s3.endpoint; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hadoop/blob/0c2914e5/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/object/TestDeleteObject.java ---------------------------------------------------------------------- diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/object/TestDeleteObject.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/object/TestDeleteObject.java deleted file mode 100644 index 6c06a76..0000000 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/object/TestDeleteObject.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.hadoop.ozone.s3.object; - -import java.io.IOException; - -import org.apache.hadoop.ozone.client.OzoneBucket; -import org.apache.hadoop.ozone.client.OzoneClient; -import org.apache.hadoop.ozone.client.OzoneClientStub; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Test delete object. - */ -public class TestDeleteObject { - - @Test - public void delete() throws IOException { - //GIVEN - OzoneClient client = new OzoneClientStub(); - client.getObjectStore().createVolume("vol1"); - client.getObjectStore().getVolume("vol1").createBucket("b1"); - OzoneBucket bucket = - client.getObjectStore().getVolume("vol1").getBucket("b1"); - bucket.createKey("key1", 0).close(); - - DeleteObject rest = new DeleteObject(); - rest.setClient(client); - - //WHEN - rest.delete("vol1", "b1", "key1"); - - //THEN - Assert.assertFalse("Bucket Should not contain any key after delete", - bucket.listKeys("").hasNext()); - } -} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
