Coverage Report - org.apache.shindig.social.opensocial.service.DataRequestHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
DataRequestHandler
92%
23/25
100%
10/10
0
DataRequestHandler$Preconditions
46%
6/13
38%
3/8
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 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  
    * Utility class for common API call preconditions
 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  
 }