How to read pair in freemarker - freemarker

I'm having little trouble with reading pair.
So I'm creating my pair
private Pair<Integer, Integer> count(somethink) {
int c1 = 2;
int c2 = 4;
return new Pair<Integer, Integer>(c1, c2);
}
And 'sending' it to ftl via java
mv.addObject("counted", count(somethink));
I won't write everything how it sends because I don't think it really matters with my issue. So i'm recieving it in "ftl". Then I was trying to 'read' it.
<#list counted?keys as key>
<#spring.message "someMsg"/>(${key}/${counted[key]})
</#list>
After then I'm getting error
Expecting a string, date or number here, Expression x is instead a freemarker.ext.beans.SimpleMethodModel
As I suppose you don't iterate pairs (or I'm wrong?) I know its pair that contains only one key and one value but still I have to do send it that way and I thought its going be to similar to iterating through map, in Java I would use pair.first() and pair.second() but it doesn't work in ftl (yes I know it shouldn't work). I also tried to cast it to String by using ?string but it didnt work too

have you tried?
${counted.first()?xml}/${counted.second()?xml}
Assuming pair.first() and second() work in your Java code.

I'm not sure how the API of Pair looks, but with ?keys you probably unwillingly list its methods. Hence the error message; you try to print the method itself, not its return value. (As of the broken error message, because there's no x there, a FreeMarker update would help.) So as Caleryn says, just call the two methods. If you need to list this thing, you probably need to put it into a List or array, unless Pair implements List (or even just Collection) or Iterable. Or if this is a bigger issue in your project, FreeMarker has this pluggable configuration component, the ObjectWrapper, and with that you can tell FreeMarker to treat Pair-s as lists, and how. That needs some deeper understanding of FreeMarker though.

Related

How to programatically evaluate expression(spel/variable reference) used in annotation?

Say that I have use case for finding method annotated via say #Scheduled(cron = "${variable}"), and I'd like to know the value of "cron" parameter. If I check via reflection, no surprise, I will find there value "${variable}".
Can someone share link/snipet how to evaluate variables/spel expression present in annotation? I found some answers, but neither of them worked.
Just to extend #crizzis answer, maybe filling the missing part.
Fist you need to inject/autowire ConfigurableBeanFactory beanFactory;. Looking at implementation of ExpressionValueMethodArgumentResolver and it's parent AbstractNamedValueMethodArgumentResolver it seems to me, that full code which does variable substitution and spell needs one more line:
BeanExpressionResolver beanExpressionResolver = beanFactory.getBeanExpressionResolver();
String expressionWithSubstitutedVariables = beanFactory.resolveEmbeddedValue(expression);
Object resultWithResolvedSPEL = beanExpressionResolver.evaluate(expressionWithSubstitutedVariables, new BeanExpressionContext(beanFactory, null));
then string like #{!${some-boolean-variable} ? 'a' : 'b'} was correctly evaluated for me. Not sure if this is the way-to-go as I don't know spring well, but this worked for me.
I'm sure there are a couple of ways, but the easiest is probably:
beanFactory.getBeanExpressionResolver().evaluate(
"${variable}",
new BeanExpressionContext(beanFactory, null))

Extract a key or part of a string using Freemarker

I'm creating a JSON message from my ITSM solution, which uses Freemarker as a template tool.
The system returns a list of materials in the form of a string like this:
{ "[ZA00344] Toner Teste", "MATNR" : "[ZA00888] Caneta" }
Is there any built-in to extract the key between [] without using keep_after, keep_befor, or such functions? Cause these can eventually cause problems.
Thanks.

How to serialize a SecTrustRef object?

