Coverage Report - org.apache.shindig.social.sample.service.SampleContainerHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SampleContainerHandler
0%
0/28
0%
0/6
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  
 
 19  
 package org.apache.shindig.social.sample.service;
 20  
 
 21  
 import com.google.inject.Inject;
 22  
 
 23  
 import org.apache.commons.httpclient.HttpClient;
 24  
 import org.apache.commons.httpclient.HttpMethod;
 25  
 import org.apache.commons.httpclient.methods.GetMethod;
 26  
 import org.apache.shindig.common.util.ImmediateFuture;
 27  
 import org.apache.shindig.social.ResponseError;
 28  
 import org.apache.shindig.social.opensocial.service.DataRequestHandler;
 29  
 import org.apache.shindig.social.opensocial.service.RequestItem;
 30  
 import org.apache.shindig.social.opensocial.spi.SocialSpiException;
 31  
 import org.apache.shindig.social.sample.spi.JsonDbOpensocialService;
 32  
 import org.json.JSONException;
 33  
 import org.json.JSONObject;
 34  
 
 35  
 import java.io.IOException;
 36  
 import java.util.concurrent.Future;
 37  
 
 38  
 public class SampleContainerHandler extends DataRequestHandler {
 39  
 
 40  
   private final JsonDbOpensocialService service;
 41  
 
 42  
   private static final String POST_PATH = "/samplecontainer/{type}/{doevil}";
 43  
 
 44  
   @Inject
 45  0
   public SampleContainerHandler(JsonDbOpensocialService dbService) {
 46  0
     this.service = dbService;
 47  0
   }
 48  
 
 49  
   /**
 50  
    * We don't support any delete methods right now.
 51  
    */
 52  
   protected Future<?> handleDelete(RequestItem request) throws SocialSpiException {
 53  0
     throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, null);
 54  
   }
 55  
 
 56  
   /**
 57  
    * We don't distinguish between put and post for these urls.
 58  
    */
 59  
   protected Future<?> handlePut(RequestItem request) throws SocialSpiException {
 60  0
     return handlePost(request);
 61  
   }
 62  
 
 63  
   /**
 64  
    * Handles /samplecontainer/setstate and /samplecontainer/setevilness/{doevil}. TODO(doll): These
 65  
    * urls aren't very resty. Consider changing the samplecontainer.html calls post.
 66  
    */
 67  
   protected Future<?> handlePost(RequestItem request) throws SocialSpiException {
 68  0
     request.applyUrlTemplate(POST_PATH);
 69  0
     String type = request.getParameter("type");
 70  0
     if (type.equals("setstate")) {
 71  
       try {
 72  0
         String stateFile = request.getParameter("fileurl");
 73  0
         service.setDb(new JSONObject(fetchStateDocument(stateFile)));
 74  0
       } catch (JSONException e) {
 75  0
         throw new SocialSpiException(ResponseError.BAD_REQUEST,
 76  
             "The json state file was not valid json", e);
 77  0
       }
 78  0
     } else if (type.equals("setevilness")) {
 79  0
       throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED,
 80  
           "evil data has not been implemented yet");
 81  
     }
 82  
 
 83  0
     return ImmediateFuture.newInstance(null);
 84  
   }
 85  
 
 86  
   /**
 87  
    * Handles /samplecontainer/dumpstate
 88  
    */
 89  
   protected Future<?> handleGet(RequestItem request) {
 90  0
     return ImmediateFuture.newInstance(service.getDb());
 91  
   }
 92  
 
 93  
   private String fetchStateDocument(String stateFileLocation) {
 94  0
     String errorMessage = "The json state file " + stateFileLocation
 95  
         + " could not be fetched and parsed.";
 96  
 
 97  0
     HttpMethod jsonState = new GetMethod(stateFileLocation);
 98  0
     HttpClient client = new HttpClient();
 99  
     try {
 100  0
       client.executeMethod(jsonState);
 101  
 
 102  0
       if (jsonState.getStatusCode() != 200) {
 103  0
         throw new RuntimeException(errorMessage);
 104  
       }
 105  0
       return jsonState.getResponseBodyAsString();
 106  0
     } catch (IOException e) {
 107  0
       throw new RuntimeException(errorMessage, e);
 108  
     } finally {
 109  0
       jsonState.releaseConnection();
 110  0
     }
 111  
   }
 112  
 }