Captures the response, processes SSI directives if the content type matches, and writes the output.
(ServletRequest request, ServletResponse response, FilterChain chain)
| 107 | * Captures the response, processes SSI directives if the content type matches, and writes the output. |
| 108 | */ |
| 109 | @Override |
| 110 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 111 | throws IOException, ServletException { |
| 112 | // cast once |
| 113 | HttpServletRequest req = (HttpServletRequest) request; |
| 114 | HttpServletResponse res = (HttpServletResponse) response; |
| 115 | |
| 116 | // setup to capture output |
| 117 | ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream(); |
| 118 | ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(res, basos); |
| 119 | |
| 120 | // process remainder of filter chain |
| 121 | chain.doFilter(req, responseIncludeWrapper); |
| 122 | |
| 123 | // we can't assume the chain flushed its output |
| 124 | responseIncludeWrapper.flushOutputStreamOrWriter(); |
| 125 | byte[] bytes = basos.toByteArray(); |
| 126 | |
| 127 | // get content type |
| 128 | String contentType = responseIncludeWrapper.getContentType(); |
| 129 | |
| 130 | // is this an allowed type for SSI processing? |
| 131 | if (contentType != null && contentTypeRegEx.matcher(contentType).matches()) { |
| 132 | String encoding = res.getCharacterEncoding(); |
| 133 | |
| 134 | // set up SSI processing |
| 135 | SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(getServletContext(), req, res, |
| 136 | isVirtualWebappRelative, debug, encoding); |
| 137 | SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec); |
| 138 | |
| 139 | // prepare readers/writers |
| 140 | Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), encoding); |
| 141 | ByteArrayOutputStream ssiout = new ByteArrayOutputStream(); |
| 142 | PrintWriter writer = new PrintWriter(new OutputStreamWriter(ssiout, encoding)); |
| 143 | |
| 144 | // do SSI processing |
| 145 | long lastModified = ssiProcessor.process(reader, responseIncludeWrapper.getLastModified(), writer); |
| 146 | |
| 147 | // set output bytes |
| 148 | writer.flush(); |
| 149 | bytes = ssiout.toByteArray(); |
| 150 | |
| 151 | // override headers |
| 152 | if (expires != null) { |
| 153 | res.setDateHeader("expires", (new java.util.Date()).getTime() + expires.longValue() * 1000); |
| 154 | } |
| 155 | if (lastModified > 0) { |
| 156 | res.setDateHeader("last-modified", lastModified); |
| 157 | } |
| 158 | res.setContentLength(bytes.length); |
| 159 | |
| 160 | Matcher shtmlMatcher = shtmlRegEx.matcher(responseIncludeWrapper.getContentType()); |
| 161 | if (shtmlMatcher.matches()) { |
| 162 | // Convert SHTML mime type to ordinary HTML mime type but preserve |
| 163 | // encoding, if any. |
| 164 | String enc = shtmlMatcher.group(1); |
| 165 | res.setContentType("text/html" + ((enc != null) ? enc : "")); |
| 166 | } |
nothing calls this directly
no test coverage detected