Java 类org.springframework.core.io.AbstractFileResolvingResource 实例源码

项目:chassis    文件:SwaggerServlet.java   
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // figure out the real path
    String pathInfo = StringUtils.trimToEmpty(req.getPathInfo());

    while (pathInfo.endsWith("/")) {
        pathInfo = StringUtils.removeEnd(pathInfo, "/");
    }

    while (pathInfo.startsWith("/")) {
        pathInfo = StringUtils.removeStart(pathInfo, "/");
    }

    if (StringUtils.isBlank(pathInfo)) {
        resp.sendRedirect(rootContextPath + "/" + WELCOME_PAGE);
    } else {
        // get the resource
        AbstractFileResolvingResource resource = (AbstractFileResolvingResource)resourceLoader.getResource(SWAGGER_DIRECTORY + "/" + pathInfo);

        // send it to the response
        if (resource.exists()) {
            StreamUtils.copy(resource.getInputStream(), resp.getOutputStream());
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.flushBuffer();
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}