out.flush();
out.close();
return;
}
Scenario scenario = null;
// ************************************************
// HACK A: if renaming an existing Scenario Name, then
// we need to update this Service's Scenario Name in a
// Service Plan. NOTE:
// * we can't ask the Service to update the Plan because it doesn't
// know about it.
// * we can't ask the Store to update the Plan because it doesn't
// know about the Scenario's 'old' name, only this Servlet does!
// Hence, we do it here via 'newName' and 'oldName'.
// ************************************************
String oldName = null;
String newName = null;
try {
scenario = service.getScenario(new Long(req
.getParameter("scenarioId")));
// ***************** HACK A ****************
oldName = scenario.getScenarioName();
// *****************************************
} catch (Exception e) {
//
}
// CREATE OR UPDATE OF SCENARIO
// If scenario is null, that means we're creating,
// not updating
if (scenario == null) {
scenario = new Scenario();
}
String scenarioName = req.getParameter("scenarioName");
if (scenarioName == null || scenarioName.trim().length() == 0) {
// Let's be nice and make up a name.
scenarioName = "Scenario for " + service.getServiceName()
+ "(name auto-generated)";
}
// ***************** HACK A ****************
newName = scenarioName;
// *****************************************
scenario.setScenarioName(scenarioName);
if (req.getParameter("tag") != null) {
scenario.setTag(req.getParameter("tag"));
}
if (req.getParameter("httpResponseStatusCode") != null) {
try {
String v = req.getParameter("httpResponseStatusCode");
int statusCodeVal = Integer.parseInt(v);
scenario.setHttpResponseStatusCode(statusCodeVal);
} catch (Exception e) {
}
}
if (req.getParameter("httpMethodType") != null) {
try {
String v = req.getParameter("httpMethodType");
scenario.setHttpMethodType(v);
} catch (Exception e) {
}
}
if (req.getParameter("responseHeader") != null) {
String responseHeader = req.getParameter("responseHeader");
if (responseHeader != null) {
scenario.setResponseHeader(responseHeader);
}
}
if (req.getParameter("responseMessage") != null) {
scenario.setResponseMessage(req.getParameter("responseMessage"));
}
if (req.getParameter("matchStringArg") != null) {
scenario.setMatchStringArg(req.getParameter("matchStringArg"));
}
String matchArgAsRegexBoolVal = req
.getParameter("matchStringArgEvaluationRulesFlag");
if (matchArgAsRegexBoolVal != null) {
try {
scenario.setMatchStringArgEvaluationRulesFlag(Boolean
.parseBoolean(matchArgAsRegexBoolVal));
} catch (Exception t) {
logger.error(
"Unable to parse the Scenario match-to-be-used-as-a-regex flag, which should be 'true' or 'false' but was "
+ matchArgAsRegexBoolVal, t);
}
}
// VALIDATION
Map<String, String> errorMap = ScenarioValidator.validate(scenario);
if ((errorMap != null) && (errorMap.size() == 0)) {
// If creating a Scenario, then the returned scenario
// will now have an id. If updating scenario, then
// scenario ID remains the same.
scenario = service.saveOrUpdateScenario(scenario);
// Make this the default 'error response' scenario
// for the service
String v = req.getParameter("errorScenario");
if (v != null && "true".equalsIgnoreCase(v.trim())) {
service.setErrorScenarioId(scenario.getId());
} else if (service.getErrorScenarioId() == scenario.getId()) {
service.setErrorScenarioId(null);
}
// Make this the default universal 'error response',
// for all services defined in Mockey.
v = req.getParameter("universalErrorScenario");
if (v != null && "true".equalsIgnoreCase(v.trim())) {
ScenarioRef scenarioRef = new ScenarioRef(scenario.getId(),
scenario.getServiceId());
store.setUniversalErrorScenarioRef(scenarioRef);
} else if (store.getUniversalErrorScenario() != null) {
store.setUniversalErrorScenarioRef(null);
}
store.saveOrUpdateService(service);
// ***************** HACK A ****************
if (newName != null && oldName != null
&& !oldName.trim().equals(newName.trim())) {
// OK, we had an existing Service Scenario with a name change.
// Let's update the appropriate Service Plan.
store.updateServicePlansWithNewScenarioName(serviceId, oldName, newName);
}
// *****************************************
resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
JSONObject object = new JSONObject();
JSONObject resultObject = new JSONObject();
try {
object.put("success", "Scenario updated");
object.put("scenarioId", scenario.getId().toString());
object.put("serviceId", service.getId().toString());
resultObject.put("result", object);
} catch (JSONException e1) {
e1.printStackTrace();
}