Filter gives me 500 instead of the response status I defined

1,210 views
Skip to first unread message

Shengjie Min

unread,
Mar 5, 2013, 8:44:21 AM3/5/13
to dropwiz...@googlegroups.com
Guys,

I have a filter added in for my web service like this:

    @Override
    public final void run(T configuration, Environment environment) throws Exception {
        environment.addFilter(new TimeValidationFilter(), "/*");
        environment.manage(***blablabla***);
        environment.addHealthCheck(***blablabla***);
        try {
            environment.addResource(new BlablaResourceImpl(configuration));
        } catch (ConfigurationException e) {
            throw new IllegalStateException(e);
        }
    }

my TimeValidationFilter is just checking whether startTime and endTime is null at this stage:

public class TimeValidationFilter implements Filter {

    public final static String START_TIME = "startTime";
    public final static String END_TIME = "endTime";

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        String startTimeStr = request.getParameter(START_TIME);
        String endTimeStr = request.getParameter(END_TIME);

        if (startTimeStr == null || endTimeStr == null) {
            throw new WebApplicationException(Response.Status.BAD_REQUEST);  //line 42
        }
        ........
        chain.doFilter(req, res);
    }

When I have startTime and endTime correctly, the response status from BlablaResourceImpl is giving back the response code 200, 400, 201 all correctly.
When I make a request without startTime and endTime, I am expecting to see 400. BUT instead I get 500 server error and the exception below:

WARN  [2013-03-05 13:13:19,490] org.eclipse.jetty.servlet.ServletHandler: /blablabla
! javax.ws.rs.WebApplicationException: null
! at com.*******.filters.TimeValidationFilter.doFilter(TimeValidationFilter.java:42)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
! at com.yammer.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
! at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)
! at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)
! at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
! at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)
! at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
! at com.yammer.metrics.jetty.InstrumentedHandler.handle(InstrumentedHandler.java:200)
! at org.eclipse.jetty.server.handler.GzipHandler.handle(GzipHandler.java:230)
! at com.yammer.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:123)
! at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
! at org.eclipse.jetty.server.Server.handle(Server.java:347)
! at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)
! at org.eclipse.jetty.server.BlockingHttpConnection.handleRequest(BlockingHttpConnection.java:47)
! at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890)
! at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944)
! at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634)
! at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)
! at org.eclipse.jetty.server.BlockingHttpConnection.handle(BlockingHttpConnection.java:66)
! at org.eclipse.jetty.server.nio.BlockingChannelConnector$BlockingChannelEndPoint.run(BlockingChannelConnector.java:293)
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)
! at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)
! at java.lang.Thread.run(Thread.java:722)

any ideas?

--
All the best,
Shengjie Min

Michael Fairley

unread,
Mar 5, 2013, 9:19:25 AM3/5/13
to dropwiz...@googlegroups.com
I response on your StackOverflow question: http://stackoverflow.com/questions/15225436/dropwizard-filter-gives-me-500-server-error-instead-of-the-response-status-def/15226108#15226108

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Coda Hale

unread,
Mar 5, 2013, 11:18:52 AM3/5/13
to dropwiz...@googlegroups.com
On Mar 5, 2013, at 5:44 AM, Shengjie Min <kelvi...@gmail.com> wrote:
> throw new WebApplicationException(Response.Status.BAD_REQUEST); //line 42

WebApplicationException is from Jersey. You're writing a servlet filter, which doesn't run inside Jersey.

You need to use ServletResponse#sendError.

---
Coda Hale
http://codahale.com




Shengjie Min

unread,
Mar 6, 2013, 4:55:56 AM3/6/13
to dropwiz...@googlegroups.com
Thanks Michael :)
Reply all
Reply to author
Forward
0 new messages