Giter Site home page Giter Site logo

Comments (3)

Zinurist avatar Zinurist commented on May 24, 2024

My solution for now is to inherit AssetsBundle and implement the previous behavior (kotlin code):

class SPAAssetsBundle(resourcePath: String, uriPath: String, indexFile: String, assetsName: String) :
	AssetsBundle(resourcePath, uriPath, indexFile, assetsName) {
	override fun createServlet(): AssetServlet {
		return SPAAssetServlet(resourcePath, uriPath, indexFile, defaultMediaType, StandardCharsets.UTF_8)
	}
}

class SPAAssetServlet(resourcePath: String, uriPath: String, indexFile: String?, defaultMediaType: String?, defaultCharset: Charset?) :
	AssetServlet(resourcePath, uriPath, indexFile, defaultMediaType, defaultCharset) {
	override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
		super.doGet(req, resp)
		if (resp.status == HttpServletResponse.SC_NOT_FOUND) {
			resp.sendError(HttpServletResponse.SC_NOT_FOUND)
			// or do something else...
		}
	}
}

Would also allow for more complex error handling.

from dropwizard.

zUniQueX avatar zUniQueX commented on May 24, 2024

Hi @Zinurist. The problem of using sendError instead of setStatus is that not catching exceptions like IOException would lead to information exposure through the servlet container's error page mechanism. Therefore we decided to use setStatus to keep Dropwizard safe in the default configuration.

If you want to use custom error pages through the container's error page mechanism, you can restore the previous behaviour yourself. setStatus doesn't commit the response, so you can modify it afterwards. The following code snippet provides a very simple Filter that resets the response and uses sendError to trigger the error pages:

environment.servlets().addFilter("sendError", new Filter() {
            @Override
            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                filterChain.doFilter(servletRequest, servletResponse);
                if (!(servletResponse instanceof HttpServletResponse)) {
                    throw new IllegalStateException("Expecting instance of HttpServletResponse");
                }
                HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
                int status = httpServletResponse.getStatus();
                if (!httpServletResponse.isCommitted() && httpServletResponse.getStatus() >= 400) {
                    httpServletResponse.reset();
                    httpServletResponse.sendError(status);
                }
            }
        }).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");

from dropwizard.

Zinurist avatar Zinurist commented on May 24, 2024

Thank you for the help! Closing the issue since I was able to work around it.

from dropwizard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.