use of net.idea.restnet.c.StringConvertor 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.restnet.c.StringConvertor in project ambit-mirror by ideaconsult.
the class AbstractDepict method process.
public Representation process(Variant variant) {
try {
Form form = getParams();
w = 400;
h = 200;
recordType = "2d";
try {
w = Integer.parseInt(form.getFirstValue("w"));
} catch (Exception x) {
w = 400;
}
try {
h = Integer.parseInt(form.getFirstValue("h"));
} catch (Exception x) {
h = 200;
}
try {
recordType = form.getFirstValue("record_type");
} catch (Exception x) {
}
try {
qType = QueryType.valueOf(form.getFirstValue("type"));
} catch (Exception x) {
qType = QueryType.smiles;
}
switch(qType) {
case mol:
{
// base64 encoded mol files
smiles = form.getValuesArray(QueryResource.b64search_param);
if (smiles != null)
for (int i = 0; i < smiles.length; i++) smiles[i] = new String(Base64.decode(smiles[i]));
break;
}
default:
{
smiles = form.getValuesArray(QueryResource.search_param);
if ((smiles == null) || (smiles.length < 1))
smiles = new String[] { null };
else
smiles[0] = smiles[0] == null ? "" : smiles[0].trim();
}
}
setSmarts(form.getFirstValue("smarts"));
setSmirks(null);
String[] smirks_patterns = form.getValuesArray("smirks");
for (String sm : smirks_patterns) if (sm != null) {
setSmirks(sm);
break;
}
if (variant.getMediaType().equals(MediaType.TEXT_HTML) || (smiles == null)) {
StringConvertor convertor = new StringConvertor(new AbstractReporter<String, Writer>() {
/**
*/
private static final long serialVersionUID = -6791292888357737097L;
public void close() throws Exception {
}
public Writer process(String target) throws AmbitException {
try {
if (headless)
output.write(target);
else {
AmbitResource.writeTopHeader(output, smiles[0] == null ? "2D structural diagram" : smiles[0], getRequest(), getResourceRef(getRequest()), header_gplus);
writeSearchForm(output, smiles[0] == null ? "N/A" : smiles[0], getRequest(), "", Method.GET, params);
output.write(target);
AmbitResource.writeHTMLFooter(output, smiles[0] == null ? "" : smiles[0], getRequest());
}
} catch (Exception x) {
}
return output;
}
}, MediaType.TEXT_HTML);
return convertor.process(getTitle(getResourceRef(getRequest()), smiles));
}
final BufferedImage image;
if (smiles[0] != null) {
image = getImage(smiles[0], w, h, recordType, qType);
if (image == null) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Invalid smiles %s", smiles));
return null;
}
} else
image = createDefaultImage(w, h);
return new OutputRepresentation(MediaType.IMAGE_PNG) {
@Override
public void write(OutputStream out) throws IOException {
try {
ImageIO.write(image, "PNG", out);
} catch (IOException x) {
throw x;
} catch (Exception x) {
} finally {
try {
out.flush();
} catch (Exception x) {
}
try {
out.close();
} catch (Exception x) {
}
}
}
};
} catch (ResourceException x) {
getResponse().setStatus(x.getStatus(), x, x.getMessage());
return null;
} catch (Exception x) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST, x, x.getMessage());
return null;
}
}
use of net.idea.restnet.c.StringConvertor in project ambit-mirror by ideaconsult.
the class CDKDepict method get.
@Override
public Representation get(Variant variant) {
try {
Form form = getParams();
smiles = form.getValuesArray(QueryResource.search_param);
if ((smiles == null) || (smiles.length < 1))
smiles = new String[] { null };
setSmarts(form.getFirstValue("smarts"));
if (MediaType.TEXT_HTML.equals(variant.getMediaType())) {
StringConvertor convertor = new StringConvertor(new AbstractReporter<String, Writer>() {
/**
*/
private static final long serialVersionUID = -1232605257681949242L;
public void close() throws Exception {
}
public Writer process(String target) throws AmbitException {
try {
AmbitResource.writeTopHeader(output, smiles[0] == null ? "2D structure diagram" : smiles[0], getRequest(), getResourceRef(getRequest()), "");
writeSearchForm(output, smiles[0], getRequest(), "", Method.GET, params);
output.write(target);
AmbitResource.writeHTMLFooter(output, smiles[0], getRequest());
} catch (Exception x) {
}
return output;
}
}, MediaType.TEXT_HTML);
return convertor.process(getTitle(getResourceRef(getRequest()), smiles));
} else {
return process(variant);
}
} catch (Exception x) {
}
return null;
}
use of net.idea.restnet.c.StringConvertor in project ambit-mirror by ideaconsult.
the class BundlePropertyResource method createConvertor.
@Override
public IProcessor<Q, Representation> createConvertor(Variant variant) throws AmbitException, ResourceException {
/* workaround for clients not being able to set accept headers */
Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
Dimension d = new Dimension(250, 250);
try {
d.width = Integer.parseInt(acceptform.getFirstValue("w").toString());
} catch (Exception x) {
}
try {
d.height = Integer.parseInt(acceptform.getFirstValue("h").toString());
} catch (Exception x) {
}
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_URI_LIST)) {
QueryURIReporter r = (QueryURIReporter) getURIReporter(getRequest());
return new StringConvertor(r, MediaType.TEXT_URI_LIST, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
return new ImageConvertor(new ImageReporter(variant.getMediaType().getMainType(), variant.getMediaType().getSubType(), d), variant.getMediaType());
} else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
String jsonpcallback = getParams().getFirstValue("jsonp");
if (jsonpcallback == null)
jsonpcallback = getParams().getFirstValue("callback");
return new OutputWriterConvertor(new PropertyJSONReporter(getRequest(), jsonpcallback), MediaType.APPLICATION_JAVASCRIPT);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TURTLE) || variant.getMediaType().equals(MediaType.TEXT_RDF_N3) || variant.getMediaType().equals(MediaType.TEXT_RDF_NTRIPLES) || variant.getMediaType().equals(ChemicalMediaType.APPLICATION_JSONLD)) {
RDFJenaConvertor convertor = new RDFJenaConvertor<Property, IQueryRetrieval<Property>>(new PropertyRDFReporter<IQueryRetrieval<Property>>(getRequest(), variant.getMediaType()), variant.getMediaType(), filenamePrefix);
return convertor;
} else {
// (variant.getMediaType().equals(MediaType.APPLICATION_JSON)) {
return new OutputWriterConvertor(new PropertyJSONReporter(getRequest()), MediaType.APPLICATION_JSON);
}
}
use of net.idea.restnet.c.StringConvertor in project ambit-mirror by ideaconsult.
the class CompoundResource method createConvertor.
@Override
public RepresentationConvertor createConvertor(Variant variant) throws AmbitException, ResourceException {
/* workaround for clients not being able to set accept headers */
if (!variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
setTemplate(createTemplate(getContext(), getRequest(), getResponse()));
setGroupProperties(getContext(), getRequest(), getResponse());
}
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 ((queryObject == null) && !(variant.getMediaType().equals(MediaType.TEXT_HTML)))
throw new NotFoundException();
if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_CML))
// StructureReporter((getRequest()==null)?null:getRequest().getRootRef()));
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CMLReporter<QueryStructureByID>(), ChemicalMediaType.CHEMICAL_CML, filenamePrefix);
else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLSDF)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(getTemplate(), 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_SMILES)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SmilesReporter<QueryStructureByID>(), 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.TEXT_PLAIN)) {
return new StringConvertor(new SmilesReporter<QueryStructureByID>(true, getTemplate()), MediaType.TEXT_PLAIN);
} else if (variant.getMediaType().equals(MediaType.IMAGE_PNG) || variant.getMediaType().equals(MediaType.IMAGE_BMP) || variant.getMediaType().equals(MediaType.IMAGE_JPEG) || variant.getMediaType().equals(MediaType.IMAGE_TIFF) || variant.getMediaType().equals(MediaType.IMAGE_GIF)) {
return createImageConvertor(variant);
} else if (variant.getMediaType().equals(ChemicalMediaType.IMAGE_JSON)) {
return createImageStringConvertor(variant);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_PDF)) {
return new PDFConvertor<IStructureRecord, QueryStructureByID, PDFReporter<QueryStructureByID>>(new PDFReporter<QueryStructureByID>(getTemplate(), getGroupProperties()));
} else if (variant.getMediaType().equals(MediaType.TEXT_URI_LIST)) {
QueryURIReporter r = (QueryURIReporter) getURIReporter();
return new StringConvertor(r, MediaType.TEXT_URI_LIST, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.WEKA_ARFF)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFFResourceReporter(getTemplate(), getGroupProperties(), getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix()), ChemicalMediaType.WEKA_ARFF, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.THREECOL_ARFF)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFF3ColResourceReporter(getTemplate(), getGroupProperties(), getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix()), ChemicalMediaType.THREECOL_ARFF, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_JSON)) {
CompoundJSONReporter cmpreporter = new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, null);
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(cmpreporter, MediaType.APPLICATION_JSON, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
String jsonpcallback = getParams().getFirstValue("jsonp");
if (jsonpcallback == null)
jsonpcallback = getParams().getFirstValue("callback");
CompoundJSONReporter cmpreporter = new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, jsonpcallback);
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(cmpreporter, MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.TEXT_CSV)) {
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CSVReporter(getRequest().getRootRef().toString(), getTemplate(), getGroupProperties(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix()), MediaType.TEXT_CSV, filenamePrefix);
} else if (variant.getMediaType().equals(ChemicalMediaType.NANO_CML)) {
// StructureReporter((getRequest()==null)?null:getRequest().getRootRef()));
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CMLReporter<QueryStructureByID>(), ChemicalMediaType.NANO_CML, filenamePrefix);
} else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML) || 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)) {
return new RDFJenaConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFReporter(getCompoundInDatasetPrefix(), getRequest(), variant.getMediaType(), getTemplate(), getGroupProperties()), variant.getMediaType(), filenamePrefix);
} else {
CompoundJSONReporter cmpreporter = new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, null);
return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(cmpreporter, MediaType.APPLICATION_JSON, filenamePrefix);
}
}
Aggregations