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