Extracting the values of elements (XML) which is in an array and put it in the message property in Camel - dom

As you can see the Availability Flag, BeamId is getting repeated. How do I traverse and set the property for Availability Flag1 and so on, so that I can later fetch it with velocity template?
Payload:<ns2:TransportFeasibilityResponse>
<ns2:Parameters>
<ns2:AvailabilityFlag>true</ns2:AvailabilityFlag>
<ns2:SatellitedID>H1B</ns2:SatellitedID>
<ns2:BeamID>675</ns2:BeamID>
<ns2:TransportName>Earth</ns2:TransportName>
</ns2:FeasibilityParameters>
<ns2:Parameters>
<ns2:AvailabilityFlag>true</ns2:AvailabilityFlag>
<ns2:SatellitedID>J34</ns2:SatellitedID>
<ns2:BeamID>111</ns2:BeamID>
<ns2:TransportName>Jupiter</ns2:TransportName>
</ns2:Parameters>
</ns2:TransportFeasibilityResponse>
</ns2:TransportFeasibilityResponseMsg>
Code: (Its not complete)
public static HashMap<String,String> extractNameValueToProperties(String msgBody, selectedKeyList, namelist) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setExpandEntityReferences(false);
factory.setNamespaceAware(true);
Document doc = null;
try{
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(msgBody)));
} catch(Exception ex) {
Exception actException = new Exception( "Exception while extracting tagvalues", ex);
throw actException;
}
HashMap<String,String> tagNameValueMap = new HashMap<String,String>();
NodeList nodeList = doc.getElementsByTagName("*");
// Trying to enter the TransportFeasibilityResponse element
for (int i = 0; i < nodeList.getLength(); i++) {
Node indNode = nodeList.item(i);
if (indNode.indexOf(String name)>-1);
//checking for Availability flag and similar namelist
dataKey = indNode.getTextContent();
message.setProperty(selectedKeyList[k], dataKey);
k++;
j++;
else
{
continue;
}
}
}
Here,
I am setting these values in my route:
<setProperty propertyName="namelist">
<constant>AvailabilityFlag,SatellitedID,BeamID</constant>
</setProperty>
<setProperty propertyName="selectedKeyList">
<constant>AvailabilityFlag1,SatellitedID1,BeamID1,AvailabilityFlag2,SatellitedID2,BeamID2 </constant>
</setProperty>
<bean beanType="com.gdg.dgdgdg.javacodename" method="extractNameValueToProperties"/>
Question: Please tell me how I can parse through the repeating elements and assign it to the property?
Thanks

I'm not sure if I understand your question correctly, but I think you could use the Splitter pattern to split your xml per Parameters tag and process each other separately and aggregate it later.
Take for example this input:
<TransportFeasibilityResponse>
<Parameters>
<AvailabilityFlag>true</AvailabilityFlag>
<SatellitedID>H1B</SatellitedID>
<BeamID>675</BeamID>
<TransportName>Earth</TransportName>
</Parameters>
<Parameters>
<AvailabilityFlag>true</AvailabilityFlag>
<SatellitedID>J34</SatellitedID>
<BeamID>111</BeamID>
<TransportName>Jupiter</TransportName>
</Parameters>
</TransportFeasibilityResponse>
A route to process this input could be something like this:
from("direct:start")
.split(xpath("/TransportFeasibilityResponse/Parameters"), new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
List<String> beamIDs = null;
if (oldExchange == null) { // first
beamIDs = new ArrayList<String>();
} else {
beamIDs = oldExchange.getIn().getBody(List.class);
}
beamIDs.add(newExchange.getIn().getBody(String.class));
newExchange.getIn().setBody(beamIDs);
return newExchange;
}
})
.setBody(xpath("/Parameters/BeamID/text()"))
.end()
.log("The final body: ${body}");
First, we split the input per Parameters tag, and then extract the BeamID from it. After that, the AggregationStrategy aggregates each message into one, grouping by BeamID.
The final message should have the a body like this:
675,111
The data I put in the body just for an example, but you could set anywhere you want into the Exchange you are manipulating inside the AggregationStrategy implementation.

Related

Using a Beakerx Custom Magic

