OpenSAML: how to get entityID of MetadataProvider? - opensaml

I have AbstractReloadingMetadataProvider object that corresponds to IdP and I need to get its entityID. How do I get it? Is there a way to avoid nasty XML parsing?

Found a way that requires a (potentially unsafe?) typecast.
MetadataProvider provider = ...
String entityID = ((EntityDescriptor) provider.getMetadata()).getEntityID()
Is there a better way?

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))

Jersey #PathParam : contains multiple parameters with no annotation

The following is my method signature which I am using in Jersey , when I debug/run the program I am getting error as :
[[FATAL] Method public javax.ws.rs.core.Response com.xxxx.xxxxx.Xxxxx.xxxxx.xxxxxxxx(java.lang.String,java.lang.String,java.lang.String,javax.ws.rs.container.ContainerRequestContext) on resource class com.xxxxxx.xxxxx.xxxxxx.xxxxxx contains multiple parameters with no annotation.
My code:
#PUT
#Path("/user/{user}/{role}")
#Consumes({MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN})
#Produces("application/json")
public Response myFunction(#PathParam("user") String user,
#PathParam("role") String role,
String rawData,
#Context ContainerRequestContext crc) {
}
What I am doing wrong here.
Thank you
Edit: This answer helped me solve my error, but as Cássio Mazzochi Molin mentioned in the comment below: it wont help you (and the documentation is for the wrong version of Jersey..). A total miss on my part.
Please excuse my attempt to help you. I hope you already have solved your error :)
Ahoy there!
I'm really new to REST (so take my answer with a bucket of herb salt),
but I think I know where your error is coming from.
You have to bind your parameter rawData.
Example: #PathParam("rawdata") String rawData or
#HeaderParam("rawdata") String rawData
Depending on where you want to extract the parameter from you have to
write a #annotation to the parameter.
You can extract the following types of parameters for use in your
resource class:
Query
URI
Path
Form
Cookie
Header
Matrix
Text above is taken from the link:
http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html You should
take a look and read a little about it if you haven't done it already
:)

How do I use the appProperties with the ruby api-client

I can't determine how to add custom properties or search for them.
Everything I have tried is giving me a Error - #<Google::Apis::ClientError: invalid: Invalid query> when I attempt to search for them. I can successfully complete other queries but I don't know if the client is setup to work with appProperties (or even properties at all).
Basically I just need the correct syntax for searching and adding since it doesn't appear to be in the documentation.
Assuming you already have a reference to an authorized DriveService, you can search based on appProperties using a q-parameter (documented here), like this:
file_list = drive.list_files(
q: "appProperties has { key='my_app_key' and value='my_val' }",
fields: 'files(id, name, appProperties)',
spaces: 'drive')
If you omit the fields parameter then the search will still work but the properties themselves won't be returned.
Updating appProperties is definitely arcane and the documentation is opaque. What you need is the ID of the file, and a File value object as a container for the attributes to update. Something like this:
new_app_properties = { 'my_app_key' => 'my_val' }
update_f = Google::Apis::DriveV3::File.new(app_properties: new_app_properties)
drive.update_file(file_id, update_f)

Mongoengine Django Rest Framework - Serializer Error - ReferenceField is not JSON serializable

Everything works great until the ObjectID value of the ReferenceField no longer points to a valid document. Then The ObjectID is left as the value, and json doesn't know how to serialize this.
How do I deal with invalid ReferenceFields?
E.g.
class Food(Document):
name = StringField()
owner = ReferenceField("Person")
class Person(Document):
first_name = StringField()
last_name = StringField()
...
p = Person(...)
apple = Food(name="apple", owner=p)
p.delete() # might be the wrong method, but you get the idea
At this point, attempting to fetch a list of foods via the REST API will fail with the is not JSON serializable error, since apple.owner no longer points to an owner that exists.
Since you are using DRF with mongoengine, you must be using django-rest-framework-mongoengine.
Apparenly, its a bug in django-rest-framework-mongoengine. Check this open issue on Github which was reported recently regarding the same.
https://github.com/umutbozkurt/django-rest-framework-mongoengine/issues/91
One way is to write your own JSONEncoder for this. This link might help.
Another option is to use the json_util library of Pymongo. They provide explicit BSON conversion to and from json.
As per json-util docs:
This module provides two helper methods dumps and loads that wrap the
native json methods and provide explicit BSON conversion to and from
json. This allows for specialized encoding and decoding of BSON
documents into Mongo Extended JSON‘s Strict mode. This lets you encode
/ decode BSON documents to JSON even when they use special BSON types.

How to read pair in 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.

Resources