List of usage examples for javax.servlet.http HttpServletResponse addHeader
public void addHeader(String name, String value);
From source file:com.kappaware.logtrawler.Utils.java
public static void setCache(HttpServletResponse response, int cacheValue) { if (cacheValue == 0) { response.setHeader(HEADER_PRAGMA, "no-cache"); // HTTP 1.0 header response.setDateHeader(HEADER_EXPIRES, 1L); // HTTP 1.1 header: "no-cache" is the standard value, // "no-store" is necessary to prevent caching on FireFox. response.setHeader(HEADER_CACHE_CONTROL, "no-cache"); response.addHeader(HEADER_CACHE_CONTROL, "no-store"); response.addHeader(HEADER_CACHE_CONTROL, "must-revalidate"); response.addHeader(HEADER_CACHE_CONTROL, "post-check=0"); response.addHeader(HEADER_CACHE_CONTROL, "pre-check=0"); } else {//from w w w. j a va 2 s . com response.setHeader(HEADER_CACHE_CONTROL, "public"); long now = (new Date()).getTime(); response.setDateHeader("Date", now); response.setDateHeader(HEADER_EXPIRES, now + (cacheValue * 1000L)); // HTTP 1.1 header String headerValue = "max-age=" + Long.toString(cacheValue); response.setHeader(HEADER_CACHE_CONTROL, headerValue); } }
From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java
public static void sendFile(DataSource ds, HttpServletResponse response) { response.setContentType(ds.getContentType()); if (ds.getName() != null && ds.getName().length() > 0) { response.addHeader("Content-Disposition", "attachment; filename=" + ds.getName()); }/*from w w w .j a v a 2 s . c om*/ OutputStream outputStream = null; BufferedInputStream bufferedInputStream = null; try { outputStream = response.getOutputStream(); bufferedInputStream = new BufferedInputStream(ds.getInputStream()); int readBytes = 0; while ((readBytes = bufferedInputStream.read()) != -1) { outputStream.write(readBytes); } if (log.isDebugEnabled()) { log.debug("finished sending bytes"); } } catch (IOException ex) { log.error("Error serving a file: " + ex.getMessage(), ex); } finally { if (outputStream != null) try { outputStream.close(); } catch (Exception ex) { } if (bufferedInputStream != null) try { bufferedInputStream.close(); } catch (Exception ex) { } } }
From source file:org.esgf.legacydatacart.WgetGeneratorController.java
private static void writeBash(String wgetText, String filename, HttpServletResponse response) { try {/*from w w w .j a v a 2 s . co m*/ //attach the sh file extension to the response response.setContentType("text/x-sh"); response.addHeader("Content-Disposition", "attachment; filename=" + filename); response.setContentLength((int) wgetText.length()); PrintWriter out = response.getWriter(); out.print(wgetText); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.zimbra.cs.servlet.util.AuthUtil.java
/** * If returns null, then resp.sendError has been called. *//*from w w w.j ava 2 s . co m*/ public static Account basicAuthRequest(HttpServletRequest req, HttpServletResponse resp, boolean sendChallenge, boolean isDav) throws IOException, ServiceException { try { return basicAuthRequest(req, !sendChallenge, isDav); } catch (UserServletException e) { if (e.getHttpStatusCode() == HttpServletResponse.SC_UNAUTHORIZED) { if (sendChallenge) { resp.addHeader(WWW_AUTHENTICATE_HEADER, getRealmHeader(req, null)); resp.sendError(e.getHttpStatusCode(), e.getMessage()); } } else { resp.sendError(e.getHttpStatusCode(), e.getMessage()); } return null; } }
From source file:com.zimbra.cs.dav.service.DavMethod.java
public static void setResponseHeader(HttpServletResponse resp, String name, String value) { while (value != null) { String val = value; if (value.length() > 70) { int index = value.lastIndexOf(',', 70); if (index == -1) { ZimbraLog.dav.warn("header value is too long for %s : %s", name, value); return; }/* w ww . j av a 2 s. c o m*/ val = value.substring(0, index); value = value.substring(index + 1).trim(); } else { value = null; } resp.addHeader(name, val); } }
From source file:com.fjn.helper.common.io.file.common.FileUpAndDownloadUtil.java
public static void downloadToBrowser(String filename, String realpath, HttpServletRequest request, HttpServletResponse response) { // ?MIME TYPE? //try {//from w w w . j a v a 2s . c o m // mimetypesMap=new MimetypesFileTypeMap(request.getSession().getServletContext().getRealPath("WEB-INF/my.mime.types")); mimetypesMap = new MimetypesFileTypeMap(); //} catch (IOException e1) { // e1.printStackTrace(); //} // response.setCharacterEncoding("UTF-8"); response.setContentType(mimetypesMap.getContentType(filename)); response.addHeader("Content-Disposition", "attachment;filename=" + toUTF8(filename)); FileInputStream in = null; OutputStream out = null; try { /* ???? BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(new File(realpath)))); BufferedWriter out = new BufferedWriter(new OutputStreamWriter( response.getOutputStream())); int length = -1; char[] chs = new char[1024]; while ((length = in.read(chs)) != -1) { out.write(chs, 0, length); } */ in = new FileInputStream(new File(realpath)); out = response.getOutputStream(); int length = -1; byte[] bs = new byte[1024]; while ((length = in.read(bs)) != -1) { out.write(bs, 0, length); } out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (Exception ex) { } } }
From source file:com.ikon.util.WebUtils.java
/** * Prepare to send the file./* w w w. ja v a2s. c o m*/ */ public static void prepareSendFile(HttpServletRequest request, HttpServletResponse response, String fileName, String mimeType, boolean inline) throws UnsupportedEncodingException { String agent = request.getHeader("USER-AGENT"); // Disable browser cache response.setHeader("Expires", "Sat, 6 May 1971 12:00:00 GMT"); response.setHeader("Cache-Control", "must-revalidate"); response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate"); response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); // Set MIME type response.setContentType(mimeType); if (null != agent && -1 != agent.indexOf("MSIE")) { log.debug("Agent: Explorer ({})", request.getServerPort()); fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", " "); if (request.getServerPort() == 443) { log.debug("HTTPS detected! Apply IE workaround..."); response.setHeader("Cache-Control", "max-age=1"); response.setHeader("Pragma", "public"); } } else if (null != agent && -1 != agent.indexOf("Mozilla")) { log.debug("Agent: Mozilla"); fileName = MimeUtility.encodeText(fileName, "UTF-8", "B"); } else { log.debug("Agent: Unknown"); } if (inline) { response.setHeader("Content-disposition", "inline; filename=\"" + fileName + "\""); } else { response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\""); } }
From source file:com.openkm.util.WebUtils.java
/** * Prepare to send the file.// w w w. j a v a 2s .com */ public static void prepareSendFile(HttpServletRequest request, HttpServletResponse response, String fileName, String mimeType, boolean inline) throws UnsupportedEncodingException { String userAgent = WebUtils.getHeader(request, "user-agent").toLowerCase(); // Disable browser cache response.setHeader("Expires", "Sat, 6 May 1971 12:00:00 GMT"); response.setHeader("Cache-Control", "must-revalidate"); response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate"); response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); // Set MIME type response.setContentType(mimeType); if (userAgent.contains("msie") || userAgent.contains("trident")) { log.debug("Agent: Explorer ({})", request.getServerPort()); fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", " "); if (request.getServerPort() == 443) { log.debug("HTTPS detected! Apply IE workaround..."); response.setHeader("Cache-Control", "max-age=1"); response.setHeader("Pragma", "public"); } } else if (userAgent.contains("iphone") || userAgent.contains("ipad")) { log.debug("Agent: iPhone - iPad"); // Do nothing } else if (userAgent.contains("android")) { log.debug("Agent: Android"); fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", " "); } else if (userAgent.contains("mozilla")) { log.debug("Agent: Mozilla"); fileName = MimeUtility.encodeText(fileName, "UTF-8", "B"); } else { log.debug("Agent: Unknown"); } if (inline) { response.setHeader("Content-disposition", "inline; filename=\"" + fileName + "\""); } else { response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\""); } }
From source file:org.fcrepo.http.api.ContentExposingResource.java
private static void evaluateRequestPreconditions(final Request request, final HttpServletResponse servletResponse, final FedoraResource resource, final Session session, final boolean cacheControl) { final String txId = TransactionServiceImpl.getCurrentTransactionId(session); if (txId != null) { // Force cache revalidation if in a transaction servletResponse.addHeader(CACHE_CONTROL, "must-revalidate"); servletResponse.addHeader(CACHE_CONTROL, "max-age=0"); return;//w ww. j a v a 2s . c o m } final EntityTag etag = new EntityTag(resource.getEtagValue()); final Date date = resource.getLastModifiedDate(); final Date roundedDate = new Date(); if (date != null) { roundedDate.setTime(date.getTime() - date.getTime() % 1000); } Response.ResponseBuilder builder = request.evaluatePreconditions(etag); if (builder != null) { builder = builder.entity("ETag mismatch"); } else { builder = request.evaluatePreconditions(roundedDate); if (builder != null) { builder = builder.entity("Date mismatch"); } } if (builder != null && cacheControl) { final CacheControl cc = new CacheControl(); cc.setMaxAge(0); cc.setMustRevalidate(true); // here we are implicitly emitting a 304 // the exception is not an error, it's genuinely // an exceptional condition builder = builder.cacheControl(cc).lastModified(date).tag(etag); } if (builder != null) { throw new WebApplicationException(builder.build()); } }
From source file:com.anjz.util.CookieUtils.java
private static void setCookie(String key, String value, int maxAge, String path, String domainName, final boolean httpOnly, final boolean secure, HttpServletResponse response) { if (response != null) { Cookie cookie = new Cookie(key, value); cookie.setMaxAge(maxAge);/* w ww .ja v a2 s .co m*/ if (StringUtils.isNotBlank(path)) { cookie.setPath(path); } else { cookie.setPath(PATH); } if (StringUtils.isNotBlank(domainName)) { cookie.setDomain(domainName); } cookie.setVersion(0); cookie.setSecure(secure); if (httpOnly) { final StringBuffer buf = new StringBuffer(); getCookieHeaderValue(cookie, buf, httpOnly); response.addHeader(getCookieHeaderName(cookie), buf.toString()); } else { response.addCookie(cookie); } } }