List of usage examples for javax.servlet.http HttpServletResponse setDateHeader
public void setDateHeader(String name, long date);
From source file:csiro.pidsvc.servlet.info.java
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response)//from w ww . j a v a 2 s. c o m */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setDateHeader("Expires", 0); response.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0"); String cmd = request.getParameter("cmd"); if (cmd == null || cmd.isEmpty()) return; ManagerJson mgr = null; JSONObject ret; try { Settings.init(this); mgr = new ManagerJson(request); response.setContentType("application/json"); _logger.info("Processing \"{}\" command -> {}?{}", cmd, request.getRequestURL(), request.getQueryString()); if (cmd.equalsIgnoreCase("search")) { int page = 1; String sPage = request.getParameter("page"); if (sPage != null && sPage.matches("\\d+")) page = Integer.parseInt(sPage); ret = mgr.getMappings(page, request.getParameter("mapping"), request.getParameter("type"), request.getParameter("creator"), Literals.toInt(request.getParameter("deprecated"), 0)); response.getWriter().write(ret == null ? null : ret.toString()); } else if (cmd.equalsIgnoreCase("get_pid_config")) { int mappingId = Literals.toInt(request.getParameter("mapping_id"), -1); String mappingPath = request.getParameter("mapping_path"); ret = mappingId > 0 ? mgr.getPidConfig(mappingId) : (mappingId == 0 ? mgr.getPidConfig((String) null) : mgr.getPidConfig(mappingPath)); response.getWriter().write(ret == null ? null : ret.toString()); } else if (cmd.equalsIgnoreCase("check_mapping_path_exists")) { response.getWriter() .write(mgr.checkMappingPathExists(request.getParameter("mapping_path")).toString()); } else if (cmd.equalsIgnoreCase("search_parent")) { int mappingId = Literals.toInt(request.getParameter("mapping_id"), -1); response.getWriter() .write(mgr.searchParentMapping(mappingId, request.getParameter("q")).toString()); } else if (cmd.equalsIgnoreCase("get_settings")) response.getWriter().write(mgr.getSettings().toString()); else if (cmd.equalsIgnoreCase("search_condition_set")) { int page = 1; String sPage = request.getParameter("page"); if (sPage != null && sPage.matches("\\d+")) page = Integer.parseInt(sPage); ret = mgr.getConditionSets(page, request.getParameter("q")); response.getWriter().write(ret == null ? null : ret.toString()); } else if (cmd.equalsIgnoreCase("get_condition_set_config")) { String name = request.getParameter("name"); ret = mgr.getConditionSetConfig(name); response.getWriter().write(ret == null ? null : ret.toString()); } else if (cmd.equalsIgnoreCase("search_lookup")) { int page = 1; String sPage = request.getParameter("page"); if (sPage != null && sPage.matches("\\d+")) page = Integer.parseInt(sPage); ret = mgr.getLookups(page, request.getParameter("ns")); response.getWriter().write(ret == null ? null : ret.toString()); } else if (cmd.equalsIgnoreCase("get_lookup_config")) { String ns = request.getParameter("ns"); ret = mgr.getLookupConfig(ns); response.getWriter().write(ret == null ? null : ret.toString()); } else if (cmd.equalsIgnoreCase("get_manifest")) { response.getWriter().write(Settings.getInstance().getManifestJson().toString()); } else if (cmd.equalsIgnoreCase("is_new_version_available")) { response.getWriter().write(Settings.getInstance().isNewVersionAvailableJson().toString()); } else if (cmd.equalsIgnoreCase("global_js")) { response.setContentType("text/javascript"); response.getWriter() .write("var GlobalSettings = " + mgr.getGlobalSettings(request).toString() + ";"); } else if (cmd.equalsIgnoreCase("chart")) { ret = mgr.getChart(); response.getWriter().write(ret == null ? null : ret.toString()); } else if (cmd.equalsIgnoreCase("get_mapping_dependencies")) { int mappingId = Literals.toInt(request.getParameter("mapping_id"), -1); String mappingPath = request.getParameter("mapping_path"); String inputJson = request.getParameter("json"); JSONObject jsonThis = (JSONObject) (new JSONParser()).parse(inputJson); if (mappingPath != null && mappingPath.isEmpty()) mappingPath = null; if (jsonThis != null && jsonThis.isEmpty()) jsonThis = null; ret = mgr.getMappingDependencies((Object) (jsonThis == null ? mappingId : jsonThis), mappingPath); response.getWriter() .write(mappingId == -1 && jsonThis == null || ret == null ? "{}" : ret.toString()); } else if (cmd.equalsIgnoreCase("echo")) { echo(request, response); } } catch (Exception e) { _logger.error(e); Http.returnErrorCode(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); } finally { if (mgr != null) mgr.close(); } }
From source file:cn.zhuqi.mavenssh.web.servlet.ShowPic.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w w w. j a v a 2 s . c om*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Pragma", "No-cache");// ????? response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expire", 0); // ?Application ServletContext application = this.getServletContext(); String sid = request.getParameter("id"); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application); // ?spring IOC userService = (IUserService) ctx.getBean(IUserService.class); User user = userService.findById(Integer.valueOf(sid)); String imgFile = user.getPicurl(); response.setContentType(application.getMimeType(imgFile)); FTPClientTemplate ftpClient = new FTPClientTemplate(ip, 21, username, pwd); ServletOutputStream sos = response.getOutputStream(); try { ftpClient.get(attachment + "/" + imgFile, sos); } catch (Exception ex) { } }
From source file:org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader.java
/** * Output http headers telling the browser (and possibly intermediate caches) how * to cache this data.//from w ww . ja va 2s .c om * <p> * The expiry time in this header info is set to 7 days. This is not a problem as * the overall URI contains a "cache key" that changes whenever the webapp is * redeployed (see AddResource.getCacheKey), meaning that all browsers will * effectively reload files on webapp redeploy. */ protected void defineCaching(HttpServletRequest request, HttpServletResponse response, String resource, long lastModified) { response.setDateHeader("Last-Modified", lastModified); Calendar expires = Calendar.getInstance(); expires.add(Calendar.DAY_OF_YEAR, 7); response.setDateHeader("Expires", expires.getTimeInMillis()); //12 hours: 43200 = 60s * 60 * 12 response.setHeader("Cache-Control", "max-age=43200"); response.setHeader("Pragma", ""); }
From source file:org.openmrs.module.usagestatistics.web.view.chart.AbstractChartView.java
/** * @see org.springframework.web.servlet.view.AbstractView *//*from w w w.j a v a 2 s. co m*/ @Override @SuppressWarnings("unchecked") protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { // Respond as a PNG image response.setContentType("image/png"); // Disable caching response.setHeader("Pragma", "No-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache"); int width = ServletRequestUtils.getIntParameter(request, "width", 500); int height = ServletRequestUtils.getIntParameter(request, "height", 300); from = (Date) model.get("from"); until = (Date) model.get("until"); untilInclusive = (Date) model.get("untilInclusive"); usageFilter = (ActionCriteria) model.get("usageFilter"); location = (Location) model.get("location"); JFreeChart chart = createChart(model, request); chart.setBackgroundPaint(Color.WHITE); chart.getPlot().setOutlineStroke(new BasicStroke(0)); chart.getPlot().setOutlinePaint(getBackgroundColor()); chart.getPlot().setBackgroundPaint(getBackgroundColor()); ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, width, height); }
From source file:org.openmrs.module.usagestatistics668.web.view.chart.AbstractChartView.java
/** * @see org.springframework.web.servlet.view.AbstractView *//*from w w w. j a v a 2 s . c om*/ @Override @SuppressWarnings("unchecked") protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { // Respond as a PNG image response.setContentType("image/png"); // Disable caching response.setHeader("Pragma", "No-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache"); int width = ServletRequestUtils.getIntParameter(request, "width", 500); int height = ServletRequestUtils.getIntParameter(request, "height", 300); from = (Date) model.get("from"); System.out.println(from); maxResults = (Integer) model.get("maxResults"); until = (Date) model.get("until"); untilInclusive = (Date) model.get("untilInclusive"); usageFilter = (ActionCriteria) model.get("usageFilter"); JFreeChart chart = createChart(model, request); chart.setBackgroundPaint(Color.WHITE); chart.getPlot().setOutlineStroke(new BasicStroke(0)); chart.getPlot().setOutlinePaint(getBackgroundColor()); chart.getPlot().setBackgroundPaint(getBackgroundColor()); ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, width, height); }
From source file:com.octo.captcha.module.web.image.ImageToJpegHelper.java
/** * retrieve a new ImageCaptcha using ImageCaptchaService and flush it to the response.<br/> Captcha are localized * using request locale.<br/>/*from w ww .j a v a2 s.c om*/ * <p/> * This method returns a 404 to the client instead of the image if the request isn't correct (missing parameters, * etc...)..<br/> The log may be null.<br/> * * @param theRequest the request * @param theResponse the response * @param log a commons logger * @param service an ImageCaptchaService instance * * @throws java.io.IOException if a problem occurs during the jpeg generation process */ public static void flushNewCaptchaToResponse(HttpServletRequest theRequest, HttpServletResponse theResponse, Log log, ImageCaptchaService service, String id, Locale locale) throws IOException { // call the ImageCaptchaService method to retrieve a captcha byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { BufferedImage challenge = service.getImageChallengeForID(id, locale); // the output stream to render the captcha image as jpeg into ImageIO.write(challenge, "jpg", jpegOutputStream); } catch (IllegalArgumentException e) { // log a security warning and return a 404... if (log != null && log.isWarnEnabled()) { log.warn("There was a try from " + theRequest.getRemoteAddr() + " to render an captcha with invalid ID :'" + id + "' or with a too long one"); theResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); // render the captcha challenge as a JPEG image in the response theResponse.setHeader("Cache-Control", "no-store"); theResponse.setHeader("Pragma", "no-cache"); theResponse.setDateHeader("Expires", 0); theResponse.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = theResponse.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush(); responseOutputStream.close(); }
From source file:org.nuxeo.ecm.webengine.servlet.ResourceServlet.java
protected void service(HttpServletRequest req, HttpServletResponse resp, Module module, String path) throws IOException { ScriptFile file = module.getSkinResource(path); if (file != null) { long lastModified = file.lastModified(); resp.setDateHeader("Last-Modified:", lastModified); resp.addHeader("Cache-Control", "public"); resp.addHeader("Server", "Nuxeo/WebEngine-1.0"); String mimeType = engine.getMimeType(file.getExtension()); if (mimeType == null) { mimeType = "text/plain"; }// w w w . j a v a 2s . c o m resp.setContentType(mimeType); if (mimeType.startsWith("text/")) { sendTextContent(file, resp); } else { sendBinaryContent(file, resp); } return; } resp.sendError(404); }
From source file:com.google.code.jcaptcha4struts2.core.actions.support.CaptchaImageResult.java
/** * Action Execution Result. This will write the image bytes to response stream. * /* w w w . j a v a2 s. c om*/ * @param invocation * ActionInvocation * @throws IOException * if an IOException occurs while writing the image to output stream. * @throws IllegalArgumentException * if the action invocation was done by an action which is not the * {@link JCaptchaImageAction}. */ public void execute(ActionInvocation invocation) throws IOException, IllegalArgumentException { // Check if the invoked action was JCaptchaImageAction if (!(invocation.getAction() instanceof JCaptchaImageAction)) { throw new IllegalArgumentException( "CaptchaImageResult expects JCaptchaImageAction as Action Invocation"); } JCaptchaImageAction action = (JCaptchaImageAction) invocation.getAction(); HttpServletResponse response = ServletActionContext.getResponse(); // Read captcha image bytes byte[] image = action.getCaptchaImage(); // Send response response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); response.setContentLength(image.length); try { response.getOutputStream().write(image); response.getOutputStream().flush(); } catch (IOException e) { LOG.error("IOException while writing image response for action : " + e.getMessage(), e); throw e; } }
From source file:csiro.pidsvc.servlet.controller.java
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response)/*from w w w . j ava 2 s . c o m*/ */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setDateHeader("Expires", 0); response.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0"); String cmd = request.getParameter("cmd"); if (cmd == null || cmd.isEmpty()) return; ManagerJson mgr = null; try { mgr = new ManagerJson(); _logger.info("Processing \"{}\" command -> {}?{}.", cmd, request.getRequestURL(), request.getQueryString()); if (cmd.matches("(?i)^(?:full|partial)_export$")) { int mappingId = Literals.toInt(request.getParameter("mapping_id"), -1); String mappingPath = request.getParameter("mapping_path"); String outputFormat = request.getParameter("format"); String serializedConfig = mappingId > 0 ? mgr.exportMapping(mappingId) : (mappingId == 0 ? mgr.exportCatchAllMapping(cmd.startsWith("full")) : mgr.exportMapping(mappingPath, cmd.startsWith("full"))); // Check output format. outputFormat = outputFormat != null && outputFormat.matches("(?i)^xml$") ? "xml" : "psb"; returnAttachment(response, "mapping." + (cmd.startsWith("full") ? "full" : "partial") + "." + _sdfBackupStamp.format(new Date()) + "." + outputFormat, outputFormat, serializedConfig); } else if (cmd.matches("(?i)^(?:full|partial)_backup$")) { String includeDeprecated = request.getParameter("deprecated"); String includeConditionSets = request.getParameter("conditionsets"); String includeLookupMaps = request.getParameter("lookup"); String outputFormat = request.getParameter("format"); String serializedConfig = mgr.backupDataStore(cmd.startsWith("full"), includeDeprecated != null && includeDeprecated.equalsIgnoreCase("true"), includeConditionSets == null || includeConditionSets.equalsIgnoreCase("true"), includeLookupMaps == null || includeLookupMaps.equalsIgnoreCase("true")); // Check output format. outputFormat = outputFormat != null && outputFormat.matches("(?i)^xml$") ? "xml" : "psb"; returnAttachment(response, "backup." + (cmd.startsWith("full") ? "full" : "partial") + "." + _sdfBackupStamp.format(new Date()) + "." + outputFormat, outputFormat, serializedConfig); } else if (cmd.matches("(?i)export_lookup$")) { String ns = request.getParameter("ns"); String outputFormat = request.getParameter("format"); String serializedConfig = mgr.exportLookup(ns); // Check output format. outputFormat = outputFormat != null && outputFormat.matches("(?i)^xml$") ? "xml" : "psl"; returnAttachment(response, "lookup." + (ns == null ? "backup." : "") + _sdfBackupStamp.format(new Date()) + "." + outputFormat, outputFormat, serializedConfig); } else if (cmd.matches("(?i)export_condition_set$")) { String name = request.getParameter("name"); String outputFormat = request.getParameter("format"); String serializedConfig = mgr.exportConditionSet(name); // Check output format. outputFormat = outputFormat != null && outputFormat.matches("(?i)^xml$") ? "xml" : "psb"; returnAttachment( response, "conditionSet." + (name == null ? "backup." : "") + _sdfBackupStamp.format(new Date()) + "." + outputFormat, outputFormat, serializedConfig); } } catch (Exception e) { _logger.error(e); Http.returnErrorCode(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); } finally { if (mgr != null) mgr.close(); } }
From source file:org.openmrs.module.usagestatistics.web.view.csv.AbstractCSVView.java
/** * @see org.springframework.web.servlet.view.AbstractView *///from w w w . j a v a 2 s . co m @Override @SuppressWarnings("unchecked") protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { // Respond as a CSV file response.setContentType("text/csv"); response.setHeader("Content-Disposition", "attachment; filename=\"" + getFilename(model) + "\""); // Disable caching response.setHeader("Pragma", "No-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache"); // Write data writeValues(model, response.getWriter()); }