List of usage examples for javax.servlet ServletResponse setContentLength
public void setContentLength(int len);
From source file:org.opencms.jsp.jsonpart.CmsJsonPartFilter.java
/** * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) *///from w w w .j av a 2 s . c o m public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (isJsonRequest(request)) { if (m_isNested.get() == null) { try { m_isNested.set(Boolean.TRUE); RequestWrapper reqWrapper = new RequestWrapper((HttpServletRequest) request); ResponseWrapper resWrapper = new ResponseWrapper((HttpServletResponse) response); chain.doFilter(reqWrapper, resWrapper); byte[] data = resWrapper.getBytes(); String content = new String(data, resWrapper.getCharacterEncoding()); String transformedContent = transformContent(content); byte[] transformedData = transformedContent.getBytes("UTF-8"); response.setContentType("application/json; charset=UTF-8"); response.setContentLength(transformedData.length); response.getOutputStream().write(transformedData); response.getOutputStream().flush(); } finally { m_isNested.set(null); } } } else { chain.doFilter(request, response); } }
From source file:org.sakaiproject.nakamura.http.i18n.I18nFilter.java
/** * Filter <code>output</code> of any message keys by replacing them with the matching * message from the language bundle associated to the user. * * @param srequest/*from www.ja v a2 s . c om*/ * @param response * @param output * @throws IOException */ private void writeFilteredResponse(SlingHttpServletRequest srequest, ServletResponse response, String output) throws IOException { StringBuilder sb = new StringBuilder(output); try { Session session = srequest.getResourceResolver().adaptTo(Session.class); Node bundlesNode = session.getNode(bundlesPath); // load the language bundle Locale locale = getLocale(srequest); Properties bndLang = getLangBundle(bundlesNode, locale.toString()); // load the default bundle Properties bndLangDefault = getLangBundle(bundlesNode, "default"); // check for message keys and replace them with the appropriate message Matcher m = messageKeyPattern.matcher(output); ArrayList<String> matchedKeys = new ArrayList<String>(); while (m.find()) { String msgKey = m.group(0); String key = m.group(1); if (!matchedKeys.contains(key)) { String message = ""; if (bndLang.containsKey(key)) { message = bndLang.getProperty(key); } else if (bndLangDefault.containsKey(key)) { message = bndLangDefault.getProperty(key); } else { String msg = "[MESSAGE KEY NOT FOUND '" + key + "']"; logger.warn(msg); if (showMissingKeys) { message = msg; } } // replace all instances of msgKey with the actual message int keyStart = sb.indexOf(msgKey); while (keyStart >= 0) { sb.replace(keyStart, keyStart + msgKey.length(), message); keyStart = sb.indexOf(msgKey, keyStart); } // track the group so we don't try to replace it again matchedKeys.add(key); } } } catch (RepositoryException e) { logger.error(e.getMessage(), e); } response.setContentLength(sb.length()); // send the output to the actual response try { response.getWriter().write(sb.toString()); } catch (IllegalStateException e) { response.getOutputStream().write(sb.toString().getBytes("UTF-8")); } }
From source file:org.uberfire.server.locale.GWTLocaleHeaderFilter.java
@Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { final CharResponseWrapper wrappedResponse = getWrapper((HttpServletResponse) response); chain.doFilter(request, wrappedResponse); final String output; final Locale locale = getLocale(request); final String injectedScript = "<meta name=\"gwt:property\" content=\"locale=" + locale.toString() + "\">"; final Document document = Jsoup.parse(wrappedResponse.toString()); document.head().append(injectedScript); output = document.html();/*from w w w.j ava 2s .co m*/ final byte[] outputBytes = output.getBytes("UTF-8"); response.setContentLength(outputBytes.length); response.getWriter().print(output); }
From source file:org.wso2.adminui.AdminUIServletFilter.java
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { try {/*from w ww .ja v a 2 s. c o m*/ HttpServletRequest httpServletRequest = (HttpServletRequest) request; if (contextRoot == null) { //Context root could be "" or some othervalue this.contextRoot = httpServletRequest.getContextPath(); } String requestURI = httpServletRequest.getRequestURI(); int indexOfDot = requestURI.lastIndexOf("."); boolean isFile = false; if (indexOfDot != -1) { isFile = requestURI.substring(indexOfDot).matches("\\.(.)*"); } if (!isFile && requestURI.lastIndexOf("/") != requestURI.length() - 1) { requestURI += "/"; } Map generatedPages = (Map) servletContext.getAttribute(AdminUIConstants.GENERATED_PAGES); if (requestURI.equals(contextRoot) || requestURI.equals(contextRoot + "/")) { response.setContentType("text/html"); boolean enableConsole = ((Boolean) servletContext.getAttribute(AdminUIConstants.ENABLE_CONSOLE)) .booleanValue(); if (!enableConsole) { ServletOutputStream out = response.getOutputStream(); out.write(("<b>Management Console has been disabled.</b> " + "Enable it in the server.xml and try again.").getBytes()); ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_FORBIDDEN); out.flush(); out.close(); return; } String fileContents = (String) generatedPages.get("index.html"); if (fileContents != null) { ServletOutputStream op = response.getOutputStream(); response.setContentLength(fileContents.getBytes().length); op.write(fileContents.getBytes()); return; } } else { String urlKey; if (contextRoot.equals("/")) { urlKey = requestURI.substring(contextRoot.length(), requestURI.length()); } else { urlKey = requestURI.substring(1 + contextRoot.length(), requestURI.length()); } if (generatedPages != null) { String fileContents = (String) generatedPages.get(urlKey); if (fileContents != null) { ServletOutputStream op = response.getOutputStream(); response.setContentType("text/html"); response.setContentLength(fileContents.getBytes().length); op.write(fileContents.getBytes()); return; } } /* || has been used to support any client who wants to access the "global_params.js" regardless of where they want to access. */ if (urlKey.equals(GLOBAL_PARAMS_JS) || urlKey.indexOf(GLOBAL_PARAMS_JS) > -1) { initGlobalParams((HttpServletResponse) response); return; } } } catch (Exception e) { String msg = "Exception occurred while processing Request"; log.error(msg, e); throw new ServletException(msg, e); } filterChain.doFilter(request, response); }
From source file:org.yestech.lib.io.FileSystemFileDownloadFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String fileName = request.getParameter("file"); FileInputStream inputStream = null; ServletOutputStream outputStream = response.getOutputStream(); if (StringUtils.isNotBlank(fileName)) { try {/*from w w w .j a v a 2 s . c om*/ File downloadFile = new File(baseDirectory, fileName); response.setContentLength((int) downloadFile.length()); inputStream = openInputStream(downloadFile); copy(inputStream, outputStream); outputStream.flush(); if (deleteAfterDownload) { downloadFile.delete(); } } catch (Exception e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } finally { closeQuietly(inputStream); closeQuietly(outputStream); } } }