Search in sources :

Example 1 with CSLSRequest

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);
    }
}
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)

Aggregations

CSLSRequest (ambit2.search.csls.CSLSRequest)1 CSLSStringRequest (ambit2.search.csls.CSLSStringRequest)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)1 NotFoundException (net.idea.modbcum.i.exceptions.NotFoundException)1 OutputRepresentation (org.restlet.representation.OutputRepresentation)1 StringRepresentation (org.restlet.representation.StringRepresentation)1 ResourceException (org.restlet.resource.ResourceException)1