Example usage for javax.servlet.http HttpServletResponseWrapper getWriter

List of usage examples for javax.servlet.http HttpServletResponseWrapper getWriter

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponseWrapper getWriter.

Prototype


public PrintWriter getWriter() throws IOException 

Source Link

Document

The default behavior of this method is to return getWriter() on the wrapped response object.

Usage

From source file:miage.ecom.web.security.ExtJsAuthenticationFailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);

    Writer out = responseWrapper.getWriter();

    out.write("{success:false, errors: { reason: 'Login failed. Try again.' }}");
    out.close();/*from   w  w  w.jav  a2s  . c  o m*/

}

From source file:miage.ecom.web.security.ExtJsAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);

    Writer out = responseWrapper.getWriter();

    out.write("{success:true}");
    out.close();//from  w  w  w  .j a v a 2 s.c o  m

}

From source file:com.marklogic.samplestack.web.security.SamplestackAccessDeniedHandler.java

@Override
/**/*from  www  . j  a v  a  2  s.c  o m*/
 * Handler override to return 403s on the HttpResponse.
 */
public void handle(HttpServletRequest request, HttpServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException {
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    responseWrapper.setStatus(HttpStatus.SC_FORBIDDEN);

    Writer out = responseWrapper.getWriter();
    errors.writeJsonResponse(out, HttpStatus.SC_FORBIDDEN, "Forbidden");
    out.close();
}

From source file:de.terrestris.shogun.security.ShogunAuthProcessingFilter.java

/**
 * React on unsuccessful authentication.
 * We again intercept the response and return a JSON object with a flag indicating unsuccessful login.
 *
 * @see WebContent/client/login.js/* www.  j ava2 s . co m*/
 */
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException failed) throws IOException, ServletException {
    super.unsuccessfulAuthentication(request, response, failed);
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    Writer out = responseWrapper.getWriter();
    out.write("{success:false}");
    out.close();
}

From source file:com.marklogic.samplestack.web.security.SamplestackAuthenticationSuccessHandler.java

@Override
/**/*from  www .j av a  2s .c  o m*/
 * Override handler that sends 200 OK to client along with JSON
 * for the logged-in user.
 */
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    String targetUrlParam = getTargetUrlParameter();
    if (savedRequest != null && isAlwaysUseDefaultTargetUrl()
            || (targetUrlParam != null && StringUtils.hasText(request.getParameter(targetUrlParam)))) {
        requestCache.removeRequest(request, response);
    }

    clearAuthenticationAttributes(request);
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    Writer writer = responseWrapper.getWriter();
    String userName = ClientRole.securityContextUserName();

    ObjectNode userNode;

    Contributor contributor = contributorService.getByUserName(userName);
    if (contributor != null) {
        userNode = mapper.convertValue(contributor, ObjectNode.class);
    } else {
        userNode = mapper.createObjectNode();
        userNode.put("userName", userName);
    }
    ArrayNode roleNode = userNode.putArray("role");
    roleNode.add(ClientRole.securityContextRole().toString());

    mapper.writeValue(writer, userNode);
    writer.close();
}

From source file:com.marklogic.samplestack.web.security.SamplestackAuthenticationEntryPoint.java

@Override
/**// w w  w .  ja v  a2  s .  c om
 * Override handler that returns 401 for any unauthenticated
 * request to a secured endpoint.
 */
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException {
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    responseWrapper.setStatus(HttpStatus.SC_UNAUTHORIZED);

    Writer out = responseWrapper.getWriter();
    errors.writeJsonResponse(out, HttpStatus.SC_UNAUTHORIZED, "Unauthorized");
    out.close();
}

From source file:com.marklogic.samplestack.web.security.SamplestackAuthenticationFailureHandler.java

@Override
/**//from   w  ww . j  a  v  a  2s  .  c  o m
 * Override handler that returns 401 after failed authentication.
 */
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    responseWrapper.setStatus(HttpStatus.SC_UNAUTHORIZED);

    Writer out = responseWrapper.getWriter();
    errors.writeJsonResponse(out, HttpStatus.SC_UNAUTHORIZED, "Unauthorized");
    out.close();
}

From source file:de.terrestris.shogun.security.ShogunAuthProcessingFilter.java

/**
 * On successful authentication by an Authentication Manager of Spring Security
 * we intercept with this method  and change the respone to include the ROLES of
 * the logged in user.//w  ww  . j a  v  a  2s . c o  m
 * This way we can react on the ROLES and redirect accordingly within the requesting login form (here login.js)
 *
 * @see WebContent/client/login.js
 */
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
        Authentication authResult) throws IOException, ServletException {
    SecurityContextHolder.getContext().setAuthentication(authResult);

    SavedRequestAwareAuthenticationSuccessHandler srh = new SavedRequestAwareAuthenticationSuccessHandler();
    this.setAuthenticationSuccessHandler(srh);
    srh.setRedirectStrategy(new RedirectStrategy() {
        @Override
        public void sendRedirect(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                String s) throws IOException {
            //do nothing, no redirect
        }
    });
    super.successfulAuthentication(request, response, authResult);

    // build a comma separated string of the ROLES
    String authorityText = StringUtils.join(authResult.getAuthorities(), ",");

    // write the servlet return object
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    Writer out = responseWrapper.getWriter();
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(out);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeBooleanField("success", true);
    jsonGenerator.writeStringField("name", authResult.getName());
    jsonGenerator.writeStringField("role", authorityText);
    jsonGenerator.writeEndObject();
    jsonGenerator.close();
}