I have a question, it is possible to use java CDI within a JPA convert?
I'm doing some tests to study and I am not able to inject the objects within my converter:
I'm using a eclipseLink, please see my code, Please analyze my code where I am going wrong? And how can I do this in the best way ?
Basically to greater understanding, I'll have a session bean that represents my User logged in, this session Bean I have the User TimeZone, I would like to inject this Time Zone within My Converter to write the data to UTC in database
My Code:
JPA Converter: org.eclipse.persistence.mappings.converters.Converter
package joda;
import inject.qualifier.UserTimeZoneQualifier;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import enumerator.UserType;
import security.UserSession;
#RequestScoped
public class JodaDateTimeUTCConverter implements Converter {
private static final long serialVersionUID = 1L;
// JUST TEST IT'S WAS INJECT AND REMOVE
private UserSession userSession = new UserSession("America/Mexico_City", UserType.HIGH_HISK);
#Inject
#UserTimeZoneQualifier
String userTimeZone;
//TODO FOR TEST
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss:SSS - z - ZZZZZZZZZZZZZZZZZZ");
SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS - z - ZZZZZZZZZZZZZZZZZZ");
#Override
public Object convertDataValueToObjectValue(Object dataValue, Session session) {
// TODO REMOVE
DateTimeZone timeZone = DateTimeZone.forID(userSession.getTimeZoneLocale());
System.out.println("BEFORE OF CONVERTION : " + dt.format(dataValue));
System.out.println("AFTER OF CONVERTION : " + dtf.print(new DateTime((Timestamp) dataValue).withZone(timeZone)));
System.out.println("userTimeZone INJECT" + userTimeZone);
return dataValue instanceof Date ? new DateTime((Timestamp) dataValue).withZone(timeZone) : null;
}
#Override
public Object convertObjectValueToDataValue(Object objectValue, Session session) {
System.out.println("GO TO DB(DATAVALUE)");
System.out.println("AFTER OF CONVERTION : " + dtf.print(((DateTime) objectValue).withZone(DateTimeZone.UTC)));
return objectValue instanceof DateTime?((DateTime) objectValue).withZone(DateTimeZone.UTC).toLocalDateTime().toDate() : null;
}
#Override
public void initialize(DatabaseMapping mapping, Session session) {
}
#Override
public boolean isMutable() {
return false;
}
public String getUserTimeZone() {
return userTimeZone;
}
public void setUserTimeZone(String userTimeZone) {
this.userTimeZone = userTimeZone;
}
}
My #UserTimeZoneQualifier:
package inject.qualifier;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
#Qualifier
#Retention(RetentionPolicy.RUNTIME)
#Target({FIELD, METHOD, TYPE, PARAMETER})
public #interface UserTimeZoneQualifier {
}
And My UserSessionProduce:
package inject;
import inject.qualifier.UserTimeZoneQualifier;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Produces;
import enumerator.UserType;
import security.UserSession;
public class UserSessionProduce {
private UserSession userSession;
#PostConstruct
public void init(){
this.userSession = new UserSession("America/Mexico_City", UserType.ADMINISTRATOR);
}
#Produces
public UserSession getUserSessionInstance(){
return this.userSession;
}
#Produces
#UserTimeZoneQualifier
public String getUserSessionTimeZone(){
return this.userSession.getTimeZoneLocale();
}
#Produces
public UserType getUserType(){
return this.userSession.getUserType();
}
}
Note: Outside the injection into the Converter, all the other features are working perfectly, I can eject the EntityManager and other classes as well as persist the data in the database
Unfortunately, you can't. The spec mandates support for CDI injection in EntityListener implementations. It was not applied to converters.
If you want to access your injection points, you can use CDI.current() to get access to a CDI<Object> instance. Using this is just like using Instance<Object> - you can do things like .select(qualifier).select(clazz).get() to retrieve the bean instance.
If you need to use the qualifier, you need a literal first.
public class UserTimeZoneQualifierLiteral extends AnnotationLiteral<UserTimeZoneQualifier> implements UserTimeZoneQualifier {
}
Then instantiate
UserTimeZoneQualifier qualifier = new UserTimeZoneQualifier(); // or use a singleton here.
Related
Hi Team,
I am using Spring Boot 2.3.12.RELEASE which internally uses Spring Data Redis 2.3.9.RELEASE as a managed dependency.
When I am trying to save an object to the Redis cache using Spring Boot CRUD repository, it is getting stored without any error and I can see the object stored via Redis Manager.
However, when I try to fetch the same object using the same id i.e. using findById() method of CRUD repository, I am unable to find it.
Moreover, when I try findAll() on the same CRUDRepository object I get Optional.empty result which is strange as findAll() should return all records present in the repository.
I have added the configuration, repository and model class codes and some screenshots below for your perusal.
Please Note: I know there are many similar questions asked on this platform related to this issue and also I tried the solutions mentioned on such questions, but that didn't work for me.
Any solutions for this issue will be really helpful.
Model Class:
package com.test.cache.entity;
import java.util.concurrent.TimeUnit;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.TimeToLive;
import org.springframework.data.redis.core.index.Indexed;
import lombok.AllArgsConstructor;
import lombok.Data;
#Data
#AllArgsConstructor
#RedisHash("OTPValidationLogCache")
public class OTPValidationLogCache {
#Id
#Indexed
private String id;
#Indexed
private int validationFailureCount;
#TimeToLive(unit = TimeUnit.MILLISECONDS)
private long expiry;
}
Repository:
package com.test.cache.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.test.cache.entity.OTPValidationLogCache;
#Repository
public interface OTPValidationLogCacheRepository extends CrudRepository<OTPValidationLogCache, String> {
}
Redis Configuration Class:
package com.test.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import java.time.Duration;
#Configuration
#EnableRedisRepositories(basePackages = "com.test")
public class RedisConfig {
public static final long REDIS_CONNECT_TIMEOUT_SECS = 10L;
#Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration() {
final RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName("*******");
redisStandaloneConfiguration.setPort(6379);
redisStandaloneConfiguration.setPassword(RedisPassword.of("**********"));
//Credentials hidden for code sharing purpose.
return redisStandaloneConfiguration;
}
#Bean
public JedisConnectionFactory redisConnectionFactory() {
final JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder()
.connectTimeout(Duration.ofSeconds(REDIS_CONNECT_TIMEOUT_SECS))
.useSsl()
.build();
return new JedisConnectionFactory(redisStandaloneConfiguration(), jedisClientConfiguration);
}
#Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
}
Redis Manager Screenshot:
Eclipse IDE - Screenshot of Debugging Screen:
Well, I also raised a defect to spring-data-redis repository on GitHub for the same but the defect got closed by one of the maintainers of this repository without even posting any proper solution. He just gave a reference to an existing issue that was even closed without posting any solution. Here is the link to that issue.
https://github.com/spring-projects/spring-data-redis/issues/2130
Hence, while doing some research, I came across a solution that I am sharing here which worked in my case.
The solution is not to use the default CRUD repository methods implemented by Spring Boot, instead, write your own repository class having methods with your criteria to store and fetch the data from the Redis cache. That's it, now you should be able to store/fetch the data using the repository methods across your project.
I am posting an example below for reference.
Custom Repository Interface
package com.test.cache.repository;
import java.io.IOException;
import java.util.Map;
import com.test.cache.entity.OTPValidationLogCache;
public interface OTPValidationLogCacheRepository {
void save(OTPValidationLogCache customer);
OTPValidationLogCache find(Long id);
Map<?,?> findAll() throws IOException;
void update(OTPValidationLogCache customer);
void delete(Long id);
}
Custom Repository Interface Implementation
package com.test.cache.repository;
import java.io.IOException;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ScanOptions.ScanOptionsBuilder;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.test.cache.entity.OTPValidationLogCache;
import com.test.configuration.AppConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
#Repository
public class OTPValidationLogCacheRepositoryImpl implements OTPValidationLogCacheRepository {
private String key;
private RedisTemplate redisTemplate;
private HashOperations hashOperations;
private ObjectMapper objMapper;
#Autowired
public OTPValidationLogCacheRepositoryImpl(RedisTemplate redisTemplate, ObjectMapper objmapper) {
this.redisTemplate = redisTemplate;
this.objMapper = objmapper;
}
#PostConstruct
private void init() {
hashOperations = redisTemplate.opsForHash();
}
#Override
public void save(OTPValidationLogCache otpvalCache) {
hashOperations.put(key.concat(otpvalCache.getId().toString()), otpvalCache.getId(), otpvalCache);
setExpiryTime(key.concat(String.valueOf(otpvalCache.getId())), AppConfig.getUserBanDurationInSeconds());
}
#Override
public OTPValidationLogCache find(Long id) {
return (OTPValidationLogCache) hashOperations.get(key.concat(String.valueOf(id)), id);
}
#Override
public Map findAll() throws IOException {
Map<Integer, OTPValidationLogCache> values = Maps.newHashMap();
Cursor c = hashOperations.scan(OTPValidationLogCache.class, new ScanOptionsBuilder().match(key.concat("*")).build());
AtomicInteger count = new AtomicInteger(1);
c.forEachRemaining(element ->
{
values.put(count.getAndIncrement(), objMapper.convertValue(element, OTPValidationLogCache.class));
}
);
c.close();
return values;
}
#Override
public void update(OTPValidationLogCache customer) {
hashOperations.put(key, customer.getId(), customer);
}
#Override
public void delete(Long id) {
hashOperations.delete(key, id);
}
private void setExpiryTime(String key, Long timeout)
{
redisTemplate.expire(key, Duration.ofSeconds(timeout));
}
public synchronized void setKey(String key)
{
this.key = key;
}
}
Hope this helps others who may encounter this issue in the future.
Also, there is one more alternative available for this issue, that is switching to a different library provider such as Redisson, however, I have not tried it yet, so if you want, you may try and check.
You need to have the same package for your entities ,
I resolved the problem by extracting a lib and putting my entities there
You would find an explication here :
https://github.com/spring-projects/spring-data-redis/issues/2114
I have no idea why my spring boot aop pointCut expression won't work into other classes annotated #Component, it seems to be working only in #Controller.
approaches I have tried:
1, separate the classes in distinct files;
2, change to AutoAnnotationConfig with #Bean;
3, ...
but none worked
what I expect:
I presume that the aop point could cut into methods in #Component annotated classes;
Here is the code, and please refer to the comment line underneath.
package com.example.demo.basic;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
#Controller
public class B19_aop {
#RequestMapping("/basic/aop")
public String main(Map<String, String> map) {
map.put("print", "test");
ApplicationContext ctx = new AnnotationConfigApplicationContext(ReaderAutoAnnotationConfig.class);
Reader reader = ctx.getBean(Reader.class);
System.out.println(reader.read());
return "common";
}
}
interface Book{
public String getData();
}
#Component
class Magazine implements Book{
private String data;
Magazine(){ }
public String getData(){
return "reading magazine";
}
}
#Component
class Reader {
private Book book;
#Autowired
Reader(Book book){
this.book = book;
}
public String read(){
return this.book.getData();
}
};
#Aspect
#Component
class AOP{
// **************************************************
// #Pointcut(value = "execution(* com.example.demo.basic.Magazine.*(..))")
// **************************************************
// works not, why ?
#Pointcut(value = "execution(* com.example.demo.basic.B19_aop.*(..))")
// works nice !
public void myPointCut(){ }
#Around("myPointCut()")
public Object myAroundAdvice(ProceedingJoinPoint pjp) throws Throwable{
String className = pjp.getClass().toString();
String method = pjp.getSignature().getName();
Object[] args = pjp.getArgs();
Object result = pjp.proceed();
System.out.println("point cut advice works!");
return result;
}
}
#Configuration
#ComponentScan(basePackages = "com.example.demo.basic")
class ReaderAutoAnnotationConfig{}
what am I doing wrong ?
We are trying to externalize a date format in a bean for a field using the #JSONFormat so as to make it configurable.
#JsonFormat(pattern = "${application.date.format}")
private Date creationtime;
OR
#JsonFormat(pattern = "yyyy-MM-DD")
private Date creationtime;
It works when I give a standard String value. However when we externalize the value as shown in the first one, I am getting an exception saying :
java.lang.IllegalArgumentException: Illegal pattern character 'p'
to assign a variable value to the second approach we need a final String which is a constant expression. How can I make the pattern configurable?
Jackson uses JacksonAnnotationIntrospector to handle standard annotations. We can extend this mechanism providing our extra introspector together with default. To do that we can use pair method.
Simple custom implementation could look like below:
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.Annotated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Objects;
public class SpringJacksonAnnotationIntrospector extends AnnotationIntrospector {
private final ApplicationContext context;
#Autowired
public SpringJacksonAnnotationIntrospector(ApplicationContext context) {
this.context = Objects.requireNonNull(context);
}
#Override
public JsonFormat.Value findFormat(Annotated memberOrClass) {
JsonFormat annotation = _findAnnotation(memberOrClass, JsonFormat.class);
if (annotation == null) {
return null;
}
String basePattern = annotation.pattern();
if (basePattern.startsWith("$")) {
String pattern = context.getEnvironment().getProperty(basePattern.substring(2, basePattern.length() - 1));
return JsonFormat.Value.forPattern(pattern);
}
return null;
}
#Override
public Version version() {
return new Version(1, 0, 0, "", "org.company", "spring-jackson");
}
}
Above implementation reuses JsonFormat annotation but we can introduce new one to avoid confusion. We need to register our custom introspector. There is many different ways how to do that and it depends from your Spring configuration. We can do that for example, like below:
import com.example.jackson.SpringJacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
#EnableWebMvc
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Autowired
private ApplicationContext context;
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//JSON
AnnotationIntrospector pairedIntrospectors = AnnotationIntrospector.pair(springJacksonAnnotationIntrospector(),
new JacksonAnnotationIntrospector());
converters.add(new MappingJackson2HttpMessageConverter(
Jackson2ObjectMapperBuilder.json()
.annotationIntrospector(pairedIntrospectors)
.build()));
}
#Bean
public SpringJacksonAnnotationIntrospector springJacksonAnnotationIntrospector() {
return new SpringJacksonAnnotationIntrospector(context);
}
}
Since now, Spring's global configuration should be used to override date format.
See also:
Configuring ObjectMapper in Spring
SpringBoot: Consume & Produce XML with a Custom Serializer + Deserializer
How config gson in Spring boot?
How do I get a property value from an ApplicationContext object? (not using an annotation)
In my Spring (4.3.2) project I'm using Swagger (2.7.0) to automatically generate docs and swagger-ui for my project. This worked great so far.
But now I determined that I need to be able to declare Path Variables at the Controller level (not method level). And I need to teach swagger to discover these path variables and add them to docs and swagger-ui.
I've created custom annotation
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface HasCommonPathVariable {
/**
* The URI template variable to bind to.
*/
String name();
Class<?> type();
String defaultValue() default "";
}
And I'm using it like this:
#RestController
#Secured(SecurityConstants.ROLE_USER)
#RequestMapping(path = "/rest/api/v1/env/{envId}/asset-type")
#HasCommonPathVariable(name = "envId", type = Long.class)
public class AssetTypeRestController extends CustomRestControllerBase<Long, AssetTypeRow, AssetTypeService> {
// ... contorller code
}
I do not have controller methods that mentions parameters with Spring's PathVariable annotation, and the point is I'm not allowed to do so (it's due to the fact that I'm building micro-framework).
So question is: how to teach swagger to discover path variables described using custom annotation HasCommonPathVariable applied at the controller level?
Ok, I've figured it out. Here is the solution. This bean needs to be registered in the context. Swagger will discover this bean and use it as one of the plugins to enrich operations
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.core.annotation.Order;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Optional;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
#Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1000)
public class CommonPathVariableOperationBuilderPlugin implements OperationBuilderPlugin {
protected Logger log = Logger.getLogger(getClass());
private TypeResolver typeResolver;
public CommonPathVariableOperationBuilderPlugin(TypeResolver typeResolver) {
this.typeResolver = typeResolver;
}
#Override
public boolean supports(DocumentationType delimiter) {
return true;
}
#Override
public void apply(OperationContext opCtx) {
List<Parameter> ret = new ArrayList<Parameter>();
Optional<HasCommonPathVariable> annSingle = opCtx.findControllerAnnotation(HasCommonPathVariable.class);
if (annSingle.isPresent()) {
ret.add(addParameter(annSingle.get()));
}
Optional<HasCommonPathVariables> annPlural = opCtx.findControllerAnnotation(HasCommonPathVariables.class);
if (annPlural.isPresent()) {
for (HasCommonPathVariable ann : annPlural.get().value()) {
ret.add(addParameter(ann));
}
}
opCtx.operationBuilder().parameters(ret);
}
private Parameter addParameter(HasCommonPathVariable ann) {
ParameterBuilder pb = new ParameterBuilder();
pb.parameterType("path").name(ann.name()).type(typeResolver.resolve(ann.type()));
pb.modelRef(new ModelRef("string"));
pb.required(true);
if (!"".equals(ann.defaultValue())) {
pb.defaultValue(ann.defaultValue());
}
return pb.build();
}
}
I'm struggling a bit with understanding how rest interceptor annotations can add different values that are later visible in the filter. Given the code below I would expect that once in the filter the permissions values would have foo and bar in them, however they are empty. Any help would be greatly appreciated.
Annotation
package edu.psu.swe.fortress.poc.interceptor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.enterprise.util.Nonbinding;
import javax.ws.rs.NameBinding;
#NameBinding
#Target({ElementType.TYPE, ElementType.METHOD})
#Retention(value=RetentionPolicy.RUNTIME)
public #interface FortressProtected
{
#Nonbinding String[] permissions() default {};
}
Filter
package edu.psu.swe.fortress.poc.interceptor;
import java.io.IOException;
import java.lang.annotation.Annotation;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
#Provider
#FortressProtected
public class FortressAuthorizer implements ContainerRequestFilter
{
#Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
System.out.println("In the interceptor");
Class<?> clazz = this.getClass();
FortressProtected annotation = clazz.getAnnotation(edu.psu.swe.fortress.poc.interceptor.FortressProtected.class);
System.out.println("Annotation? " + clazz.isAnnotation());
for (Annotation a : clazz.getAnnotations())
{
System.out.println(a);
}
for (String s : annotation.permissions())
{
System.out.println(s);
}
}
}
App config
package edu.psu.swe.fortress.poc.rest;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import edu.psu.swe.fortress.poc.interceptor.FortressAuthorizer;
import edu.psu.swe.fortress.poc.interceptor.FortressProtected;
#ApplicationPath("")
public class FortressTestApp extends Application
{
private Set<Class<?>> clazzez_ = new HashSet<>();
{
clazzez_.add(ResourceImpl.class);
clazzez_.add(FortressProtected.class);
clazzez_.add(FortressAuthorizer.class);
}
public Set<Class<?>> getClasses()
{
return clazzez_;
}
}
Resource class
package edu.psu.swe.fortress.poc.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import edu.psu.swe.fortress.poc.interceptor.FortressProtected;
#FortressProtected(permissions={"foo", "bar"})
#Path("tests")
public class ResourceImpl
{
#GET
#Produces("application/text")
public String getHello()
{
FortressProtected annotation = this.getClass().getAnnotation(edu.psu.swe.fortress.poc.interceptor.FortressProtected.class);
System.out.println(annotation.toString());
return "hello";
}
}
Log output looks like:
15:59:55,223 INFO [stdout] (default task-9) #edu.psu.swe.fortress.poc.interceptor.FortressProtected(permissions=[])
15:59:55,229 INFO [stdout] (default task-9) #edu.psu.swe.fortress.poc.interceptor.FortressProtected(permissions=[foo, bar])
Thanks in advance.
Look at this in your filter
Class<?> clazz = this.getClass();
FortressProtected annotation = clazz.getAnnotation(FortressProtected.class);
this.getClass() corresponds to the filter class (whose annotation has no values). You instead need to get the annotation on the ResourceImpl
A couple options. You could explicitly use ResourceImpl.class.getAnnotation(...). But the problem with this is that once you bind more than one class, how do you match which class corresponds to which request. For that reason, the next option is more viable.
What you do is inject ResourceInfo. With this, you can call it's getResourceMethod or getResourceClass methods. These methods return the matched method and class, respectively. You could then check for the annotation at the class level as well as the method level (as we are also allowed to bind at the method level). So you might have something more like:
#Provider
#FortressProtected
public class FortressAuthorizer implements ContainerRequestFilter {
#Context
ResourceInfo resourceInfo;
#Override
public void filter(ContainerRequestContext requestContext) throws IOException {
Class<?> resourceClass = resourceInfo.getResourceClass();
FortressProtected classAnnot = resourceClass.getAnnotation(FortressProtected.class);
if (classAnnot != null) {
// do something with annotation
}
Method resourceMethod = resourceInfo.getResourceMethod();
FortressProtected methodAnnot = resourceMethod.getAnnotation(FortressProtected.class);
if (methodAnnot != null) {
// do something with annotation
}
}
}