Search in sources :

Example 1 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class BookmarkJSONReporter method processItem.

@Override
public Object processItem(Bookmark item) throws AmbitException {
    try {
        Writer writer = getOutput();
        if (comma != null)
            writer.write(comma);
        writer.write(item.toJSON());
        comma = ",";
    } catch (Exception x) {
        logger.log(java.util.logging.Level.SEVERE, x.getMessage(), x);
    }
    return item;
}
Also used : Writer(java.io.Writer) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 2 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException 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);
    }
}
Also used : CSLSStringRequest(ambit2.search.csls.CSLSStringRequest) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) IOException(java.io.IOException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) StringRepresentation(org.restlet.representation.StringRepresentation) OutputRepresentation(org.restlet.representation.OutputRepresentation) ResourceException(org.restlet.resource.ResourceException) CSLSRequest(ambit2.search.csls.CSLSRequest) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 3 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class ModelTextReporter method processItem.

@Override
public Object processItem(ModelQueryResults model) throws AmbitException {
    try {
        ModelPredictor predictor = ModelResource.getPredictor(model, request);
        getOutput().write(predictor.toString());
        return null;
    } catch (AmbitException x) {
        throw x;
    } catch (Exception x) {
        throw new AmbitException(x);
    }
}
Also used : ModelPredictor(ambit2.rest.model.predictor.ModelPredictor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 4 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException 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;
    }
}
Also used : Form(org.restlet.data.Form) StringConvertor(net.idea.restnet.c.StringConvertor) OutputStream(java.io.OutputStream) IOException(java.io.IOException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) OutputRepresentation(org.restlet.representation.OutputRepresentation) ResourceException(org.restlet.resource.ResourceException) Writer(java.io.Writer) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 5 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException 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;
}
Also used : Form(org.restlet.data.Form) StringConvertor(net.idea.restnet.c.StringConvertor) Writer(java.io.Writer) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Aggregations

AmbitException (net.idea.modbcum.i.exceptions.AmbitException)378 ArrayList (java.util.ArrayList)119 QueryParam (net.idea.modbcum.i.query.QueryParam)109 SQLException (java.sql.SQLException)85 Property (ambit2.base.data.Property)68 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)56 IStructureRecord (ambit2.base.interfaces.IStructureRecord)48 ResourceException (org.restlet.resource.ResourceException)43 IOException (java.io.IOException)35 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)35 LiteratureEntry (ambit2.base.data.LiteratureEntry)21 StructureRecord (ambit2.base.data.StructureRecord)19 ResultSet (java.sql.ResultSet)19 SubstanceRecord (ambit2.base.data.SubstanceRecord)14 Template (ambit2.base.data.Template)13 ModelQueryResults (ambit2.core.data.model.ModelQueryResults)12 Form (org.restlet.data.Form)12 PredictedVarsTemplate (ambit2.base.data.PredictedVarsTemplate)11 StringWriter (java.io.StringWriter)11 OperationNotSupportedException (javax.naming.OperationNotSupportedException)11