I've created a custom Magic command with the intention of generating a spark query programatically. Here's the relevant part of my class that implements the MagicCommandFunctionality:
MagicCommandOutcomeItem execute(MagicCommandExecutionParam magicCommandExecutionParam) {
// get the string that was entered:
String input = magicCommandExecutionParam.command.substring(MAGIC.length())
// use the input to generate a query
String generatedQuery = Interpreter.interpret(input)
MIMEContainer result = Text(generatedQuery);
return new MagicCommandOutput(MagicCommandOutcomeItem.Status.OK, result.getData().toString());
}
This works splendidly. It returns the command that I generated. (As text)
My question is -- how do I coerce the notebook into evaluating that value in the cell? My guess is that a SimpleEvaluationObject and TryResult are involved, but I can't find any examples of their use
Rather than creating the MagicCommandOutput I probably want the Kernel to create one for me. I see that the KernelMagicCommand has an execute method that would do that. Anyone have any ideas?
Okay, I found one way to do it. Here's my solution:
You can ask the current kernelManager for the kernel you're interested in,
then call PythonEntryPoint.evaluate. It seems to do the job!
#Override
MagicCommandOutcomeItem execute(MagicCommandExecutionParam magicCommandExecutionParam) {
String input = magicCommandExecutionParam.command.substring(MAGIC.length() + 1)
// this is the Scala code I want to evaluate:
String codeToExecute = <your code here>
KernelFunctionality kernel = KernelManager.get()
PythonEntryPoint pep = kernel.getPythonEntryPoint(SCALA_KERNEL)
pep.evaluate(codeToExecute)
pep.getShellMsg()
List<Message> messages = new ArrayList<>()
//until there are messages on iopub channel available collect them into response
while (true) {
String iopubMsg = pep.getIopubMsg()
if (iopubMsg == "null") break
try {
Message msg = parseMessage(iopubMsg) //(I didn't show this part)
messages.add(msg)
String commId = (String) msg.getContent().get("comm_id")
if (commId != null) {
kernel.addCommIdManagerMapping(commId, SCALA_KERNEL)
}
} catch (IOException e) {
log.error("There was an error: ${e.getMessage()}")
return new MagicKernelResponse(MagicCommandOutcomeItem.Status.ERROR, messages)
}
}
return new MagicKernelResponse(MagicCommandOutcomeItem.Status.OK, messages)
}

Indexing fails on enabling force index in Titan/Janus

I've written a JUnit Test to check against the generate-modern.groovy graph if marko exists.
My gremlin query being
"g.V().has('name','marko')";
As you can see in the generate-modern.groovy file that indexing is already applied on the name property of the person.
I later made the following
query.force-index=true
property true in the dynamodb.properties file which blocks whole graph scan thereby making indexing mandatory.
However it throws me the following exception
org.janusgraph.core.JanusGraphException: Could not find a suitable index to answer graph query and graph scans are disabled: [(name = marko)]:VERTEX
The above exception is raised from the following StandardJanusGraphTx class's method
#Override
public Iterator<JanusGraphElement> execute(final GraphCentricQuery query, final JointIndexQuery indexQuery, final Object exeInfo, final QueryProfiler profiler) {
Iterator<JanusGraphElement> iter;
if (!indexQuery.isEmpty()) {
List<QueryUtil.IndexCall<Object>> retrievals = new ArrayList<QueryUtil.IndexCall<Object>>();
for (int i = 0; i < indexQuery.size(); i++) {
final JointIndexQuery.Subquery subquery = indexQuery.getQuery(i);
retrievals.add(new QueryUtil.IndexCall<Object>() {
#Override
public Collection<Object> call(int limit) {
final JointIndexQuery.Subquery adjustedQuery = subquery.updateLimit(limit);
try {
return indexCache.get(adjustedQuery, new Callable<List<Object>>() {
#Override
public List<Object> call() throws Exception {
return QueryProfiler.profile(subquery.getProfiler(), adjustedQuery, q -> indexSerializer.query(q, txHandle));
}
});
} catch (Exception e) {
throw new JanusGraphException("Could not call index", e.getCause());
}
}
});
}
List<Object> resultSet = QueryUtil.processIntersectingRetrievals(retrievals, indexQuery.getLimit());
iter = com.google.common.collect.Iterators.transform(resultSet.iterator(), getConversionFunction(query.getResultType()));
} else {
if (config.hasForceIndexUsage()) throw new JanusGraphException("Could not find a suitable index to answer graph query and graph scans are disabled: " + query);
log.warn("Query requires iterating over all vertices [{}]. For better performance, use indexes", query.getCondition());
QueryProfiler sub = profiler.addNested("scan");
sub.setAnnotation(QueryProfiler.QUERY_ANNOTATION,indexQuery);
sub.setAnnotation(QueryProfiler.FULLSCAN_ANNOTATION,true);
sub.setAnnotation(QueryProfiler.CONDITION_ANNOTATION,query.getResultType());
switch (query.getResultType()) {
case VERTEX:
return (Iterator) getVertices().iterator();
case EDGE:
return (Iterator) getEdges().iterator();
case PROPERTY:
return new VertexCentricEdgeIterable(getInternalVertices(),RelationCategory.PROPERTY).iterator();
default:
throw new IllegalArgumentException("Unexpected type: " + query.getResultType());
}
}
return iter;
}
};
As you can observe from the method that the exception is raised when the JointIndexQuery object is empty(arrayList being empty) and force index is true.
The problem is why the list is empty? when we have specified the indexing query against the name property in the generate-modern.groovy while querying from a JUnit Test.This works fine meaning the list is not empty when the same data is being preloaded into the gremlin server with the same file.
The personByName index definition uses a label constraint.
def personByName = mgmt.buildIndex("personByName", Vertex.class).addKey(name).indexOnly(person).buildCompositeIndex()
In order to take advantage of that index, you must use the label and the property. For example:
g.V().has('person', 'name', 'marko')
You can read more about this in the JanusGraph documentation http://docs.janusgraph.org/latest/indexes.html#_label_constraint

