From e5bc2dd5e7764710f75c2e554b33d19cde5319a1 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Fri, 3 Jul 2009 15:42:50 +0000 Subject: [PATCH] cleaned up controller git-svn-id: http://jclouds.googlecode.com/svn/trunk@1603 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../GetAllBucketsController.java | 94 +++++++++++++++++++ .../config/GuiceServletConfig.java | 4 +- .../googleappengine/domain/BucketResult.java | 31 ++++++ .../functions/MetadataToBucketResult.java | 43 +++++++++ 4 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/GetAllBucketsController.java create mode 100644 aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/domain/BucketResult.java create mode 100644 aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/functions/MetadataToBucketResult.java diff --git a/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/GetAllBucketsController.java b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/GetAllBucketsController.java new file mode 100644 index 0000000000..a9c1bd089c --- /dev/null +++ b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/GetAllBucketsController.java @@ -0,0 +1,94 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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.jclouds.samples.googleappengine; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import javax.annotation.Resource; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.jclouds.aws.s3.S3Context; +import org.jclouds.aws.s3.domain.S3Bucket; +import org.jclouds.logging.Logger; +import org.jclouds.samples.googleappengine.domain.BucketResult; +import org.jclouds.samples.googleappengine.functions.MetadataToBucketResult; + +import com.google.common.collect.Lists; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; + +/** + * Shows an example of how to use @{link S3Connection} injected with Guice. + * + * @author Adrian Cole + */ +@Singleton +public class GetAllBucketsController extends HttpServlet { + private static final long serialVersionUID = 1L; + + private final S3Context context; + private final Provider metadataToBucketResultProvider; + + @Resource + protected Logger logger = Logger.NULL; + + @Inject + public GetAllBucketsController(S3Context context, + Provider metadataToBucketResultProvider) { + this.context = context; + this.metadataToBucketResultProvider = metadataToBucketResultProvider; + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + try { + addMyBucketsToRequest(request); + RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( + "/WEB-INF/jsp/buckets.jsp"); + dispatcher.forward(request, response); + } catch (Exception e) { + logger.error(e, "Error listing buckets"); + throw new ServletException(e); + } + } + + private void addMyBucketsToRequest(HttpServletRequest request) throws InterruptedException, + ExecutionException, TimeoutException { + List myBucketMetadata = context.getConnection().listOwnedBuckets().get(25, + TimeUnit.SECONDS); + List myBuckets = Lists.transform(myBucketMetadata, + metadataToBucketResultProvider.get()); + request.setAttribute("buckets", myBuckets); + } +} \ No newline at end of file diff --git a/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java index 0265535ca9..85a59c2b8e 100644 --- a/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java +++ b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java @@ -32,7 +32,7 @@ import org.jclouds.aws.s3.S3Context; import org.jclouds.aws.s3.S3ContextFactory; import org.jclouds.aws.s3.reference.S3Constants; import org.jclouds.gae.config.URLFetchServiceClientModule; -import org.jclouds.samples.googleappengine.JCloudsServlet; +import org.jclouds.samples.googleappengine.GetAllBucketsController; import javax.servlet.ServletContextEvent; import java.io.IOException; @@ -82,7 +82,7 @@ public class GuiceServletConfig extends GuiceServletContextListener { new ServletModule() { @Override protected void configureServlets() { - serve("*.s3").with(JCloudsServlet.class); + serve("*.s3").with(GetAllBucketsController.class); requestInjection(this); } }); diff --git a/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/domain/BucketResult.java b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/domain/BucketResult.java new file mode 100644 index 0000000000..eace735559 --- /dev/null +++ b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/domain/BucketResult.java @@ -0,0 +1,31 @@ +package org.jclouds.samples.googleappengine.domain; + +public class BucketResult { + private String name; + private String size = "unknown"; + private String status = "ok"; + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setSize(String size) { + this.size = size; + } + + public String getSize() { + return size; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getStatus() { + return status; + } +} diff --git a/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/functions/MetadataToBucketResult.java b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/functions/MetadataToBucketResult.java new file mode 100644 index 0000000000..971ef39926 --- /dev/null +++ b/aws/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/functions/MetadataToBucketResult.java @@ -0,0 +1,43 @@ +package org.jclouds.samples.googleappengine.functions; + +import java.util.concurrent.TimeUnit; + +import javax.annotation.Resource; + +import org.jclouds.aws.s3.S3Connection; +import org.jclouds.aws.s3.domain.S3Bucket; +import org.jclouds.aws.s3.domain.S3Bucket.Metadata; +import org.jclouds.logging.Logger; +import org.jclouds.samples.googleappengine.domain.BucketResult; + +import com.google.common.base.Function; +import com.google.inject.Inject; + +public class MetadataToBucketResult implements Function { + private final S3Connection connection; + + @Resource + protected Logger logger = Logger.NULL; + + @Inject + public MetadataToBucketResult(S3Connection connection) { + this.connection = connection; + } + + public BucketResult apply(Metadata from) { + BucketResult result = new BucketResult(); + result.setName(from.getName()); + try { + S3Bucket bucket = connection.listBucket(from.getName()).get(10, TimeUnit.SECONDS); + if (bucket == S3Bucket.NOT_FOUND) { + result.setStatus("not found"); + } else { + result.setSize(bucket.getSize() + ""); + } + } catch (Exception e) { + logger.error(e, "Error listing bucket %1$s", result.getName()); + result.setStatus(e.getMessage()); + } + return result; + } +} \ No newline at end of file