2013-09-03 16 views
5

Co próbuję zrobić?Filtr serwletów nie działa, gdy zostanie zgłoszony wyjątek.

Próbuję wygenerować nowe żetony o czasie, od strony serwera, klient może wykorzystać w swoim kolejnym wniosku

Co Próbowałem?

Mam filtr serwletu, który otacza REST połączeń i wygląda

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

iw jednym z moich punktów końcowych robię

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

Używam RESTEasy dla wszystkich REST pracy opartej.

i jestem również za pomocą Seam REST bibliotekę map wyjątków serwera do REST wyjątków opartych

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

problem? - gdy punkt końcowy zgłasza wyjątek, wywołanie zwrotne nigdy nie jest zwracane do kodu filtra.
- To nawet gdy używam try/catch/finally następująco

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

- Mogę jednak sprawdzić, czy IllegalArgumentException jest odwzorowywany HTTP 400 oparciu o Seam REST mapowania wyjątek, ale to nigdy nie jest zwracana z powrotem do kodu SecurityFilter w przypadku wyjątki od serwera.

Wymagany?
- Chcę, aby wygenerować serwera żetony nawet gdy aplikacja zgłasza wyjątek (s), dzięki czemu klient może z nich korzystać
- jak w przypadku wyjątków, mogę trasa moją odpowiedź poprzez SecurityFilter?

+2

+1 Dobrze zadawane pytanie. –

Odpowiedz

0

Myślę, że powinieneś użyć w tym celu obsługi własnych wyjątków, które można zdefiniować w web.xml, aw przypadku wyjątku powinieneś przetworzyć go wewnątrz obsługi wyjątku, a nie w filtrze.

można uzyskać więcej szczegółów w artykule "Servlets - Exception Handling"

+0

to jest to, co jest obsługiwane przez '@ ExceptionMapping' w powyższym kodzie – daydreamer

Powiązane problemy