Search in sources :

Example 71 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class BiomodelSimulationStartServerResource method start.

@Override
public Representation start() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
    RestDatabaseService restDatabaseService = application.getRestDatabaseService();
    try {
        SimulationRep simRep = restDatabaseService.startSimulation(this, vcellUser);
        Representation representation = new StringRepresentation("simulation started", MediaType.TEXT_PLAIN);
        redirectSeeOther("/" + VCellApiApplication.SIMTASK + "?" + SimulationTasksServerResource.PARAM_SIM_ID + "=" + simRep.getKey().toString() + "&" + SimulationTasksServerResource.PARAM_STATUS_COMPLETED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_DISPATCHED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_FAILED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_QUEUED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_RUNNING + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_STOPPED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_WAITING + "=on" + "&" + SimulationTasksServerResource.PARAM_START_ROW + "=1" + "&" + SimulationTasksServerResource.PARAM_MAX_ROWS + "=" + Integer.toString(simRep.getScanCount() * 4));
        return representation;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized to start simulation");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "simulation not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) User(org.vcell.util.document.User) StringRepresentation(org.restlet.representation.StringRepresentation) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) ResourceException(org.restlet.resource.ResourceException) SimulationRep(cbit.vcell.modeldb.SimulationRep) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

Example 72 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class BiomodelSimulationStopServerResource method stop.

@Override
public Representation stop() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
    RestDatabaseService restDatabaseService = application.getRestDatabaseService();
    try {
        SimulationRep simRep = restDatabaseService.stopSimulation(this, vcellUser);
        Representation representation = new StringRepresentation("simulation stopped", MediaType.TEXT_PLAIN);
        redirectSeeOther("/" + VCellApiApplication.SIMTASK + "?" + SimulationTasksServerResource.PARAM_SIM_ID + "=" + simRep.getKey().toString() + "&" + SimulationTasksServerResource.PARAM_STATUS_COMPLETED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_DISPATCHED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_FAILED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_QUEUED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_RUNNING + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_STOPPED + "=on" + "&" + SimulationTasksServerResource.PARAM_STATUS_WAITING + "=on" + "&" + SimulationTasksServerResource.PARAM_START_ROW + "=1" + "&" + SimulationTasksServerResource.PARAM_MAX_ROWS + "=" + Integer.toString(simRep.getScanCount() * 4));
        return representation;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized to stop simulation");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "simulation not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) User(org.vcell.util.document.User) StringRepresentation(org.restlet.representation.StringRepresentation) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) ResourceException(org.restlet.resource.ResourceException) SimulationRep(cbit.vcell.modeldb.SimulationRep) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