Codefluent Potentially dangerous Request.Form value with richtext fields

After adding the multivalue (flags) enumeration solution (which works very well, thank you) from
http://blog.codefluententities.com/tag/multi-enumeration-values/
to our MVC project we are now getting the dreaded "Potentially dangerous Request.Form value" on richtext fields across the board that we're using to generate html with a wysiwyg editor (summernote in this case).
If I remove summernote and just submit plain text the fields work perfectly, however putting any html code into the text input generates the error.
Fortunately, the error is coming out of the code just added (above) for the multi-enumeration on line 246:
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (Description="...rem ipsum <strong>dolor</stron...").
Source Error:
Line 244: continue;
Line 245:
Line 246: Add(name, nvc[name]);
Line 247:
Line 248: }
EDIT:
For clarity, here is the whole method in question:
private void AddRange(NameValueCollection nvc)
{
foreach (string name in nvc)
{
// handle MultiSelectList templates
const string listSelectedToken = ".list.item.Selected";
const string listValueToken = ".list.item.Value";
if (name.EndsWith(listSelectedToken))
{
List<bool> bools = CodeFluent.Runtime.Utilities.ConvertUtilities.SplitToList<bool>(nvc[name], ',');
string propertyName = name.Substring(0, name.Length - listSelectedToken.Length);
string valueKey = propertyName + listValueToken;
List<string> keys = CodeFluent.Runtime.Utilities.ConvertUtilities.SplitToList<string>(nvc[valueKey], ',');
int j = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.Count; i++)
{
if (bools[j])
{
if (sb.Length > 0)
{
sb.Append(CodeFluentConfiguration.EntityKeyListSeparator);
}
sb.Append(keys[i]);
j++;
}
j++;
}
Add(propertyName, sb.ToString());
continue;
}
if (name.EndsWith(listValueToken))
continue;
Add(name, nvc[name]);
}
}
Have I missed something in the multi-value implementation?
Thanks,
Russ
I don't think this error is related to the use of a multi-valued enumeration. In fact you post a value for the Description field that contains HTML tags (strong). By default ASP.NET prevents this and throw a validation exception.
If you expect your users to enter HTML, you must instruct ASP.NET to authorize HTML.
Change the EntityValueProvider
AddRange(context.HttpContext.Request.Unvalidated.Form); // Add Unvalidated
AddRange(context.HttpContext.Request.Unvalidated.QueryString);
Or use the web.config: validateRequest or requestValidationMode
<system.web>
<pages validateRequest="false" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
Use AllowHtmlAttribute
public class Sample
{
[AllowHtml]
public string Description {get;set;}
}
Or ValidateInputAttribute
[HttpPost, ValidateInput(true, Exclude = "Description")]
public ActionResult Edit(int id, FormCollection collection)
{
...
}

How to read non repeating elements while implementing an Item Reader?

