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);
}
}
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;
}
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());
}
// }
}
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());
}
}
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());
}
// }
}
Aggregations