Search in sources :

Example 16 with VCellApiApplication

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

the class NewUserRestlet method handle.

@Override
public void handle(Request request, Response response) {
    if (request.getMethod().equals(Method.POST)) {
        Representation entity = request.getEntity();
        if (entity.getMediaType().equals(MediaType.APPLICATION_JSON)) {
            handleJsonRequest(request, response);
            return;
        }
        String content = request.getEntityAsText();
        System.out.println(content);
        Form form = new Form(entity);
        String userid = form.getFirstValue(VCellApiApplication.NEWUSERID_FORMNAME, "");
        String password1 = form.getFirstValue(VCellApiApplication.NEWPASSWORD1_FORMNAME, "");
        String password2 = form.getFirstValue(VCellApiApplication.NEWPASSWORD2_FORMNAME, "");
        String email = form.getFirstValue(VCellApiApplication.NEWEMAIL_FORMNAME, "");
        String firstName = form.getFirstValue(VCellApiApplication.NEWFIRSTNAME_FORMNAME, "");
        String lastName = form.getFirstValue(VCellApiApplication.NEWLASTNAME_FORMNAME, "");
        String institute = form.getFirstValue(VCellApiApplication.NEWINSTITUTE_FORMNAME, "");
        String country = form.getFirstValue(VCellApiApplication.NEWCOUNTRY_FORMNAME, "");
        String notify = form.getFirstValue(VCellApiApplication.NEWNOTIFY_FORMNAME, "on");
        String formprocessing = form.getFirstValue(VCellApiApplication.NEWFORMPROCESSING_FORMNAME, null);
        Status status = null;
        String errorMessage = "";
        // validate
        if (!password1.equals(password2)) {
            status = Status.CLIENT_ERROR_FORBIDDEN;
            errorMessage = "passwords dont match";
        }
        int MIN_PASSWORD_LENGTH = 5;
        if (password1.length() < MIN_PASSWORD_LENGTH || password1.contains(" ") || password1.contains("'") || password1.contains("\"") || password1.contains(",")) {
            status = Status.CLIENT_ERROR_FORBIDDEN;
            errorMessage = "password must be at least " + MIN_PASSWORD_LENGTH + " characters, and must not contains spaces, commas, or quotes";
        }
        if (email.length() < 4) {
            status = Status.CLIENT_ERROR_FORBIDDEN;
            errorMessage = "valid email required";
        }
        if (userid.length() < 4 || !userid.equals(org.vcell.util.TokenMangler.fixTokenStrict(userid))) {
            status = Status.CLIENT_ERROR_FORBIDDEN;
            errorMessage = "userid must be at least 4 characters and contain only alpha-numeric characters";
        }
        if (errorMessage.length() > 0 && formprocessing != null) {
            Form newform = new Form();
            newform.add(VCellApiApplication.NEWERRORMESSAGE_FORMNAME, errorMessage);
            newform.add(VCellApiApplication.NEWUSERID_FORMNAME, userid);
            newform.add(VCellApiApplication.NEWPASSWORD1_FORMNAME, password1);
            newform.add(VCellApiApplication.NEWPASSWORD2_FORMNAME, password2);
            newform.add(VCellApiApplication.NEWEMAIL_FORMNAME, email);
            newform.add(VCellApiApplication.NEWFIRSTNAME_FORMNAME, firstName);
            newform.add(VCellApiApplication.NEWLASTNAME_FORMNAME, lastName);
            newform.add(VCellApiApplication.NEWINSTITUTE_FORMNAME, institute);
            newform.add(VCellApiApplication.NEWCOUNTRY_FORMNAME, country);
            newform.add(VCellApiApplication.NEWNOTIFY_FORMNAME, notify);
            Reference redirectRef;
            try {
                redirectRef = new Reference(request.getResourceRef().getHostIdentifier() + "/" + VCellApiApplication.REGISTRATIONFORM + "?" + newform.encode());
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage());
            }
            response.redirectSeeOther(redirectRef);
            return;
        }
        // form new UnverifiedUserInfo
        UserInfo newUserInfo = new UserInfo();
        newUserInfo.company = institute;
        newUserInfo.country = country;
        newUserInfo.digestedPassword0 = new DigestedPassword(password1);
        newUserInfo.email = email;
        newUserInfo.wholeName = firstName + " " + lastName;
        newUserInfo.notify = notify.equals("on");
        newUserInfo.title = " ";
        newUserInfo.userid = userid;
        Date submitDate = new Date();
        // one hour
        long timeExpiresMS = 1000 * 60 * 60 * 1;
        Date expirationDate = new Date(System.currentTimeMillis() + timeExpiresMS);
        DigestedPassword emailVerifyToken = new DigestedPassword(Long.toString(System.currentTimeMillis()));
        UnverifiedUser unverifiedUser = new UnverifiedUser(newUserInfo, submitDate, expirationDate, emailVerifyToken.getString());
        // add Unverified UserInfo and send email
        VCellApiApplication vcellApiApplication = (VCellApiApplication) getApplication();
        vcellApiApplication.getUserVerifier().addUnverifiedUser(unverifiedUser);
        try {
            // Send new password to user
            PropertyLoader.loadProperties();
            BeanUtils.sendSMTP(PropertyLoader.getRequiredProperty(PropertyLoader.vcellSMTPHostName), new Integer(PropertyLoader.getRequiredProperty(PropertyLoader.vcellSMTPPort)).intValue(), PropertyLoader.getRequiredProperty(PropertyLoader.vcellSMTPEmailAddress), newUserInfo.email, "new VCell account verification", "You have received this email to verify that a Virtual Cell account has been associated " + "with this email address.  To activate this account, please follow this link: " + request.getResourceRef().getHostIdentifier() + "/" + VCellApiApplication.NEWUSER_VERIFY + "?" + VCellApiApplication.EMAILVERIFYTOKEN_FORMNAME + "=" + emailVerifyToken.getString());
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(Status.SERVER_ERROR_INTERNAL);
            response.setEntity("we failed to send a verification email to " + newUserInfo.email, MediaType.TEXT_PLAIN);
        }
        response.setStatus(Status.SUCCESS_CREATED);
        response.setEntity("we sent you a verification email at " + newUserInfo.email + ", please follow the link in that email", MediaType.TEXT_PLAIN);
    }
}
Also used : Status(org.restlet.data.Status) Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) UserInfo(org.vcell.util.document.UserInfo) IOException(java.io.IOException) DigestedPassword(org.vcell.util.document.UserLoginInfo.DigestedPassword) Date(java.util.Date) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) UseridIDExistsException(org.vcell.util.UseridIDExistsException) SQLException(java.sql.SQLException) VCellApiApplication(org.vcell.rest.VCellApiApplication)

