use of com.twinsoft.convertigo.engine.requesters.InternalRequester in project convertigo by convertigo.
the class Sequence method getSessionId.
public String getSessionId() {
String sessionId = null;
try {
// Case of internal requester
if (context.httpSession == null) {
try {
InternalRequester requester = (InternalRequester) context.requestedObject.requester;
Map<String, String[]> request = GenericUtils.cast(requester.inputData);
sessionId = request.get(Parameter.SessionId.getName())[0];
Engine.logBeans.debug("Sequence session ID (internal requester case): " + sessionId);
} catch (Exception e) {
// Exception case
sessionId = context.contextID.substring(0, context.contextID.indexOf("_"));
Engine.logBeans.debug("Sequence session ID (internal requester case, but with exception): " + sessionId);
Engine.logBeans.debug(e.getMessage());
}
} else // Case of servlet requester
{
sessionId = context.httpSession.getId();
Engine.logBeans.debug("Sequence session ID (servlet requester case): " + sessionId);
}
} catch (Exception e) {
Engine.logBeans.error("Unable to retrieve sessionID of sequence", e);
}
return sessionId;
}
use of com.twinsoft.convertigo.engine.requesters.InternalRequester in project convertigo by convertigo.
the class SchedulerJob method executeJob.
public void executeJob(AbstractJob job, String jdName) {
if (job.isEnable()) {
long start = System.currentTimeMillis();
if (job instanceof AbstractConvertigoJob) {
AbstractConvertigoJob convertigoJob = (AbstractConvertigoJob) job;
HttpServletRequest request = null;
try {
Engine.logScheduler.info("Prepare job " + jdName + " for " + convertigoJob.getProjectName());
Map<String, String[]> parameters = convertigoJob.getConvertigoParameters();
InternalRequester requester = new InternalRequester(GenericUtils.<Map<String, Object>>cast(parameters));
HttpSessionListener.checkSession(request = requester.getHttpServletRequest());
Object response = requester.processRequest();
String message = "Completed job " + jdName + " with success";
if (convertigoJob.isWriteOutput()) {
if (response instanceof Document) {
response = XMLUtils.prettyPrintDOM((Document) response);
}
message += "\n" + response;
}
Engine.logScheduler.info(message);
} catch (Exception e) {
Engine.logScheduler.error("Failed job " + jdName, e);
} finally {
if (request != null) {
request.getSession(true).invalidate();
}
}
} else if (job instanceof JobGroupJob) {
JobGroupJob jobGroupJob = (JobGroupJob) job;
SortedSet<AbstractJob> jobs = jobGroupJob.getJobGroup();
Engine.logScheduler.info("Prepare job " + jdName + " for " + jobs.size() + " jobs. Serial ? " + jobGroupJob.isSerial());
int parallelJob = jobGroupJob.getParallelJob();
if (parallelJob <= 1) {
for (AbstractJob abstractJob : jobs) {
executeJob(abstractJob, jdName + "[" + abstractJob.getName() + "]");
}
} else {
int[] jobCount = { 0 };
Set<Thread> threads = new HashSet<>();
List<AbstractJob> list = new ArrayList<>(jobs);
while (!list.isEmpty()) {
synchronized (jobCount) {
if (jobCount[0] == parallelJob) {
try {
jobCount.wait();
} catch (InterruptedException e) {
}
}
jobCount[0]++;
AbstractJob abstractJob = list.remove(0);
final String subname = jdName + "[" + abstractJob.getName() + "]";
Thread thread = new Thread(() -> {
try {
executeJob(abstractJob, subname);
} finally {
synchronized (jobCount) {
jobCount[0]--;
jobCount.notify();
}
}
});
threads.add(thread);
thread.setDaemon(true);
thread.start();
}
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
Engine.logScheduler.error("Unexpected exception", e);
}
}
}
}
Engine.logScheduler.info("Completed job " + jdName + " in " + (System.currentTimeMillis() - start) + "ms");
} else {
Engine.logScheduler.info("Trying to start " + jdName + " failed because the job is disabled !");
}
}
use of com.twinsoft.convertigo.engine.requesters.InternalRequester in project convertigo by convertigo.
the class AbstractFullSyncListener method executeSequence.
protected void executeSequence(InternalHttpServletRequest request, JSONArray docs) throws EngineException {
if (isEnabled()) {
if (targetSequence == null || targetSequence.isEmpty()) {
throw new EngineException("No target sequence defined");
}
if (docs == null) {
throw new EngineException("Parameter 'docs' is null");
}
int len = docs.length();
if (len == 0) {
return;
}
for (int i = 0; i < len; i++) {
try {
CouchKey.c8oHash.remove(docs.getJSONObject(i));
CouchKey.c8oAcl.remove(docs.getJSONObject(i));
} catch (JSONException e) {
throw new EngineException("Incoming documents error", e);
}
}
try {
Document document = XMLUtils.getDefaultDocumentBuilder().newDocument();
Element itemsElement = document.createElement("items");
XMLUtils.jsonToXml(docs, itemsElement);
NodeList docList = XPathAPI.selectNodeList(itemsElement, "./item");
StringTokenizer st = new StringTokenizer(getTargetSequence(), ".");
String projectName = st.nextToken();
String sequenceName = st.nextToken();
Engine.logBeans.debug("(FullSyncListener) Listener \"" + getName() + "\" : execute sequence \"" + sequenceName + "\"");
try {
Map<String, Object> requestParams = new HashMap<String, Object>();
requestParams.put(Parameter.Project.getName(), new String[] { projectName });
requestParams.put(Parameter.Sequence.getName(), new String[] { sequenceName });
requestParams.put(Parameter.Context.getName(), new String[] { "listener_" + getName() });
requestParams.put(Parameter.RemoveContext.getName(), new String[] { "true" });
requestParams.put("doc", docList);
Engine.logBeans.debug("(FullSyncListener) Listener \"" + getName() + "\" : internal invoke requested");
InternalRequester internalRequester = new InternalRequester(requestParams, request);
Object result = internalRequester.processRequest();
if (result != null) {
Document xmlHttpDocument = (Document) result;
String contents = XMLUtils.prettyPrintDOMWithEncoding(xmlHttpDocument, "UTF-8");
Engine.logBeans.debug("(FullSyncListener) Listener \"" + getName() + "\" : sequence successfully executed with following result\n" + contents + "\n");
}
} catch (Exception e) {
throw new EngineException("Sequence named \"" + sequenceName + "\" failed", e);
}
} catch (Exception e) {
throw new EngineException("Unable to execute sequence for \"" + getName() + "\" listener", e);
}
}
}
use of com.twinsoft.convertigo.engine.requesters.InternalRequester in project convertigo by convertigo.
the class AbstractRestOperation method handleRequest.
@Override
@SuppressWarnings("deprecation")
public String handleRequest(HttpServletRequest request, HttpServletResponse response) throws EngineException {
String targetRequestableQName = getTargetRequestable();
if (targetRequestableQName.isEmpty()) {
throw new EngineException("Mapping operation \"" + getName() + "\" has no target requestable defined");
}
StringTokenizer st = new StringTokenizer(targetRequestableQName, ".");
int count = st.countTokens();
String projectName = st.nextToken();
String sequenceName = count == 2 ? st.nextToken() : "";
String connectorName = count == 3 ? st.nextToken() : "";
String transactionName = count == 3 ? st.nextToken() : "";
try {
Map<String, Object> map = new HashMap<String, Object>();
String responseContentType = null;
String content = null;
try {
// Check multipart request
if (ServletFileUpload.isMultipartContent(request)) {
Engine.logBeans.debug("(AbstractRestOperation) \"" + getName() + "\" Multipart resquest");
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(1000);
File temporaryFile = File.createTempFile("c8o-multipart-files", ".tmp");
int cptFile = 0;
temporaryFile.delete();
temporaryFile.mkdirs();
factory.setRepository(temporaryFile);
Engine.logBeans.debug("(AbstractRestOperation) \"" + getName() + "\" Temporary folder for upload is : " + temporaryFile.getAbsolutePath());
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(EnginePropertiesManager.getPropertyAsLong(PropertyName.FILE_UPLOAD_MAX_REQUEST_SIZE));
upload.setFileSizeMax(EnginePropertiesManager.getPropertyAsLong(PropertyName.FILE_UPLOAD_MAX_FILE_SIZE));
// Parse the request
List<FileItem> items = GenericUtils.cast(upload.parseRequest(request));
for (FileItem fileItem : items) {
String parameterName = fileItem.getFieldName();
String parameterValue;
if (fileItem.isFormField()) {
parameterValue = fileItem.getString();
Engine.logBeans.debug("(AbstractRestOperation) \"" + getName() + "\" Value for field '" + parameterName + "' : " + parameterValue);
} else {
String name = fileItem.getName().replaceFirst("^.*(?:\\\\|/)(.*?)$", "$1");
if (name.length() > 0) {
File wDir = new File(temporaryFile, "" + (++cptFile));
wDir.mkdirs();
File wFile = new File(wDir, name);
fileItem.write(wFile);
fileItem.delete();
parameterValue = wFile.getAbsolutePath();
Engine.logBeans.debug("(AbstractRestOperation) \"" + getName() + "\" Temporary uploaded file for field '" + parameterName + "' : " + parameterValue);
} else {
Engine.logBeans.debug("(AbstractRestOperation) \"" + getName() + "\" No temporary uploaded file for field '" + parameterName + "', empty name");
parameterValue = "";
}
}
if (parameterValue != null && !parameterValue.isEmpty()) {
UrlMappingParameter param = null;
try {
param = getParameterByName(parameterName);
} catch (Exception e) {
}
if (param != null) {
String variableName = param.getMappedVariableName();
if (!variableName.isEmpty()) {
parameterName = variableName;
}
}
Object mapValue = map.get(parameterName);
if (mapValue == null) {
map.put(parameterName, parameterValue);
} else {
List<String> values = new ArrayList<String>();
if (mapValue instanceof String) {
values.add((String) mapValue);
} else if (mapValue instanceof List) {
values.addAll(GenericUtils.cast(mapValue));
}
values.add(parameterValue);
map.put(parameterName, values);
}
}
}
}
String contextName = request.getParameter(Parameter.Context.getName());
map.put(Parameter.Context.getName(), new String[] { contextName });
map.put(Parameter.Project.getName(), new String[] { projectName });
if (sequenceName.isEmpty()) {
map.put(Parameter.Connector.getName(), new String[] { connectorName });
map.put(Parameter.Transaction.getName(), new String[] { transactionName });
} else {
map.put(Parameter.Sequence.getName(), new String[] { sequenceName });
map.put(Parameter.RemoveContext.getName(), new String[] { "" });
map.put(Parameter.RemoveSession.getName(), new String[] { "" });
}
// Add path variables parameters
Map<String, String> varMap = ((UrlMapping) getParent()).getPathVariableValues(request);
for (String varName : varMap.keySet()) {
String varValue = varMap.get(varName);
map.put(varName, varValue);
}
// Add other parameters
for (UrlMappingParameter param : getParameterList()) {
String paramName = param.getName();
String variableName = param.getMappedVariableName();
Object paramValue = null;
if (param.getType() == Type.Header) {
paramValue = request.getHeader(paramName);
} else if ((param.getType() == Type.Query || param.getType() == Type.Form)) {
String[] pvalues = request.getParameterValues(paramName);
if (pvalues != null) {
paramValue = pvalues;
}
} else if (param.getType() == Type.Path) {
String varValue = varMap.get(param.getName());
paramValue = varValue;
} else if (param.getType() == Type.Body) {
if (request.getInputStream() != null) {
// Retrieve data
paramValue = IOUtils.toString(request.getInputStream(), "UTF-8");
// Get input content type
DataContent dataInput = param.getInputContent();
if (dataInput.equals(DataContent.useHeader)) {
String requestContentType = request.getContentType();
if (requestContentType == null || MimeType.Xml.is(requestContentType)) {
dataInput = DataContent.toXml;
} else if (MimeType.Json.is(requestContentType)) {
dataInput = DataContent.toJson;
}
}
// Transform input data
try {
if (dataInput.equals(DataContent.toJson)) {
// String modelName = param instanceof IMappingRefModel ? ((IMappingRefModel)param).getModelReference() : "";
// String objectName = modelName.isEmpty() ? paramName : modelName;
// Document doc = XMLUtils.parseDOMFromString("<"+objectName+"/>");
Document doc = XMLUtils.parseDOMFromString("<" + paramName + "/>");
Element root = doc.getDocumentElement();
JSONObject json = new JSONObject((String) paramValue);
XMLUtils.jsonToXml(json, root);
paramValue = root.getChildNodes();
} else if (dataInput.equals(DataContent.toXml)) {
// Document doc = XMLUtils.parseDOMFromString((String)paramValue);
// paramValue = doc.getDocumentElement();
Document xml = XMLUtils.parseDOMFromString((String) paramValue);
if (xml.getDocumentElement().getTagName().equals(paramName)) {
paramValue = xml.getDocumentElement();
} else {
NodeList nl = xml.getDocumentElement().getChildNodes();
Document doc = XMLUtils.parseDOMFromString("<" + paramName + "/>");
Element root = doc.getDocumentElement();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
root.appendChild(doc.adoptNode(node));
}
}
paramValue = doc.getDocumentElement();
}
}
} catch (Exception e) {
Engine.logBeans.error("(AbstractRestOperation) \"" + getName() + "\" : unable to decode body", e);
}
}
}
// retrieve default value if necessary
if (paramValue == null) {
paramValue = param.getValueOrNull();
}
if (paramValue != null) {
// map parameter to variable
if (!variableName.isEmpty()) {
paramName = variableName;
}
// add parameter with value to input map
if (paramValue instanceof String) {
map.put(paramName, new String[] { paramValue.toString() });
} else if (paramValue instanceof String[]) {
String[] values = (String[]) paramValue;
map.put(paramName, values);
} else {
map.put(paramName, paramValue);
}
} else if (param.isRequired()) {
if (param.getType() == Type.Path) {
// ignore : already handled
} else if (param.getDataType().equals(DataType.File)) {
// ignore : already handled
} else {
Engine.logBeans.warn("(AbstractRestOperation) \"" + getName() + "\" : missing parameter " + param.getName());
}
}
}
} catch (IOException ioe) {
Engine.logBeans.error("(AbstractRestOperation) \"" + getName() + "\" : invalid body", ioe);
throw ioe;
}
// Execute requestable
Engine.logBeans.debug("(AbstractRestOperation) \"" + getName() + "\" executing requestable \"" + targetRequestableQName + "\"");
InternalRequester internalRequester = new InternalRequester(map, request);
request.setAttribute("convertigo.requester", internalRequester);
Object result = internalRequester.processRequest();
String encoding = "UTF-8";
if (result != null) {
Document xmlHttpDocument = (Document) result;
// Extract the encoding Char Set from PI
Node firstChild = xmlHttpDocument.getFirstChild();
if ((firstChild.getNodeType() == Document.PROCESSING_INSTRUCTION_NODE) && (firstChild.getNodeName().equals("xml"))) {
String piValue = firstChild.getNodeValue();
int encodingOffset = piValue.indexOf("encoding=\"");
if (encodingOffset != -1) {
encoding = piValue.substring(encodingOffset + 10);
encoding = encoding.substring(0, encoding.length() - 1);
}
}
// Get output content type
DataContent dataOutput = getOutputContent();
if (dataOutput.equals(DataContent.useHeader)) {
String h_Accept = HeaderName.Accept.getHeader(request);
if (MimeType.Xml.is(h_Accept)) {
dataOutput = DataContent.toXml;
} else if (h_Accept == null || MimeType.Json.is(h_Accept)) {
dataOutput = DataContent.toJson;
}
}
// Modify status according to XPath condition of Response beans
int statusCode = HttpServletResponse.SC_OK;
String statusText = "";
if (RequestAttribute.responseStatus.get(request) == null) {
for (UrlMappingResponse umr : getResponseList()) {
if (umr instanceof OperationResponse) {
OperationResponse or = (OperationResponse) umr;
if (or.isMatching(xmlHttpDocument)) {
try {
statusCode = Integer.valueOf(or.getStatusCode(), 10);
statusText = or.getStatusText();
} catch (Exception e) {
}
break;
}
}
}
}
if (statusText.isEmpty())
response.setStatus(statusCode);
else
response.setStatus(statusCode, statusText);
// Transform XML data
if (dataOutput.equals(DataContent.toJson)) {
JsonRoot jsonRoot = getProject().getJsonRoot();
boolean useType = getProject().getJsonOutput() == JsonOutput.useType;
Document document = useType ? Engine.theApp.schemaManager.makeXmlRestCompliant(xmlHttpDocument) : xmlHttpDocument;
XMLUtils.logXml(document, Engine.logContext, "Generated Rest XML (useType=" + useType + ")");
content = XMLUtils.XmlToJson(document.getDocumentElement(), true, useType, jsonRoot);
responseContentType = MimeType.Json.value();
} else {
content = XMLUtils.prettyPrintDOMWithEncoding(xmlHttpDocument, "UTF-8");
responseContentType = MimeType.Xml.value();
}
} else {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
// Set response content-type header
if (responseContentType != null) {
HeaderName.ContentType.addHeader(response, responseContentType);
}
// Set response content
if (content != null) {
response.setCharacterEncoding(encoding);
if (Engine.logContext.isInfoEnabled()) {
try {
String json = new JSONObject(content).toString(1);
int len = json.length();
if (len > 5000) {
String txt = json.substring(0, 5000) + "\n... (see the complete message in DEBUG log level)";
Engine.logContext.info("Generated REST Json:\n" + txt);
Engine.logContext.debug("Generated REST Json:\n" + json);
} else {
Engine.logContext.info("Generated REST Json:\n" + json);
}
} catch (Exception e) {
}
}
}
return content;
} catch (Throwable t) {
throw new EngineException("Operation \"" + getName() + "\" failed to handle request", t);
} finally {
if (terminateSession) {
request.setAttribute("convertigo.requireEndOfContext", true);
}
}
}
use of com.twinsoft.convertigo.engine.requesters.InternalRequester in project convertigo by convertigo.
the class ConvertigoPlugin method runRequestable.
public void runRequestable(final String projectName, final Map<String, String[]> parameters) {
if (!Engine.isStartFailed && Engine.isStarted) {
parameters.put(Parameter.Project.getName(), new String[] { projectName });
new Thread(new Runnable() {
@Override
public void run() {
try {
InternalHttpServletRequest request;
if (session == null || session.getMaxInactiveInterval() <= 1) {
request = new InternalHttpServletRequest();
session = request.getSession("studio");
} else {
request = new InternalHttpServletRequest(session);
}
InternalRequester requester = new InternalRequester(GenericUtils.<Map<String, Object>>cast(parameters), request);
HttpSessionListener.checkSession(requester.getHttpServletRequest());
requester.processRequest();
} catch (Exception e) {
logException(e, "Failed to run the requestable of project " + projectName);
}
}
}).start();
} else {
logInfo("Cannot run the requestable of project " + projectName + ", the embedded tomcat is not correctly started.");
}
}
Aggregations