I have a scenario where I have to read an xml like this:
<MovieList language="English">
<Movie>..<Movie>
<Movie>..<Movie>
</MovieList>
I have to read the Movie Tag which is a complex object(tag) and insert the details into a movie table. I have set the fragmentRootElementName as Movie and able to read the Movie tag completely. How ever, I am not able to read the language attribute, which is not a repeating tag.
How should I be fetching the non repeating tag details? Should I parse the XML myself to read it? or Should I write one more fragmentRootElementName just to read the language attribute?
The configuration for item reader is as below:
<bean id="movieReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="unmarshaller" ref="marshaller" />
<property name="fragmentRootElementName" value="Movie" />
<property name="resource" value="file:#{jobParameters['inputFile']}" />
</bean>
Ok, the simple way would be to define your fragmentRootElementName = MovieList but i guess this is not a good idea if your MovieList can contains millions of movies!!
I had a similar problem where i needed to knew the parent tag of my fragmentRootElementName.
So we created a CustomStaxEventItemReader that extends the original StaxEventItemReader.
We added a property parentElement that you can set in your config. and we override the method moveCursorToNextFragment() and doRead() to be able to deal with this problem!
Now the code i have don't do exactly what you need, but i modified it and it look like it works!!!
protected boolean moveCursorToNextFragment(XMLEventReader reader) {
try {
while (true) {
while (reader.peek() != null && !reader.peek().isStartElement()) {
reader.nextEvent();
}
if (reader.peek() == null) {
return false;
}
XMLEvent ev = reader.peek();
QName startElementName = ((StartElement) ev).getName();
// Take note of current parent element. Must be one of
// ParentTags
String tmp = startElementName.getLocalPart();
for (ParentTags aTag : ParentTags.values()) {
if (aTag.toString().equals(tmp)) {
currentParent = tmp;
Attribute attr = ((StartElement) ev)
.getAttributeByName(new QName("Language"));
if (null != attr) {
parentAttribute = attr.getValue();
}
break;
}
}
if (startElementName.getLocalPart().equals(
fragmentRootElementName)) {
if ((fragmentRootElementNameSpace == null && parentElement
.equals(currentParent))
|| startElementName.getNamespaceURI().equals(
fragmentRootElementNameSpace)) {
return true;
}
}
reader.nextEvent();
}
} catch (XMLStreamException e) {
throw new DataAccessResourceFailureException(
"Error while reading from event reader", e);
}
}
So basically, if you look at the code of the original StaxEventReader, you will see that it pass thru all the element of the xml file. Each time it get an element with the name = to your rootElement, it return true and the doRead unmarshall it and return the object associated.
Now, we only add some code to also find a given parent element. I have used an Enum called ParentTags because my XML was a bit more complexe, but you could only compare the name of the new parentElement defined in your config.
So if the actual element isEquals to your parentElement, you simply assign it to currentParent and try to get your attribute. If not null, assign it to parentAttribute property.
then in your doRead() method, you can access the parentAttribute property and set it in your domain object!
protected T doRead() throws Exception {
if (noInput) {
return null;
}
T item = null;
if (moveCursorToNextFragment(fragmentReader)) {
fragmentReader.markStartFragment();
#SuppressWarnings("unchecked")
T mappedFragment = (T) unmarshaller.unmarshal(StaxUtils
.createStaxSource(fragmentReader));
item = mappedFragment;
logger.info("Item read : " + item);
currentIndex = cIndex.getAndIncrement();
T p = (T) item;
if (p instanceof myDomainObj) {
myDomainObj pp = (myDomainObj) p;
logger.info(pp);
logger.info("attribute parent = " + parentAttribute);
pp.setLanguage(parentAttribute);
}
fragmentReader.markFragmentProcessed();
}
return item;
}
I hope this is clear enough!
Good luck and regards

Cannot create/update a Rally TestCaseResult with a reference to a specified TestSet

