How to dynamically create a document in the GAE Search API? - java

I am trying to understand how i can dynamically add fields in my document.
I am following the doc . However i'd like to use the addField method inside a "for" statement and thus programmatically generate fields.
I've tried this :
Builder document = Document.newBuilder();
document.setId(id);
// for statement start
com.google.appengine.api.search.Field.Builder field = Field.newBuilder().setName(key).setAtom(value);
document.addField(field);
// for statement end
document.build();
But it doesn't seems to work. When i'm trying i got this printed out
com.google.appengine.api.search.Document$Builder#2604ed54
Whereas when i use the original code :
String myDocId = "PA6-5000";
Document doc = Document.newBuilder()
// Setting the document identifer is optional.
// If omitted, the search service will create an identifier.
.setId(myDocId)
.addField(Field.newBuilder().setName("content").setText("the rain in spain"))
.addField(Field.newBuilder().setName("email").setText(userEmail))
.addField(Field.newBuilder().setName("domain").setAtom(userDomain))
.addField(Field.newBuilder().setName("published").setDate(new Date()))
.build();
I get the expected result with the code above.
How i can manipulate this in order to put addfield() into for statement ?
thanks for helping.

The docs imply the Document.Builder is mutable, but typically you should reassign the builder after each invocation in case it isn't.
Builder document = Document.newBuilder();
document.setId(id);
com.google.appengine.api.search.Field.Builder field = Field.newBuilder().setName(key).setAtom(value);
document = document.addField(field);
document.build();
In your case what you're seeing is the toString() method on the builder, not the document itself. The problem is the last line.
Try this:
Builder builder = Document.newBuilder();
builder.setId(id);
com.google.appengine.api.search.Field.Builder field = Field.newBuilder().setName(key).setAtom(value);
builder = builder.addField(field);
Document document = builder.build();
Then you can just drop in your for loop and away you go.

Related

How to execute MongoDB findAndModify query in MongoCollection Java driver 3?

MongoDB 2.5 driver have DBCollection.findAndModify() method for this, but MongoCollection misses this method. After some search, I found that findOneAndUpdate() now has the same role.
But this method has different signature, don't understand how to use it. Here is command I want to execute
db.COL1.findAndModify({
query: { id: 2 },
update: {
$setOnInsert: { date: new Date(), reptype: 'EOD' }
},
new: true, // return new doc if one is upserted
upsert: true // insert the document if it does not exist
})
Documentation for findOneAndUpdate method states that
Returns:
the document that was updated. Depending on the value of the returnOriginal property, this will either be the document as it was before the update or as it is after the update.
but cannot find anything about this returnOriginal property. Anyone knows how to set it correctly?
A Java equivalent of your query should go roughly like this:
Document query = new Document("id", 2);
Document setOnInsert = new Document();
setOnInsert.put("date", new Date());
setOnInsert.put("reptype", "EOD");
Document update = new Document("$setOnInsert", setOnInsert);
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
options.returnDocument(ReturnDocument.AFTER);
options.upsert(true);
db.getCollection("COL1").findOneAndUpdate(query, update, options);
Regarding the returnOriginal property - you're right - there is no such thing. The javadoc is irrelevant in this place. However, there is a returnDocument property in FindOneAndUpdateOptions. You can set it to ReturnDocument.AFTER or ReturnDocument.BEFORE which is equivalent to new: true/false.

MongoDB Java Async Driver replaceOne doesn't seem to work

I am trying to update a document with MongoDB Async Java Driver and my code is below,
// jsonString is a string of "{_id=5715e426ed3522391f106e68, name=Alex}
final Document document = Document.parse(jsonString);
Document newDocument = document.append("status", "processing");
mongoDbCollection.replaceOne(document, newDocument, (updateResult, throwable) -> {
if (updateResult != null) {
log.info("UPDATED DOC ::::::>>> " + newDocument.toJson());
log.info("UPDATED RESULT ::::>> "+updateResult.toString());
} else {
throwable.printStackTrace();
log.error(throwable.getMessage());
}
});
As per the logging, I do see the updated document as below,
INFO: UPDATED DOC ::::::>>> { "_id" : { "$oid" : "5715e426ed3522391f106e68" }, "status":"processing"}
INFO: UPDATED RESULT ::::>> AcknowledgedUpdateResult{matchedCount=0, modifiedCount=0, upsertedId=null}
But when I see the collection via Robmongo I do not see the updated document and it still shows the old document. I have double checked I am looking in the same collection and there aren't any exceptions. Am I doing something wrong here?
The problem here is this line:
Document newDocument = document.append("status", "processing");
Where you "thought" you were just assigning a "new document copy", but actually this "also" modifies the document object to append the field.
As such, the query does not match, just as indicated in your output:
{matchedCount=0, modifiedCount=0, upsertedId=null}
// ^ Right here! See 0 matched
So what you want is a "clone". It's not straightforward with a Document, but can be done with this slightly "hacky" method:
Document document = Document.parse(jsonString);
Document newDocument = Document.parse(document.toJson()).append("status","processing");
System.out.println(newDocument);
System.out.println(document);
Now you will see that newDocument contains the addition, whilst document remains unaltered, which is not the case with your current code, and why the query does not match anything to update.

xades4j: Sign only one element

