Search in sources :

Example 51 with VCellApiApplication

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

the class BiomodelSimulationServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    SimulationRepresentation simulation = getBiomodelSimulationRepresentation(vcellUser);
    if (simulation == null) {
        throw new RuntimeException("simulation not found");
    }
    Map<String, Object> dataModel = new HashMap<String, Object>();
    dataModel.put("simulation", simulation);
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(simulation));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/simulation.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) SimulationRepresentation(org.vcell.rest.common.SimulationRepresentation) Representation(org.restlet.representation.Representation) SimulationRepresentation(org.vcell.rest.common.SimulationRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 52 with VCellApiApplication

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

the class AdminJobsRestlet method handle.

@Override
public void handle(Request req, Response response) {
    if (req.getMethod().equals(Method.GET)) {
        try {
            VCellApiApplication application = ((VCellApiApplication) getApplication());
            // User vcellUser = application.getVCellUser(req.getChallengeResponse(),AuthenticationPolicy.prohibitInvalidCredentials);
            // User adminUser = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
            // if (!vcellUser.equals(adminUser)) {
            // getLogger().severe("AdminJobsRestlet: user '"+vcellUser.toString()+"' is not authorized");
            // response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
            // response.setEntity("not authorized for this service", MediaType.TEXT_PLAIN);
            // return;
            // }
            HttpRequest request = (HttpRequest) req;
            Form form = request.getResourceRef().getQueryAsForm();
            String jsonQuery = form.getFirstValue(PARAM_JSON_QUERY, true);
            Gson gson = new Gson();
            SimpleJobStatusQuerySpec querySpec = new SimpleJobStatusQuerySpec();
            if (jsonQuery != null) {
                querySpec = gson.fromJson(jsonQuery, SimpleJobStatusQuerySpec.class);
            } else {
                querySpec.submitLowMS = getLongParam(form, PARAM_SUBMIT_LOW, null);
                querySpec.submitHighMS = getLongParam(form, PARAM_SUBMIT_HIGH, null);
                querySpec.startLowMS = getLongParam(form, PARAM_START_LOW, null);
                querySpec.startHighMS = getLongParam(form, PARAM_START_HIGH, null);
                querySpec.endLowMS = getLongParam(form, PARAM_END_LOW, null);
                querySpec.endHighMS = getLongParam(form, PARAM_END_HIGH, null);
                querySpec.startRow = getIntegerParam(form, PARAM_START_ROW, 0);
                querySpec.maxRows = getIntegerParam(form, PARAM_MAX_ROWS, 100);
                querySpec.serverId = getStringParam(form, PARAM_SERVERID, null);
                querySpec.computeHost = getStringParam(form, PARAM_COMPUTEHOST, null);
                querySpec.userid = getStringParam(form, PARAM_USERID, null);
                querySpec.simId = getLongParam(form, PARAM_SIMID, null);
                querySpec.jobId = getLongParam(form, PARAM_JOBID, null);
                querySpec.taskId = getLongParam(form, PARAM_TASKID, null);
                querySpec.hasData = getBooleanParam(form, PARAM_HAS_DATA, null);
                querySpec.waiting = getBooleanParam(form, PARAM_STATUS_WAITING, true);
                querySpec.queued = getBooleanParam(form, PARAM_STATUS_QUEUED, true);
                querySpec.dispatched = getBooleanParam(form, PARAM_STATUS_DISPATCHED, true);
                querySpec.running = getBooleanParam(form, PARAM_STATUS_RUNNING, true);
                querySpec.completed = getBooleanParam(form, PARAM_STATUS_COMPLETED, true);
                querySpec.failed = getBooleanParam(form, PARAM_STATUS_FAILED, true);
                querySpec.stopped = getBooleanParam(form, PARAM_STATUS_STOPPED, true);
            }
            if (querySpec.serverId != null) {
                querySpec.serverId = querySpec.serverId.toLowerCase();
            }
            AdminService adminService = application.getAdminService();
            SimulationJobStatus[] jobStatusArray = adminService.query(querySpec);
            SimpleJobStatusRepresentation[] reps = new SimpleJobStatusRepresentation[jobStatusArray.length];
            for (int i = 0; i < jobStatusArray.length; i++) {
                reps[i] = jobStatusArray[i].toRep();
            }
            String jobStatusArrayJson = gson.toJson(reps);
            response.setStatus(Status.SUCCESS_OK);
            response.setEntity(new JsonRepresentation(jobStatusArrayJson));
        } catch (Exception e) {
            getLogger().severe("failed to retrieve job status: " + e.getMessage());
            e.printStackTrace();
            response.setStatus(Status.SERVER_ERROR_INTERNAL);
            response.setEntity("failed to retrieve job status: " + e.getMessage(), MediaType.TEXT_PLAIN);
        }
    }
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) SimpleJobStatusQuerySpec(cbit.vcell.server.SimpleJobStatusQuerySpec) SimpleJobStatusRepresentation(org.vcell.api.common.SimpleJobStatusRepresentation) Form(org.restlet.data.Form) Gson(com.google.gson.Gson) SimulationJobStatus(cbit.vcell.server.SimulationJobStatus) VCellApiApplication(org.vcell.rest.VCellApiApplication) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Example 53 with VCellApiApplication

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

the class AuthenticationTokenRestlet 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 userId = form.getFirstValue(PARAM_USER_ID, false);
            if (userId == null) {
                throw new RuntimeException("expecting " + PARAM_USER_ID + " query parameter");
            }
            String clientId = form.getFirstValue(PARAM_CLIENT_ID, false);
            if (clientId == null) {
                throw new RuntimeException("expecting " + PARAM_CLIENT_ID + " query parameter");
            }
            String userPassword = form.getFirstValue(PARAM_USER_PASSWORD, false);
            if (userPassword == null) {
                throw new RuntimeException("expecting " + PARAM_USER_PASSWORD + " query parameter");
            }
            ApiClient apiClient = application.getUserVerifier().getApiClient(clientId);
            if (apiClient == null) {
                if (lg.isWarnEnabled())
                    lg.warn("client not found");
                response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
                response.setEntity("authentication error, unknown client", MediaType.TEXT_PLAIN);
                return;
            }
            User authenticatedUser = application.getUserVerifier().authenticateUser(userId, userPassword.toCharArray());
            if (authenticatedUser == null) {
                if (lg.isWarnEnabled())
                    lg.warn("unable to authenticate user");
                response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
                response.setEntity("authentication error, either userid or password is incorrect", MediaType.TEXT_PLAIN);
                return;
            }
            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());
            response.setCacheDirectives(cacheDirectives);
            Gson gson = new Gson();
            String tokenRepJSON = gson.toJson(tokenRep);
            response.setStatus(Status.SUCCESS_OK, "authentication token returned");
            response.setEntity(new JsonRepresentation(tokenRepJSON));
        } catch (Exception e) {
            lg.error(e.getMessage(), e);
            response.setStatus(Status.SERVER_ERROR_INTERNAL);
            response.setEntity("internal error returning authentication token: " + e.getMessage(), MediaType.TEXT_PLAIN);
        }
    }
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) User(org.vcell.util.document.User) Form(org.restlet.data.Form) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) ApiClient(cbit.vcell.modeldb.ApiClient) CacheDirective(org.restlet.data.CacheDirective) ApiAccessToken(cbit.vcell.modeldb.ApiAccessToken) VCellApiApplication(org.vcell.rest.VCellApiApplication) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) AccessTokenRepresentation(org.vcell.rest.common.AccessTokenRepresentation)

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