Search in sources :

Example 1 with VCellApiApplication

use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.

the class AccessTokenServerResource method get_json.

@Override
public AccessTokenRepresentation get_json() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    String clientId = getQueryValue(PARAM_CLIENT_ID);
    String userId = getQueryValue(PARAM_USER_ID);
    String userPassword = getQueryValue(PARAM_USER_PASSWORD);
    try {
        ApiClient apiClient = application.getUserVerifier().getApiClient(clientId);
        if (apiClient == null) {
            throw new RuntimeException("client not found");
        }
        User authenticatedUser = application.getUserVerifier().authenticateUser(userId, userPassword.toCharArray());
        if (authenticatedUser == null) {
            throw new RuntimeException("unable to authenticate user");
        }
        ApiAccessToken apiAccessToken = application.getUserVerifier().generateApiAccessToken(apiClient.getKey(), authenticatedUser);
        AccessTokenRepresentation tokenRep = new AccessTokenRepresentation(apiAccessToken);
        // 
        // indicate no caching of response.
        // 
        ArrayList<CacheDirective> cacheDirectives = new ArrayList<CacheDirective>();
        cacheDirectives.add(CacheDirective.noCache());
        getResponse().setCacheDirectives(cacheDirectives);
        return tokenRep;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : User(org.vcell.util.document.User) CacheDirective(org.restlet.data.CacheDirective) ApiAccessToken(cbit.vcell.modeldb.ApiAccessToken) ArrayList(java.util.ArrayList) VCellApiApplication(org.vcell.rest.VCellApiApplication) ApiClient(cbit.vcell.modeldb.ApiClient) AccessTokenRepresentation(org.vcell.rest.common.AccessTokenRepresentation) ResourceException(org.restlet.resource.ResourceException)

Example 2 with VCellApiApplication

use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.

the class BiomodelBNGLServerResource method getBiomodelBNGL.

private String getBiomodelBNGL(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, BiomodelBNGLServerResource.this.biomodelid);
                return hashMap;
            }

            @Override
            public Request getRequest() {
                // TODO Auto-generated method stub
                return BiomodelBNGLServerResource.this.getRequest();
            }
        };
        StringWriter bnglStringWriter = new StringWriter();
        PrintWriter pw = new PrintWriter(bnglStringWriter);
        String biomodelVCML = restDatabaseService.query(bmsr, vcellUser);
        BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(biomodelVCML));
        SimulationContext chosenSimContext = bioModel.getSimulationContext(0);
        RbmNetworkGenerator.writeBngl(chosenSimContext, pw, false, true);
        String resultString = bnglStringWriter.toString();
        return resultString;
    } 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) SimulationContext(cbit.vcell.mapping.SimulationContext) PermissionException(org.vcell.util.PermissionException) ResourceException(org.restlet.resource.ResourceException) StringWriter(java.io.StringWriter) BioModel(cbit.vcell.biomodel.BioModel) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) XMLSource(cbit.vcell.xml.XMLSource) PrintWriter(java.io.PrintWriter)

Example 3 with VCellApiApplication

use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.

the class BiomodelSimulationServerResource method get_json.

@Override
public SimulationRepresentation get_json() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
    SimulationRepresentation simulationRep = getBiomodelSimulationRepresentation(vcellUser);
    if (simulationRep != null) {
        return simulationRep;
    }
    throw new RuntimeException("simulation not found");
}
Also used : User(org.vcell.util.document.User) VCellApiApplication(org.vcell.rest.VCellApiApplication) SimulationRepresentation(org.vcell.rest.common.SimulationRepresentation)

Example 4 with VCellApiApplication

use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.

the class BiomodelsServerResource method get_json.

@Override
public BiomodelRepresentation[] get_json() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
    BiomodelRepresentation[] bmReps = new BiomodelRepresentation[0];
    try {
        bmReps = getBiomodelRepresentations(vcellUser);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bmReps;
}
Also used : BiomodelRepresentation(org.vcell.rest.common.BiomodelRepresentation) User(org.vcell.util.document.User) VCellApiApplication(org.vcell.rest.VCellApiApplication) ParseException(java.text.ParseException)

Example 5 with VCellApiApplication

use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.

the class HealthRestlet method handle.