I'm using this code to sign a xml document:
Document doc = getDocument(xml_to_sign);
Element elemToSign = doc.getDocumentElement();
String file_uri_path = elemToSign.getBaseURI();
DataObjectDesc obj1 = new DataObjectReference(file_uri_path).withType("http://www.gzs.si/shemas/eslog/racun/1.5#Racun");
SignedDataObjects dataObjs = new SignedDataObjects(obj1);
signer.sign(dataObjs, elemToSign);
xml_to_sign is the full path to the xml file.
The problem is, that I would like to sign only the node with the id "data" (#data), but append the signature to the node elemToSign.
Is it possible to do this with xades4j?
Yes, it is. The sign method's argument is the parent node, not the element to sign (it could be the same node, depending on the configured references). In your example you should add a reference for "#data":
Document doc = getDocument(xml_to_sign);
Element parent = doc.getDocumentElement();
DataObjectDesc obj1 = new DataObjectReference("#data").withType("http://www.gzs.si/shemas/eslog/racun/1.5#Racun");
SignedDataObjects dataObjs = new SignedDataObjects(obj1);
signer.sign(dataObjs, parent);
Another option is to add a reference for the whole XML document (empty URI) and use a XPath transform.
You should specify that the attribute named "Id" in your xml document is the XML ID attribute that Apache Santuario (used internally by Xades4j) will use in the getElementById() (as lgoncalves has pointed out in his commments to his own answer).
Element parent = doc.getDocumentElement();
parent.setIdAttribute("Id", true);
//or parent.setIdAttributeNS("http://your.name.space", "Id", true);
I had the same problem, and this additional line of code solved it.

Parser JSoup change the tags to lower case letter

I did some research and it seems that is standard Jsoup make this change. I wonder if there is a way to configure this or is there some other Parser I can be converted to a document of Jsoup, or some way to fix this?
Unfortunately not, the constructor of Tag class changes the name to lower case:
private Tag(String tagName) {
this.tagName = tagName.toLowerCase();
}
But there are two ways to change this behavour:
If you want a clean solution, you can clone / download the JSoup Git and change this line.
If you want a dirty solution, you can use reflection.
Example for #2:
Field tagName = Tag.class.getDeclaredField("tagName"); // Get the field which contains the tagname
tagName.setAccessible(true); // Set accessible to allow changes
for( Element element : doc.select("*") ) // Iterate over all tags
{
Tag tag = element.tag(); // Get the tag of the element
String value = tagName.get(tag).toString(); // Get the value (= name) of the tag
if( !value.startsWith("#") ) // You can ignore all tags starting with a '#'
{
tagName.set(tag, value.toUpperCase()); // Set the tagname to the uppercase
}
}
tagName.setAccessible(false); // Revert to false
Here is a code sample (version >= 1.11.x):
Parser parser = Parser.htmlParser();
parser.settings(new ParseSettings(true, true));
Document doc = parser.parseInput(html, baseUrl);
There is ParseSettings class introduced in version 1.9.3.
It comes with options to preserve case for tags and attributes.
You must use xmlParser instead of htmlParser and the tags will remain unchanged. One line does the trick:
String html = "<camelCaseTag>some text</camelCaseTag>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
I am using 1.11.1-SNAPSHOT version which does not have this piece of code.
private Tag(String tagName) {
this.tagName = tagName.toLowerCase();
}
So I checked ParseSettings as suggested above and changed this piece of code from:
static {
htmlDefault = new ParseSettings(false, false);
preserveCase = new ParseSettings(true, true);
}
to:
static {
htmlDefault = new ParseSettings(true, true);
preserveCase = new ParseSettings(true, true);
}
and skipped test cases while building JAR.

OpenSAML custom attribute value

I'm trying to create a SAML response. One of the attributes that makes up the assertion is called address and the attribute value needs to be a custom type that is defined in an XSD. How do I add custom attribute value types to the response?
If your attribute value XML is in String form:
String yourXMLFragment = "...";
AttributeStatementBuilder attributeStatementBuilder =
(AttributeStatementBuilder) builderFactory.getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME);
AttributeStatement attributeStatement = attributeStatementBuilder.buildObject();
AttributeBuilder attributeBuilder =
(AttributeBuilder) builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
Attribute attr = attributeBuilder.buildObject();
attr.setName("yourAttributeName");
XSAnyBuilder sb2 = (XSAnyBuilder) builderFactory.getBuilder(XSAny.TYPE_NAME);
XSAny attrAny = sb2.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
attrAny.setTextContent(yourXMLFragment.trim());
attr.getAttributeValues().add(attrAny);
attributeStatement.getAttributes().add(attr);
Actually this above does not yeld correct results. The above example can be used only to create xsany with text content not xml content (xml content gets escaped).
So after digging in opensaml sources the following did work as needed:
public XSAny createXSAny(Element dom)
{
XSAnyBuilder anyBuilder = (XSAnyBuilder) Configuration.getBuilderFactory().getBuilder(XSAny.TYPE_NAME);
XSAny any = anyBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
// this builds only the root element not the whole dom
XSAny xo=anyBuilder.buildObject(dom);
// set/populate dom so whole dom gets into picture
xo.setDOM(dom);
any.getUnknownXMLObjects().add(xo);
return any;
}

Categories

Resources