Example usage for javax.servlet.http HttpServletResponse addHeader

List of usage examples for javax.servlet.http HttpServletResponse addHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addHeader.

Prototype

public void addHeader(String name, String value);

Source Link

Document

Adds a response header with the given name and value.

Usage

From source file:com.seleniumtests.it.driver.support.server.PageServlet.java

/**
  * Allow downloading of files in upload folder
  *///  w w  w  .ja v  a 2  s.  c o  m
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        if (this.resourceFile.endsWith(".css")) {
            resp.addHeader("Content-Type", "text/css ");
        }
        IOUtils.copy(getClass().getResourceAsStream(this.resourceFile), resp.getOutputStream());
    } catch (IOException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Error while handling request: " + e.getMessage());
    }
}

From source file:com.ocpsoft.socialpm.faces.CacheControlPhaseListener.java

public void beforePhase(final PhaseEvent event) {
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
            .getResponse();//from w w  w  . j av a2s  . c om
    response.addHeader("Pragma", "no-cache");
    response.addHeader("Cache-Control", "no-cache");
    response.addHeader("Cache-Control", "must-revalidate");
    response.addHeader("Expires", "Mon, 1 Aug 1999 10:00:00 GMT");
}

From source file:com.thoughtworks.go.server.security.BasicAuthenticationFilter.java

private void generateResponse(HttpServletResponse httpResponse, String type, String msg) throws IOException {
    httpResponse.addHeader("WWW-Authenticate", "Basic realm=\"GoCD\"");
    httpResponse.setContentType(type);/*w  w  w . ja va  2s .  c o  m*/
    httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    httpResponse.getOutputStream().print(msg);
}

From source file:com.gsr.myschool.server.reporting.convocation.ConvocationController.java

@RequestMapping(method = RequestMethod.GET, produces = "application/pdf")
@ResponseStatus(HttpStatus.OK)/*w w w .  ja  v  a 2s. co m*/
public void generateConvocation(@RequestParam String number, HttpServletResponse response) {
    ReportDTO dto = convocationService.generateConvocation(number);
    try {
        response.addHeader("Content-Disposition", "attachment; filename=" + dto.getFileName());

        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        byte[] result = reportService.generatePdf(dto);

        outputStream.write(result, 0, result.length);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.cloudfoundry.identity.uaa.error.JsonAwareAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    response.addHeader("WWW-Authenticate", String.format("%s realm=\"%s\"", typeName, realmName));
    String accept = request.getHeader("Accept");
    boolean json = false;
    if (StringUtils.hasText(accept)) {
        for (MediaType mediaType : MediaType.parseMediaTypes(accept)) {
            if (mediaType.includes(MediaType.APPLICATION_JSON)) {
                json = true;// ww  w .  j  a  v  a2s.com
                break;
            }
        }
    }
    if (json) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.getWriter().append(String.format("{\"error\":\"%s\"}", authException.getMessage()));
    } else {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
    }
}

From source file:org.fishwife.jrugged.spring.StatusController.java

private void setAppropriateWarningHeaders(HttpServletResponse resp, Status currentStatus) {
    if (Status.DEGRADED.equals(currentStatus)) {
        resp.addHeader("Warning", "199 jrugged \"Status degraded\"");
    }//from ww w  .j  av  a  2  s  . c  o  m
}

From source file:gateway.auth.PiazzaBasicAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
        throws IOException, ServletException {
    response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.setContentType("application/json");
    PrintWriter writer = response.getWriter();
    // Create a Response Object
    ErrorResponse error = new ErrorResponse("Gateway is unable to authenticate the provided user.", "Gateway");

    try {/*from   w ww.  j ava2  s.  co m*/
        // Log the request
        logger.log(
                String.format("Unable to authenticate a user with Auth Type %s and Header %s",
                        request.getAuthType(), request.getHeader("Authorization").toString()),
                PiazzaLogger.ERROR);
    } catch (Exception exception) {
        String errorString = String.format("Exception encountered during Authorization check: %s.",
                exception.getMessage());
        LOGGER.error(errorString, exception);
        logger.log(errorString, PiazzaLogger.ERROR);
    }

    // Write back the response
    writer.println(new ObjectMapper().writeValueAsString(error));
}

From source file:org.apache.archiva.redback.integration.filter.authentication.basic.HttpBasicAuthentication.java

/**
 * Return a HTTP 403 - Access Denied response.
 *
 * @param request   the request to use./*from w  w w.ja va2 s .  c o  m*/
 * @param response  the response to use.
 * @param realmName the realm name to state.
 * @param exception the exception to base the message off of.
 * @throws IOException if there was a problem with the {@link HttpServletResponse#sendError(int,String)} call.
 */
public void challenge(HttpServletRequest request, HttpServletResponse response, String realmName,
        AuthenticationException exception) throws IOException {
    response.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\"");
    String message = "You must provide a username and password to access this resource.";
    if ((exception != null) && StringUtils.isNotEmpty(exception.getMessage())) {
        message = exception.getMessage();
    }
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, message);
}

From source file:org.simbasecurity.core.service.http.LoginController.java

private void makeSimbaSSOCookieForCORS(HttpServletResponse response, ActionDescriptor actionDescriptor) {
    // Path cannot be /simba/manager/ browsers won't accept on redirect if not /
    response.addHeader("Set-Cookie", RequestConstants.SIMBA_SSO_TOKEN + "="
            + actionDescriptor.getSsoToken().getToken() + "; HttpOnly; Path=/");
}

From source file:gwtupload.server.UploadServlet.java

/**
 * Writes a response to the client.// w w  w . ja v a 2s .com
 */
protected static void renderMessage(HttpServletResponse response, String message, String contentType)
        throws IOException {
    response.addHeader("Cache-Control", "no-cache");
    response.setContentType(contentType + "; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    out.print(message);
    out.flush();
    out.close();
}