cmd = api.getMetaDataManager().getMetaDataForClass(className, clr);
}
}
catch (ClassNotResolvedException ex)
{
JSONObject error = new JSONObject();
error.put("exception", ex.getMessage());
resp.getWriter().write(error.toString());
resp.setStatus(404);
resp.setHeader("Content-Type", "application/json");
return;
}
JSONObject id = getId(api, req);
if (id == null)
{
// no id provided
try
{
// get the whole extent
String queryString = "SELECT FROM " + cmd.getFullClassName();
if (req.getQueryString() != null)
{
// query by filter
queryString += " WHERE " + URLDecoder.decode(req.getQueryString(), "UTF-8");
}
try
{
api.getTransaction().begin();
Query query = api.newQuery("JDOQL", queryString);
List result = (List)query.execute();
JSONArray array = new JSONArray(result);
resp.getWriter().write(array.toString());
resp.setHeader("Content-Type", "application/json");
resp.setStatus(200);
api.getTransaction().commit();
}
finally
{
if (api.getTransaction().isActive())
{
api.getTransaction().rollback();
}
api.close();
}
return;
}
catch (NucleusUserException e)
{
JSONObject error = new JSONObject();
error.put("exception", e.getMessage());
resp.getWriter().write(error.toString());
resp.setStatus(400);
resp.setHeader("Content-Type", "application/json");
return;
}
catch (NucleusException ex)
{
JSONObject error = new JSONObject();
error.put("exception", ex.getMessage());
resp.getWriter().write(error.toString());
resp.setStatus(404);
resp.setHeader("Content-Type", "application/json");
return;
}
catch (RuntimeException ex)
{
ex.printStackTrace();
// errors from the google appengine may be raised when running queries
JSONObject error = new JSONObject();
error.put("exception", ex.getMessage());
resp.getWriter().write(error.toString());
resp.setStatus(404);
resp.setHeader("Content-Type", "application/json");
return;
}
}
if (cmd.getIdentityType() != IdentityType.APPLICATION)
{
JSONObject error = new JSONObject();
error.put("exception", "Only application identity types are support.");
resp.getWriter().write(error.toString());
resp.setStatus(404);
resp.setHeader("Content-Type", "application/json");
return;
}
try
{
api.getTransaction().begin();
id.put("class", getClassNameForPath(req));
Object result = api.findObject(id);
resp.getWriter().write(result.toString());
resp.setHeader("Content-Type", "application/json");
api.getTransaction().commit();
return;
}
catch (NucleusObjectNotFoundException ex)
{
resp.setContentLength(0);
resp.setStatus(404);
return;
}
catch (NucleusException ex)
{
JSONObject error = new JSONObject();
error.put("exception", ex.getMessage());
resp.getWriter().write(error.toString());
resp.setStatus(404);
resp.setHeader("Content-Type", "application/json");
return;
}
finally
{
if (api.getTransaction().isActive())
{
api.getTransaction().rollback();
}
api.close();
}
}
catch (JSONException e)
{
try
{
JSONObject error = new JSONObject();
error.put("exception", e.getMessage());
resp.getWriter().write(error.toString());
resp.setStatus(404);
resp.setHeader("Content-Type", "application/json");
}
catch (JSONException e1)
{