Passing null value to a CXF method (JAX-WS) - java

I have an interface containing the declaration of this method :
#WebMethod(operationName = "foo")
public long[] foo(String params[]);
Right now if I call this method with 'param' containing null values they are ignored and only non null values are taken in account.
How can I pass null values? I heard of using 'nillable' within the 'XmlElement' annotation but it doesn't work for me.
Thanks in advance,
Marc.

Related

Unable to mock enum static method with null parameter

I am trying to mock a static method of enum which has null value like below
try (MockedStatic<SomEnum> e = Mockito.mockStatic(SomEnum.class)) {
e.when(() -> SomEnum.methodWhichAcceptingNullParam(any())).thenReturn(somValue);
}
here any() is not working... i am not sure I am passing null parameter inside method
methodWhichAcceptingNullParam
I have tried both any and isNull.The fact is SomEnum.methodWhichAcceptingNullParam is always get called however it should not because I provided a mocked value already
any help?
Is your code called with a null or not-null object? This information is missing (at least for me).
Assuming you are calling SomeEnum.methodWhichAcceptingNullParam(null), then you need to use
ArgumentMatchers.isNull()
instead of any().

Spring SPEL object reference failing to "dereference" method input argument object

I am using Spring Security (version 4.0.3.RELEASE) with ACL's-level permissions - I am trying to apply PreAuthorize ACL's permissions to a delete method in my CrudRepository interface using the #PreAuthorize annotation with a call to the SPEL method "hasPermission" (see below). The hasPermission is referencing the input argument using the hash notation that I've seen in several forums, tutorials, and reference docs, but the input argument (the object that I want to delete) is not being picked up by the SPEL parser or supporting framework. So, in other words, I want to delete the object named "apiKey" which is the input argument below. I reference it in the SPEL annotation using "#apiKey" but when I put a breakpoint in the method org.springframework.security.acls.AclPermissionEvaluator.hasPermission(...) the "domainObject" input argument is NULL.
I'm pretty new to Spring and ACL's so I must be doing something fundamentally wrong. I've tried putting breakpoints in the Spring SPEL Parser and walking through during runtime, but I can't find the piece of the code that resolves the reflective arguments based on the SPEL token. I think it's "hidden" in a native method or something.
I've also tried fiddling with the SPEL reference syntax. Nothing else seems to parse correctly, so I think the "#argument_name" is the correct syntax - it's just that SPEL fails to dereference the argument object.
My CrudRepository code w/ the object reference ("#apiKey") that isn't working:
public interface ApiKeyRepository extends CrudRepository<ApiKey, Long> {
#Override
#PreAuthorize("hasPermission(#apiKey, 'DELETE')")
void delete(ApiKey apiKey);
}
Spring Security code which should be receiving my apiKey object, but instead is getting a NULL value for the domainObject:
public class AclPermissionEvaluator implements PermissionEvaluator {
...
public boolean hasPermission(Authentication authentication, Object domainObject, Object permission) {
if (domainObject == null) {
return false; //<== This is where I'm getting access denied from
}
...
Any ideas as to why SPEL is unable to properly reference by apiKey input argument? Thanks in advance.
I figured out my problem thanks to THIS POST which referenced THIS DOC:
Apparently you can't make object references to method arguments in an interface without using the #Param annotation on the arg. So, here is my updated code that fixes the issue:
public interface ApiKeyRepository extends CrudRepository<ApiKey, Long> {
#Override
#PreAuthorize("hasPermission(#apiKey, 'DELETE')")
void delete(#Param("apiKey") ApiKey apiKey);
}

How to write an interceptor which logs the arguments and return value?

I am trying to write an interceptor using javax interceptor api. It has to log the arguments passed to a method and the return value of the method.
Below is a sample code snippet which logs the arguments
for(final Object object: context.getParameters()) {
final Methods[] methods = object.getClass().getMethods();
for(final Method method: methods){ if(method.getName().startsWith("get")) {
LOGGER.info(method.getName() + ":" + method.invoke(object));
}
}
}
I am having trouble logging complex/user-defined types.
Let's say there is a getter method which returns students address of type Address. My code does not check if the invoke method returns primitive or user defined type. So it prints hash code of Address when getAddress method is invoked.
I know that I have to write some kind of recursive code by checking the return type of the getter method. If the getter method returns user defined type then I will again use reflection to find all getter method and then print them.
To do this I have to use a if else condition something like below
Pseudo code:
type =method.getReturnType().getSimpleName();
if type != string or int or char or boolean and so on
then
Call the recursive method which again does the above
I want to know if there is a better solution. How do I solve this problem?
use method.getReturnType().isPrimitive()
https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isPrimitive%28%29
you can find some other is...() methods that can be useful to you
May be we can use Jackson Mapper api and just pass the instance to the mapper and print the result to the log.
I think this is the easiest way.

Jersey : Is it possible to specify multiple values in #DefaultValue() annotation

I am playing around with Java API for RESTful Web Services (JAX-RS) ,
and encountered #DefaultValue annotation in Jersey implementation of JAX-RS.
Here is the code snippet
#GET
#Path("/query")
public Response getUserWithQueryParams(
#DefaultValue("defaultId") #QueryParam("from")String from,
#DefaultValue("defaultName") #QueryParam("to") String to,
#DefaultValue("mobileNo")#QueryParam("orderBy") List<String> orderBy
){
My third argument is of List<String> which can have multiple values
for example I explicitly pass the parameters
users/query?from=100&to=200&orderBy=age&orderBy=name
Now my third argument have values [age,name] ,
but if i don't pass any explicitly parameter then Is there any way to set multiple default values . ?
This won't work how you want it to. If the object is type List it will have a single value inserted in the list. The object in the list will be the value of your default value for the list. Check this out Why not try checking if orderBy == null if it does then add your default values to orderBy?

How to copy a private attribute when using CGLIB?

I have the following example where I try to copy a private attribute from the source instance to the target instance.
public class MyClass {
public void cloneTo(MyClass target) {
target.identifier = this.identifier; // identifier is not null
System.out.println(target.getIdentifier()) // problem: prints null
}
}
This code usually should work, but the problem is that the MyClass instance is a CGLIB proxy: MyClass$EnhancerBySpringCGLIB$someId, and in this case, the identifier attribute is not set in the proxied target class, so when I call getIdentifier(), it returns null instead of the identifier.
Is it possible to copy private attributes without creating a getter/setter for each attribute?
This is not possible.
I take it from your question that you created a proxy MyClass$EnhancerBySpringCGLIB$someId that delegates its method invocations to another instance of MyClass?
Field operations in Java are not dispatched dynamically, i.e. there is no way to trigger an action when a field is set or read. This is only possible when a method is invoked. This means that there is no way to set the field of MyClass when MyClass$EnhancerBySpringCGLIB$someId is set.
Instead you need to:
Define a setter for writing fields.
Make MyClass$EnhancerBySpringCGLIB$someId not to be a delegator but an actual substitute for MyClass.

Categories