Java Code Examples for org.apache.sling.api.SlingHttpServletResponse#setHeader()

The following examples show how to use org.apache.sling.api.SlingHttpServletResponse#setHeader() . 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: CacheControlFilter.java    From sling-org-apache-sling-dynamic-include with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
        ServletException {
    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    final String resourceType = slingRequest.getResource().getResourceType();
    final Configuration config = configurationWhiteboard.getConfiguration(slingRequest, resourceType);

    if (config != null && config.hasTtlSet()) {
        SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
        slingResponse.setHeader(HEADER_CACHE_CONTROL, "max-age=" + config.getTtl());
        LOG.debug("set \"{}: max-age={}\" to {}", HEADER_CACHE_CONTROL, config.getTtl(), resourceType);
        if (!slingResponse.containsHeader(HEADER_DATE)) {
            slingResponse.setDateHeader(HEADER_DATE, System.currentTimeMillis());
        }
    }

    chain.doFilter(request, response);
}
 
Example 2
Source File: ScriptResultServlet.java    From APM with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws IOException {

  String fileName = request.getParameter("filename");
  String content = request.getParameter("content");

  if (fileName == null || fileName.length() == 0) {
    LOGGER.error("Parameter fileName is required");
    return;
  }

  response.setContentType("application/octet-stream");
  response.setHeader("Content-Disposition",
      "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

  InputStream input = IOUtils.toInputStream(content);
  IOUtils.copy(input, response.getOutputStream());
}
 
Example 3
Source File: HealthCheckExecutorServlet.java    From aem-healthcheck with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
                                                    throws ServletException, IOException {
    response.setContentType("application/json");
    response.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");

    // parse query parameters
    String tagsStr = StringUtils.defaultString(request.getParameter(PARAM_TAGS));
    String[] tags = tagsStr.split("[, ;]+");
    String combineTagsOr = StringUtils.defaultString(request.getParameter(PARAM_COMBINE_TAGS_OR), "true");

    // execute health checks
    HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
    options.setCombineTagsWithOr(Boolean.valueOf(combineTagsOr));
    options.setForceInstantExecution(true);
    List<HealthCheckExecutionResult> results = healthCheckExecutor.execute(options, tags);

    // check results
    boolean allOk = true;
    for(HealthCheckExecutionResult result : results) {
        if(!result.getHealthCheckResult().isOk()) {
            allOk = false;
            break;
        }
    }

    // set appropriate status code
    if(!allOk) {
        response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    } else {
        response.setStatus(HttpServletResponse.SC_OK);
    }

    // write out JSON response
    JSONObject resultJson = new JSONObject();
    try {
        generateResponse(results, resultJson);
    } catch(JSONException ex) {
        logger.error("Could not serialize result into JSON", ex);
    }
    response.getWriter().write(resultJson.toString());
}
 
Example 4
Source File: ScriptDownloadServlet.java    From APM with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
		throws ServletException, IOException {
	String fileName = request.getParameter("filename");
	String filePath = request.getParameter("filepath");

	String mode = request.getParameter("mode");

	try {
		final ResourceResolver resourceResolver = request.getResourceResolver();
		final Session session = resourceResolver.adaptTo(Session.class);

		if (!("view").equals(mode)) {
			response.setContentType("application/octet-stream"); // Your content type
			response.setHeader("Content-Disposition",
					"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
		}

		String path = StringUtils.replace(filePath, "_jcr_content", "jcr:content");

		Node jcrContent = session.getNode(path + "/jcr:content");

		InputStream input = jcrContent.getProperty("jcr:data").getBinary().getStream();

		session.save();
		int read;
		byte[] bytes = new byte[BYTES_DOWNLOAD];
		OutputStream os = response.getOutputStream();

		while ((read = input.read(bytes)) != -1) {
			os.write(bytes, 0, read);
		}
		input.close();
		os.flush();
		os.close();

	} catch (RepositoryException e) {
		LOGGER.error(e.getMessage(), e);
		response.sendRedirect("/etc/cqsm.html");
		// response.sendError(500);
	}
}