use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class BiomodelSimulationStartServerResource method start.
@Override
public Representation start() {
VCellApiApplication application = ((VCellApiApplication) getApplication());
User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
RestDatabaseService restDatabaseService = application.getRestDatabaseService();
try {
SimulationRep simRep = restDatabaseService.startSimulation(this, vcellUser);
Representation representation = new StringRepresentation("simulation started", 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 start 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());
}
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class BiomodelVCMLModelInfoResource method getBiomodel.
private BioModel getBiomodel(KeyValue bmKey, User vcellUser) {
RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
try {
// VCellApiApplication application = ((VCellApiApplication)getApplication());
BigString cachedVcml = restDatabaseService.getBioModelXML(bmKey, vcellUser);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(cachedVcml.toString()));
return bioModel;
} 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, "biomodel 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 BiomodelVCMLModelInfoResource method getBiomodel.
private BioModel getBiomodel(String modelName, User vcellUser) {
RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
try {
// VCellApiApplication application = ((VCellApiApplication)getApplication());
final VCInfoContainer vcInfoContainer = restDatabaseService.getVCInfoContainer(vcellUser);
for (int i = 0; i < vcInfoContainer.getBioModelInfos().length; i++) {
if (vcInfoContainer.getBioModelInfos()[i].getVersion().getName().equals(modelName)) {
BigString cachedVcml = restDatabaseService.getBioModelXML(vcInfoContainer.getBioModelInfos()[i].getVersion().getVersionKey(), vcellUser);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(cachedVcml.toString()));
return bioModel;
}
}
throw new Exception("VCDocument named '" + modelName + "'" + " not found");
} 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, "biomodel 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 EventsRestlet 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);
HttpRequest request = (HttpRequest) req;
Form form = request.getResourceRef().getQueryAsForm();
String beginTimestampString = form.getFirstValue(VCellApiApplication.EVENTS_BEGINTIMESTAMP, true);
if (beginTimestampString == null) {
throw new RuntimeException("expecting " + VCellApiApplication.EVENTS_BEGINTIMESTAMP + " query parameter");
}
long beginTimestamp = Long.parseLong(beginTimestampString);
RestEventService rpcService = application.getEventsService();
EventWrapper[] eventWrappers = rpcService.query(vcellUser.getName(), beginTimestamp);
Gson gson = new Gson();
String eventWrappersJSON = gson.toJson(eventWrappers);
response.setStatus(Status.SUCCESS_OK, "event query succeeded");
response.setEntity(new JsonRepresentation(eventWrappersJSON));
} catch (Exception e) {
getLogger().severe("internal error retrieving events: " + e.getMessage());
e.printStackTrace();
response.setStatus(Status.SERVER_ERROR_INTERNAL);
response.setEntity("internal error retrieving events: " + e.getMessage(), MediaType.TEXT_PLAIN);
}
}
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class RpcRestlet method handle.
@Override
public void handle(Request req, Response response) {
if (req.getMethod().equals(Method.GET)) {
try {
User authuser = null;
HttpRequest request = (HttpRequest) req;
// Use "WWW-Authenticate - Basic" authentication scheme
// Browser takes care of asking user for credentials and sending them
// Must be used with https connection to hide credentials
Header authHeader = request.getHeaders().getFirst("Authorization");
if (authHeader != null) {
// caller included a user and password
String typeAndCredential = authHeader.getValue();
// System.out.println("--"+up);
java.util.StringTokenizer st = new java.util.StringTokenizer(typeAndCredential, " ");
String type = st.nextToken();
String userAndPasswordB64 = st.nextToken();
String s = new String(Base64.getDecoder().decode(userAndPasswordB64));
// System.out.println("type="+type+" decoded="+s);
if (type.equals("Basic")) {
java.util.StringTokenizer st2 = new java.util.StringTokenizer(s, ":");
if (st2.countTokens() == 2) {
String usr = st2.nextToken();
String pw = st2.nextToken();
// System.out.println("user="+usr+" password="+pw);
UserLoginInfo.DigestedPassword dpw = new UserLoginInfo.DigestedPassword(pw);
// System.out.println(dpw);
VCellApiApplication application = ((VCellApiApplication) getApplication());
authuser = application.getUserVerifier().authenticateUser(usr, dpw.getString().toCharArray());
// System.out.println(authuser);
}
}
}
if (authuser == null) {
// Response headers container might be null so add one if necessary
if (((HttpResponse) response).getHeaders() == null) {
((HttpResponse) response).getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, new Series(Header.class));
}
// Tell whoever called us we want a user and password that we will check against admin vcell users
HttpResponse.addHeader(response, "WWW-Authenticate", "Basic");
response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
return;
}
Form form = request.getResourceRef().getQueryAsForm();
if (form.getFirst("stats") != null) {
// get .../rpc?stats=value 'value'
String requestTypeString = form.getFirstValue("stats", true);
if ((authuser.getName().equals("frm") || authuser.getName().equals("les") || authuser.getName().equals("ion") || authuser.getName().equals("danv") || authuser.getName().equals("mblinov") || authuser.getName().equals("ACowan"))) {
String result = restDatabaseService.getBasicStatistics();
response.setStatus(Status.SUCCESS_OK);
response.setEntity(result, MediaType.TEXT_HTML);
return;
}
}
} catch (Exception e) {
String errMesg = "<html><body>Error RpcRestlet.handle(...) req='" + req.toString() + "' <br>err='" + e.getMessage() + "'</br>" + "</body></html>";
getLogger().severe(errMesg);
e.printStackTrace();
response.setStatus(Status.SERVER_ERROR_INTERNAL);
response.setEntity(errMesg, MediaType.TEXT_HTML);
}
} else if (req.getMethod().equals(Method.POST)) {
String destination = null;
String method = null;
try {
VCellApiApplication application = ((VCellApiApplication) getApplication());
User vcellUser = application.getVCellUser(req.getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
HttpRequest request = (HttpRequest) req;
String username = vcellUser.getName();
String userkey = vcellUser.getID().toString();
destination = request.getHeaders().getFirstValue("Destination");
method = request.getHeaders().getFirstValue("Method");
String returnRequired = request.getHeaders().getFirstValue("ReturnRequired");
String timeoutMS = request.getHeaders().getFirstValue("TimeoutMS");
String compressed = request.getHeaders().getFirstValue("Compressed");
String klass = request.getHeaders().getFirstValue("Class");
if (lg.isTraceEnabled()) {
lg.trace("username=" + username + ", userkey=" + userkey + ", destination=" + destination + ", method=" + method + ", returnRequired=" + returnRequired + ", timeoutMS=" + timeoutMS + ", compressed=" + compressed + ", class=" + klass);
}
req.bufferEntity();
Serializable rpcRequestBodyObject = VCellApiClient.fromCompressedSerialized(req.getEntity().getStream());
if (!(rpcRequestBodyObject instanceof VCellApiRpcBody)) {
throw new RuntimeException("expecting post content of type VCellApiRpcBody");
}
VCellApiRpcBody rpcBody = (VCellApiRpcBody) rpcRequestBodyObject;
if (rpcBody.rpcRequest.methodName == null || (User.isGuest(username) && !vcellguestAllowed.contains(rpcBody.rpcRequest.methodName))) {
throw new IllegalArgumentException(User.createGuestErrorMessage(rpcBody.rpcRequest.methodName));
}
RpcServiceType st = null;
VCellQueue queue = null;
switch(rpcBody.rpcRequest.rpcDestination) {
case DataRequestQueue:
{
st = RpcServiceType.DATA;
queue = VCellQueue.DataRequestQueue;
break;
}
case DbRequestQueue:
{
st = RpcServiceType.DB;
queue = VCellQueue.DbRequestQueue;
break;
}
case SimReqQueue:
{
st = RpcServiceType.DISPATCH;
queue = VCellQueue.SimReqQueue;
break;
}
default:
{
throw new RuntimeException("unsupported RPC Destination: " + rpcBody.rpcDestination);
}
}
VCellApiRpcRequest vcellapiRpcRequest = rpcBody.rpcRequest;
Serializable serializableResultObject = null;
/*if(vcellapiRpcRequest.methodName != null && vcellapiRpcRequest.methodName.equals("getVCInfoContainer")) {
serializableResultObject = restDatabaseService.getVCInfoContainer(vcellUser);
}else if(vcellapiRpcRequest.methodName != null && vcellapiRpcRequest.methodName.equals("getBioModelXML")) {
serializableResultObject = restDatabaseService.getBioModelXML((KeyValue)vcellapiRpcRequest.args[1], vcellUser);
}else if(vcellapiRpcRequest.methodName != null && vcellapiRpcRequest.methodName.equals("getMathModelXML")) {
serializableResultObject = restDatabaseService.getMathModelXML((KeyValue)vcellapiRpcRequest.args[1], vcellUser);
}else */
if (vcellapiRpcRequest.methodName != null && vcellapiRpcRequest.methodName.equals("getSimulationStatus")) {
if (vcellapiRpcRequest.args[1] instanceof KeyValue[]) {
serializableResultObject = restDatabaseService.getSimulationStatus((KeyValue[]) vcellapiRpcRequest.args[1], vcellUser);
} else if (vcellapiRpcRequest.args[1] instanceof KeyValue) {
serializableResultObject = restDatabaseService.getSimulationStatus((KeyValue) vcellapiRpcRequest.args[1], vcellUser);
}
} else if (vcellapiRpcRequest.methodName != null && vcellapiRpcRequest.methodName.equals("getSpecialUsers")) {
serializableResultObject = restDatabaseService.getSpecialUsers(vcellUser);
} else {
Object[] arglist = vcellapiRpcRequest.args;
String[] specialProperties = rpcBody.specialProperties;
Object[] specialValues = rpcBody.specialValues;
VCRpcRequest vcRpcRequest = new VCRpcRequest(vcellUser, st, method, arglist);
VCellApiApplication vcellApiApplication = (VCellApiApplication) getApplication();
RpcService rpcService = vcellApiApplication.getRpcService();
serializableResultObject = rpcService.sendRpcMessage(queue, vcRpcRequest, new Boolean(rpcBody.returnedRequired), specialProperties, specialValues, new UserLoginInfo(username, null));
}
byte[] serializedResultObject = VCellApiClient.toCompressedSerialized(serializableResultObject);
response.setStatus(Status.SUCCESS_OK, "rpc method=" + method + " succeeded");
response.setEntity(new ByteArrayRepresentation(serializedResultObject));
} catch (Exception e) {
getLogger().severe("internal error invoking " + destination + ":" + method + "(): " + e.getMessage());
e.printStackTrace();
response.setStatus(Status.SERVER_ERROR_INTERNAL);
if (e.getCause() instanceof ServerRejectedSaveException) {
// send back actual exception, client needs specific cause
try {
byte[] serializedResultObject = VCellApiClient.toCompressedSerialized(e.getCause());
response.setEntity(new ByteArrayRepresentation(serializedResultObject));
return;
} catch (Exception e1) {
e1.printStackTrace();
// continue and send error message
}
}
response.setEntity("internal error invoking " + destination + ":" + method + "(): " + e.getMessage(), MediaType.TEXT_PLAIN);
}
}
}
Aggregations