View Javadoc

1   package org.apache.helix;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.nio.ByteBuffer;
23  import java.nio.CharBuffer;
24  import java.nio.channels.ReadableByteChannel;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.helix.tools.ClusterSetup;
29  import org.apache.log4j.Logger;
30  import org.codehaus.jackson.JsonGenerationException;
31  import org.codehaus.jackson.map.JsonMappingException;
32  import org.restlet.Context;
33  import org.restlet.data.Form;
34  import org.restlet.data.MediaType;
35  import org.restlet.data.Method;
36  import org.restlet.data.Request;
37  import org.restlet.data.Response;
38  import org.restlet.data.Status;
39  import org.restlet.resource.Representation;
40  import org.restlet.resource.Resource;
41  import org.restlet.resource.ResourceException;
42  import org.restlet.resource.StringRepresentation;
43  import org.restlet.resource.Variant;
44  
45  
46  public class EspressoResource extends Resource {
47  	
48  	private static final int POST_BODY_BUFFER_SIZE = 1024*10;
49  	
50  	private static final Logger logger = Logger
51  			.getLogger(EspressoResource.class);
52  	
53  	Context _context;
54  	
55  	 public EspressoResource(Context context,
56  	            Request request,
57  	            Response response) 
58  	  {
59  	    super(context, request, response);
60  	    getVariants().add(new Variant(MediaType.TEXT_PLAIN));
61  	    getVariants().add(new Variant(MediaType.APPLICATION_JSON));
62  	    _context = context;
63  	  }
64  	 
65  	 public boolean allowGet()
66  	  {
67  		 System.out.println("PutResource.allowGet()");
68  	    return true;
69  	  }
70  	  
71  	  public boolean allowPost()
72  	  {
73  		  System.out.println("PutResource.allowPost()");
74  	    return true;
75  	  }
76  	  
77  	  public boolean allowPut()
78  	  {
79  		  System.out.println("PutResource.allowPut()");
80  	    return true;
81  	  }
82  	  
83  	  public boolean allowDelete()
84  	  {
85  	    return false;
86  	  }
87  	  
88  	  /*
89  	   * Handle get requests
90  	   * @see org.restlet.resource.Resource#represent(org.restlet.resource.Variant)
91  	   */
92  	  public Representation represent(Variant variant)
93  	  {
94  	    StringRepresentation presentation = null;
95  	    try
96  	    {
97  	    	String databaseId = (String)getRequest().getAttributes().get(MockEspressoService.DATABASENAME);
98  	    	String tableId = (String)getRequest().getAttributes().get(MockEspressoService.TABLENAME);
99  	    	String resourceId = (String)getRequest().getAttributes().get(MockEspressoService.RESOURCENAME);
100 	    	String subResourceId = (String)getRequest().getAttributes().get(MockEspressoService.SUBRESOURCENAME);
101 	    	logger.debug("Done getting request components");
102 	    	logger.debug("method: "+getRequest().getMethod());
103 	    	String composedKey = databaseId + tableId + resourceId; // + subResourceId;
104 	    	EspressoStorageMockNode mock = (EspressoStorageMockNode)_context.getAttributes().get(MockEspressoService.CONTEXT_MOCK_NODE_NAME);
105 
106 	    	
107 	    	if (getRequest().getMethod() == Method.PUT) {
108 	    		logger.debug("processing PUT");
109 	    		Reader postBodyReader;
110 	    		//TODO: get to no fixed size on buffer
111 	    		char[] postBody = new char[POST_BODY_BUFFER_SIZE];	    		
112 	    		postBodyReader = getRequest().getEntity().getReader();
113 	    		postBodyReader.read(postBody);
114 	    		logger.debug("postBody: "+new String(postBody));
115 	    		mock.doPut(databaseId, composedKey, new String(postBody));
116 	    		presentation = new StringRepresentation("Put succeeded", MediaType.APPLICATION_JSON);
117 	    	}
118 	    	else if (getRequest().getMethod() == Method.GET) {
119 	    		logger.debug("processing GET");
120 	    		String result = mock.doGet(databaseId, composedKey);
121 			      logger.debug("result: "+result);
122 			      if (result == null) {
123 			    	  presentation = new StringRepresentation("Record not found", MediaType.APPLICATION_JSON);
124 			    	  getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND,"Record not found");
125 			      }
126 			      else {
127 			    	  getResponse().setStatus(Status.SUCCESS_OK,"Success");
128 			    	  presentation = new StringRepresentation(result, MediaType.APPLICATION_JSON);
129 			      }
130 	    	}
131 	    }
132 
133 	    catch (IOException e) {
134 	    	presentation = new StringRepresentation(e.getMessage(), MediaType.APPLICATION_JSON);
135 	    	e.printStackTrace();
136 	    }
137 
138 	    catch(Exception e)
139 	    {
140 	    	String error = "Error with op"; //ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
141 	    	presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);	      
142 	    	e.printStackTrace();
143 	    }  
144 	    return presentation;
145 	  }
146 	  
147 	  /*
148 	   * Handle put requests (non-Javadoc)
149 	   * @see org.restlet.resource.Resource#storeRepresentation(org.restlet.resource.Representation)
150 	   */
151 	 public void storeRepresentation(Representation entity) throws ResourceException {
152 		 logger.debug("in storeRepresentation");
153 		 StringRepresentation presentation = null;
154 		// try {
155 		 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
156 		 Map<String, String> headerMap = requestHeaders.getValuesMap();
157 		 logger.debug("HEADERS MAP");
158 		 for (String key : headerMap.keySet()) {
159 			 logger.debug(key+" : "+headerMap.get(key));
160 		 }
161 	//	} catch (IOException e1) {
162 			// TODO Auto-generated catch block
163 			//e1.printStackTrace();
164 		//}   
165 		 try
166 		    {
167 		    	logger.debug("in PutResource handle");
168 		    	String databaseId = (String)getRequest().getAttributes().get(MockEspressoService.DATABASENAME);
169 		    	String tableId = (String)getRequest().getAttributes().get(MockEspressoService.TABLENAME);
170 		    	String resourceId = (String)getRequest().getAttributes().get(MockEspressoService.RESOURCENAME);
171 		    	String subResourceId = (String)getRequest().getAttributes().get(MockEspressoService.SUBRESOURCENAME);
172 		    	logger.debug("Done getting request components");
173 		    	logger.debug("method: "+getRequest().getMethod());
174 		    	String composedKey = databaseId + tableId + resourceId; // + subResourceId;
175 		    	EspressoStorageMockNode mock = (EspressoStorageMockNode)_context.getAttributes().get(MockEspressoService.CONTEXT_MOCK_NODE_NAME);
176 
177 		    	if (getRequest().getMethod() == Method.PUT) {
178 		    		logger.debug("processing PUT");
179 		    		Reader postBodyReader;
180 		    		//TODO: get to no fixed size on buffer
181 		    		char[] postBody = new char[POST_BODY_BUFFER_SIZE];
182 		    		postBodyReader = getRequest().getEntity().getReader();
183 		    		postBodyReader.read(postBody);
184 		    		logger.debug("postBody: "+new String(postBody));
185 		    		mock.doPut(databaseId, composedKey, new String(postBody));
186 		    		presentation = new StringRepresentation("Put succeeded", MediaType.APPLICATION_JSON);
187 		    	}
188 		    	else if (getRequest().getMethod() == Method.GET) {
189 		    		logger.debug("Processing GET");
190 		    		String result = mock.doGet(databaseId, composedKey);
191 				      logger.debug("result: "+result);
192 				      if (result == null) {
193 				    	  presentation = new StringRepresentation("Record not found", MediaType.APPLICATION_JSON);
194 				      }
195 				      else {
196 				    	  presentation = new StringRepresentation(result, MediaType.APPLICATION_JSON);
197 				      }
198 		    	}
199 		    }
200 
201 		    catch (IOException e) {
202 		    	presentation = new StringRepresentation(e.getMessage(), MediaType.APPLICATION_JSON);
203 		    	e.printStackTrace();
204 		    }
205 
206 		    catch(Exception e)
207 		    {
208 		    	String error = "Error with op";
209 		    	presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);	      
210 		    	e.printStackTrace();
211 		    }  
212 		    finally {
213 		    	entity.release();
214 		    }
215 	 }
216 }