Spring REST Web Service | Update an XML and send it in response - java

I am working on a Spring Restful Web service wherein I am returning an xml file as a response. This XML file is placed inside the main/resources folder of the MAVEN project build in eclipse. The service accepts certain parameters from the caller and based on those parameters, it should update the XML file. This project is deployed as WAR in the production server. On my local project, I can see the xml file being updated but in the production server it is not. How can I get this working in the production server ?
Below is the code for controller accepting the incoming request
#RestController
public class HelloWorldRestController {
#Autowired
UserService userService; // Service which will do all data
// retrieval/manipulation work
#Autowired
DialogServiceXml dialogService;
// Returning an xml file in the response
#CrossOrigin(origins = "*")
#RequestMapping(value = "/getUpdatedDialog", method = RequestMethod.POST, produces = "application/xml")
public ResponseEntity<InputStreamResource> downloadXMLFile(#RequestBody Dialog dialog,
UriComponentsBuilder ucBuilder) throws IOException {
// Update the xml file named : "mor_dialog.xml"
dialogService.updateXml(new StringBuilder(dialog.getClassName()), new StringBuilder(dialog.getResponse()));
// Pick up the updated file from the classpath
ClassPathResource xmlFile = null;
try {
xmlFile = new ClassPathResource("mor_dialog.xml");
} catch (Exception exception) {
exception.printStackTrace();
}
// Code to prevent caching so that always the latest version of the file
// is being sent.
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Cache-Control", "no-cache, np-store, must-revalidate");
httpHeaders.add("Pragma", "no-cache");
httpHeaders.add("Expires", "0");
//httpHeaders.add("Access-Control-Allow-Origin", "http://nlc-mor-furniture.mybluemix.net");
return ResponseEntity.ok().headers(httpHeaders).contentLength(xmlFile.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(xmlFile.getInputStream()));
}
}
Below is the class that unmarshals the XML, updates it based on the input parameters, then marshals it back
#Service("dialogService")
public class DialogServiceXml implements DialogServiceXmlImpl {
#SuppressWarnings({ "rawtypes", "unchecked" })
#Override
public void updateXml(StringBuilder className, StringBuilder response) {
JAXBContext jaxbContext = null;
ClassLoader classLoader = getClass().getClassLoader();
try {
jaxbContext = JAXBContext.newInstance("com.sau.watson.dialog.xsd.beans");
Unmarshaller JAXBUnmarshaller = jaxbContext.createUnmarshaller();
Marshaller JAXBMarshaller = jaxbContext.createMarshaller();
JAXBMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "WatsonDialogDocument_1.0.xsd");
File mor_dialog = new File(classLoader.getResource("mor_dialog.xml").getFile());
//File mor_dialog = new File(classLoader.getResource("../../mor_dialog.xml").getFile());
mor_dialog.setWritable(true);
//File mor_dialog_updated = new File(classLoader.getResource("mor_dialog_updated.xml").getFile());
InputStream is = new FileInputStream(mor_dialog);
JAXBElement dialog = (JAXBElement) JAXBUnmarshaller.unmarshal(is);
is.close();
//JAXBElement dialog = (JAXBElement) JAXBUnmarshaller.unmarshal(new FileInputStream("src/main/java/com/sau/watson/dialog/xml/mor_dialog.xml"));
DialogType dialogType = (DialogType) dialog.getValue();
// System.out.println(dialogType.toString());
// System.out.println(dialogType);
FlowType flowType = (FlowType) dialogType.getFlow();
for (FolderNodeType folderNodeType : flowType.getFolder()) {
// System.out.println(folderNodeType.getLabel());
if (folderNodeType.getLabel().equalsIgnoreCase("Library")) {
for (ChatflowNode libChatFlowNode : folderNodeType.getInputOrOutputOrDefault()) {
FolderNodeType libraryFolderNode = (FolderNodeType) libChatFlowNode;
// System.out.println(libraryFolderNode.getId());
// System.out.println(libraryFolderNode.getLabel());
StringBuilder classNameFromXml = new StringBuilder();
for (ChatflowNode node : libraryFolderNode.getInputOrOutputOrDefault()) {
InputNodeType inputNodeType = (InputNodeType) node;
// Getting the class. Class name are encapsulated
// inside the <grammar> node
/**
* <grammar> <item>Salesperson_Great</item>
* <item>Great</item> </grammar>
*/
for (Object grammerTypeObj : inputNodeType.getActionOrScriptOrGrammar()) {
GrammarType grammarType = (GrammarType) grammerTypeObj;
// We are always getting the first item as it is
// assumed that there is only one class in each
// grammar node
classNameFromXml
.append(grammarType.getItemOrSourceOrParam().get(0).getValue().toString());
System.out.println("Class Name is : " + className);
}
// We are always getting the first item as it is
// assumed that there is only one class in each
// grammar node
/*
* List<Object> grammarTypeObj =
* inputNodeType.getActionOrScriptOrGrammar();
* GrammarType grammarType = (GrammarType)
* grammarTypeObj;
*
* String className =
* grammarType.getItemOrSourceOrParam().get(0).
* getValue().toString();
*
* System.out.println("Class Name is : "+className);
*/
if (!classNameFromXml.toString().equalsIgnoreCase(className.toString())) {
continue;
}
// Getting all the response items corresponding to
// this class
for (ChatflowNode outputNodeObj : inputNodeType.getInputOrOutputOrDefault()) {
OutputNodeType outputNode = (OutputNodeType) outputNodeObj;
for (Object promptTypeObj : outputNode.getActionOrScriptOrPrompt()) {
PromptType promptType = (PromptType) promptTypeObj;
List<Serializable> responseItemObjs = promptType.getContent();
for (Object responseItemObj : responseItemObjs) {
/*
* if (responseItemObj instanceof
* String) {
* System.out.println(((String)
* responseItemObj).trim()); }
*/
if (responseItemObj instanceof JAXBElement) {
// System.out.println("JAXBElement
// Instance");
JAXBElement responseItem = (JAXBElement) responseItemObj;
System.out.println("The old response is : " + responseItem.getValue().toString());
responseItem.setValue(response.toString());
}
}
}
}
}
}
}
}
FileOutputStream os = new FileOutputStream(mor_dialog);
JAXBMarshaller.marshal(dialog, os);
//os.flush();
os.close();
//JAXBMarshaller.marshal(dialog, new FileOutputStream("src/main/java/com/sau/watson/dialog/xml/mor_dialog.xml"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

The src/main/resources folder is only available on your development machine before maven builds the war file. After building the war the resources are added to the war file from the root of the classpath.
If you want to be updating the file then you probably want to access the file from the file system rather than from the classpath.

Related

SaveAll JpaRepository method - Audit4j java version not supported

I'm trying to load data into the database from a file when the application starts. The file mapping to the list of entities works fine, but when I call saveAll method I get a strange error -
Audit4j:ERROR Your Java version (13.0.2) is not supported for Audit4j. see http://audit4j.org/errors#javaVersion for further details. Audit4j:ERROR Failed to initialize Audit4j: Java version is not supported.
I changed the version, error remained the same, and what does Java version have to do with it, if logging works fine without call to saveAll.
I already have the functionality to load data through the controller, and tried to load same file through it - everything works fine. What can be the cause of problem?
Code:
#Component
#AllArgsConstructor
public class MetaLoader {
private final ResourceLoader resourceLoader;
private EntityMetaRepository entityMetaRepository;
#PostConstruct
public void init() throws IOException {
Resource metaResource = resourceLoader.getResource("classpath:metadata/metadata.json");
log.info("**MetaLoader | resource exists?=" + metaResource.exists());
File metaFile = metaResource.getFile();
// mapping
ObjectMapper mapper = new ObjectMapper();
List<Meta> metaList;
CollectionType metaListType = mapper.getTypeFactory()
.constructCollectionType(List.class, Meta.class);
metaList= mapper.readValue(metaFile, metaListType);
log.info("**MetaLoader | metaList=" + metaList.toString());
if (!metaList.isEmpty()) {
log.info("**MetaLoader | saving metadata...");
List<EntityMeta> entityMetaList = metaList.stream().map(EntityMeta::new)
.collect(Collectors.toUnmodifiableList());
try {
entityMetaRepository.saveAll(entityMetaList);
} catch (EntityExistsException e) {
log.error("**MetaLoader | something went wrong=" + e.getMessage() + "\n" + e.getCause());
}
}
Controller
#Controller
public class MetaApi {
#Autowired private EntityMetaRepository entityMetaRepository;
#PostMapping(value = "/meta", consumes = "multipart/form-data")
public ResponseEntity<ErrorResponse> postMetaTableFile(MultipartFile file) {
ObjectMapper mapper = new ObjectMapper();
List<Meta> metaList = null;
try {
CollectionType metaListType = mapper.getTypeFactory()
.constructCollectionType(List.class, Meta.class);
metaList = mapper.readValue(file.getBytes(), metaListType);
} catch (IOException e) {
ErrorResponse response = new ErrorResponse().code(400).type("BAD REQUEST").message("Malformed file");
e.printStackTrace();
}
try {
List<EntityMeta> entityMetaList = metaList.stream().map(EntityMeta::new)
.collect(Collectors.toUnmodifiableList());
entityMetaRepository.saveAll(entityMetaList);
} catch (EntityExistsException e) {
throw new ResponseException(e.getMessage(),HttpStatus.BAD_REQUEST);
}
ErrorResponse response = new ErrorResponse().code(200).type("OK").message("OK");
return ResponseEntity.ok(response);
}
}

Return a zip (or any file) from the server on the client browser (REST)

So I am using Java for my Server and Angular for the Client. I am currently working on a feature where you can select multiple files from a table and when you press on download, it generates a zip file and downloads it to your browser. As of right now, the server now creates the zip file and I can access it in the server files. All that is left to do is to make it download on the client's browser. (the zip file is deleted after the client downloads it)
After doing some research, I found out that you can use a fileOutputStream to do this. I also saw some tools like retrofit... I am using REST and this is what my code looks like. How would I achieve my goal as simply as possible?
Angular
httpGetDownloadZip(target: string[]): Observable<ServerAnswer> {
const params = new HttpParams().set('target', String(target)).set('numberOfFiles', String(target.length));
const headers = new HttpHeaders().set('token', this.tokenService.getStorageToken());
const options = {
headers,
params,
};
return this.http
.get<ServerAnswer>(this.BASE_URL + '/files/downloadZip', options)
.pipe(catchError(this.handleError<ServerAnswer>('httpGetZip')));
}
Java zipping method
public void getDownloadZip(String[] files, String folderName) throws IOException {
[...] // The method is huge but basically I generate a folder called "Download/" in the server
// Zipping the "Download/" folder
ZipUtil.pack(new File("Download"), new File("selected-files.zip"));
// what do I return ???
return;
}
Java context
server.createContext("/files/downloadZip", new HttpHandler() {
#Override
public void handle(HttpExchange exchange) throws IOException {
if (!handleTokenPreflight(exchange)) { return; }
System.out.println(exchange.getRequestURI());
Map<String, String> queryParam = parseQueryParam(exchange.getRequestURI().getQuery());
String authToken = exchange.getRequestHeaders().getFirst("token");
String target = queryParam.get("target") + ",";
String[] files = new String[Integer.parseInt(queryParam.get("numberOfFiles"))];
[...] // I process the data in this entire method and send it to the previous method that creates a zip
Controller.getDownloadZip(files, folderName);
// what do I return to download the file on the client's browser ????
return;
}
});
A possible approach to successfully download your zip file can be the described in the following paragraphs.
First, consider returning a reference to the zip file obtained as the compression result in your downloadZip method:
public File getDownloadZip(String[] files, String folderName) throws IOException {
[...] // The method is huge but basically I generate a folder called "Download/" in the server
// Zipping the "Download/" folder
File selectedFilesZipFile = new File("selected-files.zip")
ZipUtil.pack(new File("Download"), selectedFilesZipFile);
// return the zipped file obtained as result of the previous operation
return selectedFilesZipFile;
}
Now, modify your HttpHandler to perform the download:
server.createContext("/files/downloadZip", new HttpHandler() {
#Override
public void handle(HttpExchange exchange) throws IOException {
if (!handleTokenPreflight(exchange)) { return; }
System.out.println(exchange.getRequestURI());
Map<String, String> queryParam = parseQueryParam(exchange.getRequestURI().getQuery());
String authToken = exchange.getRequestHeaders().getFirst("token");
String target = queryParam.get("target") + ",";
String[] files = new String[Integer.parseInt(queryParam.get("numberOfFiles"))];
[...] // I process the data in this entire method and send it to the previous method that creates a zip
// Get a reference to the zipped file
File selectedFilesZipFile = Controller.getDownloadZip(files, folderName);
// Set the appropiate Content-Type
exchange.getResponseHeaders().set("Content-Type", "application/zip");
// Optionally, if the file is downloaded in an anchor, set the appropiate content disposition
// exchange.getResponseHeaders().add("Content-Disposition", "attachment; filename=selected-files.zip");
// Download the file. I used java.nio.Files to copy the file contents, but please, feel free
// to use other option like java.io or the Commons-IO library, for instance
exchange.sendResponseHeaders(200, selectedFilesZipFile.length());
try (OutputStream responseBody = httpExchange.getResponseBody()) {
Files.copy(selectedFilesZipFile.toPath(), responseBody);
responseBody.flush();
}
}
});
Now the problem is how to deal with the download in Angular.
As suggested in the previous code, if the resource is public or you have a way to manage your security token, including it as a parameter in the URL, for instance, one possible solution is to not use Angular HttpClient but an anchor with an href that points to your ever backend handler method directly.
If you need to use Angular HttpClient, perhaps to include your auth tokens, then you can try the approach proposed in this great SO question.
First, in your handler, encode to Base64 the zipped file contents to simplify the task of byte handling (in a general use case, you can typically return from your server a JSON object with the file content and metadata describing that content, like content-type, etcetera):
server.createContext("/files/downloadZip", new HttpHandler() {
#Override
public void handle(HttpExchange exchange) throws IOException {
if (!handleTokenPreflight(exchange)) { return; }
System.out.println(exchange.getRequestURI());
Map<String, String> queryParam = parseQueryParam(exchange.getRequestURI().getQuery());
String authToken = exchange.getRequestHeaders().getFirst("token");
String target = queryParam.get("target") + ",";
String[] files = new String[Integer.parseInt(queryParam.get("numberOfFiles"))];
[...] // I process the data in this entire method and send it to the previous method that creates a zip
// Get a reference to the zipped file
File selectedFilesZipFile = Controller.getDownloadZip(files, folderName);
// Set the appropiate Content-Type
exchange.getResponseHeaders().set("Content-Type", "application/zip");
// Download the file
byte[] fileContent = Files.readAllBytes(selectedFilesZipFile.toPath());
byte[] base64Data = Base64.getEncoder().encode(fileContent);
exchange.sendResponseHeaders(200, base64Data.length);
try (OutputStream responseBody = httpExchange.getResponseBody()) {
// Here I am using Commons-IO IOUtils: again, please, feel free to use other alternatives for writing
// the base64 data to the response outputstream
IOUtils.write(base64Data, responseBody);
responseBody.flush();
}
}
});
After that, use the following code in you client side Angular component to perform the download:
this.downloadService.httpGetDownloadZip(['target1','target2']).pipe(
tap((b64Data) => {
const blob = this.b64toBlob(b64Data, 'application/zip');
const blobUrl = URL.createObjectURL(blob);
window.open(blobUrl);
})
).subscribe()
As indicated in the aforementioned question, b64toBlob will look like this:
private b64toBlob(b64Data: string, contentType = '', sliceSize = 512) {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
Probably you will need to slightly modify the httpGetDownloadZip method in your service to take into account the returned base 64 data - basically, change ServerAnswer to string as the returned information type:
httpGetDownloadZip(target: string[]): Observable<string> {
const params = new HttpParams().set('target', String(target)).set('numberOfFiles', String(target.length));
const headers = new HttpHeaders().set('token', this.tokenService.getStorageToken());
const options = {
headers,
params,
};
return this.http
.get<string>(this.BASE_URL + '/files/downloadZip', options)
.pipe(catchError(this.handleError<ServerAnswer>('httpGetZip')));
}
You could try using responseType as arraybuffer.
Eg:
return this.http.get(URL_API_REST + 'download?filename=' + filename, {
responseType: 'arraybuffer'
});
In My Project including both front end (angular) and back end (java).
We used the below solution ( hope it work for you ):
Angular:
https://github.com/eligrey/FileSaver.js
let observable = this.downSvc.download(opts);
this.handleData(observable, (data) => {
let content = data;
const blob = new Blob([content], { type: 'application/pdf' });
saveAs(blob, file);
});
Java:
public void download(HttpServletRequest request,HttpServletResponse response){
....
response.setHeader("Content-Disposition",
"attachment;filename=\"" + fileName + "\"");
try (
OutputStream os = response.getOutputStream();
InputStream is = new FileInputStream(file);) {
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) > -1) {
os.write(buf, 0, len);
}
os.flush();
}
You can still use HttpServletRequest on the server...
Then get its OutputStream and write to it.
#RequestMapping(method = RequestMethod.POST , params="action=downloadDocument")
public String downloadDocument(#RequestParam(value="documentId", required=true) String documentId,
HttpServletRequest request,
HttpServletResponse response )
{
try {
String docName = null;
String documentSavePath = getDocumentSavePath();
PDocument doc = mainService.getDocumentById(iDocumentId);
if(doc==null){
throw new RuntimeException("document with id: " + documentId + " not found!");
}
docName = doc.getName();
String path = documentSavePath + ContextUtils.fileSeperator() + docName;
response.setHeader("Content-Disposition", "inline;filename=\"" + docName + "\"");
OutputStream out = response.getOutputStream();
response.setContentType("application/word");
FileInputStream stream = new FileInputStream(path);
IOUtils.copy(stream, out);
out.flush();
out.close();
} catch(FileNotFoundException fnfe){
logger.error("Error downloading document! - document not found!!!! " + fnfe.getMessage() , fnfe);
} catch (IOException e) {
logger.error("Error downloading document!!! " + e.getMessage(),e);
}
return null;
}

How can I update custom properties in alfresco workflow task using only Java?

First, I want to say thanks to everyone that took their time to help me figure this out because I was searching for more than a week for a solution to my problem. Here it is:
My goal is to start a custom workflow in Alfresco Community 5.2 and to set some custom properties in the first task trough a web script using only the Public Java API. My class is extending AbstractWebScript. Currently I have success with starting the workflow and setting properties like bpm:workflowDescription, but I'm not able to set my custom properties in the tasks.
Here is the code:
public class StartWorkflow extends AbstractWebScript {
/**
* The Alfresco Service Registry that gives access to all public content services in Alfresco.
*/
private ServiceRegistry serviceRegistry;
public void setServiceRegistry(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
#Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
// Create JSON object for the response
JSONObject obj = new JSONObject();
try {
// Check if parameter defName is present in the request
String wfDefFromReq = req.getParameter("defName");
if (wfDefFromReq == null) {
obj.put("resultCode", "1 (Error)");
obj.put("errorMessage", "Parameter defName not found.");
return;
}
// Get the WFL Service
WorkflowService workflowService = serviceRegistry.getWorkflowService();
// Build WFL Definition name
String wfDefName = "activiti$" + wfDefFromReq;
// Get WorkflowDefinition object
WorkflowDefinition wfDef = workflowService.getDefinitionByName(wfDefName);
// Check if such WorkflowDefinition exists
if (wfDef == null) {
obj.put("resultCode", "1 (Error)");
obj.put("errorMessage", "No workflow definition found for defName = " + wfDefName);
return;
}
// Get parameters from the request
Content reqContent = req.getContent();
if (reqContent == null) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing request body.");
}
String content;
content = reqContent.getContent();
if (content.isEmpty()) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Content is empty");
}
JSONTokener jsonTokener = new JSONTokener(content);
JSONObject json = new JSONObject(jsonTokener);
// Set the workflow description
Map<QName, Serializable> params = new HashMap();
params.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "Workflow started from JAVA API");
// Start the workflow
WorkflowPath wfPath = workflowService.startWorkflow(wfDef.getId(), params);
// Get params from the POST request
Map<QName, Serializable> reqParams = new HashMap();
Iterator<String> i = json.keys();
while (i.hasNext()) {
String paramName = i.next();
QName qName = QName.createQName(paramName);
String value = json.getString(qName.getLocalName());
reqParams.put(qName, value);
}
// Try to update the task properties
// Get the next active task which contains the properties to update
WorkflowTask wfTask = workflowService.getTasksForWorkflowPath(wfPath.getId()).get(0);
// Update properties
WorkflowTask updatedTask = workflowService.updateTask(wfTask.getId(), reqParams, null, null);
obj.put("resultCode", "0 (Success)");
obj.put("workflowId", wfPath.getId());
} catch (JSONException e) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
e.getLocalizedMessage());
} catch (IOException ioe) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Error when parsing the request.",
ioe);
} finally {
// build a JSON string and send it back
String jsonString = obj.toString();
res.getWriter().write(jsonString);
}
}
}
Here is how I call the webscript:
curl -v -uadmin:admin -X POST -d #postParams.json localhost:8080/alfresco/s/workflow/startJava?defName=nameOfTheWFLDefinition -H "Content-Type:application/json"
In postParams.json file I have the required pairs for property/value which I need to update:
{
"cmprop:propOne" : "Value 1",
"cmprop:propTwo" : "Value 2",
"cmprop:propThree" : "Value 3"
}
The workflow is started, bpm:workflowDescription is set correctly, but the properties in the task are not visible to be set.
I made a JS script which I call when the workflow is started:
execution.setVariable('bpm_workflowDescription', 'Some String ' + execution.getVariable('cmprop:propOne'));
And actually the value for cmprop:propOne is used and the description is properly updated - which means that those properties are updated somewhere (on execution level maybe?) but I cannot figure out why they are not visible when I open the task.
I had success with starting the workflow and updating the properties using the JavaScript API with:
if (wfdef) {
// Get the params
wfparams = {};
if (jsonRequest) {
for ( var prop in jsonRequest) {
wfparams[prop] = jsonRequest[prop];
}
}
wfpackage = workflow.createPackage();
wfpath = wfdef.startWorkflow(wfpackage, wfparams);
The problem is that I only want to use the public Java API, please help.
Thanks!
Do you set your variables locally in your tasks? From what I see, it seems that you define your variables at the execution level, but not at the state level. If you take a look at the ootb adhoc.bpmn20.xml file (https://github.com/Activiti/Activiti-Designer/blob/master/org.activiti.designer.eclipse/src/main/resources/templates/adhoc.bpmn20.xml), you can notice an event listener that sets the variable locally:
<extensionElements>
<activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
<activiti:field name="script">
<activiti:string>
if (typeof bpm_workflowDueDate != 'undefined') task.setVariableLocal('bpm_dueDate', bpm_workflowDueDate);
if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;
</activiti:string>
</activiti:field>
</activiti:taskListener>
</extensionElements>
Usually, I just try to import all tasks for my custom model prefix. So for you, it should look like that:
import java.util.Set;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.apache.log4j.Logger;
public class ImportVariables extends AbstractTaskListener {
private Logger logger = Logger.getLogger(ImportVariables.class);
#Override
public void notify(DelegateTask task) {
logger.debug("Inside ImportVariables.notify()");
logger.debug("Task ID:" + task.getId());
logger.debug("Task name:" + task.getName());
logger.debug("Task proc ID:" + task.getProcessInstanceId());
logger.debug("Task def key:" + task.getTaskDefinitionKey());
DelegateExecution execution = task.getExecution();
Set<String> executionVariables = execution.getVariableNamesLocal();
for (String variableName : executionVariables) {
// If the variable starts by "cmprop_"
if (variableName.startsWith("cmprop_")) {
// Publish it at the task level
task.setVariableLocal(variableName, execution.getVariableLocal(variableName));
}
}
}
}

Programatic java ATL launch not working

I have a huge problem:
I have an ATL transformation that works flawlessly when I use the normal way to do it, using the atl plugin.
But when I try to java launch, it cannot find the classes of a model (org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'operationalTemplateGroup' is not found or is abstract)
Exemple:
I have a "operationalTemplateGroup" class on my model, but the metamodel describes it as :
<eClassifiers xsi:type="ecore:EClass" name="OPERATIONALTEMPLATEGROUP">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="OPERATIONALTEMPLATEGROUP"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
See, OPERATIONALTEMPLATEGROUP and not operationalTemplateGroup, why the plugin can parse it but the java launcher dont?
I have this problem with only 1 model, the others are working fine, and if I change it to operationalTemplateGroup it will work, until the next class that has the same problem, ( About 6000 lines of code ).
Method:
public static void main(String[] args) {
try {
System.out.println("Running...");
/*
* Paths
*/
String inModelPath = "models/architectures/AToMS.acmehealth";
String inModelPath2 = "models/openehr/TemplatesAToMS.openehr2008v4";
String outModelPath = "models/richubi/TemplatesAToMS22.rich_interface_model";
String archMMPath = "metamodels/architectures/ACMEHealthv2.ecore";
String openEHRMMPath = "metamodels/openehr/openehr2008v4.ecore";
String richUbiMMPath = "metamodels/richubi/rich_interface_components.ecore";
System.out.println("inModelPath: " inModelPath);
System.out.println("inModelPath2: " inModelPath2);
System.out.println("outModelPath: " outModelPath);
System.out.println("archMMPath: " archMMPath);
System.out.println("openEHRMMPath: " openEHRMMPath);
System.out.println("richUbiMMPath: " richUbiMMPath);
/*
* Initializations
*/
ILauncher transformationLauncher = new EMFVMLauncher();
ModelFactory modelFactory = new EMFModelFactory();
IInjector injector = new EMFInjector();
IExtractor extractor = new EMFExtractor();
/*
* Load metamodels
*/
IReferenceModel archMetamodel = modelFactory.newReferenceModel();
injector.inject(archMetamodel, archMMPath);
IReferenceModel openEHRMetamodel = modelFactory.newReferenceModel();
injector.inject(openEHRMetamodel, openEHRMMPath);
IReferenceModel richUbiMetamodel = modelFactory.newReferenceModel();
injector.inject(richUbiMetamodel, richUbiMMPath);
System.out.println("Metamodels loaded.");
/*
* Load models and run transformation
*/
IModel inModel2 = modelFactory.newModel(openEHRMetamodel);
injector.inject(inModel2, inModelPath2); // ERROR HERE!!!!
IModel inModel = modelFactory.newModel(archMetamodel);
injector.inject(inModel, inModelPath);
IModel outModel = modelFactory.newModel(richUbiMetamodel);
System.out.println("IN, OUT models loaded.");
System.out.print("Running ATL trasformation...");
transformationLauncher.initialize(new HashMap<String, Object>());
transformationLauncher.addLibrary("LibHelpers",
new FileInputStream("transformations/LibHelpers.asm"));
transformationLauncher.addInModel(inModel, "archMM", "openEHRMM");
transformationLauncher.addInModel(inModel2, null, "openEHRMM");
transformationLauncher.addOutModel(outModel, "archM", "openEHRM");
transformationLauncher
.launch(ILauncher.RUN_MODE,
new NullProgressMonitor(),
new HashMap<String, Object>(),
new FileInputStream(
"/home/operador/workspace/Transformation/transformations/OpenEHRTemplates2RichUbi.asm"));
System.out.println("Done.");
System.out.print("Extracting OUT model...");
extractor.extract(outModel, outModelPath);
System.out.println("Done.");
/*
* Unload all models and metamodels (EMF-specific)
*/
EMFModelFactory emfModelFactory = (EMFModelFactory) modelFactory;
emfModelFactory.unload((EMFModel) inModel);
emfModelFactory.unload((EMFModel) outModel);
emfModelFactory.unload((EMFReferenceModel) archMetamodel);
emfModelFactory.unload((EMFReferenceModel) openEHRMetamodel);
} catch (ATLCoreException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

how can i unmarshall in jaxb and enjoy the schema validation without using an explicit schema file

I am using jaxb for my application configurations
I feel like I am doing something really crooked and I am looking for a way to not need an actual file or this transaction.
As you can see in code I:
1.create a schema into a file from my JaxbContext (from my class annotation actually)
2.set this schema file in order to allow true validation when I unmarshal
JAXBContext context = JAXBContext.newInstance(clazz);
Schema mySchema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);
jaxbContext.generateSchema(new MySchemaOutputResolver()); // ultimately creates schemaFile
Unmarshaller u = m_context.createUnmarshaller();
u.setSchema(mySchema);
u.unmarshal(...);
do any of you know how I can validate jaxb without needing to create a schema file that sits in my computer?
Do I need to create a schema for validation, it looks redundant when I get it by JaxbContect.generateSchema ?
How do you do this?
Regarding ekeren's solution above, it's not a good idea to use PipedOutputStream/PipedInputStream in a single thread, lest you overflow the buffer and cause a deadlock. ByteArrayOutputStream/ByteArrayInputStream works, but if your JAXB classes generate multiple schemas (in different namespaces) you need multiple StreamSources.
I ended up with this:
JAXBContext jc = JAXBContext.newInstance(Something.class);
final List<ByteArrayOutputStream> outs = new ArrayList<ByteArrayOutputStream>();
jc.generateSchema(new SchemaOutputResolver(){
#Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
outs.add(out);
StreamResult streamResult = new StreamResult(out);
streamResult.setSystemId("");
return streamResult;
}});
StreamSource[] sources = new StreamSource[outs.size()];
for (int i=0; i<outs.size(); i++) {
ByteArrayOutputStream out = outs.get(i);
// to examine schema: System.out.append(new String(out.toByteArray()));
sources[i] = new StreamSource(new ByteArrayInputStream(out.toByteArray()),"");
}
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
m.setSchema(sf.newSchema(sources));
m.marshal(docs, new DefaultHandler()); // performs the schema validation
I had the exact issue and found a solution in the Apache Axis 2 source code:
protected List<DOMResult> generateJaxbSchemas(JAXBContext context) throws IOException {
final List<DOMResult> results = new ArrayList<DOMResult>();
context.generateSchema(new SchemaOutputResolver() {
#Override
public Result createOutput(String ns, String file) throws IOException {
DOMResult result = new DOMResult();
result.setSystemId(file);
results.add(result);
return result;
}
});
return results;
}
and after you've acquired your list of DOMResults that represent the schemas, you will need to transform them into DOMSource objects before you can feed them into a schema generator. This second step might look something like this:
Unmarshaller u = myJAXBContext.createUnmarshaller();
List<DOMSource> dsList = new ArrayList<DOMSource>();
for(DOMResult domresult : myDomList){
dsList.add(new DOMSource(domresult.getNode()));
}
String schemaLang = "http://www.w3.org/2001/XMLSchema";
SchemaFactory sFactory = SchemaFactory.newInstance(schemaLang);
Schema schema = sFactory.newSchema((DOMSource[]) dsList.toArray(new DOMSource[0]));
u.setSchema(schema);
I believe you just need to set a ValidationEventHandler on your unmarshaller. Something like this:
public class JAXBValidator extends ValidationEventCollector {
#Override
public boolean handleEvent(ValidationEvent event) {
if (event.getSeverity() == event.ERROR ||
event.getSeverity() == event.FATAL_ERROR)
{
ValidationEventLocator locator = event.getLocator();
// change RuntimeException to something more appropriate
throw new RuntimeException("XML Validation Exception: " +
event.getMessage() + " at row: " + locator.getLineNumber() +
" column: " + locator.getColumnNumber());
}
return true;
}
}
And in your code:
Unmarshaller u = m_context.createUnmarshaller();
u.setEventHandler(new JAXBValidator());
u.unmarshal(...);
If you use maven using jaxb2-maven-plugin can help you. It generates schemas in generate-resources phase.

Categories