Annotation AliasFor suddenly stopped working - spring

I am about to have nervous break down. Things just randomly stops working for no apparent reason.
The question I am asking is have someone encountered similar problems or possibly could help me with my specific problem.
I have basic Spring Boot web application and I have defined end points using GetMapping and Postmapping annotations and I introduced my own annotation. Things worked fine and when looking at GetMapping and PostMapping source they clearly maps some attributes for RequesyMapping using the AliasFor annotation. I am working on code that scan all Controller annotated beans and scan their methods for PermissionRequired annotation. When on is found it tries to find RequestMapping annotation and get the path(s) from it. The path(s) are empty and only the method type from GetMapping is present. It all worked fine for sometime, but then I typed more code and it stopped working, I am just wondering why.
Here are some code examples:
The method that tries to scan method with specific annotation:
#Bean
public Set<Pair<String, String>> requiredPermissions(ApplicationContext ac)
{
LOG.trace("Configuring required permissions");
if (ac != null)
{
for (String bean : ac.getBeanNamesForAnnotation(Controller.class))
{
for (Method m : ac.getAutowireCapableBeanFactory().getType(bean).getDeclaredMethods())
{
RequiresPermission rp = m.getAnnotation(RequiresPermission.class);
if (rp != null)
{
RequestMapping rm = AnnotationUtils.getAnnotation(m, RequestMapping.class);
if (rm != null)
{
LOG.trace("=============================================== Bean method {}[{}] annotation: {}", rm.path(), rp.value(), rm);
}
}
}
}
}
Set<Pair<String, String>> req = new HashSet<>();
req.add(Pair.of("/login", "GET"));
req.add(Pair.of("/login", "POST"));
// TODO: Scan methods
Method[] mtds = {};
for (Method m : mtds)
{
RequiresPermission rp = m.getAnnotation(RequiresPermission.class);
if (rp != null)
{
RequestMapping rm = m.getAnnotation(RequestMapping.class);
if (rm != null)
{
for (RequestMethod remet : rm.method())
{
for (String path : rm.path())
{
Pair<String, String> p = Pair.of(path, remet.toString().toUpperCase());
LOG.trace("Found URI requiring permission {}", p);
req.add(p);
}
}
}
}
}
return req;
}
The custom annotation:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface RequiresPermission
{
String value() default "";
}
The controller class:
#Controller
public class PermissionedController
{
#GetMapping("/permmisioned/{param}")
#RequiresPermission("perm1:perm2")
protected String permissioned()
{
return "";
}
Yes there are typos and lines missing etc. But I am too tired (fatiqued) to fix those issues. This is quite simple and it should basically work, but it doesnt. Sorry, but no gist maybe someday if I am feeling better, but I doubt it. I could do with a vacation, but you do not get vacation from neurological disease.

Related

failed to validate request params in a Spring Boot/Kotlin Coroutines controller

In a SpringBoot/Kotlin Coroutines project, I have a controller class like this.
#RestContollser
#Validated
class PostController(private val posts: PostRepository) {
suspend fun search(#RequestParam q:String, #RequestParam #Min(0) offset:Int, #RequestParam #Min(1) limit:Int): ResponseEntity<Any> {}
}
The validation on the #ResquestBody works as the general Spring WebFlux, but when testing
validating request params , it failed and throws an exception like:
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.base/java.util.Arrays$ArrayList.get(Arrays.java:4165)
Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below:
It is not a ConstraintViolationException.
I think this is a bug in the framework when you are using coroutines (update , it is, I saw Happy Songs comment). In summary:
"#Validated is indeed not yet Coroutines compliant, we need to fix that by using Coroutines aware methods to discover method parameters."
The trouble is that the signature of the method on your controller is actually enhanced by Spring to have an extra parameter, like this, adding a continuation:
public java.lang.Object com.example.react.PostController.search(java.lang.String,int,int,kotlin.coroutines.Continuation)
so when the hibernate validator calls getParameter names to get the list of parameters on your method, it thinks there are 4 in total on the request, and then gets an index out of bounds exception trying to get the 4th (index 3).
If you put a breakpoint on the return of this:
#Override
public E get(int index) {
return a[index];
}
and put a breakpoint condition of index ==3 && a.length <4 you can see what is going on.
I'd report it as a bug on the Spring issue tracker.
You might be better off taking an alternative approach, as described here, using a RequestBody as a DTO and using the #Valid annotation
https://www.vinsguru.com/spring-webflux-validation/
Thanks for the happy songs' comments, I found the best solution by now to overcome this barrier from the Spring Github issues#23499.
As explained in comments of this issue and PaulNuk's answer, there is a Continuation will be appended to the method arguments at runtime, which will fail the index computation of the method parameter names in the Hibernate Validator.
The solution is changing the ParameterNameDiscoverer.getParameterNames(Method) method and adding a empty string as the additional parameter name when it is a suspend function.
class KotlinCoroutinesLocalValidatorFactoryBean : LocalValidatorFactoryBean() {
override fun getClockProvider(): ClockProvider = DefaultClockProvider.INSTANCE
override fun postProcessConfiguration(configuration: javax.validation.Configuration<*>) {
super.postProcessConfiguration(configuration)
val discoverer = PrioritizedParameterNameDiscoverer()
discoverer.addDiscoverer(SuspendAwareKotlinParameterNameDiscoverer())
discoverer.addDiscoverer(StandardReflectionParameterNameDiscoverer())
discoverer.addDiscoverer(LocalVariableTableParameterNameDiscoverer())
val defaultProvider = configuration.defaultParameterNameProvider
configuration.parameterNameProvider(object : ParameterNameProvider {
override fun getParameterNames(constructor: Constructor<*>): List<String> {
val paramNames: Array<String>? = discoverer.getParameterNames(constructor)
return paramNames?.toList() ?: defaultProvider.getParameterNames(constructor)
}
override fun getParameterNames(method: Method): List<String> {
val paramNames: Array<String>? = discoverer.getParameterNames(method)
return paramNames?.toList() ?: defaultProvider.getParameterNames(method)
}
})
}
}
class SuspendAwareKotlinParameterNameDiscoverer : ParameterNameDiscoverer {
private val defaultProvider = KotlinReflectionParameterNameDiscoverer()
override fun getParameterNames(constructor: Constructor<*>): Array<String>? =
defaultProvider.getParameterNames(constructor)
override fun getParameterNames(method: Method): Array<String>? {
val defaultNames = defaultProvider.getParameterNames(method) ?: return null
val function = method.kotlinFunction
return if (function != null && function.isSuspend) {
defaultNames + ""
} else defaultNames
}
}
Then declare a new validator factory bean.
#Primary
#Bean
#Role(BeanDefinition.ROLE_INFRASTRUCTURE)
fun defaultValidator(): LocalValidatorFactoryBean {
val factoryBean = KotlinCoroutinesLocalValidatorFactoryBean()
factoryBean.messageInterpolator = MessageInterpolatorFactory().getObject()
return factoryBean
}
Get the complete sample codes from my Github.

#Cacheable is working in Controller but not inside service

I have this strange problem in Spring Boot where #Cacheable is working in controller but not inside service. I can see GET call in Redis but not a PUT call.
This is working since it is inside controller
#RestController
#RequestMapping(value="/places")
public class PlacesController {
private AwesomeService awesomeService;
#Autowired
public PlacesController(AwesomeService awesomeService) {
this.awesomeService = awesomeService;
}
#GetMapping(value = "/search")
#Cacheable(value = "com.example.webservice.controller.PlacesController", key = "#query", unless = "#result != null")
public Result search(#RequestParam(value = "query") String query) {
return this.awesomeService.queryAutoComplete(query);
}
}
But #Cacheable is not working when I do this in Service like this
#Service
public class AwesomeApi {
private final RestTemplate restTemplate = new RestTemplate();
#Cacheable(value = "com.example.webservice.api.AwesomeApi", key = "#query", unless = "#result != null")
public ApiResult queryAutoComplete(String query) {
try {
return restTemplate.getForObject(query, ApiResult.class);
} catch (Throwable e) {
return null;
}
}
}
I can see the GET call in Redis but not a PUT call.
Your caching should work fine as it is. Make sure that you have the #EnableCaching annotation and that your unless criteria is correct.
Right now, you're using unless="#result != null", which means it will cache the result, unless it's not null. This means that it will almost never cache, unless the restTemplate.getForObject() returns null, or when an exception occurs, because then you're also returning null.
I'm assuming that you want to cache each value, except null, but in that case you have to inverse your condition, e.g.:
#Cacheable(
value = "com.example.webservice.api.AwesomeApi",
key = "#query",
unless = "#result == null") // Change '!=' into '=='
Or, as mentioned in the comments, in stead of reversing the condition, you can use condition in stead of unless:
#Cacheable(
value = "com.example.webservice.api.AwesomeApi",
key = "#query",
condition = "#result != null") // Change 'unless' into 'condition'

