use of net.idea.modbcum.i.exceptions.NotFoundException in project ambit-mirror by ideaconsult.
the class StructureQueryResource method createConvertor.
@Override
public RepresentationConvertor createConvertor(Variant variant) throws AmbitException, ResourceException {
/* workaround for clients not being able to set accept headers */
if ((queryObject == null) && !(variant.getMediaType().equals(MediaType.TEXT_HTML)))
throw new NotFoundException();
setTemplate(template);
Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
String media = acceptform.getFirstValue("accept-header");
if (media != null) {
variant.setMediaType(new MediaType(media));
}
String filenamePrefix = getRequest().getResourceRef().getPath();
if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLSDF)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(template, getGroupProperties(), changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLSDF, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLMOL)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(new Template(), getGroupProperties(), true, changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLMOL, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_CML)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CMLReporter<QueryStructureByID>(), ChemicalMediaType.CHEMICAL_CML, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_SMILES)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SmilesReporter<QueryStructureByID>(true, getTemplate()), ChemicalMediaType.CHEMICAL_SMILES, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_INCHI)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SmilesReporter<QueryStructureByID>(false, Mode.InChI, getTemplate()), ChemicalMediaType.CHEMICAL_INCHI, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_PDF)) {
return new PDFConvertor<IStructureRecord, QueryStructureByID, PDFReporter<QueryStructureByID>>(new PDFReporter<QueryStructureByID>(getTemplate(), getGroupProperties()), filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createTXTReporter(), MediaType.TEXT_PLAIN);
} else if (variant.getMediaType().equals(MediaType.TEXT_URI_LIST)) {
ConformerURIReporter<QueryStructureByID> reporter = new ConformerURIReporter<QueryStructureByID>(getCompoundInDatasetPrefix(), getRequest(), queryObject.isPrescreen());
return new StringConvertor(reporter, MediaType.TEXT_URI_LIST, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
return new ImageConvertor<IStructureRecord, QueryStructureByID>(new ImageReporter<QueryStructureByID>(MediaType.IMAGE_PNG.getMainType(), MediaType.IMAGE_PNG.getSubType()), MediaType.IMAGE_PNG);
} else if (variant.getMediaType().equals(MediaType.IMAGE_GIF)) {
return new ImageConvertor<IStructureRecord, QueryStructureByID>(new ImageReporter<QueryStructureByID>(MediaType.IMAGE_GIF.getMainType(), MediaType.IMAGE_GIF.getSubType()), MediaType.IMAGE_GIF);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_JSON)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, null), MediaType.APPLICATION_JSON, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
String jsonpcallback = getParams().getFirstValue("jsonp");
if (jsonpcallback == null)
jsonpcallback = getParams().getFirstValue("callback");
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, jsonpcallback), MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.WEKA_ARFF)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFFResourceReporter(getTemplate(), getGroupProperties(), getRequest(), String.format("%s%s", getRequest().getRootRef(), getCompoundInDatasetPrefix())), ChemicalMediaType.WEKA_ARFF, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.THREECOL_ARFF)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFF3ColResourceReporter(getTemplate(), getGroupProperties(), getRequest(), String.format("%s%s", getRequest().getRootRef(), getCompoundInDatasetPrefix())), ChemicalMediaType.THREECOL_ARFF, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.TEXT_CSV)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createCSVReporter(), MediaType.TEXT_CSV, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML)) {
switch(rdfwriter) {
case stax:
{
return new RDFStaXConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFStaxReporter(getCompoundInDatasetPrefix(), getRequest(), getTemplate(), getGroupProperties()), filenamePrefix);
}
default:
{
// jena
return new RDFJenaConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFReporter(getCompoundInDatasetPrefix(), getRequest(), variant.getMediaType(), getTemplate(), getGroupProperties()), variant.getMediaType(), filenamePrefix);
}
}
} else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_TURTLE) || variant.getMediaType().equals(MediaType.TEXT_RDF_N3) || variant.getMediaType().equals(MediaType.TEXT_RDF_NTRIPLES) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TRIG) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TRIX) || variant.getMediaType().equals(ChemicalMediaType.APPLICATION_JSONLD)) {
return new RDFJenaConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFReporter(getCompoundInDatasetPrefix(), getRequest(), variant.getMediaType(), getTemplate(), getGroupProperties()), variant.getMediaType(), filenamePrefix);
} else
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(template, getGroupProperties(), changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLSDF, filenamePrefix);
}
use of net.idea.modbcum.i.exceptions.NotFoundException 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);
}
}
use of net.idea.modbcum.i.exceptions.NotFoundException 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 net.idea.modbcum.i.exceptions.NotFoundException in project ambit-mirror by ideaconsult.
the class CompoundImageJSONResource method createConvertor.
@Override
public RepresentationConvertor createConvertor(Variant variant) throws AmbitException, ResourceException {
Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
String media = acceptform.getFirstValue("accept-header");
if (media != null) {
variant.setMediaType(new MediaType(media));
}
if ((queryObject == null) && !(variant.getMediaType().equals(MediaType.TEXT_HTML)))
throw new NotFoundException();
return createImageStringConvertor(variant);
}
use of net.idea.modbcum.i.exceptions.NotFoundException in project ambit-mirror by ideaconsult.
the class AbstractPairwiseResource method createConvertor.
@Override
public RepresentationConvertor createConvertor(Variant variant) throws AmbitException, ResourceException {
/* workaround for clients not being able to set accept headers */
if ((queryObject == null) && !(variant.getMediaType().equals(MediaType.TEXT_HTML)))
throw new NotFoundException();
// setTemplate(template);
Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
String media = acceptform.getFirstValue("accept-header");
if (media != null) {
variant.setMediaType(new MediaType(media));
}
String filenamePrefix = getRequest().getResourceRef().getPath();
if (variant.getMediaType().equals(MediaType.TEXT_HTML)) {
Dimension d = new Dimension(150, 150);
Form form = getResourceRef(getRequest()).getQueryAsForm();
try {
d.width = Integer.parseInt(form.getFirstValue("w").toString());
} catch (Exception x) {
}
try {
d.height = Integer.parseInt(form.getFirstValue("h").toString());
} catch (Exception x) {
}
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createHTMLReporter(d), MediaType.TEXT_HTML);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
String jsonpcallback = getParams().getFirstValue("jsonp");
if (jsonpcallback == null)
jsonpcallback = getParams().getFirstValue("callback");
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createJSONReporter(jsonpcallback), MediaType.APPLICATION_JSON, filenamePrefix);
}
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createJSONReporter(), MediaType.APPLICATION_JSON, filenamePrefix);
}
Aggregations