Search in sources :

Example 96 with StringRepresentation

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

the class PublicationServerResource method handleForm.

@Post
public Representation handleForm(Representation entity) {
    try {
        String myHost = getHostRef().getHostDomain();
        String allowedPublicationHost = PropertyLoader.getProperty(PropertyLoader.vcellPublicationHost, "");
        if (!allowedPublicationHost.equals(myHost)) {
            throw new PermissionException("Host '" + myHost + "' not allowed to edit publications");
        }
        Form form = new Form(entity);
        VCellApiApplication application = ((VCellApiApplication) getApplication());
        User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
        if (vcellUser == null) {
            throw new PermissionException("must be authenticated to edit publication info");
        }
        // The form contains input with names "user" and "password"
        String pubop = form.getFirstValue("pubop");
        // String password = form.getFirstValue("password");
        Map<String, Object> dataModel = new HashMap<String, Object>();
        if (pubop == null) {
            return new StringRepresentation(("value of publication 'operation' cannot be null"));
        } else if (pubop.equals("editNew")) {
            PublicationRepresentation value = new PublicationRepresentation();
            value.pubKey = AUTOMATICALLY_GENERATED;
            dataModel.put("publicationRepr", value);
        } else if (pubop.equals("editWithKey")) {
            Object pubidObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
            PublicationRepresentation publication = getPublicationRepresentation(((VCellApiApplication) getApplication()).getRestDatabaseService(), vcellUser, new KeyValue(pubidObj.toString()));
            dataModel.put("publicationRepr", publication);
        } else if (pubop.equals("applyEdit")) {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM/dd/yyyy", java.util.Locale.US);
            // PublicationRepresentation publication = getPublicationRepresentation(((VCellApiApplication)getApplication()).getRestDatabaseService(),vcellUser,new KeyValue(form.getFirstValue("pubid")));
            String[] authors = StringUtils.split(form.getFirstValue("authors"), ";");
            for (int i = 0; i < authors.length; i++) {
                authors[i] = authors[i].trim();
            }
            String thePubID = form.getFirstValue("pubId");
            BioModelReferenceRep[] bioModelReferenceReps = (BioModelReferenceRep[]) convertToReferenceRep(form.getFirstValue("biomodelReferences"), BioModelReferenceRep.class);
            MathModelReferenceRep[] mathModelReferenceReps = (MathModelReferenceRep[]) convertToReferenceRep(form.getFirstValue("mathmodelReferences"), MathModelReferenceRep.class);
            PublicationRep publicationRep = new PublicationRep((thePubID == null || thePubID.equals(AUTOMATICALLY_GENERATED) ? null : new KeyValue(Integer.valueOf(thePubID).toString())), form.getFirstValue("title"), authors, (form.getFirstValue("year") == null || form.getFirstValue("year").trim().length() == 0 ? null : Integer.valueOf(form.getFirstValue("year"))), form.getFirstValue("citation"), form.getFirstValue("pubmedid"), form.getFirstValue("doi"), form.getFirstValue("endnoteid"), form.getFirstValue("url"), bioModelReferenceReps, mathModelReferenceReps, form.getFirstValue("wittid"), (form.getFirstValue("pubdate") == null || form.getFirstValue("pubdate").trim().length() == 0 ? null : sdf.parse(form.getFirstValue("pubdate"))));
            KeyValue savedOrEditedPubID = ((VCellApiApplication) getApplication()).getRestDatabaseService().savePublicationRep(publicationRep, vcellUser);
            // String address = getRequest().getClientInfo().getAddress();
            // int port = getRequest().getClientInfo().getPort();
            StringRepresentation s = getPubInfoHtml(myHost, savedOrEditedPubID);
            return s;
        } else if (pubop.equals("makepublic")) {
            String[] bmKeys = form.getValuesArray("bmpublic");
            KeyValue[] publishTheseBiomodels = new KeyValue[(bmKeys == null ? 0 : bmKeys.length)];
            for (int i = 0; i < publishTheseBiomodels.length; i++) {
                publishTheseBiomodels[i] = new KeyValue(bmKeys[i]);
            }
            String[] mmKeys = form.getValuesArray("mmpublic");
            KeyValue[] publishTheseMathmodels = new KeyValue[(mmKeys == null ? 0 : mmKeys.length)];
            for (int i = 0; i < publishTheseMathmodels.length; i++) {
                publishTheseMathmodels[i] = new KeyValue(mmKeys[i]);
            }
            if (publishTheseBiomodels.length > 0 || publishTheseMathmodels.length > 0) {
                ((VCellApiApplication) getApplication()).getRestDatabaseService().publishDirectly(publishTheseBiomodels, publishTheseMathmodels, vcellUser);
            }
            Object pubidObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
            StringRepresentation s = getPubInfoHtml(myHost, new KeyValue(pubidObj.toString()));
            return s;
        } else {
            return new StringRepresentation(("value of pubop=" + pubop + " not expected").toCharArray());
        }
        Configuration templateConfiguration = application.getTemplateConfiguration();
        Representation myRepresentation = new ClientResource(LocalReference.createClapReference("/newpublication.ftl")).get();
        TemplateRepresentation templateRepresentation = new TemplateRepresentation(myRepresentation, templateConfiguration, dataModel, MediaType.TEXT_HTML);
        return templateRepresentation;
    } catch (Exception e) {
        return new StringRepresentation(e.getClass().getName() + " " + e.getMessage());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Configuration(freemarker.template.Configuration) Form(org.restlet.data.Form) HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) BioModelReferenceRep(cbit.vcell.modeldb.BioModelReferenceRep) PublicationRep(cbit.vcell.modeldb.PublicationRep) StringRepresentation(org.restlet.representation.StringRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource) MathModelReferenceRep(cbit.vcell.modeldb.MathModelReferenceRep) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) MathmodelReferenceRepresentation(org.vcell.rest.common.MathmodelReferenceRepresentation) BiomodelReferenceRepresentation(org.vcell.rest.common.BiomodelReferenceRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException) SimpleDateFormat(java.text.SimpleDateFormat) Post(org.restlet.resource.Post)