JAXBElement: providing codec (/converter?) for class java.lang.Class

I have been evaluating to adopt spring-data-mongodb for a project. In summary, my aim is:
Using existing XML schema files to generate Java classes.
This is achieved using JAXB xjc
The root class is TSDProductDataType and is further modeled as below:
The thing to note here is that ExtensionType contains protected List<Object> any; allowing it to store Objects of any class. In my case, it is amongst the classes named TSDModule_Name_HereModuleType and can be browsed here
Use spring-data-mongodb as persistence store
This is achieved using a simple ProductDataRepository
#RepositoryRestResource(collectionResourceRel = "product", path = "product")
public interface ProductDataRepository extends MongoRepository<TSDProductDataType, String> {
TSDProductDataType queryByGtin(#Param("gtin") String gtin);
}
The unmarshalled TSDProductDataType, however, contains JAXBElement which spring-data-mongodb doesn't seem to handle by itself and throws a CodecConfigurationException org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.lang.Class.
Here is the faulty statement:
TSDProductDataType tsdProductDataType = jaxbElement.getValue();
repository.save(tsdProductDataType);
I tried playing around with Converters for spring-data-mongodb as explained here, however, it seems I am missing something since the exception is about "Codecs" and not "Converters".
Any help is appreciated.
EDIT:
Adding converters for JAXBElement
Note: Works with version 1.5.6.RELEASE of org.springframework.boot::spring-boot-starter-parent. With version 2.0.0.M3, hell breaks loose
It seems that I missed something while trying to add converter earlier. So, I added it like below for testing:
#Component
#ReadingConverter
public class JAXBElementReadConverter implements Converter<DBObject, JAXBElement> {
//#Autowired
//MongoConverter converter;
#Override
public JAXBElement convert(DBObject dbObject) {
Class declaredType, scope;
QName name = qNameFromString((String)dbObject.get("name"));
Object rawValue = dbObject.get("value");
try {
declaredType = Class.forName((String)dbObject.get("declaredType"));
} catch (ClassNotFoundException e) {
if (rawValue.getClass().isArray()) declaredType = List.class;
else declaredType = LinkedHashMap.class;
}
try {
scope = Class.forName((String) dbObject.get("scope"));
} catch (ClassNotFoundException e) {
scope = JAXBElement.GlobalScope.class;
}
//Object value = rawValue instanceof DBObject ? converter.read(declaredType, (DBObject) rawValue) : rawValue;
Object value = "TODO";
return new JAXBElement(name, declaredType, scope, value);
}
QName qNameFromString(String s) {
String[] parts = s.split("[{}]");
if (parts.length > 2) return new QName(parts[1], parts[2], parts[0]);
if (parts.length == 1) return new QName(parts[0]);
return new QName("undef");
}
}
#Component
#WritingConverter
public class JAXBElementWriteConverter implements Converter<JAXBElement, DBObject> {
//#Autowired
//MongoConverter converter;
#Override
public DBObject convert(JAXBElement jaxbElement) {
DBObject dbObject = new BasicDBObject();
dbObject.put("name", qNameToString(jaxbElement.getName()));
dbObject.put("declaredType", jaxbElement.getDeclaredType().getName());
dbObject.put("scope", jaxbElement.getScope().getCanonicalName());
//dbObject.put("value", converter.convertToMongoType(jaxbElement.getValue()));
dbObject.put("value", "TODO");
dbObject.put("_class", JAXBElement.class.getName());
return dbObject;
}
public String qNameToString(QName name) {
if (name.getNamespaceURI() == XMLConstants.NULL_NS_URI) return name.getLocalPart();
return name.getPrefix() + '{' + name.getNamespaceURI() + '}' + name.getLocalPart();
}
}
#SpringBootApplication
public class TsdApplication {
public static void main(String[] args) {
SpringApplication.run(TsdApplication.class, args);
}
#Bean
public CustomConversions customConversions() {
return new CustomConversions(Arrays.asList(
new JAXBElementReadConverter(),
new JAXBElementWriteConverter()
));
}
}
So far so good. However, how do I instantiate MongoConverter converter;?
MongoConverter is an interface so I guess I need an instantiable class adhering to this interface. Any suggestions?
I understand the desire for convenience in being able to just map an existing domain object to the database layer with no boilerplate, but even if you weren't having the JAXB class structure issue, I would still be recommending away from using it verbatim. Unless this is a simple one-off project, you almost definitely will hit a point where your domain models will need to change but your persisted data need to remain in an existing state. If you are just straight persisting the data, you have no mechanism to convert between a newer domain schema and an older persisted data scheme. Versioning of the persisted data scheme would be wise too.
The link you posted for writing the customer converters is one way to achieve this and fits in nicely with the Spring ecosystem. That method should also solve the issue you are experiencing (about the underlying messy JAXB data structure not converting cleanly).
Are you unable to get that method working? Ensure you are loading them into the Spring context with #Component plus auto-class scanning or manually via some Configuration class.
EDIT to address your EDIT:
Add the following to each of your converters:
private final MongoConverter converter;
public JAXBElement____Converter(MongoConverter converter) {
this.converter = converter;
}
Try changing your bean definition to:
#Bean
public CustomConversions customConversions(#Lazy MongoConverter converter) {
return new CustomConversions(Arrays.asList(
new JAXBElementReadConverter(converter),
new JAXBElementWriteConverter(converter)
));
}

Get resteasy servlet context without annotation params

Quick project explanation: We have a built application based on JSF2 + Spring with Dynamic data sources. The data reference control is made with a spring-config:
<bean id="dataSource" class="com.xxxx.xxxx.CustomerRoutingDataSource">
....
and a class (referenced above):
public class CustomerRoutingDataSource extends AbstractRoutingDataSource {
#Override
protected Object determineCurrentLookupKey() {
return CustomerContextHolder.getCustomerType();
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
the CustomerContextHolder called above is as follows:
public class CustomerContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
String manager = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dataBaseManager");
if (manager != null) {
contextHolder.set(manager);
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dataBaseManager", null);
} else {
String base = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("currentDatabBase");
if (base != null)
contextHolder.set(base);
}
return (String) contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
The problem is that the last guy is calling FacesContext.getCurrentInstance() to get the servlet context. Just to explain, it uses the session Attribute dataBaseManager to tell which base it should use.
For the actual solution it was working fine, but with the implementation of a RESTEASY web service, when we make a get request the FacesContext.getCurrentInstance() is obviously returning null and crashing.
I searched a lot and could not find a way of getting the servlet-context from outside of the #GET params. I would like to know if is there any way of getting it, or if there is another solution for my dynamic datasource problem.
Thanks!
Like magic and probably not much people know.
I searched deep into the Resteasy documentation, and found a part of springmvc plugin that comes with the resteasy jars, that has a class called RequestUtil.class.
With that I was able to use the method getRequest() without the "#Context HttpServletRequest req" param.
Using that I was able to set the desired database on the request attributes, and from another thread (called by spring) get it and load the stuff from the right place!
I'm using it for a week now and it works like a charm. Only thing that I needed to do is change the determineLookupKey() above to this:
#Override
protected String determineCurrentLookupKey() {
if (FacesContext.getCurrentInstance() == null) {
//RESTEASY
HttpServletRequest hsr = RequestUtil.getRequest();
String lookUpKey = (String) hsr.getAttribute("dataBaseManager");
return lookUpKey;
}else{
//JSF
return CustomerContextHolder.getCustomerType();
}
}
Hope this helps other people!
Thiago

Can Guice be configured to hide the class path in stack traces?

Guice's stack traces can get so verbose that they are very painful to read. Here's an example:
1) No implementation for java.util.Set<com.mydomain.myapp.android.activities.catbrowser.generalizedbrowser.listview.helpers.databaseitem.itemmanipulators.ItemManipulator<com.mydomain.myapp.flash.Cat>> annotated with #com.google.inject.assistedinject.Assisted(value=) was bound.
while locating java.util.Set<com.mydomain.myapp.android.activities.catbrowser.generalizedbrowser.listview.helpers.databaseitem.itemmanipulators.ItemManipulator<com.mydomain.myapp.flash.Cat>> annotated with #com.google.inject.assistedinject.Assisted(value=)
...
If I could hide the classpath, it would look like:
1) No implementation for Set<ItemManipulator<Cat>> annotated with #Assisted(value=) was bound.
while locating Set<ItemManipulator<Cat>> annotated with #Assisted(value=)
Is there any way to configure Guice to do this?
So the answer is no.
If you take look at guice source code you will find com.google.inject.internal.Errors class that is responsible for building error messages. In this class it is coded that the Key is converting the following way:
new Converter<Key>(Key.class) {
public String toString(Key key) {
if (key.getAnnotationType() != null) {
return key.getTypeLiteral() + " annotated with "
+ (key.getAnnotation() != null ? key.getAnnotation() : key.getAnnotationType());
} else {
return key.getTypeLiteral().toString();
}
}
}
Next step is to take a look at TypeLiteral#toString method:
#Override public final String toString() {
return MoreTypes.typeToString(type);
}
Where MoreTypes#typeToString is a static method that cannot be configured
public static String typeToString(Type type) {
return type instanceof Class ? ((Class) type).getName() : type.toString();
}

Resources