| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.shindig.social.opensocial.service; |
| 19 | |
|
| 20 | |
import org.apache.shindig.common.util.ImmediateFuture; |
| 21 | |
import org.apache.shindig.social.ResponseError; |
| 22 | |
import org.apache.shindig.social.opensocial.spi.SocialSpiException; |
| 23 | |
|
| 24 | |
import com.google.common.collect.ImmutableSet; |
| 25 | |
|
| 26 | |
import java.util.Collection; |
| 27 | |
import java.util.Set; |
| 28 | |
import java.util.concurrent.Future; |
| 29 | |
|
| 30 | 71 | public abstract class DataRequestHandler { |
| 31 | |
|
| 32 | 1 | private static final Set<String> GET_SYNONYMS = ImmutableSet.of("get"); |
| 33 | 1 | private static final Set<String> CREATE_SYNONYMS = ImmutableSet.of("put", "create"); |
| 34 | 1 | private static final Set<String> UPDATE_SYNONYMS = ImmutableSet.of("post", "update"); |
| 35 | 1 | private static final Set<String> DELETE_SYNONYMS = ImmutableSet.of("delete"); |
| 36 | |
|
| 37 | |
public Future<?> handleItem(RequestItem request) { |
| 38 | 49 | if (request.getOperation() == null) { |
| 39 | 1 | return ImmediateFuture.errorInstance(new SocialSpiException(ResponseError.NOT_IMPLEMENTED, |
| 40 | |
"Unserviced operation")); |
| 41 | |
} |
| 42 | 48 | String operation = request.getOperation().toLowerCase(); |
| 43 | |
Future<?> responseItem; |
| 44 | |
try { |
| 45 | 48 | if (GET_SYNONYMS.contains(operation)) { |
| 46 | 35 | responseItem = handleGet(request); |
| 47 | 35 | } else if (UPDATE_SYNONYMS.contains(operation)) { |
| 48 | 5 | responseItem = handlePost(request); |
| 49 | 5 | } else if (CREATE_SYNONYMS.contains(operation)) { |
| 50 | 1 | responseItem = handlePut(request); |
| 51 | 1 | } else if (DELETE_SYNONYMS.contains(operation)) { |
| 52 | 5 | responseItem = handleDelete(request); |
| 53 | 5 | } else { |
| 54 | 2 | throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, |
| 55 | |
"Unserviced operation " + operation); |
| 56 | |
} |
| 57 | 2 | } catch (SocialSpiException spe) { |
| 58 | 2 | return ImmediateFuture.errorInstance(spe); |
| 59 | 0 | } catch (Throwable t) { |
| 60 | 0 | return ImmediateFuture.errorInstance(new SocialSpiException(ResponseError.INTERNAL_ERROR, |
| 61 | |
"Unknown error " + t.getMessage(), t)); |
| 62 | 46 | } |
| 63 | 46 | return responseItem; |
| 64 | |
} |
| 65 | |
|
| 66 | |
protected abstract Future<?> handleDelete(RequestItem request) |
| 67 | |
throws SocialSpiException; |
| 68 | |
|
| 69 | |
protected abstract Future<?> handlePut(RequestItem request) |
| 70 | |
throws SocialSpiException; |
| 71 | |
|
| 72 | |
protected abstract Future<?> handlePost(RequestItem request) |
| 73 | |
throws SocialSpiException; |
| 74 | |
|
| 75 | |
protected abstract Future<?> handleGet(RequestItem request) |
| 76 | |
throws SocialSpiException; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | 0 | public static class Preconditions { |
| 82 | |
|
| 83 | |
public static void requireNotEmpty(Collection<?> coll, String message) |
| 84 | |
throws SocialSpiException { |
| 85 | 66 | if (coll.isEmpty()) { |
| 86 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, message); |
| 87 | |
} |
| 88 | 66 | } |
| 89 | |
|
| 90 | |
public static void requireEmpty(Collection<?> list, String message) throws SocialSpiException { |
| 91 | 4 | if (!list.isEmpty()) { |
| 92 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, message); |
| 93 | |
} |
| 94 | 4 | } |
| 95 | |
|
| 96 | |
public static void requireSingular(Collection<?> coll, String message) |
| 97 | |
throws SocialSpiException { |
| 98 | 16 | if (coll.size() != 1) { |
| 99 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, message); |
| 100 | |
} |
| 101 | 16 | } |
| 102 | |
|
| 103 | |
public static void requirePlural(Collection<?> coll, String message) throws SocialSpiException { |
| 104 | 0 | if (coll.size() <= 1) { |
| 105 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, message); |
| 106 | |
} |
| 107 | 0 | } |
| 108 | |
} |
| 109 | |
} |