Search in sources :

Example 56 with ResourceException

use of org.restlet.resource.ResourceException in project qi4j-sdk by Qi4j.

the class SPARQLResource method get.

@Override
public Representation get(final Variant variant) throws ResourceException {
    try {
        // TODO There's probably a helper somewhere that can do this more nicely
        if (getRequest().getOriginalRef().getLastSegment().equals("sparqlhtml.xsl")) {
            InputStream resourceAsStream = getClass().getResourceAsStream("sparqlhtml.xsl");
            return new InputRepresentation(resourceAsStream, MediaType.TEXT_XML);
        }
        Form form;
        if (getRequest().getMethod().equals(Method.POST)) {
            form = new Form(getRequest().getEntity());
        } else {
            form = getRequest().getResourceRef().getQueryAsForm();
        }
        final RepositoryConnection conn = repository.getConnection();
        String queryStr = form.getFirstValue("query");
        if (queryStr == null) {
            InputStream resourceAsStream = getClass().getResourceAsStream("sparqlform.html");
            return new InputRepresentation(resourceAsStream, MediaType.TEXT_HTML);
        }
        Query query = getQuery(repository, conn, queryStr);
        if (query instanceof TupleQuery) {
            TupleQuery tQuery = (TupleQuery) query;
            final TupleQueryResult queryResult = tQuery.evaluate();
            if (variant.getMediaType().equals(MediaType.TEXT_HTML)) {
                return new OutputRepresentation(MediaType.TEXT_XML) {

                    @Override
                    public void write(OutputStream outputStream) throws IOException {
                        try {
                            PrintWriter out = new PrintWriter(outputStream);
                            out.println("<?xml version='1.0' encoding='UTF-8'?>");
                            out.println("<?xml-stylesheet type=\"text/xsl\" href=\"query/sparqlhtml.xsl\"?>");
                            out.flush();
                            TupleQueryResultWriter qrWriter = new SPARQLResultsXMLWriter(new XMLWriter(outputStream) {

                                @Override
                                public void startDocument() throws IOException {
                                // Ignore
                                }
                            });
                            QueryResultUtil.report(queryResult, qrWriter);
                        } catch (Exception e) {
                            throw new IOException(e);
                        } finally {
                            try {
                                conn.close();
                            } catch (RepositoryException e) {
                            // Ignore
                            }
                        }
                    }
                };
            } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML)) {
                return new OutputRepresentation(MediaType.APPLICATION_XML) {

                    @Override
                    public void write(OutputStream outputStream) throws IOException {
                        try {
                            TupleQueryResultWriter qrWriter = new SPARQLResultsXMLWriter(new XMLWriter(outputStream));
                            QueryResultUtil.report(queryResult, qrWriter);
                        } catch (Exception e) {
                            throw new IOException(e);
                        } finally {
                            try {
                                conn.close();
                            } catch (RepositoryException e) {
                            // Ignore
                            }
                        }
                    }
                };
            } else if (variant.getMediaType().equals(RestApplication.APPLICATION_SPARQL_JSON)) {
                return new OutputRepresentation(RestApplication.APPLICATION_SPARQL_JSON) {

                    @Override
                    public void write(OutputStream outputStream) throws IOException {
                        try {
                            TupleQueryResultWriter qrWriter = new SPARQLResultsJSONWriterFactory().getWriter(outputStream);
                            QueryResultUtil.report(queryResult, qrWriter);
                        } catch (Exception e) {
                            throw new IOException(e);
                        } finally {
                            try {
                                conn.close();
                            } catch (RepositoryException e) {
                            // Ignore
                            }
                        }
                    }
                };
            }
        } else if (query instanceof GraphQuery) {
            GraphQuery gQuery = (GraphQuery) query;
            /*
                                queryResult = gQuery.evaluate();
                                registry = RDFWriterRegistry.getInstance();
                                view = GraphQueryResultView.getInstance();
                */
            conn.close();
        } else if (query instanceof BooleanQuery) {
            BooleanQuery bQuery = (BooleanQuery) query;
            /*
                                queryResult = bQuery.evaluate();
                                registry = BooleanQueryResultWriterRegistry.getInstance();
                                view = BooleanQueryResultView.getInstance();
                */
            conn.close();
        } else {
            conn.close();
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported query type: " + query.getClass().getName());
        }
    } catch (RepositoryException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    } catch (QueryEvaluationException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    }
    return null;
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TupleQueryResultWriter(org.openrdf.query.resultio.TupleQueryResultWriter) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) XMLWriter(info.aduna.xml.XMLWriter) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) RepositoryException(org.openrdf.repository.RepositoryException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) SPARQLResultsJSONWriterFactory(org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriterFactory) OutputRepresentation(org.restlet.representation.OutputRepresentation) ResourceException(org.restlet.resource.ResourceException) PrintWriter(java.io.PrintWriter)

Example 57 with ResourceException

use of org.restlet.resource.ResourceException in project vcell by virtualcell.

the class OptimizationServerResource method get_json.

