2016-07-26 49 views
6

我有一個現有的類,我試圖掛鉤以獲取一些頭參數,以便將SSO用戶加入到我們的系統中。這個班級如下。將@Context參數傳遞給類中的方法

import java.util.Map; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context; 
import javax.ws.rs.core.HttpHeaders; 
import javax.ws.rs.core.MediaType; 

import org.springframework.stereotype.Component; 

@Component 
@Path("/http") 
public class HttpResource { 
    @GET 
    @Path("/getHeaders") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Map<String, String> getAllHeaders(@Context HttpHeaders headers) { 
     Map<String, String> headerList = new HashMap<String, String>(); 
     for (String key : headers.getRequestHeaders().keySet()) { 
      String value = headers.getRequestHeader(key).get(0); 
      System.out.println(key + " : " + value); 
      headerList.put(key, value); 
     } 
     return headerList; 
    } 
} 

我試圖搞清楚的是我怎麼叫getAllHeaders()@Context說法?我發現了很多我擁有的課程的例子,但沒有顯示如何調用它。

我也試過把註釋放在類裏面而不是作爲參數。

@Context 
HttpHeaders httpHeaders; 

,但是當我試圖訪問httpHeaders.getAllHeaders()返回null。我假設它並沒有真正創建,因爲javax文檔說它永遠不會返回null。

我想在我的SSOAuthorizationFilter.java中調用它,但也嘗試通過控制器訪問它。

+0

您正在使用哪個框架?春/ RestEasy的/新澤西? –

+0

@ Shiraaz.M春天 –

回答

4

先寫一個Annotation。

@Retention(RUNTIME) 
@Target({ PARAMETER }) 
@Documented 
public @interface SSOAuthorization {} 

然後解決這個問題。

public class SSOAuthorizationResolver { 

    public static class SSOAuthorizationInjectionResolver extends 
      ParamInjectionResolver<SSOAuthorization> { 
     public SSOAuthorizationInjectionResolver() { 
      super(SSOAuthorizationValueFactoryProvider.class); 
     } 
    } 


    @Singleton 
    public static class SSOAuthorizationValueFactoryProvider extends 
      AbstractValueFactoryProvider { 

     @Context 
     private HttpHeaders httpHeaders; 

     @Inject 
     public SSOAuthorizationValueFactoryProvider(
       final MultivaluedParameterExtractorProvider mpep, 
       final ServiceLocator injector) { 
      super(mpep, injector, Parameter.Source.UNKNOWN); 
     } 

     @Override 
     protected Factory<?> createValueFactory(final Parameter parameter) { 
      final Class<?> classType = parameter.getRawType(); 

      if (!Language.class.equals(classType) 
        || parameter.getAnnotation(SSOAuthorization.class) == null) { 
       return null; 
      } 

      return new AbstractContainerRequestValueFactory<String>() { 
       @Override 
       public String provide() { 
        // Can use httpHeader to get your header here. 
        return httpHeader.getHeaderString("SSOAuthorization"); 
       } 
      }; 
     } 

    } 

    // Binder implementation 
    public static class Binder extends AbstractBinder { 
     @Override 
     protected void configure() { 

      bind(SSOAuthorizationValueFactoryProvider.class).to(
        ValueFactoryProvider.class).in(Singleton.class); 

      bind(SSOAuthorizationInjectionResolver.class).to(
        new TypeLiteral<InjectionResolver<SSOAuthorization>>() { 
        }).in(Singleton.class); 

     } 

    } 
} 

而在ResourceConfig剛剛註冊的解析器

public class MyResourceConfig extends ResourceConfig { 

    public MyResourceConfig(Class... classes) { 
     super(classes); 
     register(new SSOAuthorizationResolver.Binder()); 
    } 
} 

最後在你的控制器與@SSOAuthorization註釋使用它。

@GET 
@Path("/get") 
@Produces(MediaType.APPLICATION_JSON) 
public String someMethod(@SSOAuthorization String auth) { 
    return auth; 
}