use of ambit2.search.csls.CSLSRequest in project ambit-mirror by ideaconsult.
the class CSLSResource method get.
@Override
protected Representation get(Variant variant) throws ResourceException {
setFrameOptions("SAMEORIGIN");
try {
if (term != null) {
if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLSDF))
return new OutputRepresentation(variant.getMediaType()) {
@Override
public void write(OutputStream stream) throws IOException {
try {
CSLSRequest<InputStream> q = new CSLSRequest<InputStream>() {
/**
*/
private static final long serialVersionUID = 2638378178348461366L;
@Override
protected InputStream read(InputStream in) throws Exception {
return in;
}
};
q.setRepresentation(representation);
DownloadTool.download(q.process(term), stream);
stream.flush();
} catch (ResourceException x) {
throw x;
} catch (AmbitException x) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
// throw new IOException(x.getMessage());
} finally {
try {
if (stream != null)
stream.flush();
} catch (Exception x) {
getLogger().log(Level.WARNING, x.getMessage(), x);
}
}
}
};
else {
CSLSStringRequest q = new CSLSStringRequest();
q.setRepresentation(representation);
return new StringRepresentation(q.process(term), MediaType.TEXT_PLAIN);
}
} else
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
} catch (NotFoundException x) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND, "No results for query " + term);
return null;
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x);
}
}
Aggregations