2010-11-18 5 views
5

特定のアノテーションのターゲットサービス/メソッドを検査し、そのアノテーションに対して特別な処理を行うApache CXF JAX-RS実装用のインターセプタを作成したいと思います。CXFインターセプタからターゲットサービス/メソッドを決定する

これを行う方法を説明するインターセプタのドキュメントに何も見当たりません。誰にもアイデアはありますか?

ありがとうございます!あなたができるようにバインドされたメソッドを与える必要があります


Exchange exchange = msg.getExchange(); 
BindingOperationInfo bop = exchange.get(BindingOperationInfo.class); 
MethodDispatcher md = (MethodDispatcher) 
       exchange.get(Service.class).get(MethodDispatcher.class.getName()); 
Method meth = md.getMethod(bop); 

答えて

4

Ah。私はCXFのJAX-RS部分を使用しているとは指定しませんでした。それがダニエル・クルップの答えに影響するかどうかはわかりませんが、彼の解決策は実際に私のためには機能しませんでした。 CXFがJAX-RSを扱うときには別のことをするからだと私は信じています。

私はCXFの[JAXRSInInterceptor][1]のソースに出くわしたと私はこのインターセプタがそうのようExchangeオブジェクトにメソッドの情報を入れていることをそのコードで見た:UNMARSHAL期に...

message.getExchange().put(OperationResourceInfo.class, ori);

CXF interceptor docsによれば、*_LOGICALフェーズ前に発生します。だから私が行うことができますUSER_LOGICAL相扱うInterceptor書き込むことによって:コールを処理ServiceMethodClass<?>にそこでのアクセスを得るために...

message.getExchange().get(OperationResourceInfo.class)

を!

+0

サブリソースを使用している場合、これは間違った結果を返します!返されたORIは、サブリソースロケータメソッドを選択するために使用されるものですが、acutalハンドラメソッドではありません –

+1

メソッド名が必要な場合は、 'operationResInfo.getMethodToInvoke()。getName(); 'を使用するだけで完全に動作します。 –

+0

ifあなたは 'operationResInfo.getMethodToInvoke()。getDeclaringClass()'を使用してクラスを必要とします。 – fafl

8

インターセプタはかなり遅く(USER_LOGICAL 相など)チェーンで実行されている場合、あなたのような何かを行うことができるはず宣言した クラスまたは注釈などを取得します。

+0

を思い付いクラスを横断する。 :)私はそのショットを与えるよ、ありがとう! –

+1

MethodDispatchedは推奨されていません。他の選択肢をお勧めしますか? PRE_STREAM&RECEIVEフェーズを使用しています – Harish

1

受け入れられた回答以来、まあまあです。しかし、その中

cxf-rt-core-2.7.3.jar 

一方に設けられ、いくつかの支援抽象化がソースからorg.apache.cxf.interceptor.security.AbstractAuthorizingInInterceptor

サンプルの抜粋が良いかもしれません参照されています。オリジナル、質問の答えオフ

protected Method getTargetMethod(Message m) { 
    BindingOperationInfo bop = m.getExchange().get(BindingOperationInfo.class); 
    if (bop != null) { 
     MethodDispatcher md = (MethodDispatcher) 
      m.getExchange().get(Service.class).get(MethodDispatcher.class.getName()); 
     return md.getMethod(bop); 
    } 
    Method method = (Method)m.get("org.apache.cxf.resource.method"); 
    if (method != null) { 
     return method; 
    } 
    throw new AccessDeniedException("Method is not available : Unauthorized"); 
} 
0

ビル、私はなるほど、それは私が探していたものの一種だが、私は私がにCXFでの用語を知っていないと思い、この

public UserContextInterceptor() { 
    super(Phase.USER_LOGICAL); 
} 

@Override 
public void handleMessage(Message message) { 
    if(StringUtils.isEmpty(getHeader("some-header-name", message))) { 
     final Method method = getTargetMethod(message); 
     if(isAnnotated(method.getDeclaringClass().getAnnotations()) || isAnnotated(method.getAnnotations())) { 
      final Fault fault = new Fault(new LoginException("Missing user id")); 
      fault.setStatusCode(HttpServletResponse.SC_UNAUTHORIZED); 
      throw fault; 
     } 
    } 
} 

private static Method getTargetMethod(Message message) { 
    final Exchange exchange = message.getExchange(); 
    final OperationResourceInfo resource = exchange.get(OperationResourceInfo.class); 
    if(resource == null || resource.getMethodToInvoke() == null) { 
     throw new AccessDeniedException("Method is not available"); 
    } 
    return resource.getMethodToInvoke(); 
} 

private static boolean isAnnotated(Annotation[] annotations) { 
    for(Annotation annotation : annotations) { 
     if(UserRequired.class.equals(annotation.annotationType())) { 
      return true; 
     } 
    } 
    return false; 
} 
関連する問題