| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 51 | |
|
| 52 | |
protected Future<?> handleDelete(RequestItem request) throws SocialSpiException { |
| 53 | 0 | throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, null); |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
protected Future<?> handlePut(RequestItem request) throws SocialSpiException { |
| 60 | 0 | return handlePost(request); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 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 | |
|
| 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 | |
} |