I have a SecTrustRef object from the system that I'd like to evaluate myself. Just calling SecTrustEvaluateAsync will be sufficient for this job. The problem is, I must evaluate it in a different process as only this other process has access to the keychains where the CA certificates are stored that may cause evaluation to succeed.
The two processes have an IPC link that allows me to exchange arbitrary byte data between them but I don't see any way to easily serialize a SecTrustRef into byte data and deserialize that data back to an object at the other process. There doesn't seem to be a persistent storage mechanism for SecTrustRef.
So am I overlooking something important here, or do I really have to get all the certs (SecTrustGetCertificateAtIndex) and all the policies (SecTrustCopyPolicies) and serialize these myself?
And if so, how would I serialize a policy?
For the certificate (SecCertificateRef) it's rather easy, I just call SecCertificateCopyData and later on SecCertificateCreateWithData.
But for policies I can only call SecPolicyCopyProperties on one side and later on SecPolicyCreateWithProperties, however the later one requires a 2nd parameter, a policyIdentifier and I see no way to get that value from an existing policy. What am I missing?
Reading through the source of the Security framework, I finally figured it out how to copy a SecPolicyRef:
SecPolicyCreateWithProperties wants what it calls a "policyIdentifier". It's a constant like kSecPolicyAppleIPsec.
This does not get stored directly by the function, it's comparing the value and calling dedicated internal "initializers" (like SecPolicyCreateIPsec).
These in turn call SecPolicyCreate (which is private). They end up passing the same identifier value that you passed to SecPolicyCreateWithProperties.
And this value then gets stored as-is in the _oid field!
The identifier is actually the OID. You can get it either via SecPolicyCopyProperties(policy) (stored in the dictionary with key kSecPolicyOid) or via SecPolicyGetOID (but that returns it as an inconvenient CSSM_OID). Some of those specialized initializers also use values from the properties dictionary passed to SecPolicyCreateWithProperties, those should be present in the copied properties dictionary already.
So this gives us:
Serialization:
CFDictionaryRef createSerializedPolicy(SecPolicyRef policy) {
// Already contains all the information needed.
return SecPolicyCopyProperties(policy);
}
Deserialization:
SecPolicyRef createDeserializedPolicy (CFDictionaryRef serialized) {
CFStringRef oid = CFDictionaryGetValue(serialized, kSecPolicyOid);
if (oid == NULL || CFGetTypeID(oid) != CFStringGetTypeID()) {
return NULL;
}
return SecPolicyCreateWithProperties(oid, serialized);
}
To reproduce the original SecTrustRef as closely as possible, the anchors need to be copied as well. There is an internal variable _anchorsOnly which is set to true once you set anchors. Unfortunately, there is no way to query this value and I've seen it being false in trusts passed by NSURLSession, for example. No idea yet on how to get this value in a public way.
Another problematic bit are the exceptions: if _exceptions is NULL but you query them via SecTrustCopyExceptions(trust), you do get data! And if you assign that to the deserialized trust via SecTrustSetExceptions(trust, exceptions) you suddenly end up with exceptions that were not there before and can change the evaluation result! (I've seen those suddenly appearing exceptions lead to an evaluation result of "proceed" instead of "recoverable trust failure").

Map Java Stream to Map of Objects with Object as Key

I have some classes and I am doing some work in a List<WorkDay> which contains a List<LedgerItem>, I have everything working but one part. Well it works, but not exactly how I would like it to.
public Stream<Map<WorkDay, Set<LedgerItem>>> adjustWorkDays(List<WorkDay> workDays) {
return workDays.stream()
.sorted((d1,d2) -> d1.getCreated().compareTo(d2.getCreated()))
.map(day -> createGroupByWorkDay(day))/*need it to collect here*/;
}
If you can see the return type is Stream<Map<WorkDay, Set<LedgerItem>>> but I want to map this out of the Stream as Map<WorkDay, Set<LedgerItem>> with a collector but just cannot seem to get Collectors.toMap() syntax to do anything but break.
Like I said, everything works perfectly, so I dont need anything outside of the mapping to work.
Just FYI: createGroupByWorkDay returns Map<WorkDay, Set<LedgerItem>> already but only accepts a single WorkDay as this is a requirement so I cannot change the way this is executed...
thanks in advance
EDIT:
So the method that I have createGroupByWorkDay that is not listed here works perfectly as expected, and will never be changed. It returns the correct type of Map<WorkDay, Set<LedgerItem>> but only has a signature for one WorkDay like this createGroupByWorkDay(WorkDay day) to the method in question in the original comment uses that to build individual Maps which are grouped by WorkDay and returns, but there could be N number of those, so the method public Stream<Map<WorkDay, Set<LedgerItem>>> adjustWorkDays(List<WorkDay> workDays) should return all of those Maps collected into one map in the collector. If that makes any sense?
Your question is not clear to me. But I guess you may be asking for something like this?
Map<WorkDay, List<LedgerItem>> result = workDays.stream()
.collect(Collectors.toMap(Function.identity(), WorkDay::getLedgerItems));
If not please explain your problem statement clearly. This is just a guess.
Here's an update,
Map<WorkDay, List<LedgerItem>> result = workDays.stream()
.map(d -> createGroupByWorkDay(d))
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

parse.com - cloud code - what do i do with a pointer?

Someone doesn't understand what i want. I want to know what i can do with a pointer.
I have js cloud code.
I have a pointer.
What can i do with it?
Example:
var query = new Parse.Query("Messages"); //POINTER QUERY
console.log(userMessages[0].get("messageId"));
console.log("end2");
query.equalTo("objectId",userMessages[position].get("messageId"));
In the example, userMessages is the result of a prior cloud query.
This line
console.log(userMessages[0].get("messageId"));
helpfully outputs
{"__type":"Pointer","className":"Messages","objectId":"5J4eOletgz"}
This is less useful than you might imagine. I cannot seem to call the objectId from it, and the query
query.equalTo("objectId",userMessages[position].get("messageId"));
query.find ({ ... });
returns nothing. Note that the query should find the pointer-object the pointer-points-to, but instead it helpfully throws the error
Error: 102 bad special key: __type
Which is just about useless.
What can i do with a pointer?
Why don't the people at parse.com bother to write this stuff up anywhere?
That second question is more like a buddhist koan for them to meditate over, no need to respond!
you can use:
userMessagesQuery.include("messageId")
before you execute your query that returns userMessages and you will get the entire object in "messageId" instead of just a pointer.
Also you use
userMessages[0].get("messageId").fetch({success:function(){}})
to get the full object if you don't want to use "include"
Suggestion: I'd rename "messageId" to "message" to make it clear that it's an object pointer and not an ID field.
A pointer is a Parse object with just the minimal data used for linking. If you want to get the rest of the data for a pointer (fully populate it), use the fetch() method.
If you just want the objectId from a pointer, you retrieve it just like you would any other Parse object, by using the myPointer.objectId property.
In your case the following would work, but isn't the most optimal solution:
// I would suggest renaming messageId if you're actually storing pointers
var messagePointer = userMessages[position].get("messageId");
query.equalTo("objectId", messagePointer.objectId);
Instead, as stated by #RipRyness, you could just change your previous query to include() the full object, avoiding many extra queries.
userMessagesQuery.include("messageId");
// ... later in success handler ...
// now a fully populated Message object
var message = userMessages[position].get("messageId");
console.log(message);
Okay so a pointer is does not only point to a certain object, but it can BE that object.
In the query that returns the userMessages result, you can use the line .include("messageId") - this makes your query not only return a pointer to that object, it actually includes that object in place of the pointer.
After using the include statement, userMessages[0].get("messageId") will return the Message object linked that that userMessage object. You no longer need to query the Message collection to get the object.

Resources