* Handle put requests (non-Javadoc)
* @see org.restlet.resource.Resource#storeRepresentation(org.restlet.resource.Representation)
*/
public void storeRepresentation(Representation entity) throws ResourceException {
logger.debug("in storeRepresentation");
StringRepresentation presentation = null;
// try {
Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
Map<String, String> headerMap = requestHeaders.getValuesMap();
logger.debug("HEADERS MAP");
for (String key : headerMap.keySet()) {
logger.debug(key+" : "+headerMap.get(key));
}
// } catch (IOException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
//}
try
{
logger.debug("in PutResource handle");
String databaseId = (String)getRequest().getAttributes().get(MockEspressoService.DATABASENAME);
String tableId = (String)getRequest().getAttributes().get(MockEspressoService.TABLENAME);
String resourceId = (String)getRequest().getAttributes().get(MockEspressoService.RESOURCENAME);
String subResourceId = (String)getRequest().getAttributes().get(MockEspressoService.SUBRESOURCENAME);
logger.debug("Done getting request components");
logger.debug("method: "+getRequest().getMethod());
String composedKey = databaseId + tableId + resourceId; // + subResourceId;
EspressoStorageMockNode mock = (EspressoStorageMockNode)_context.getAttributes().get(MockEspressoService.CONTEXT_MOCK_NODE_NAME);
if (getRequest().getMethod() == Method.PUT) {
logger.debug("processing PUT");
Reader postBodyReader;
//TODO: get to no fixed size on buffer
char[] postBody = new char[POST_BODY_BUFFER_SIZE];
postBodyReader = getRequest().getEntity().getReader();
postBodyReader.read(postBody);
logger.debug("postBody: "+new String(postBody));
mock.doPut(databaseId, composedKey, new String(postBody));
presentation = new StringRepresentation("Put succeeded", MediaType.APPLICATION_JSON);
}
else if (getRequest().getMethod() == Method.GET) {
logger.debug("Processing GET");
String result = mock.doGet(databaseId, composedKey);
logger.debug("result: "+result);
if (result == null) {
presentation = new StringRepresentation("Record not found", MediaType.APPLICATION_JSON);
}
else {
presentation = new StringRepresentation(result, MediaType.APPLICATION_JSON);
}
}
}
catch (IOException e) {
presentation = new StringRepresentation(e.getMessage(), MediaType.APPLICATION_JSON);
e.printStackTrace();
}
catch(Exception e)
{
String error = "Error with op";
presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
e.printStackTrace();
}
finally {
entity.release();
}