@Override
public void handle(Request req, Response response) {
    if (req.getMethod().equals(Method.GET)) {
        try {
            VCellApiApplication application = ((VCellApiApplication) getApplication());
            HttpRequest request = (HttpRequest) req;
            Form form = request.getResourceRef().getQueryAsForm();
            String requestTypeString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK, true);
            RequestType requestType = null;
            if (requestTypeString != null) {
                if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_LOGIN)) {
                    requestType = RequestType.login;
                } else if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_SIM)) {
                    requestType = RequestType.sim;
                } else if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_ALL)) {
                    requestType = RequestType.all;
                }
            }
            if (requestType == null) {
                throw new RuntimeException("expecting /" + VCellApiApplication.HEALTH + "?" + VCellApiApplication.HEALTH_CHECK + "=" + "(" + VCellApiApplication.HEALTH_CHECK_LOGIN + "|" + VCellApiApplication.HEALTH_CHECK_SIM + "|" + VCellApiApplication.HEALTH_CHECK_ALL + ")" + "[&" + VCellApiApplication.HEALTH_CHECK_ALL_START_TIMESTAMP + "=<start_ms>]" + "[&" + VCellApiApplication.HEALTH_CHECK_ALL_END_TIMESTAMP + "=<end_ms>]" + "[&" + VCellApiApplication.HEALTH_CHECK_STATUS_TIMESTAMP + "=<status_ms>]");
            }
            // defaults to current status
            long status_timestamp = System.currentTimeMillis();
            String statusTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_STATUS_TIMESTAMP, true);
            if (statusTimestampString != null) {
                status_timestamp = Long.parseLong(statusTimestampString);
            }
            // defaults to returning logs (all option) starting two hours ago
            long start_timestamp = System.currentTimeMillis() - (2 * 60 * 60 * 1000);
            String startTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_ALL_START_TIMESTAMP, true);
            if (startTimestampString != null) {
                start_timestamp = Long.parseLong(startTimestampString);
            }
            // defaults to returning logs (all option) up to present time
            long end_timestamp = System.currentTimeMillis();
            String endTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_ALL_END_TIMESTAMP, true);
            if (endTimestampString != null) {
                end_timestamp = Long.parseLong(endTimestampString);
            }
            HealthService healthService = application.getHealthService();
            switch(requestType) {
                case all:
                    {
                        HealthEvent[] events = healthService.query(start_timestamp, end_timestamp);
                        ArrayUtils.reverse(events);
                        Gson gson = new Gson();
                        String healthEventsJSON = gson.toJson(events);
                        response.setStatus(Status.SUCCESS_OK);
                        response.setEntity(new JsonRepresentation(healthEventsJSON));
                        break;
                    }
                case login:
                    {
                        NagiosStatus nagiosStatus = healthService.getLoginStatus(status_timestamp);
                        Gson gson = new Gson();
                        String statusJSON = gson.toJson(nagiosStatus);
                        response.setStatus(Status.SUCCESS_OK);
                        response.setEntity(new JsonRepresentation(statusJSON));
                        break;
                    }
                case sim:
                    {
                        NagiosStatus nagiosStatus = healthService.getRunsimStatus(status_timestamp);
                        Gson gson = new Gson();
                        String statusJSON = gson.toJson(nagiosStatus);
                        response.setStatus(Status.SUCCESS_OK);
                        response.setEntity(new JsonRepresentation(statusJSON));
                        break;
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(Status.SERVER_ERROR_INTERNAL);
            response.setEntity("failed to retrieve system health: " + e.getMessage(), MediaType.TEXT_PLAIN);
        }
    }
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) Form(org.restlet.data.Form) VCellApiApplication(org.vcell.rest.VCellApiApplication) Gson(com.google.gson.Gson) NagiosStatus(org.vcell.rest.health.HealthService.NagiosStatus) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Aggregations

VCellApiApplication (org.vcell.rest.VCellApiApplication)53 User (org.vcell.util.document.User)34 ResourceException (org.restlet.resource.ResourceException)21 PermissionException (org.vcell.util.PermissionException)20 Gson (com.google.gson.Gson)14 Representation (org.restlet.representation.Representation)14 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)14 HashMap (java.util.HashMap)13 Configuration (freemarker.template.Configuration)12 TemplateRepresentation (org.restlet.ext.freemarker.TemplateRepresentation)11 ClientResource (org.restlet.resource.ClientResource)11 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)9 Form (org.restlet.data.Form)8 StringRepresentation (org.restlet.representation.StringRepresentation)8 BioModel (cbit.vcell.biomodel.BioModel)6 XMLSource (cbit.vcell.xml.XMLSource)6 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 BiomodelRepresentation (org.vcell.rest.common.BiomodelRepresentation)6 PublicationRepresentation (org.vcell.rest.common.PublicationRepresentation)6