2013-09-03 13 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); 
     } 
    } 

तरह लग रहा है और मेरी अंतिम बिंदुओं में से एक में मैं

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

कर मैं सभी REST आधारित काम के लिए RESTEasy का उपयोग कर रहा है।

और मैं भी Seam REST पुस्तकालय का उपयोग कर रहा 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 { 
} 

समस्या के लिए सर्वर अपवाद मैप करने के लिए?
- जब एंडपॉइंट अपवाद फेंकता है, तो कॉलबैक कभी भी फ़िल्टर कोड पर वापस नहीं आ जाता है।
- यह तब भी जब मैं try/catch/finally का उपयोग के रूप में

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

इस प्रकार है - मैं फिर भी परीक्षण कर सकते हैं कि IllegalArgumentExceptionHTTP 400Seam REST अपवाद मानचित्रण के आधार पर करने के लिए मैप किया गया है, लेकिन यह करने के मामले में SecurityFilter कोड को वापस कभी नहीं दिया जाता है सर्वर अपवाद।

आवश्यक है?
- मैं सर्वर उत्पन्न करना चाहते टोकन भी जब आवेदन अपवाद (रों) फेंकता है ताकि ग्राहक उनका उपयोग कर सकते
- कैसे कर सकते हैं, अपवाद के मामले में, मैं रूट कर सकते हैं SecurityFilter के माध्यम से मेरी प्रतिक्रिया?

+2

+1 में अधिक जानकारी प्राप्त कर सकते हैं अच्छी तरह से सवाल पूछा था। –

उत्तर

0

मुझे लगता है कि आपको इस उद्देश्य के लिए अपने अपवाद हैंडलर का उपयोग करना चाहिए जिसे वेब.एक्सएमएल में परिभाषित किया जा सकता है और अपवाद के मामले में आपको इसे अपवाद हैंडलर के अंदर संसाधित करना चाहिए, फ़िल्टर में नहीं।

आप लेख "Servlets - Exception Handling"

+0

यह मेरे उपरोक्त कोड में '@ अपवाद मैपिंग' द्वारा संभाला जाता है – daydreamer

संबंधित मुद्दे