mirror of https://github.com/apache/jclouds.git
Fixed code that was broken after the getBlob API change.
git-svn-id: http://jclouds.googlecode.com/svn/trunk@2633 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
befd1f0a4e
commit
c0e08d54ec
|
@ -3,22 +3,17 @@
|
|||
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.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
|
||||
* Licensed 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
|
||||
* 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.
|
||||
* 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.demo.tweetstore.controller;
|
||||
|
|
|
@ -53,77 +53,78 @@ import com.google.common.base.Function;
|
|||
@Singleton
|
||||
public class StoreTweetsController extends HttpServlet {
|
||||
|
||||
private static final class StatusToBlob implements Function<Status, Blob> {
|
||||
private final BlobMap map;
|
||||
private static final class StatusToBlob implements Function<Status, Blob> {
|
||||
private final BlobMap map;
|
||||
|
||||
private StatusToBlob(BlobMap map) {
|
||||
this.map = map;
|
||||
}
|
||||
private StatusToBlob(BlobMap map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Blob apply(Status from) {
|
||||
Blob to = map.newBlob();
|
||||
to.getMetadata().setContentType(MediaType.TEXT_PLAIN);
|
||||
to.getMetadata().setName(from.getId() + "");
|
||||
to.setPayload(from.getText());
|
||||
to.getMetadata().getUserMetadata().put(TweetStoreConstants.SENDER_NAME,
|
||||
from.getUser().getScreenName());
|
||||
return to;
|
||||
}
|
||||
}
|
||||
public Blob apply(Status from) {
|
||||
Blob to = map.newBlob(from.getId() + "");
|
||||
to.getMetadata().setContentType(MediaType.TEXT_PLAIN);
|
||||
to.getMetadata().setName(from.getId() + "");
|
||||
to.setPayload(from.getText());
|
||||
to.getMetadata().getUserMetadata().put(TweetStoreConstants.SENDER_NAME,
|
||||
from.getUser().getScreenName());
|
||||
return to;
|
||||
}
|
||||
}
|
||||
|
||||
/** The serialVersionUID */
|
||||
private static final long serialVersionUID = 7215420527854203714L;
|
||||
/** The serialVersionUID */
|
||||
private static final long serialVersionUID = 7215420527854203714L;
|
||||
|
||||
private final Map<String, BlobStoreContext<?, ?>> contexts;
|
||||
private final TwitterClient client;
|
||||
private final String container;
|
||||
private final Map<String, BlobStoreContext<?, ?>> contexts;
|
||||
private final TwitterClient client;
|
||||
private final String container;
|
||||
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
@Inject
|
||||
public StoreTweetsController(Map<String, BlobStoreContext<?, ?>> contexts,
|
||||
@Inject
|
||||
public StoreTweetsController(
|
||||
Map<String, BlobStoreContext<?, ?>> contexts,
|
||||
@Named(TweetStoreConstants.PROPERTY_TWEETSTORE_CONTAINER) final String container,
|
||||
TwitterClient client) {
|
||||
this.container = container;
|
||||
this.contexts = contexts;
|
||||
this.client = client;
|
||||
}
|
||||
this.container = container;
|
||||
this.contexts = contexts;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void addMyTweets(String contextName, SortedSet<Status> allAboutMe) {
|
||||
BlobStoreContext<?, ?> context = checkNotNull(contexts.get(contextName), "no context for "
|
||||
+ contextName + " in " + contexts.keySet());
|
||||
BlobMap map = context.createBlobMap(container);
|
||||
for (Status status : allAboutMe) {
|
||||
try {
|
||||
map.put(status.getId() + "", new StatusToBlob(map).apply(status));
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Error storing tweet %s on map %s/%s", status.getId(), context,
|
||||
container);
|
||||
}
|
||||
}
|
||||
}
|
||||
@VisibleForTesting
|
||||
void addMyTweets(String contextName, SortedSet<Status> allAboutMe) {
|
||||
BlobStoreContext<?, ?> context = checkNotNull(contexts.get(contextName),
|
||||
"no context for " + contextName + " in " + contexts.keySet());
|
||||
BlobMap map = context.createBlobMap(container);
|
||||
for (Status status : allAboutMe) {
|
||||
try {
|
||||
map.put(status.getId() + "", new StatusToBlob(map).apply(status));
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Error storing tweet %s on map %s/%s", status.getId(),
|
||||
context, container);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
if (request.getHeader("X-AppEngine-QueueName") != null
|
||||
&& request.getHeader("X-AppEngine-QueueName").equals("twitter")) {
|
||||
try {
|
||||
String contextName = checkNotNull(request.getHeader("context"),
|
||||
"missing header context");
|
||||
logger.info("retrieving tweets");
|
||||
addMyTweets(contextName, client.getMyMentions());
|
||||
logger.debug("done storing tweets");
|
||||
response.setContentType(MediaType.TEXT_PLAIN);
|
||||
response.getWriter().println("Done!");
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Error storing tweets");
|
||||
throw new ServletException(e);
|
||||
}
|
||||
} else {
|
||||
response.sendError(401);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
if (request.getHeader("X-AppEngine-QueueName") != null
|
||||
&& request.getHeader("X-AppEngine-QueueName").equals("twitter")) {
|
||||
try {
|
||||
String contextName =
|
||||
checkNotNull(request.getHeader("context"), "missing header context");
|
||||
logger.info("retrieving tweets");
|
||||
addMyTweets(contextName, client.getMyMentions());
|
||||
logger.debug("done storing tweets");
|
||||
response.setContentType(MediaType.TEXT_PLAIN);
|
||||
response.getWriter().println("Done!");
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Error storing tweets");
|
||||
throw new ServletException(e);
|
||||
}
|
||||
} else {
|
||||
response.sendError(401);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -54,8 +54,7 @@ public class AddTweetsControllerTest {
|
|||
BlobStoreContext<AsyncBlobStore, BlobStore> context = new StubBlobStoreContextBuilder()
|
||||
.buildContext();
|
||||
context.getAsyncBlobStore().createContainer(container).get();
|
||||
Blob blob = context.getAsyncBlobStore().newBlob();
|
||||
blob.getMetadata().setName("1");
|
||||
Blob blob = context.getAsyncBlobStore().newBlob("1");
|
||||
blob.getMetadata().getUserMetadata().put(TweetStoreConstants.SENDER_NAME, "frank");
|
||||
blob.setPayload("I love beans!");
|
||||
context.getAsyncBlobStore().putBlob(container, blob).get();
|
||||
|
|
|
@ -50,8 +50,7 @@ public class KeyToStoredTweetStatusTest {
|
|||
|
||||
public void testStoreTweets() throws IOException, InterruptedException, ExecutionException {
|
||||
BlobMap map = createMap();
|
||||
Blob blob = map.newBlob();
|
||||
blob.getMetadata().setName("1");
|
||||
Blob blob = map.newBlob("1");
|
||||
blob.getMetadata().getUserMetadata().put(TweetStoreConstants.SENDER_NAME, "frank");
|
||||
blob.setPayload("I love beans!");
|
||||
map.put("1", blob);
|
||||
|
|
|
@ -51,8 +51,7 @@ public class ServiceToStoredTweetStatusesTest {
|
|||
BlobStoreContext<AsyncBlobStore, BlobStore> context = new StubBlobStoreContextBuilder()
|
||||
.buildContext();
|
||||
context.getAsyncBlobStore().createContainer(container).get();
|
||||
Blob blob = context.getAsyncBlobStore().newBlob();
|
||||
blob.getMetadata().setName("1");
|
||||
Blob blob = context.getAsyncBlobStore().newBlob("1");
|
||||
blob.getMetadata().getUserMetadata().put(TweetStoreConstants.SENDER_NAME, "frank");
|
||||
blob.setPayload("I love beans!");
|
||||
context.getAsyncBlobStore().putBlob(container, blob).get();
|
||||
|
|
Loading…
Reference in New Issue