Java Code Examples for javax.servlet.http.HttpServletResponse#getCharacterEncoding()
The following examples show how to use
javax.servlet.http.HttpServletResponse#getCharacterEncoding() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultServerResponseBuilder.java From spring-analysis-note with MIT License | 6 votes |
private void writeHeaders(HttpServletResponse servletResponse) { this.headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { servletResponse.addHeader(headerName, headerValue); } }); // HttpServletResponse exposes some headers as properties: we should include those if not already present if (servletResponse.getContentType() == null && this.headers.getContentType() != null) { servletResponse.setContentType(this.headers.getContentType().toString()); } if (servletResponse.getCharacterEncoding() == null && this.headers.getContentType() != null && this.headers.getContentType().getCharset() != null) { servletResponse .setCharacterEncoding(this.headers.getContentType().getCharset().name()); } }
Example 2
Source File: TomcatHttpHandlerAdapter.java From spring-analysis-note with MIT License | 6 votes |
@Override protected void applyHeaders() { HttpServletResponse response = getNativeResponse(); MediaType contentType = getHeaders().getContentType(); if (response.getContentType() == null && contentType != null) { response.setContentType(contentType.toString()); } Charset charset = (contentType != null ? contentType.getCharset() : null); if (response.getCharacterEncoding() == null && charset != null) { response.setCharacterEncoding(charset.name()); } long contentLength = getHeaders().getContentLength(); if (contentLength != -1) { response.setContentLengthLong(contentLength); } }
Example 3
Source File: JettyHttpHandlerAdapter.java From spring-analysis-note with MIT License | 6 votes |
@Override protected void applyHeaders() { MediaType contentType = getHeaders().getContentType(); HttpServletResponse response = getNativeResponse(); if (response.getContentType() == null && contentType != null) { response.setContentType(contentType.toString()); } Charset charset = (contentType != null ? contentType.getCharset() : null); if (response.getCharacterEncoding() == null && charset != null) { response.setCharacterEncoding(charset.name()); } long contentLength = getHeaders().getContentLength(); if (contentLength != -1) { response.setContentLengthLong(contentLength); } }
Example 4
Source File: TomcatHttpHandlerAdapter.java From java-technology-stack with MIT License | 6 votes |
@Override protected void applyHeaders() { HttpServletResponse response = getNativeResponse(); MediaType contentType = getHeaders().getContentType(); if (response.getContentType() == null && contentType != null) { response.setContentType(contentType.toString()); } Charset charset = (contentType != null ? contentType.getCharset() : null); if (response.getCharacterEncoding() == null && charset != null) { response.setCharacterEncoding(charset.name()); } long contentLength = getHeaders().getContentLength(); if (contentLength != -1) { response.setContentLengthLong(contentLength); } }
Example 5
Source File: JettyHttpHandlerAdapter.java From java-technology-stack with MIT License | 6 votes |
@Override protected void applyHeaders() { MediaType contentType = getHeaders().getContentType(); HttpServletResponse response = getNativeResponse(); if (response.getContentType() == null && contentType != null) { response.setContentType(contentType.toString()); } Charset charset = (contentType != null ? contentType.getCharset() : null); if (response.getCharacterEncoding() == null && charset != null) { response.setCharacterEncoding(charset.name()); } long contentLength = getHeaders().getContentLength(); if (contentLength != -1) { response.setContentLengthLong(contentLength); } }
Example 6
Source File: UrlRewritingResponseWrapper.java From development with Apache License 2.0 | 6 votes |
/** * Basic constructor. * * pock: For BES we use a new server for every request. We can't determine * this server from the serverChain --> use the server of the request as * parameter (Bug 5487) * * @param response * The response we are wrapping * @param server * The server that was matched * @param ownHostName * String we are rewriting servers to * @throws IOException * When there is a problem with the streams */ public UrlRewritingResponseWrapper(HttpServletResponse response, Server server, String ownHostName, String contextPath, ServerChain serverChain) throws IOException { super(response); this.server = server; this.ownHostName = ownHostName; this.contextPath = contextPath; this.serverChain = serverChain; log = LogFactory.getLog(UrlRewritingResponseWrapper.class); outStream = new UrlRewritingOutputStream(response.getOutputStream(), contextPath, response .getCharacterEncoding()); outWriter = new PrintWriter(outStream); originalWriter = new PrintWriter(response.getOutputStream()); }
Example 7
Source File: ServletExportHandler.java From spacewalk with GNU General Public License v2.0 | 5 votes |
/** * Write the contents of the ExportWriter to the HttpServletResponse * @param response to write the contents of the ExportWriter to * @param pageList List of data to be exported to the Response * @throws IOException if there is an error trying to write to the Response */ public void writeExporterToOutput(HttpServletResponse response, List pageList) throws IOException { String charSet = response.getCharacterEncoding(); response.setContentType(writer.getMimeType() + ";charset=" + charSet); response.setHeader("Content-Disposition", "attachment; filename=download." + writer.getFileExtension()); writer.write(pageList); OutputStream out = response.getOutputStream(); out.write(writer.getContents().getBytes()); out.flush(); }
Example 8
Source File: FileDownloadAction.java From spacewalk with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { Map params = makeParamMap(request); ConfigFileForm cff = (ConfigFileForm) form; request.setAttribute(CSRF_TOKEN, request.getSession().getAttribute("csrf_token")); request.setAttribute("documentation", ConfigDefaults.get().isDocAvailable()); ConfigRevision cr = ConfigActionHelper.findRevision(request); String charSet = response.getCharacterEncoding(); String mimeType = getMimeType(cr); response.setContentType(mimeType + ";charset=" + charSet); response.setHeader("Content-Disposition", "attachment; filename=" + getDownloadFilename(cr)); try { OutputStream out = response.getOutputStream(); out.write(cr.getConfigContent().getContents()); out.flush(); } catch (IOException ioe) { ActionMessages msgs = new ActionMessages(); ActionMessage am = new ActionMessage("filedetails.jsp.error.download", ioe.getLocalizedMessage(), cr.getConfigFile().getConfigFileName().getPath()); msgs.add(ActionMessages.GLOBAL_MESSAGE, am); saveMessages(request, msgs); } cff.updateFromRevision(request, cr); return null; }
Example 9
Source File: cfIncludeHttpServletResponseWrapper.java From openbd-core with GNU General Public License v3.0 | 5 votes |
public cfIncludeHttpServletResponseWrapper( HttpServletResponse response ) { super( response ); res = response; encoding = response.getCharacterEncoding(); outputBuffer = new ByteArrayOutputStream( 8192 ); responseHeaders = new cfStructData(); outWriter = null; statusCode = SC_OK; }
Example 10
Source File: ServletExportHandler.java From uyuni with GNU General Public License v2.0 | 5 votes |
/** * Write the contents of the ExportWriter to the HttpServletResponse * @param response to write the contents of the ExportWriter to * @param pageList List of data to be exported to the Response * @throws IOException if there is an error trying to write to the Response */ public void writeExporterToOutput(HttpServletResponse response, List pageList) throws IOException { String charSet = response.getCharacterEncoding(); response.setContentType(writer.getMimeType() + ";charset=" + charSet); response.setHeader("Content-Disposition", "attachment; filename=download." + writer.getFileExtension()); writer.write(pageList); OutputStream out = response.getOutputStream(); out.write(writer.getContents().getBytes()); out.flush(); }
Example 11
Source File: FileDownloadAction.java From uyuni with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { Map params = makeParamMap(request); ConfigFileForm cff = (ConfigFileForm) form; request.setAttribute(CSRF_TOKEN, request.getSession().getAttribute("csrf_token")); request.setAttribute("documentation", ConfigDefaults.get().isDocAvailable()); ConfigRevision cr = ConfigActionHelper.findRevision(request); String charSet = response.getCharacterEncoding(); String mimeType = getMimeType(cr); response.setContentType(mimeType + ";charset=" + charSet); response.setHeader("Content-Disposition", "attachment; filename=" + getDownloadFilename(cr)); try { OutputStream out = response.getOutputStream(); out.write(cr.getConfigContent().getContents()); out.flush(); } catch (IOException ioe) { ActionMessages msgs = new ActionMessages(); ActionMessage am = new ActionMessage("filedetails.jsp.error.download", ioe.getLocalizedMessage(), cr.getConfigFile().getConfigFileName().getPath()); msgs.add(ActionMessages.GLOBAL_MESSAGE, am); saveMessages(request, msgs); } cff.updateFromRevision(request, cr); return null; }
Example 12
Source File: DefaultCharacterEncodingServlet.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { String requestCharacterEncoding = req.getCharacterEncoding(); String responseCharacterEncoding = resp.getCharacterEncoding(); PrintWriter writer = resp.getWriter(); writer.write(String.format("requestCharacterEncoding=%s;responseCharacterEncoding=%s;", requestCharacterEncoding, responseCharacterEncoding)); writer.close(); }
Example 13
Source File: SSIFilter.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // cast once HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; // indicate that we're in SSI processing req.setAttribute(Globals.SSI_FLAG_ATTR, "true"); // setup to capture output ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream(); ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(config.getServletContext(),req, res, basos); // process remainder of filter chain chain.doFilter(req, responseIncludeWrapper); // we can't assume the chain flushed its output responseIncludeWrapper.flushOutputStreamOrWriter(); byte[] bytes = basos.toByteArray(); // get content type String contentType = responseIncludeWrapper.getContentType(); // is this an allowed type for SSI processing? if (contentTypeRegEx.matcher(contentType).matches()) { String encoding = res.getCharacterEncoding(); // set up SSI processing SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(config.getServletContext(), req, res, isVirtualWebappRelative, debug, encoding); SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec); // prepare readers/writers Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), encoding); ByteArrayOutputStream ssiout = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(ssiout, encoding)); // do SSI processing long lastModified = ssiProcessor.process(reader, responseIncludeWrapper.getLastModified(), writer); // set output bytes writer.flush(); bytes = ssiout.toByteArray(); // override headers if (expires != null) { res.setDateHeader("expires", (new java.util.Date()).getTime() + expires.longValue() * 1000); } if (lastModified > 0) { res.setDateHeader("last-modified", lastModified); } res.setContentLength(bytes.length); Matcher shtmlMatcher = shtmlRegEx.matcher(responseIncludeWrapper.getContentType()); if (shtmlMatcher.matches()) { // Convert shtml mime type to ordinary html mime type but preserve // encoding, if any. String enc = shtmlMatcher.group(1); res.setContentType("text/html" + ((enc != null) ? enc : "")); } } // write output OutputStream out = null; try { out = res.getOutputStream(); } catch (IllegalStateException e) { // Ignore, will try to use a writer } if (out == null) { res.getWriter().write(new String(bytes)); } else { out.write(bytes); } }
Example 14
Source File: SSIFilter.java From tomcatsrc with Apache License 2.0 | 4 votes |
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // cast once HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; // indicate that we're in SSI processing req.setAttribute(Globals.SSI_FLAG_ATTR, "true"); // setup to capture output ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream(); ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(config.getServletContext(),req, res, basos); // process remainder of filter chain chain.doFilter(req, responseIncludeWrapper); // we can't assume the chain flushed its output responseIncludeWrapper.flushOutputStreamOrWriter(); byte[] bytes = basos.toByteArray(); // get content type String contentType = responseIncludeWrapper.getContentType(); // is this an allowed type for SSI processing? if (contentTypeRegEx.matcher(contentType).matches()) { String encoding = res.getCharacterEncoding(); // set up SSI processing SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(config.getServletContext(), req, res, isVirtualWebappRelative, debug, encoding); SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec); // prepare readers/writers Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), encoding); ByteArrayOutputStream ssiout = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(ssiout, encoding)); // do SSI processing long lastModified = ssiProcessor.process(reader, responseIncludeWrapper.getLastModified(), writer); // set output bytes writer.flush(); bytes = ssiout.toByteArray(); // override headers if (expires != null) { res.setDateHeader("expires", (new java.util.Date()).getTime() + expires.longValue() * 1000); } if (lastModified > 0) { res.setDateHeader("last-modified", lastModified); } res.setContentLength(bytes.length); Matcher shtmlMatcher = shtmlRegEx.matcher(responseIncludeWrapper.getContentType()); if (shtmlMatcher.matches()) { // Convert shtml mime type to ordinary html mime type but preserve // encoding, if any. String enc = shtmlMatcher.group(1); res.setContentType("text/html" + ((enc != null) ? enc : "")); } } // write output OutputStream out = null; try { out = res.getOutputStream(); } catch (IllegalStateException e) { // Ignore, will try to use a writer } if (out == null) { res.getWriter().write(new String(bytes)); } else { out.write(bytes); } }
Example 15
Source File: DownloadDiffAction.java From uyuni with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { RequestContext requestContext = new RequestContext(request); User user = requestContext.getCurrentUser(); Long ocrid = requestContext.getRequiredParam("ocrid"); //Get the objects. ConfigFile file = ConfigActionHelper.getFile(request); ConfigRevision revision = ConfigActionHelper.getRevision(request, file); ConfigRevision other = ConfigurationManager.getInstance() .lookupConfigRevision(user, ocrid); //Get the content that we will diff. String[] rev = revision.getConfigContent().getContentsString().split("\n"); String[] orev = other.getConfigContent().getContentsString().split("\n"); Diff diff = new Diff(rev, orev); String charSet = response.getCharacterEncoding(); String mimeType = "text/plain"; response.setContentType(mimeType + ";charset=" + charSet); response.setHeader("Content-Disposition", "attachment; filename=rhnpatch"); try { OutputStream out = response.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out); String path = file.getConfigFileName().getPath(); String opath = other.getConfigFile().getConfigFileName().getPath(); Date date = revision.getCreated(); Date odate = other.getCreated(); writer.write(diff.patchDiff(path, opath, date, odate)); writer.flush(); return null; } catch (IOException ioe) { ActionMessages msgs = new ActionMessages(); ActionMessage am = new ActionMessage("filedetails.jsp.error.download", ioe.getLocalizedMessage(), file.getConfigFileName().getPath()); msgs.add(ActionMessages.GLOBAL_MESSAGE, am); saveMessages(request, msgs); } return getStrutsDelegate().forwardParams(mapping.findForward( RhnHelper.DEFAULT_FORWARD), request.getParameterMap()); }
Example 16
Source File: CSVDownloadAction.java From uyuni with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(false); if (null == session) { throw new Exception("Missing session"); } String exportColumns = getExportColumns(request, session); List pageData = getPageData(request, session); // Read the CSV separator from user preferences User user = new RequestContext(request).getCurrentUser(); CSVWriter expW = new CSVWriter(new StringWriter(), user.getCsvSeparator()); String[] columns = exportColumns.split("\\s*,\\s*"); expW.setColumns(Arrays.asList(columns)); String header = getHeaderText(request, session); if (header != null) { expW.setHeaderText(header); } Elaborator elab = TagHelper.lookupElaboratorFor( getUniqueName(request), request); if (elab != null) { elab.elaborate(pageData); if (!pageData.isEmpty() && pageData.get(0) instanceof SystemSearchResult) { pageData = mergeWithPartialResult(pageData, (Map)session.getAttribute("ssr_" + request .getParameter(QUERY_DATA))); } } String contentType = expW.getMimeType() + ";charset=" + response.getCharacterEncoding(); response.setHeader("Content-Disposition", "attachment; filename=download." + expW.getFileExtension()); expW.write(pageData); return new ByteArrayStreamInfo(contentType, expW.getContents().getBytes()); }
Example 17
Source File: CSVDownloadAction.java From spacewalk with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(false); if (null == session) { throw new Exception("Missing session"); } String exportColumns = getExportColumns(request, session); List pageData = getPageData(request, session); // Read the CSV separator from user preferences User user = new RequestContext(request).getCurrentUser(); CSVWriter expW = new CSVWriter(new StringWriter(), user.getCsvSeparator()); String[] columns = exportColumns.split("\\s*,\\s*"); expW.setColumns(Arrays.asList(columns)); String header = getHeaderText(request, session); if (header != null) { expW.setHeaderText(header); } Elaborator elab = TagHelper.lookupElaboratorFor( getUniqueName(request), request); if (elab != null) { elab.elaborate(pageData); if (!pageData.isEmpty() && pageData.get(0) instanceof SystemSearchResult) { pageData = mergeWithPartialResult(pageData, (Map)session.getAttribute("ssr_" + request .getParameter(QUERY_DATA))); } } String contentType = expW.getMimeType() + ";charset=" + response.getCharacterEncoding(); response.setHeader("Content-Disposition", "attachment; filename=download." + expW.getFileExtension()); expW.write(pageData); return new ByteArrayStreamInfo(contentType, expW.getContents().getBytes()); }
Example 18
Source File: DownloadDiffAction.java From spacewalk with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { RequestContext requestContext = new RequestContext(request); User user = requestContext.getCurrentUser(); Long ocrid = requestContext.getRequiredParam("ocrid"); //Get the objects. ConfigFile file = ConfigActionHelper.getFile(request); ConfigRevision revision = ConfigActionHelper.getRevision(request, file); ConfigRevision other = ConfigurationManager.getInstance() .lookupConfigRevision(user, ocrid); //Get the content that we will diff. String[] rev = revision.getConfigContent().getContentsString().split("\n"); String[] orev = other.getConfigContent().getContentsString().split("\n"); Diff diff = new Diff(rev, orev); String charSet = response.getCharacterEncoding(); String mimeType = "text/plain"; response.setContentType(mimeType + ";charset=" + charSet); response.setHeader("Content-Disposition", "attachment; filename=rhnpatch"); try { OutputStream out = response.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out); String path = file.getConfigFileName().getPath(); String opath = other.getConfigFile().getConfigFileName().getPath(); Date date = revision.getCreated(); Date odate = other.getCreated(); writer.write(diff.patchDiff(path, opath, date, odate)); writer.flush(); return null; } catch (IOException ioe) { ActionMessages msgs = new ActionMessages(); ActionMessage am = new ActionMessage("filedetails.jsp.error.download", ioe.getLocalizedMessage(), file.getConfigFileName().getPath()); msgs.add(ActionMessages.GLOBAL_MESSAGE, am); saveMessages(request, msgs); } return getStrutsDelegate().forwardParams(mapping.findForward( RhnHelper.DEFAULT_FORWARD), request.getParameterMap()); }
Example 19
Source File: SSIFilter.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // cast once HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; // setup to capture output ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream(); ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(res, basos); // process remainder of filter chain chain.doFilter(req, responseIncludeWrapper); // we can't assume the chain flushed its output responseIncludeWrapper.flushOutputStreamOrWriter(); byte[] bytes = basos.toByteArray(); // get content type String contentType = responseIncludeWrapper.getContentType(); // is this an allowed type for SSI processing? if (contentType != null && contentTypeRegEx.matcher(contentType).matches()) { String encoding = res.getCharacterEncoding(); // set up SSI processing SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(config.getServletContext(), req, res, isVirtualWebappRelative, debug, encoding); SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec); // prepare readers/writers Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), encoding); ByteArrayOutputStream ssiout = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(ssiout, encoding)); // do SSI processing long lastModified = ssiProcessor.process(reader, responseIncludeWrapper.getLastModified(), writer); // set output bytes writer.flush(); bytes = ssiout.toByteArray(); // override headers if (expires != null) { res.setDateHeader("expires", (new java.util.Date()).getTime() + expires.longValue() * 1000); } if (lastModified > 0) { res.setDateHeader("last-modified", lastModified); } res.setContentLength(bytes.length); Matcher shtmlMatcher = shtmlRegEx.matcher(responseIncludeWrapper.getContentType()); if (shtmlMatcher.matches()) { // Convert shtml mime type to ordinary html mime type but preserve // encoding, if any. String enc = shtmlMatcher.group(1); res.setContentType("text/html" + ((enc != null) ? enc : "")); } } // write output OutputStream out = null; try { out = res.getOutputStream(); } catch (IllegalStateException e) { // Ignore, will try to use a writer } if (out == null) { res.getWriter().write(new String(bytes)); } else { out.write(bytes); } }