use of org.cerberus.crud.entity.AppServiceContent in project cerberus-source by cerberustesting.
the class UpdateAppService method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
final void processRequest(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
response.setContentType("text/html;charset=UTF-8");
String charset = request.getCharacterEncoding();
// Parameter that are already controled by GUI (no need to decode) --> We SECURE them
// Parameter that needs to be secured --> We SECURE+DECODE them
String service = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("service"), null, charset);
String application = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("application"), null, charset);
String type = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("type"), null, charset);
String method = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("method"), "", charset);
String operation = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("operation"), null, charset);
String attachementurl = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("attachementurl"), null, charset);
String group = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("group"), null, charset);
String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), null, charset);
// Parameter that we cannot secure as we need the html --> We DECODE them
String servicePath = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("servicePath"), null, charset);
String serviceRequest = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("serviceRequest"), null, charset);
// Prepare the final answer.
MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer finalAnswer = new Answer(msg1);
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(service)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "AppService").replace("%OPERATION%", "Update").replace("%REASON%", "AppService ID (service) is missing."));
finalAnswer.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
appServiceService = appContext.getBean(IAppServiceService.class);
appServiceHeaderService = appContext.getBean(IAppServiceHeaderService.class);
appServiceContentService = appContext.getBean(IAppServiceContentService.class);
appServiceContentFactory = appContext.getBean(IFactoryAppServiceContent.class);
appServiceHeaderFactory = appContext.getBean(IFactoryAppServiceHeader.class);
AnswerItem resp = appServiceService.readByKey(service);
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
/**
* Object could not be found. We stop here and report the error.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
} else {
/**
* The service was able to perform the query and confirm the
* object exist, then we can update it.
*/
AppService appService = (AppService) resp.getItem();
appService.setGroup(group);
appService.setAttachementURL(attachementurl);
appService.setDescription(description);
appService.setServiceRequest(serviceRequest);
appService.setOperation(operation);
appService.setType(type);
appService.setApplication(application);
appService.setMethod(method);
appService.setServicePath(servicePath);
appService.setUsrModif(request.getRemoteUser());
ans = appServiceService.update(appService.getService(), appService);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Update was successful. Adding Log entry.
*/
logEventService = appContext.getBean(ILogEventService.class);
logEventService.createForPrivateCalls("/UpdateAppService", "UPDATE", "Updated AppService : ['" + service + "']", request);
}
// Update content
if (request.getParameter("contentList") != null) {
JSONArray objContentArray = new JSONArray(request.getParameter("contentList"));
List<AppServiceContent> contentList = new ArrayList();
contentList = getContentListFromRequest(request, appContext, service, objContentArray);
// Update the Database with the new list.
ans = appServiceContentService.compareListAndUpdateInsertDeleteElements(service, contentList);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
// Update header
if (request.getParameter("headerList") != null) {
JSONArray objHeaderArray = new JSONArray(request.getParameter("headerList"));
List<AppServiceHeader> headerList = new ArrayList();
headerList = getHeaderListFromRequest(request, appContext, service, objHeaderArray);
// Update the Database with the new list.
ans = appServiceHeaderService.compareListAndUpdateInsertDeleteElements(service, headerList);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.crud.entity.AppServiceContent in project cerberus-source by cerberustesting.
the class ServiceService method callService.
@Override
public AnswerItem<AppService> callService(String service, String database, String request, String servicePathParam, String operation, TestCaseExecution tCExecution) {
MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
String decodedRequest;
String decodedServicePath = null;
String decodedOperation;
String decodedAttachement;
AnswerItem result = new AnswerItem();
String system = tCExecution.getApplicationObj().getSystem();
String country = tCExecution.getCountry();
String environment = tCExecution.getEnvironment();
LOG.debug("Starting callService : " + service + " with database : " + database);
try {
AppService appService;
// If Service information is not defined, we create it from request, servicePath and operation parameters forcing in SOAP mode.
if (StringUtil.isNullOrEmpty(service)) {
LOG.debug("Creating AppService from parameters.");
appService = factoryAppService.create("null", AppService.TYPE_SOAP, "", "", "", request, "Automatically created Service from datalib.", servicePathParam, "", operation, null, null, null, null);
service = "null";
} else {
// If Service information is defined, we get it from database.
LOG.debug("Getting AppService from service : " + service);
appService = appServiceService.convert(appServiceService.readByKeyWithDependency(service, "Y"));
}
String servicePath;
if (appService == null) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
message.setDescription(message.getDescription().replace("%DESCRIPTION%", "Service does not exist !!"));
} else if (StringUtil.isNullOrEmpty(appService.getServicePath())) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE).resolveDescription("DESCRIPTION", "Service path is not defined");
} else {
// We start by calculating the servicePath and decode it.
servicePath = appService.getServicePath();
if (!(StringUtil.isURL(servicePath))) {
if (StringUtil.isNullOrEmpty(database)) {
// We reformat servicePath in order to add the context from the application execution.
servicePath = StringUtil.getURLFromString(tCExecution.getCountryEnvironmentParameters().getIp(), tCExecution.getCountryEnvironmentParameters().getUrl(), appService.getServicePath(), "http://");
} else {
// We reformat servicePath in order to add the context from the databaseUrl definition and corresponding from the country and environment of the execution.
try {
CountryEnvironmentDatabase countryEnvironmentDatabase;
countryEnvironmentDatabase = countryEnvironmentDatabaseService.convert(this.countryEnvironmentDatabaseService.readByKey(system, country, environment, database));
if (countryEnvironmentDatabase == null) {
message = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SERVICE_URLKOANDDATABASESOAPURLNOTEXIST);
message.setDescription(message.getDescription().replace("%SERVICEURL%", appService.getServicePath()).replace("%SYSTEM%", system).replace("%COUNTRY%", country).replace("%ENV%", environment).replace("%DATABASE%", database));
result.setResultMessage(message);
return result;
} else {
String soapURL = countryEnvironmentDatabase.getSoapUrl();
if (StringUtil.isNullOrEmpty(soapURL)) {
message = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SERVICE_URLKOANDDATABASESOAPURLEMPTY);
message.setDescription(message.getDescription().replace("%SERVICEURL%", appService.getServicePath()).replace("%SYSTEM%", system).replace("%COUNTRY%", country).replace("%ENV%", environment).replace("%DATABASE%", database));
result.setResultMessage(message);
return result;
}
// soapURL from database is not empty so we prefix the Service URL with it.
servicePath = StringUtil.getURLFromString(soapURL, "", servicePath, "");
if (!StringUtil.isURL(servicePath)) {
message = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SERVICE_URLKO);
message.setDescription(message.getDescription().replace("%SERVICEURL%", servicePath).replace("%SOAPURL%", soapURL).replace("%SERVICEPATH%", appService.getServicePath()));
result.setResultMessage(message);
return result;
}
}
} catch (CerberusException ex) {
message = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_GETFROMDATALIB_SERVICE_URLKOANDDATABASESOAPURLNOTEXIST);
message.setDescription(message.getDescription().replace("%SERVICEURL%", servicePath).replace("%SYSTEM%", system).replace("%COUNTRY%", country).replace("%ENV%", environment).replace("%DATABASE%", database));
result.setResultMessage(message);
return result;
}
}
}
// appService object and target servicePath is now clean. We can start to decode.
decodedServicePath = servicePath;
decodedRequest = appService.getServiceRequest();
LOG.debug("AppService with correct path is now OK : " + servicePath);
AnswerItem<String> answerDecode = new AnswerItem();
try {
// Decode Service Path
answerDecode = variableService.decodeStringCompletly(decodedServicePath, tCExecution, null, false);
decodedServicePath = (String) answerDecode.getItem();
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
message = answerDecode.getResultMessage().resolveDescription("FIELD", "Service Path");
LOG.debug("Property interupted due to decode 'Service Path'.");
result.setResultMessage(message);
return result;
}
// Decode Request
answerDecode = variableService.decodeStringCompletly(decodedRequest, tCExecution, null, false);
decodedRequest = (String) answerDecode.getItem();
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
message = answerDecode.getResultMessage().resolveDescription("FIELD", "Service Request");
LOG.debug("Property interupted due to decode 'Service Request'.");
result.setResultMessage(message);
return result;
}
// Decode Header List
List<AppServiceHeader> objectResponseHeaderList = new ArrayList<>();
for (AppServiceHeader object : appService.getHeaderList()) {
answerDecode = variableService.decodeStringCompletly(object.getKey(), tCExecution, null, false);
object.setKey((String) answerDecode.getItem());
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
String field = "Header Key " + object.getKey();
message = answerDecode.getResultMessage().resolveDescription("FIELD", field);
LOG.debug("Property interupted due to decode '" + field + "'.");
result.setResultMessage(message);
return result;
}
answerDecode = variableService.decodeStringCompletly(object.getValue(), tCExecution, null, false);
object.setValue((String) answerDecode.getItem());
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
String field = "Header Value " + object.getKey();
message = answerDecode.getResultMessage().resolveDescription("FIELD", field);
LOG.debug("Property interupted due to decode '" + field + "'.");
result.setResultMessage(message);
return result;
}
objectResponseHeaderList.add(object);
}
// Decode ContentDetail List
appService.setResponseHeaderList(objectResponseHeaderList);
List<AppServiceContent> objectResponseContentList = new ArrayList<>();
for (AppServiceContent object : appService.getContentList()) {
answerDecode = variableService.decodeStringCompletly(object.getKey(), tCExecution, null, false);
object.setKey((String) answerDecode.getItem());
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
String field = "Content Key " + object.getKey();
message = answerDecode.getResultMessage().resolveDescription("FIELD", field);
LOG.debug("Property interupted due to decode '" + field + "'.");
result.setResultMessage(message);
return result;
}
answerDecode = variableService.decodeStringCompletly(object.getValue(), tCExecution, null, false);
object.setValue((String) answerDecode.getItem());
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
String field = "Content Value " + object.getKey();
message = answerDecode.getResultMessage().resolveDescription("FIELD", field);
LOG.debug("Property interupted due to decode '" + field + "'.");
result.setResultMessage(message);
return result;
}
objectResponseContentList.add(object);
}
appService.setContentList(objectResponseContentList);
} catch (CerberusEventException cee) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICEWITHPATH);
message.setDescription(message.getDescription().replace("%SERVICENAME%", service));
message.setDescription(message.getDescription().replace("%SERVICEPATH%", decodedServicePath));
message.setDescription(message.getDescription().replace("%DESCRIPTION%", cee.getMessageError().getDescription()));
result.setResultMessage(message);
return result;
}
// Get from parameter whether we define a token or not (in order to trace the cerberus calls in http header)
String token = null;
if (parameterService.getParameterBooleanByKey("cerberus_callservice_enablehttpheadertoken", system, true)) {
token = String.valueOf(tCExecution.getId());
}
// Get from parameter the call timeout to be used.
int timeOutMs = parameterService.getParameterIntegerByKey("cerberus_callservice_timeoutms", system, 60000);
// The rest of the data will be prepared depending on the TYPE and METHOD used.
switch(appService.getType()) {
case AppService.TYPE_SOAP:
LOG.debug("This is a SOAP Service");
/**
* SOAP. Decode Envelope and Operation replacing
* properties encapsulated with %
*/
decodedOperation = appService.getOperation();
decodedAttachement = appService.getAttachementURL();
try {
answerDecode = variableService.decodeStringCompletly(decodedOperation, tCExecution, null, false);
decodedOperation = (String) answerDecode.getItem();
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
String field = "Operation";
message = answerDecode.getResultMessage().resolveDescription("FIELD", field);
LOG.debug("Property interupted due to decode '" + field + "'.");
result.setResultMessage(message);
return result;
}
answerDecode = variableService.decodeStringCompletly(decodedAttachement, tCExecution, null, false);
decodedAttachement = (String) answerDecode.getItem();
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
String field = "Attachement URL";
message = answerDecode.getResultMessage().resolveDescription("FIELD", field);
LOG.debug("Property interupted due to decode '" + field + "'.");
result.setResultMessage(message);
return result;
}
} catch (CerberusEventException cee) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP);
message.setDescription(message.getDescription().replace("%SERVICENAME%", service));
message.setDescription(message.getDescription().replace("%SERVICEPATH%", decodedServicePath));
message.setDescription(message.getDescription().replace("%DESCRIPTION%", cee.getMessageError().getDescription()));
result.setResultMessage(message);
return result;
}
/**
* Call SOAP and store it into the execution.
*/
result = soapService.callSOAP(decodedRequest, decodedServicePath, decodedOperation, decodedAttachement, appService.getHeaderList(), token, timeOutMs, system);
LOG.debug("SOAP Called done.");
LOG.debug("Result message." + result.getResultMessage());
message = result.getResultMessage();
break;
case AppService.TYPE_REST:
/**
* REST.
*/
switch(appService.getMethod()) {
case AppService.METHOD_HTTPGET:
case AppService.METHOD_HTTPPOST:
/**
* Call REST and store it into the execution.
*/
result = restService.callREST(decodedServicePath, decodedRequest, appService.getMethod(), appService.getHeaderList(), appService.getContentList(), token, timeOutMs, system);
message = result.getResultMessage();
break;
case AppService.METHOD_HTTPDELETE:
result = restService.callREST(decodedServicePath, decodedRequest, appService.getMethod(), appService.getHeaderList(), appService.getContentList(), token, timeOutMs, system);
message = result.getResultMessage();
break;
case AppService.METHOD_HTTPPUT:
result = restService.callREST(decodedServicePath, decodedRequest, appService.getMethod(), appService.getHeaderList(), appService.getContentList(), token, timeOutMs, system);
message = result.getResultMessage();
break;
case AppService.METHOD_HTTPPATCH:
result = restService.callREST(decodedServicePath, decodedRequest, appService.getMethod(), appService.getHeaderList(), appService.getContentList(), token, timeOutMs, system);
message = result.getResultMessage();
break;
default:
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
message.setDescription(message.getDescription().replace("%DESCRIPTION%", "Method : '" + appService.getMethod() + "' for REST Service is not supported by the engine."));
result.setResultMessage(message);
}
break;
default:
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
message.setDescription(message.getDescription().replace("%SERVICE%", service));
message.setDescription(message.getDescription().replace("%DESCRIPTION%", "Service Type : '" + appService.getType() + "' is not supported by the engine."));
result.setResultMessage(message);
}
}
} catch (CerberusException ex) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
message.setDescription(message.getDescription().replace("%SERVICENAME%", service));
message.setDescription(message.getDescription().replace("%DESCRIPTION%", "Cerberus exception on CallService : " + ex.getMessageError().getDescription()));
result.setResultMessage(message);
return result;
} catch (Exception ex) {
LOG.error("Exception when performing CallService Action. " + ex.toString());
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
message.setDescription(message.getDescription().replace("%SERVICENAME%", service));
message.setDescription(message.getDescription().replace("%DESCRIPTION%", "Cerberus exception on CallService : " + ex.toString()));
return result;
}
message.setDescription(message.getDescription().replace("%SERVICENAME%", service));
result.setResultMessage(message);
LOG.debug("Ended callService : " + service + " with database : " + database + " Result : " + message.getDescription());
return result;
}
use of org.cerberus.crud.entity.AppServiceContent in project cerberus-source by cerberustesting.
the class AppServiceService method convertContentListToQueryString.
@Override
public String convertContentListToQueryString(List<AppServiceContent> serviceContent) {
String result = "";
if (serviceContent == null || serviceContent.isEmpty()) {
return result;
}
for (AppServiceContent object : serviceContent) {
if (object.getActive().equalsIgnoreCase("Y")) {
result += object.getKey();
result += "=";
result += object.getValue();
result += "&";
}
}
result = StringUtil.removeLastChar(result, 1);
return result;
}
use of org.cerberus.crud.entity.AppServiceContent in project cerberus-source by cerberustesting.
the class AppServiceService method readByKeyWithDependency.
@Override
public AnswerItem readByKeyWithDependency(String key, String activedetail) {
AnswerItem answerAppService = this.readByKey(key);
AppService appService = (AppService) answerAppService.getItem();
try {
AnswerList content = appServiceContentService.readByVarious(key, activedetail);
appService.setContentList((List<AppServiceContent>) content.getDataList());
AnswerList header = appServiceHeaderService.readByVarious(key, activedetail);
appService.setHeaderList((List<AppServiceHeader>) header.getDataList());
answerAppService.setItem(appService);
} catch (Exception e) {
LOG.error(e);
}
return answerAppService;
}
Aggregations