2015-03-22 14 views
7

Tôi đang vật lộn một chút với sự hiểu biết cách chú thích của phần đánh chặn phần còn lại có thể thêm các giá trị khác nhau mà sau này có thể nhìn thấy trong bộ lọc. Với mã dưới đây, tôi sẽ mong đợi rằng một khi trong bộ lọc các giá trị quyền sẽ có foo và bar trong chúng, tuy nhiên chúng trống. Mọi sự trợ giúp sẽ rất được trân trọng.Nhận các giá trị chú thích lớp tài nguyên bên trong ContainerRequestFilter

Chú

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 {}; 
} 

Lọc

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 cấu hình

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_; 
    } 
} 

lớp Resource

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"; 
    } 
} 

đầu ra Log trông giống như:

15: 59: 55.223 INFO [stdout] (mặc định nhiệm vụ-9) @ edu.psu.swe.fortress.poc.interceptor.FortressProtected (quyền = []) 15: 59: 55,229 INFO [stdout] (nhiệm vụ mặc định-9) @ edu.psu.swe.fortress.poc.interceptor.FortressProtected (permission = [foo, bar])

Cảm ơn trước.

Trả lời

12

Nhìn vào này trong bộ lọc của bạn

Class<?> clazz = this.getClass(); 
FortressProtected annotation = clazz.getAnnotation(FortressProtected.class); 

this.getClass() tương ứng với lớp lọc (có chú thích không có giá trị). Thay vào đó, bạn cần nhận chú thích trên các số ResourceImpl

Một vài tùy chọn. Bạn có thể sử dụng rõ ràng ResourceImpl.class.getAnnotation(...). Nhưng vấn đề với điều này là một khi bạn liên kết nhiều hơn một lớp, làm thế nào để bạn phù hợp với lớp nào tương ứng với yêu cầu nào. Vì lý do đó, tùy chọn tiếp theo là khả thi hơn.

Việc bạn cần làm là tiêm ResourceInfo. Với điều này, bạn có thể gọi nó là phương pháp getResourceMethod hoặc getResourceClass. Các phương thức này trả về phương thức và lớp được so khớp tương ứng. Sau đó, bạn có thể kiểm tra chú thích ở cấp lớp cũng như cấp phương thức (vì chúng tôi cũng được phép liên kết ở cấp phương thức). Vì vậy, bạn có thể có một cái gì đó giống như:

@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 
    } 
    } 
} 
+2

Đó chính xác là những gì tôi đang tìm kiếm. Cảm ơn nhiều. –

Các vấn đề liên quan