With Rally, I need to
Update a TestCaseResult with a new TestSet Ref.
OR
Create a new TestCaseResult by copying everything from a previous TestCaseResult and just changing the TestSet Ref.
I am trying to do the same through the Java REST toolkit from Rally. It uses the JSON REST API internally, it seems.
When I try to do this with CreateRequest or UpdateRequest, I get an error from the API "Could not set value for Test Set: null"
Is it not possible to update the TestSet of a TestCaseResult (whether existing or newly created)?
Here's some sample code I am using (showing create testcaseresult from existing by changing testset).
//get testcaseresult object
GetRequest tcrReq = new GetRequest("/testcaseresult/12345.js");
tcrReq.setFetch(new Fetch("FormattedID", "Name"));
GetResponse tcrResponse = restApi.get(tcrReq);
//update testcaseresult object with new testset
JsonObject tsRef = new JsonObject();
tsRef.addProperty("_ref", "https://rally1.rallydev.com/slm/webservice/1.39/testset/1029348.js");
tcrResponse.getObject().add("TestSet",tsRef);
tcrResponse.getObject().remove("_ref");
//Create API for new testcaseresult object
CreateRequest createRequest = new CreateRequest("testcaseresult", tcrResponse.getObject());
CreateResponse createResponse = restApi.create(createRequest);
if(createResponse.wasSuccessful()){
System.out.println(createResponse.getObject());
}else{
String[] ss = createResponse.getErrors();
for(int i=0; i<ss.length; i++){
System.out.println(ss[i]);
}
}
Can you please help to understand whether I am doing something wrong or is this a Rally limitation?
I believe the reason that you are getting the "Could not set value for Test Set: null" error message is that there is an "invisible" constraint on TestCaseResults whereby the TestCase to which they are associated must be scheduled into the TestSet of interest, before the TestCaseResult can be assigned that TestSet as an attribute.
Unfortunately, there's no TestSet attribute on TestCases, so you have to query the TestCases collection off of the TestSet, and then loop through that collection to check and see if the parent TestCase is a member of that collection. Once verified that the TestCase is in that TestSet's collection of TestCases, then you can proceed to successfully update a member TestCaseResult with that TestSet attribute of interest. I tested the below and it works as expected.
Here's a code snippet illustrating how to accomplish this:
// Create and configure a new instance of RallyRestApi
// Connection parameters
String rallyURL = "https://rally1.rallydev.com";
String wsapiVersion = "1.38";
String applicationName = "RestExample_UpdateTestSetOnTestCaseResult";
// Credentials
String userName = "user#company.com";
String userPassword = "password";
RallyRestApi restApi = new RallyRestApi(
new URI(rallyURL),
userName,
userPassword);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);
// Ref to Test Case Result of Interest
String testCaseResultRef = "/testcaseresult/1234567891.js";
GetRequest testCaseResultRequest = new GetRequest(testCaseResultRef);
GetResponse testCaseResultResponse = restApi.get(testCaseResultRequest);
JsonObject testCaseResultObj = testCaseResultResponse.getObject();
// Get the Test Case Result's Parent Test Case
JsonObject testCase = testCaseResultObj.get("TestCase").getAsJsonObject();
String testCaseRef = testCase.get("_ref").getAsString();
GetRequest testCaseRequest = new GetRequest(testCaseRef);
GetResponse testCaseResponse = restApi.get(testCaseRequest);
JsonObject testCaseObj = testCaseResponse.getObject();
System.out.println(testCaseRef);
// Ref to Test Set of Interest
String testSetRef = "/TestSet/12345678910.js";
// Get the Test Set of interest
GetRequest testSetRequest = new GetRequest(testSetRef);
GetResponse testSetResponse = restApi.get(testSetRequest);
JsonObject testSetObject = testSetResponse.getObject();
// Grab the Test Cases in this Test Set
JsonArray testCasesInTestSet = testSetObject.get("TestCases").getAsJsonArray();
// Loop through and see if our Test Case of interest is a member
boolean testCaseIsInSet = false;
for (int i=0; i<testCasesInTestSet.size(); i++) {
JsonObject thisTestCase = testCasesInTestSet.get(i).getAsJsonObject();
String thisTestCaseRef = thisTestCase.get("_ref").getAsString();
if (thisTestCaseRef.equals(testCaseRef)) {
testCaseIsInSet = true;
}
}
if (testCaseIsInSet) {
// Update Test Set on Existing Test Case Result
try {
//Add Test Set
System.out.println("\nUpdating Existing Test Case Result's Test Set attribute...");
testCaseResultObj.addProperty("TestSet", testSetRef);
UpdateRequest updateExistTestCaseResultRequest = new UpdateRequest(testCaseResultRef, testCaseResultObj);
UpdateResponse updateExistTestCaseResultResponse = restApi.update(updateExistTestCaseResultRequest);
if (updateExistTestCaseResultResponse.wasSuccessful()) {
System.out.println("Updated Test Case Result with new Test Set");
String[] updateExistTestCaseResultWarnings;
updateExistTestCaseResultWarnings = updateExistTestCaseResultResponse.getWarnings();
System.out.println("Warning(s) occurred updating Test Case Result: ");
for (int i=0; i<updateExistTestCaseResultWarnings.length;i++) {
System.out.println(updateExistTestCaseResultWarnings[i]);
}
} else {
String[] updateExistTestCaseResultErrors;
updateExistTestCaseResultErrors = updateExistTestCaseResultResponse.getErrors();
System.out.println("Error occurred updating Test Case Result: ");
for (int i=0; i<updateExistTestCaseResultErrors.length;i++) {
System.out.println(updateExistTestCaseResultErrors[i]);
}
}
} catch (Exception e) {
System.out.println("Exception occurred while updating Tags on existing Test Case: ");
e.printStackTrace();
}
finally {
//Release all resources
restApi.close();
}
} else {
System.out.println("Unable to Update Test Case Result with specified Test Set");
System.out.println("Parent Test Case is not a member of this Test Set");
}
}
When updating the TestSet you can just set the value as its ref- you don't need the wrapper object.
tcrResponse.getObject().add("TestSet", "/testset/1029348.js");