@Override
public JsonRepresentation get_json() {
    try {
        // VCellApiApplication application = ((VCellApiApplication)getApplication());
        // User vcellUser = application.getVCellUser(getChallengeResponse(),AuthenticationPolicy.prohibitInvalidCredentials);
        String optimizationId = (String) getRequestAttributes().get(VCellApiApplication.OPTIMIZATIONID);
        VCellOptClient optClient = new VCellOptClient("opt", 8080);
        String optRunString = optClient.getOptRunJson(optimizationId);
        JsonRepresentation optRunJsonRep = new JsonRepresentation(optRunString);
        return optRunJsonRep;
    // } catch (PermissionException e) {
    // e.printStackTrace();
    // throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : VCellOptClient(org.vcell.api.client.VCellOptClient) ResourceException(org.restlet.resource.ResourceException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceException(org.restlet.resource.ResourceException)

Example 58 with ResourceException

use of org.restlet.resource.ResourceException in project vcell by virtualcell.

the class PublicationsServerResource method getPublicationRepresentations.

private PublicationRepresentation[] getPublicationRepresentations(User vcellUser) {
    ArrayList<PublicationRepresentation> publicationRepresentations = new ArrayList<PublicationRepresentation>();
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        PublicationRep[] publicationReps = restDatabaseService.query(this, vcellUser);
        for (PublicationRep publicationRep : publicationReps) {
            PublicationRepresentation publicationRepresentation = new PublicationRepresentation(publicationRep);
            publicationRepresentations.add(publicationRepresentation);
        }
    } catch (PermissionException ee) {
        ee.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized");
    } catch (DataAccessException | SQLException | ExpressionException e) {
        e.printStackTrace();
        throw new RuntimeException("failed to retrieve biomodels from VCell Database : " + e.getMessage());
    }
    return publicationRepresentations.toArray(new PublicationRepresentation[0]);
}
Also used : PermissionException(org.vcell.util.PermissionException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ExpressionException(cbit.vcell.parser.ExpressionException) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) PublicationRep(cbit.vcell.modeldb.PublicationRep) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) DataAccessException(org.vcell.util.DataAccessException)

Example 59 with ResourceException

use of org.restlet.resource.ResourceException in project vcell by virtualcell.

the class SimDataServerResource method getSimDataRepresentation.

private SimDataRepresentation getSimDataRepresentation(User vcellUser) {
    // if (!application.authenticate(getRequest(), getResponse())){
    // // not authenticated
    // return new SimulationTaskRepresentation[0];
    // }else{
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        DataSetMetadata dataSetMetadata = restDatabaseService.getDataSetMetadata(this, vcellUser);
        SimulationRep simRep = restDatabaseService.getSimulationRep(dataSetMetadata.getSimKey());
        SimDataRepresentation simDataRepresentation = new SimDataRepresentation(dataSetMetadata, simRep.getScanCount());
        return simDataRepresentation;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "simulation metadata not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
// }
}
Also used : PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) SimDataRepresentation(org.vcell.rest.common.SimDataRepresentation) ResourceException(org.restlet.resource.ResourceException) DataSetMetadata(cbit.vcell.simdata.DataSetMetadata) SimulationRep(cbit.vcell.modeldb.SimulationRep) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

Example 60 with ResourceException

use of org.restlet.resource.ResourceException in project vcell by virtualcell.

the class BiomodelSBMLServerResource method getBiomodelSBML.

private String getBiomodelSBML(User vcellUser) {
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        // Make temporary resource compatible with restDatabaseService so we can re-use
        BiomodelVCMLServerResource bmsr = new BiomodelVCMLServerResource() {

            @Override
            public Map<String, Object> getRequestAttributes() {
                HashMap<String, Object> hashMap = new HashMap<String, Object>();
                hashMap.put(VCellApiApplication.BIOMODELID, BiomodelSBMLServerResource.this.biomodelid);
                return hashMap;
            }

            @Override
            public Request getRequest() {
                // TODO Auto-generated method stub
                return BiomodelSBMLServerResource.this.getRequest();
            }
        };
        String biomodelVCML = restDatabaseService.query(bmsr, vcellUser);
        BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(biomodelVCML));
        // public SBMLExporter(BioModel argBioModel, int argSbmlLevel, int argSbmlVersion, boolean isSpatial) {
        SimulationContext simulationContext = null;
        if (appName != null) {
            simulationContext = bioModel.getSimulationContext(appName);
        } else {
            simulationContext = bioModel.getSimulationContext(0);
        }
        SBMLExporter sbmlExporter = new SBMLExporter(simulationContext, 3, 1, simulationContext.getGeometryContext().getGeometry().getDimension() > 0);
        return sbmlExporter.getSBMLString();
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) HashMap(java.util.HashMap) SBMLExporter(org.vcell.sbml.vcell.SBMLExporter) SimulationContext(cbit.vcell.mapping.SimulationContext) PermissionException(org.vcell.util.PermissionException) ResourceException(org.restlet.resource.ResourceException) BioModel(cbit.vcell.biomodel.BioModel) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) XMLSource(cbit.vcell.xml.XMLSource)

Aggregations

ResourceException (org.restlet.resource.ResourceException)70 Representation (org.restlet.representation.Representation)20 PermissionException (org.vcell.util.PermissionException)20 VCellApiApplication (org.vcell.rest.VCellApiApplication)18 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)15 IOException (java.io.IOException)13 StringRepresentation (org.restlet.representation.StringRepresentation)12 ArrayList (java.util.ArrayList)10 Reference (org.restlet.data.Reference)8 BioModel (cbit.vcell.biomodel.BioModel)7 Writer (java.io.Writer)7 HashMap (java.util.HashMap)7 JSONObject (org.json.JSONObject)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)7 XMLSource (cbit.vcell.xml.XMLSource)6 Response (org.restlet.Response)6 User (org.vcell.util.document.User)6 EntitlementException (com.sun.identity.entitlement.EntitlementException)4 EntityReference (org.qi4j.api.entity.EntityReference)4 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)4