Example 97 with StringRepresentation

use of org.restlet.representation.StringRepresentation 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 98 with StringRepresentation

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

the class BiomodelVCMLServerResource method get_xml.

@Override
@Get(BiomodelVCMLResource.APPLICATION_VCML_XML)
public StringRepresentation get_xml() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    String vcml = getBiomodelVCML(vcellUser);
    if (vcml != null) {
        String bioModelID = (String) getRequestAttributes().get(VCellApiApplication.BIOMODELID);
        // setAttribute("Content-type", "application/vcml+xml");
        setAttribute("Content-Disposition", "attachment; filename=\"VCBioModel_" + bioModelID + ".vcml\"");
        return new StringRepresentation(vcml, BiomodelVCMLResource.VCDOC_MEDIATYPE);
    }
    throw new RuntimeException("biomodel not found");
}
Also used : User(org.vcell.util.document.User) StringRepresentation(org.restlet.representation.StringRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) Get(org.restlet.resource.Get)

Example 99 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project openems by OpenEMS.

the class DeviceNatureRestlet method get.

/**
 * handle HTTP GET request
 *
 * @param thingId
 * @param channelId
 * @return
 */
private Representation get() {
    JsonObject j = new JsonObject();
    for (Class<? extends Thing> clazz : thingRepository.getThingClasses()) {
        if (DeviceNature.class.isAssignableFrom(clazz)) {
            // clazz is a DeviceNature
            Set<Thing> things = thingRepository.getThingsByClass(clazz);
            JsonArray jThings = new JsonArray();
            for (Thing thing : things) {
                jThings.add(thing.id());
            }
            j.add(clazz.getCanonicalName(), jThings);
        }
    }
    return new StringRepresentation(j.toString(), MediaType.APPLICATION_JSON);
}
Also used : JsonArray(com.google.gson.JsonArray) StringRepresentation(org.restlet.representation.StringRepresentation) JsonObject(com.google.gson.JsonObject) Thing(io.openems.api.thing.Thing)

Example 100 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project helix by apache.

the class ZkChildResource method get.

@Override
public Representation get() {
    StringRepresentation presentation = null;
    String zkPath = getZKPath();
    try {
        ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
        ZNRecord result = readZkChild(zkPath, zkClient);
        presentation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(result), MediaType.APPLICATION_JSON);
    } catch (Exception e) {
        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
        LOG.error("Error in read zkPath: " + zkPath, e);
    }
    return presentation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

StringRepresentation (org.restlet.representation.StringRepresentation)130 HttpVerb (com.linkedin.pinot.common.restlet.swagger.HttpVerb)30 Paths (com.linkedin.pinot.common.restlet.swagger.Paths)30 Summary (com.linkedin.pinot.common.restlet.swagger.Summary)30 IOException (java.io.IOException)30 Tags (com.linkedin.pinot.common.restlet.swagger.Tags)29 ZkClient (org.apache.helix.manager.zk.ZkClient)29 JSONObject (org.json.JSONObject)23 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)19 ZNRecord (org.apache.helix.ZNRecord)17 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)17 JSONException (org.json.JSONException)17 Representation (org.restlet.representation.Representation)15 Responses (com.linkedin.pinot.common.restlet.swagger.Responses)14 HelixException (org.apache.helix.HelixException)12 JSONArray (org.json.JSONArray)12 Builder (org.apache.helix.PropertyKey.Builder)11 Get (org.restlet.resource.Get)10 Post (org.restlet.resource.Post)9 ResourceException (org.restlet.resource.ResourceException)9