Example 73 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class BiomodelsServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    BiomodelRepresentation[] biomodels = getBiomodelRepresentations(vcellUser);
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+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());
    }
    dataModel.put("userId", getAttribute(PARAM_USER));
    dataModel.put("bmName", getQueryValue(PARAM_BM_NAME));
    dataModel.put("bmId", getQueryValue(PARAM_BM_ID));
    dataModel.put("savedLow", getLongQueryValue(PARAM_SAVED_LOW));
    dataModel.put("savedHigh", getLongQueryValue(PARAM_SAVED_HIGH));
    dataModel.put("ownerName", getQueryValue(PARAM_BM_OWNER));
    dataModel.put("category", getQueryValue(PARAM_CATEGORY));
    dataModel.put("orderBy", getQueryValue(PARAM_ORDERBY));
    Long startRowParam = getLongQueryValue(PARAM_START_ROW);
    if (startRowParam != null) {
        dataModel.put("startRow", startRowParam);
    } else {
        dataModel.put("startRow", 1);
    }
    Long maxRowsParam = getLongQueryValue(PARAM_MAX_ROWS);
    if (maxRowsParam != null) {
        dataModel.put("maxRows", maxRowsParam);
    } else {
        dataModel.put("maxRows", 10);
    }
    dataModel.put("biomodels", Arrays.asList(biomodels));
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(biomodels));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/biomodels.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : BiomodelRepresentation(org.vcell.rest.common.BiomodelRepresentation) 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) BiomodelRepresentation(org.vcell.rest.common.BiomodelRepresentation) Representation(org.restlet.representation.Representation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 74 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class OptimizationRunServerResource method run.

@Override
@Post
public Representation run(Representation optProblemJson) throws JSONException {
    try {
        VCellApiApplication application = ((VCellApiApplication) getApplication());
        // User vcellUser = application.getVCellUser(getChallengeResponse(),AuthenticationPolicy.ignoreInvalidCredentials);
        if (optProblemJson != null && optProblemJson.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) {
            JsonRepresentation jsonRep = new JsonRepresentation(optProblemJson);
            JSONObject json = jsonRep.getJsonObject();
            System.out.println(json);
            TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
            OptProblem optProblem = new OptProblem();
            deserializer.deserialize(optProblem, json.toString().getBytes());
            String optimizationId = application.getOptServerImpl().submit(optProblem);
            String redirectURL = "/" + VCellApiApplication.OPTIMIZATION + "/" + optimizationId;
            System.out.println("should be redirected to " + redirectURL + " but leaving as client responsibility for now to create new url");
            getResponse().setLocationRef(redirectURL);
            getResponse().setStatus(Status.SUCCESS_ACCEPTED);
            Representation representation = new StringRepresentation(optimizationId, MediaType.TEXT_PLAIN);
            return representation;
        } else {
            throw new RuntimeException("unexpected post representation " + optProblemJson);
        }
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized to submit optimization");
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) TDeserializer(org.apache.thrift.TDeserializer) OptProblem(org.vcell.optimization.thrift.OptProblem) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) PermissionException(org.vcell.util.PermissionException) JSONException(org.json.JSONException) ResourceException(org.restlet.resource.ResourceException) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Post(org.restlet.resource.Post)

Example 75 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class OptimizationServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    OptRun optRun = getOptRun(vcellUser);
    if (optRun == null) {
        throw new RuntimeException("optimization not found");
    }
    try {
        Map<String, Object> dataModel = new HashMap<String, Object>();
        // +"?"+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());
        }
        dataModel.put("optId", getQueryValue(VCellApiApplication.OPTIMIZATIONID));
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optRunJson = new String(serializer.serialize(optRun));
        dataModel.put("optimization", new JSONObject(optRunJson));
        dataModel.put("jsonResponse", optRunJson);
        Configuration templateConfiguration = application.getTemplateConfiguration();
        Representation formFtl = new ClientResource(LocalReference.createClapReference("/optimization.ftl")).get();
        TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
        return templateRepresentation;
    } catch (Exception e) {
        e.printStackTrace();
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException) TSerializer(org.apache.thrift.TSerializer) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) JSONObject(org.json.JSONObject) OptRun(org.vcell.optimization.thrift.OptRun) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource) JSONObject(org.json.JSONObject) ResourceException(org.restlet.resource.ResourceException)

Aggregations

Representation (org.restlet.representation.Representation)101 HashMap (java.util.HashMap)28 Test (org.testng.annotations.Test)27 StringRepresentation (org.restlet.representation.StringRepresentation)24 Request (org.restlet.Request)23 Response (org.restlet.Response)23 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)23 ResourceException (org.restlet.resource.ResourceException)21 Reference (org.restlet.data.Reference)19 StringWriter (java.io.StringWriter)17 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)16 IOException (java.io.IOException)14 Map (java.util.Map)14 Form (org.restlet.data.Form)14 VCellApiApplication (org.vcell.rest.VCellApiApplication)14 User (org.vcell.util.document.User)13 Configuration (freemarker.template.Configuration)10 StringReader (java.io.StringReader)10 ZNRecord (org.apache.helix.ZNRecord)10 TypeReference (org.codehaus.jackson.type.TypeReference)10