Example 17 with VCellApiApplication

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

the class PublicationsServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    PublicationRepresentation[] publications = getPublicationRepresentations(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("pubId", getQueryValue(PARAM_PUB_ID));
    dataModel.put("orderBy", getQueryValue(PARAM_ORDERBY));
    dataModel.put("publications", Arrays.asList(publications));
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(publications));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/publications.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) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 18 with VCellApiApplication

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

the class OptimizationServerResource method getOptRun.

private OptRun getOptRun(User vcellUser) {
    // }else{
    try {
        String optimizationId = (String) getRequestAttributes().get(VCellApiApplication.OPTIMIZATIONID);
        VCellApiApplication application = ((VCellApiApplication) getApplication());
        OptRunContext optRunContext = application.getOptServerImpl().getOptRunContextByOptimizationId(optimizationId);
        if (optRunContext == null) {
            throw new ObjectNotFoundException("optimization id '" + optimizationId + "' not found");
        }
        switch(optRunContext.getStatus()) {
            case Complete:
                {
                    OptRun optRun = CopasiServicePython.readOptRun(optRunContext.getOptRunBinaryFile());
                    return optRun;
                }
            case Queued:
            case Running:
            case Failed:
                {
                    OptProblem optProblem = CopasiServicePython.readOptProblem(optRunContext.getOptProblemBinaryFile());
                    OptRun optRun = new OptRun();
                    optRun.setOptProblem(optProblem);
                    optRun.setStatus(optRunContext.getStatus());
                    optRun.setStatusMessage(optRunContext.getStatus().name());
                    return optRun;
                }
            default:
                {
                    throw new RuntimeException("unexpected optimization status '" + optRunContext.getStatus() + "'");
                }
        }
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "optimization not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
// }
}
Also used : PermissionException(org.vcell.util.PermissionException) OptRunContext(org.vcell.optimization.OptServerImpl.OptRunContext) OptProblem(org.vcell.optimization.thrift.OptProblem) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) OptRun(org.vcell.optimization.thrift.OptRun) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

Example 19 with VCellApiApplication

use of org.vcell.rest.VCellApiApplication 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)

Example 20 with VCellApiApplication

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

the class PublicationServerResource method getPublicationRepresentation.

private PublicationRepresentation getPublicationRepresentation(User vcellUser) {
    // if (!application.authenticate(getRequest(), getResponse())){
    // // not authenticated
    // return new SimulationTaskRepresentation[0];
    // }else{
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        PublicationRep publicationRep = restDatabaseService.query(this, vcellUser);
        PublicationRepresentation publicationRepresentation = new PublicationRepresentation(publicationRep);
        return publicationRepresentation;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "publication not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
// }
}
Also used : PermissionException(org.vcell.util.PermissionException) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) PublicationRep(cbit.vcell.modeldb.PublicationRep) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

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