cleaned up controller

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1603 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-07-03 15:42:50 +00:00
parent 08ea6293fb
commit e5bc2dd5e7
4 changed files with 170 additions and 2 deletions

View File

@ -0,0 +1,94 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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<MetadataToBucketResult> metadataToBucketResultProvider;
@Resource
protected Logger logger = Logger.NULL;
@Inject
public GetAllBucketsController(S3Context context,
Provider<MetadataToBucketResult> 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<S3Bucket.Metadata> myBucketMetadata = context.getConnection().listOwnedBuckets().get(25,
TimeUnit.SECONDS);
List<BucketResult> myBuckets = Lists.transform(myBucketMetadata,
metadataToBucketResultProvider.get());
request.setAttribute("buckets", myBuckets);
}
}

View File

@ -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);
}
});

View File

@ -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;
}
}

View File

@ -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<S3Bucket.Metadata, BucketResult> {
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;
}
}