use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class AccessTokenServerResource method get_json.
@Override
public AccessTokenRepresentation get_json() {
VCellApiApplication application = ((VCellApiApplication) getApplication());
String clientId = getQueryValue(PARAM_CLIENT_ID);
String userId = getQueryValue(PARAM_USER_ID);
String userPassword = getQueryValue(PARAM_USER_PASSWORD);
try {
ApiClient apiClient = application.getUserVerifier().getApiClient(clientId);
if (apiClient == null) {
throw new RuntimeException("client not found");
}
User authenticatedUser = application.getUserVerifier().authenticateUser(userId, userPassword.toCharArray());
if (authenticatedUser == null) {
throw new RuntimeException("unable to authenticate user");
}
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());
getResponse().setCacheDirectives(cacheDirectives);
return tokenRep;
} catch (Exception e) {
e.printStackTrace(System.out);
throw new RuntimeException(e.getMessage(), e);
}
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class BiomodelBNGLServerResource method getBiomodelBNGL.
private String getBiomodelBNGL(User vcellUser) {
RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
try {
// Make temporary resource compatible with restDatabaseService so we can re-use
BiomodelVCMLServerResource bmsr = new BiomodelVCMLServerResource() {
@Override
public Map<String, Object> getRequestAttributes() {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put(VCellApiApplication.BIOMODELID, BiomodelBNGLServerResource.this.biomodelid);
return hashMap;
}
@Override
public Request getRequest() {
// TODO Auto-generated method stub
return BiomodelBNGLServerResource.this.getRequest();
}
};
StringWriter bnglStringWriter = new StringWriter();
PrintWriter pw = new PrintWriter(bnglStringWriter);
String biomodelVCML = restDatabaseService.query(bmsr, vcellUser);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(biomodelVCML));
SimulationContext chosenSimContext = bioModel.getSimulationContext(0);
RbmNetworkGenerator.writeBngl(chosenSimContext, pw, false, true);
String resultString = bnglStringWriter.toString();
return resultString;
} catch (PermissionException e) {
e.printStackTrace();
throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
} 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 BiomodelSimulationServerResource method get_json.
@Override
public SimulationRepresentation get_json() {
VCellApiApplication application = ((VCellApiApplication) getApplication());
User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
SimulationRepresentation simulationRep = getBiomodelSimulationRepresentation(vcellUser);
if (simulationRep != null) {
return simulationRep;
}
throw new RuntimeException("simulation not found");
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class BiomodelsServerResource method get_json.
@Override
public BiomodelRepresentation[] get_json() {
VCellApiApplication application = ((VCellApiApplication) getApplication());
User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
BiomodelRepresentation[] bmReps = new BiomodelRepresentation[0];
try {
bmReps = getBiomodelRepresentations(vcellUser);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmReps;
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class HealthRestlet 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 requestTypeString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK, true);
RequestType requestType = null;
if (requestTypeString != null) {
if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_LOGIN)) {
requestType = RequestType.login;
} else if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_SIM)) {
requestType = RequestType.sim;
} else if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_ALL)) {
requestType = RequestType.all;
}
}
if (requestType == null) {
throw new RuntimeException("expecting /" + VCellApiApplication.HEALTH + "?" + VCellApiApplication.HEALTH_CHECK + "=" + "(" + VCellApiApplication.HEALTH_CHECK_LOGIN + "|" + VCellApiApplication.HEALTH_CHECK_SIM + "|" + VCellApiApplication.HEALTH_CHECK_ALL + ")" + "[&" + VCellApiApplication.HEALTH_CHECK_ALL_START_TIMESTAMP + "=<start_ms>]" + "[&" + VCellApiApplication.HEALTH_CHECK_ALL_END_TIMESTAMP + "=<end_ms>]" + "[&" + VCellApiApplication.HEALTH_CHECK_STATUS_TIMESTAMP + "=<status_ms>]");
}
// defaults to current status
long status_timestamp = System.currentTimeMillis();
String statusTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_STATUS_TIMESTAMP, true);
if (statusTimestampString != null) {
status_timestamp = Long.parseLong(statusTimestampString);
}
// defaults to returning logs (all option) starting two hours ago
long start_timestamp = System.currentTimeMillis() - (2 * 60 * 60 * 1000);
String startTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_ALL_START_TIMESTAMP, true);
if (startTimestampString != null) {
start_timestamp = Long.parseLong(startTimestampString);
}
// defaults to returning logs (all option) up to present time
long end_timestamp = System.currentTimeMillis();
String endTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_ALL_END_TIMESTAMP, true);
if (endTimestampString != null) {
end_timestamp = Long.parseLong(endTimestampString);
}
HealthService healthService = application.getHealthService();
switch(requestType) {
case all:
{
HealthEvent[] events = healthService.query(start_timestamp, end_timestamp);
ArrayUtils.reverse(events);
Gson gson = new Gson();
String healthEventsJSON = gson.toJson(events);
response.setStatus(Status.SUCCESS_OK);
response.setEntity(new JsonRepresentation(healthEventsJSON));
break;
}
case login:
{
NagiosStatus nagiosStatus = healthService.getLoginStatus(status_timestamp);
Gson gson = new Gson();
String statusJSON = gson.toJson(nagiosStatus);
response.setStatus(Status.SUCCESS_OK);
response.setEntity(new JsonRepresentation(statusJSON));
break;
}
case sim:
{
NagiosStatus nagiosStatus = healthService.getRunsimStatus(status_timestamp);
Gson gson = new Gson();
String statusJSON = gson.toJson(nagiosStatus);
response.setStatus(Status.SUCCESS_OK);
response.setEntity(new JsonRepresentation(statusJSON));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
response.setStatus(Status.SERVER_ERROR_INTERNAL);
response.setEntity("failed to retrieve system health: " + e.getMessage(), MediaType.TEXT_PLAIN);
}
}
}
Aggregations