2013-09-03 5 views
5

何をしようとしていますか?例外がスローされたときにサーブレットフィルタが機能しない

私は、クライアントが、私が試してみました何その後続の要求に

を使用することができ、サーバー側からの新しいタイムスタンプトークンを生成しようとしていますか?

は、私の周りREST呼び出しをラップし、

@WebFilter(urlPatterns = "/rest/secure") 
public class SecurityFilter implements Filter { 

    private static final Pattern PATTERN = Pattern.compile(":"); 
    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityFilter.class); 

    @Override 
    public void init(final FilterConfig filterConfig) throws ServletException { 
     //LOGGER.info("initializing SecurityFilter"); 
    } 

    @Override 
    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { 
     final HttpServletResponse httpServletResponse = (HttpServletResponse) response; 
     final String authToken = getAuthenticationHeaderValue((HttpServletRequest) request); 

     try { 
      validateAuthToken(authToken); 
     } catch (IllegalArgumentException tokenNotValidException) { 
      LOGGER.error("invalid token"); 
      httpServletResponse.sendError(401); 
     } 

     try { 
      chain.doFilter(request, response); 
     } catch (Exception e) { 
      LOGGER.error("exception: " + e.getMessage()); 
     }finally { 
      final String newAuthToken = generateNewAuthToken(authToken); 
      httpServletResponse.addHeader(AUTH_TOKEN, newAuthToken); 
      LOGGER.info("added new security token: " + newAuthToken); 
     } 
    } 

のように見えて、私のエンドポイントの1つに、私は私がすべてのRESTベースの仕事のためRESTEasyを使用しています

@PUT 
public Response updateUser() { 
    throw new IllegalArgumentException("just for test purposes"); 
} 

を行うサーブレットフィルタを持っています。

私もRESTベースの例外

@ExceptionMapping.List({ 
     @ExceptionMapping(exceptionType = IllegalArgumentException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = PersistenceException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = ConstraintViolationException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = ValidationException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = NoResultException.class, status = 404, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = IllegalStateException.class, status = 406, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = NoClassDefFoundError.class, status = 404, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = UnsupportedOperationException.class, status = 400, useExceptionMessage = true), 
}) 
@ApplicationPath("/rest") 
public class MarketApplicationConfiguration extends Application { 
} 

問題へのサーバーの例外をマップするためにSeam RESTライブラリを使用していますか?
- エンドポイントが例外をスローすると、コールバックは決してフィルタコードに返されません。
は - 私はしかしIllegalArgumentExceptionSeam REST例外マッピングに基づいてHTTP 400にマッピングされていることをテストすることができますが、それはの場合にSecurityFilterコードに戻されることはありません -

 try { 
       chain.doFilter(request, response); 
      } catch (Exception e) { 
       LOGGER.error("exception: " + e.getMessage()); 
      }finally { 
       final String newAuthToken = generateNewAuthToken(authToken); 
       httpServletResponse.addHeader(AUTH_TOKEN, newAuthToken); 
       LOGGER.info("added new security token: " + newAuthToken); 
      } 

を次のように私はtry/catch/finallyを使用する場合にも、ありますサーバー例外。

必須ですか?
- アプリケーションが例外をスローしてクライアントが使用できるようにしても、サーバートークンを生成したい
- 例外が発生した場合、どうすればSecurityFilter経由で応答をルーティングできますか?

+2

+1で詳細を取得することができます。 –

答えて

0

私はweb.xmlで定義できる独自の例外ハンドラを使用するべきだと考えています。例外の場合は、例外ハンドラ内でフィルタで処理する必要があります。

あなたはまあ質問を記事"Servlets - Exception Handling"

+0

これは上記のコードで '@ ExceptionMapping'によって処理されるものです – daydreamer

関連する問題