use of ambit2.rest.exception.RResourceException in project ambit-mirror by ideaconsult.
the class QueryResource method getRepresentation.
protected Representation getRepresentation(Variant variant) throws ResourceException {
try {
if (MediaType.APPLICATION_JAVA_OBJECT.equals(variant.getMediaType())) {
if ((queryObject != null) && (queryObject instanceof Serializable))
return new ObjectRepresentation((Serializable) returnQueryObject(), MediaType.APPLICATION_JAVA_OBJECT);
else
throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
}
if (MediaType.APPLICATION_JAVASCRIPT.equals(variant.getMediaType())) {
if (!isJSONPEnabled())
throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
}
if (queryObject != null) {
IProcessor<Q, Representation> convertor = null;
Connection connection = null;
int retry = 0;
while (retry < maxRetry) {
try {
DBConnection dbc = new DBConnection(getContext());
configureRDFWriterOption(((AmbitApplication) getApplication()).getProperties().getRDFwriter());
configureSDFLineSeparators(((AmbitApplication) getApplication()).getProperties().getConfigChangeLineSeparator());
configureDatasetMembersPrefixOption(((AmbitApplication) getApplication()).getProperties().isDatasetMembersPrefix());
convertor = createConvertor(variant);
if (convertor instanceof RepresentationConvertor)
((RepresentationConvertor) convertor).setLicenseURI(getLicenseURI());
connection = dbc.getConnection();
Reporter reporter = ((RepresentationConvertor) convertor).getReporter();
if (reporter instanceof IDBProcessor)
((IDBProcessor) reporter).setConnection(connection);
Representation r = convertor.process(queryObject);
r.setCharacterSet(CharacterSet.UTF_8);
return r;
} catch (ResourceException x) {
throw x;
} catch (NotFoundException x) {
Representation r = processNotFound(x, retry);
retry++;
if (r != null)
return r;
} catch (BatchProcessingException x) {
if (x.getCause() instanceof NotFoundException) {
Representation r = processNotFound((NotFoundException) x.getCause(), retry);
retry++;
if (r != null)
return r;
} else {
Context.getCurrentLogger().severe(x.getMessage());
throw new RResourceException(Status.SERVER_ERROR_INTERNAL, x, variant);
}
} catch (SQLException x) {
Representation r = processSQLError(x, retry, variant);
retry++;
if (r == null)
continue;
else
return r;
} catch (Exception x) {
Context.getCurrentLogger().severe(x.getMessage());
throw new RResourceException(Status.SERVER_ERROR_INTERNAL, x, variant);
} finally {
// try { if (connection !=null) connection.close(); }
// catch (Exception x) {};
// try { if ((convertor !=null) &&
// (convertor.getReporter() !=null))
// convertor.getReporter().close(); } catch (Exception
// x) {}
}
}
return null;
} else {
if (variant.getMediaType().equals(MediaType.TEXT_HTML))
try {
IProcessor<Q, Representation> convertor = createConvertor(variant);
Representation r = convertor.process(null);
return r;
} catch (Exception x) {
throw new RResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x, variant);
}
else {
throw new RResourceException(Status.CLIENT_ERROR_BAD_REQUEST, error, variant);
}
}
} catch (RResourceException x) {
throw x;
} catch (ResourceException x) {
throw new RResourceException(x.getStatus(), x, variant);
} catch (Exception x) {
throw new RResourceException(Status.SERVER_ERROR_INTERNAL, x, variant);
}
}
use of ambit2.rest.exception.RResourceException in project ambit-mirror by ideaconsult.
the class AbstractResource method getRepresentation.
protected Representation getRepresentation(Variant variant) throws ResourceException {
try {
setTokenCookies(variant, useSecureCookie(getRequest()));
setStatus(Status.SUCCESS_OK);
if (MediaType.APPLICATION_JAVA_OBJECT.equals(variant.getMediaType())) {
if ((queryObject != null) && (queryObject instanceof Serializable))
return new ObjectRepresentation((Serializable) queryObject, MediaType.APPLICATION_JAVA_OBJECT);
else
throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
}
if (queryObject != null) {
IProcessor<Q, Representation> convertor = null;
try {
getResponse().setStatus(response_status);
convertor = createConvertor(variant);
Representation r = convertor.process(queryObject);
return r;
} catch (NotFoundException x) {
Representation r = processNotFound(x, variant);
return r;
} catch (BatchProcessingException x) {
if (x.getCause() instanceof NotFoundException) {
Representation r = processNotFound((NotFoundException) x.getCause(), variant);
return r;
} else {
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, x);
return null;
}
} catch (RResourceException x) {
getResponse().setStatus(x.getStatus());
return x.getRepresentation();
} catch (ResourceException x) {
getResponse().setStatus(x.getStatus());
return null;
} catch (Exception x) {
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, x);
return null;
} finally {
}
} else {
getResponse().setStatus(response_status == null ? Status.CLIENT_ERROR_BAD_REQUEST : response_status, error);
return null;
}
} catch (Exception x) {
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, x);
return null;
}
}
use of ambit2.rest.exception.RResourceException in project ambit-mirror by ideaconsult.
the class QueryResource method process.
protected Representation process(Representation entity, final Variant variant, final boolean async) throws ResourceException {
synchronized (this) {
Connection conn = null;
try {
final Form form = new Form(entity);
final Reference reference = new Reference(getObjectURI(form));
// models
IQueryRetrieval<T> query = createQuery(getContext(), getRequest(), getResponse());
if (query == null)
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
TaskCreator<Object, T> taskCreator = new TaskCreator<Object, T>(form, async) {
/**
*/
private static final long serialVersionUID = -1964726528437949752L;
@Override
protected ICallableTask getCallable(Form form, T item) throws ResourceException {
return createCallable(form, item);
}
@Override
protected ITask<Reference, Object> createTask(ICallableTask callable, T item) throws ResourceException {
return ((ITaskApplication) getApplication()).addTask(String.format("Apply %s %s %s", item.toString(), reference == null ? "" : "to", reference == null ? "" : reference), callable, getRequest().getRootRef(), getToken());
}
};
DBConnection dbc = new DBConnection(getApplication().getContext());
conn = dbc.getConnection(30, true, 5);
List<UUID> r = null;
try {
taskCreator.setConnection(conn);
r = taskCreator.process(query);
} finally {
try {
taskCreator.setConnection(null);
taskCreator.close();
} catch (Exception x) {
}
try {
conn.close();
conn = null;
} catch (Exception x) {
}
}
if ((r == null) || (r.size() == 0))
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
else {
ITaskStorage storage = ((ITaskApplication) getApplication()).getTaskStorage();
FactoryTaskConvertor<Object> tc = new FactoryTaskConvertor<Object>(storage);
if (r.size() == 1) {
ITask<Reference, Object> task = storage.findTask(r.get(0));
task.update();
setStatus(task.isDone() ? Status.SUCCESS_OK : Status.SUCCESS_ACCEPTED);
return tc.createTaskRepresentation(r.get(0), variant, getRequest(), getResponse(), null);
} else
return tc.createTaskRepresentation(r.iterator(), variant, getRequest(), getResponse(), null);
}
} catch (RResourceException x) {
throw x;
} catch (ResourceException x) {
throw new RResourceException(x.getStatus(), x, variant);
} catch (AmbitException x) {
throw new RResourceException(new Status(Status.SERVER_ERROR_INTERNAL, x), variant);
} catch (SQLException x) {
throw new RResourceException(new Status(Status.SERVER_ERROR_INTERNAL, x), variant);
} catch (Exception x) {
throw new RResourceException(new Status(Status.SERVER_ERROR_INTERNAL, x), variant);
} finally {
try {
if (conn != null)
conn.close();
} catch (Exception x) {
}
}
}
}
Aggregations