Example usage for javax.servlet.http HttpServletResponse getContentType

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

Introduction

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

Prototype

public String getContentType();

Source Link

Document

Returns the content type used for the MIME body sent in this response.

Usage

From source file:edu.usc.lunchnlearn.springmvc.interceptor.BreadCrumbInterceptor.java

/**
 * This method is called after every controller has finished processing a request.  This sets up
 * the breadcrumbs that will be displayed on the resulting view page.
 * @param request           {@link javax.servlet.http.HttpServletRequest}
 * @param response          {@link javax.servlet.http.HttpServletResponse}
 * @param handler           Handler object
 * @param modelAndView      {@link org.springframework.web.servlet.ModelAndView}
 * @throws Exception        Throws Exception.
 *///from www .j  a  va 2  s .  c o  m
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    super.postHandle(request, response, handler, modelAndView);

    //do not intercept REST calls
    if (!"application/json".equals(response.getContentType()) && !isExclusion(request)) {
        HttpSession httpSession = request.getSession(true);
        boolean firstTime = false;
        if (httpSession.getAttribute(BREADCRUMBS) == null) {
            httpSession.setAttribute(BREADCRUMBS, new HashMap<Long, Crumb>());
            firstTime = true;
        }
        if (httpSession.getAttribute(BREADCRUMBS_URLS) == null) {
            httpSession.setAttribute(BREADCRUMBS_URLS, new HashMap<String, Long>());
        }

        @SuppressWarnings("unchecked")
        Map<Long, Crumb> breadcrumbs = (Map<Long, Crumb>) httpSession.getAttribute(BREADCRUMBS);
        @SuppressWarnings("unchecked")
        Map<String, Long> breadcrumbUrls = (Map<String, Long>) httpSession.getAttribute(BREADCRUMBS_URLS);

        Crumb crumb = getCurrentCrumb(request, breadcrumbUrls, breadcrumbs, modelAndView);

        //if first page in session is not the index, plant an index crumb
        if (firstTime && !request.getRequestURL().toString().contains("index")) {
            Crumb indexCrumb = createIndexCrumb(crumb, request);
            breadcrumbs.put(indexCrumb.getX(), indexCrumb);
            breadcrumbUrls.put(indexCrumb.getUrl(), indexCrumb.getX());

            crumb.setParentX(indexCrumb.getX());
        }

        //put this URL into the list so that we don't recreate this page's breadcrumb
        breadcrumbs.put(crumb.getX(), crumb);
        breadcrumbUrls.put(crumb.getUrl(), crumb.getX());

        modelAndView.getModelMap().put("x", crumb.getX());
        modelAndView.getModelMap().put("bCrumbs", breadcrumbs);
    }
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testValidTemplateName() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    MockServletContext sc = new MockServletContext();

    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    Map configs = new HashMap();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());
    configs.put("configurer", configurer);
    wmc.setReturnValue(configs);//from   w  w w .  j  av a2 s.c o m
    wac.getParentBeanFactory();
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(sc, 5);
    wmc.replay();

    fv.setUrl("templateName");
    fv.setApplicationContext(wac);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();

    Map model = new HashMap();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);

    wmc.verify();
    assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}

From source file:edu.usc.lunchnlearn.elasticsearch.interceptor.BreadCrumbInterceptor.java

/**
 * This method is called after every controller has finished processing a request.  This sets up
 * the breadcrumbs that will be displayed on the resulting view page.
 * @param request           {@link javax.servlet.http.HttpServletRequest}
 * @param response          {@link javax.servlet.http.HttpServletResponse}
 * @param handler           Handler object
 * @param modelAndView      {@link org.springframework.web.servlet.ModelAndView}
 * @throws Exception        Throws Exception.
 */// w  w w. ja  v a  2 s  .c om
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    super.postHandle(request, response, handler, modelAndView);

    //do not intercept REST calls
    if (!"application/json".equals(response.getContentType()) && !isExclusion(request)) {
        HttpSession httpSession = request.getSession(true);
        boolean firstTime = false;
        if (httpSession.getAttribute(BREADCRUMBS) == null) {
            httpSession.setAttribute(BREADCRUMBS, new HashMap<Long, Crumb>());
            firstTime = true;
        }
        if (httpSession.getAttribute(BREADCRUMBS_URLS) == null) {
            httpSession.setAttribute(BREADCRUMBS_URLS, new HashMap<String, Long>());
        }

        @SuppressWarnings("unchecked")
        Map<Long, Crumb> breadcrumbs = (Map<Long, Crumb>) httpSession.getAttribute(BREADCRUMBS);
        @SuppressWarnings("unchecked")
        Map<String, Long> breadcrumbUrls = (Map<String, Long>) httpSession.getAttribute(BREADCRUMBS_URLS);

        Crumb crumb = getCurrentCrumb(request, breadcrumbUrls, breadcrumbs, modelAndView);

        //if first page in session is not the index, plant an index crumb
        if (firstTime && !request.getRequestURL().toString().contains("index")) {
            Crumb indexCrumb = createIndexCrumb(crumb, request);
            breadcrumbs.put(indexCrumb.getX(), indexCrumb);
            breadcrumbUrls.put(indexCrumb.getUrl(), indexCrumb.getX());

            crumb.setParentX(indexCrumb.getX());
        }

        //put this URL into the list so that we don't recreate this page's breadcrumb
        breadcrumbs.put(crumb.getX(), crumb);
        breadcrumbUrls.put(crumb.getUrl(), crumb.getX());

        if (modelAndView.getModelMap().containsKey("urlParams")) {
            Map urlParams = (Map) modelAndView.getModelMap().get("urlParams");
            urlParams.put("x", crumb.getX());
        }

        modelAndView.getModelMap().put("x", crumb.getX());
        modelAndView.getModelMap().put("bCrumbs", breadcrumbs);

    }
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testKeepExistingContentType() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    MockServletContext sc = new MockServletContext();

    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    Map configs = new HashMap();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());
    configs.put("configurer", configurer);
    wmc.setReturnValue(configs);//from  w w w .  ja  va2s  .co m
    wac.getParentBeanFactory();
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(sc, 5);
    wmc.replay();

    fv.setUrl("templateName");
    fv.setApplicationContext(wac);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("myContentType");

    Map model = new HashMap();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);

    wmc.verify();
    assertEquals("myContentType", response.getContentType());
}

From source file:com.manydesigns.portofino.stripes.AuthenticationRequiredResolution.java

public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
    if (request.getParameter("__portofino_quiet_auth_failure") != null) {
        return;/*  w w w  .j  a  v a2 s. com*/
    }
    ServletContext servletContext = request.getServletContext();
    Configuration configuration = (Configuration) servletContext
            .getAttribute(BaseModule.PORTOFINO_CONFIGURATION);
    String loginPage = configuration.getString(PortofinoProperties.LOGIN_PAGE);
    if (response.getContentType() == null || response.getContentType().contains("text/html")) {
        ElementsActionBeanContext context = new ElementsActionBeanContext();
        context.setRequest(request);
        String originalPath = context.getActionPath();
        UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), originalPath, false);
        Map<?, ?> parameters = request.getParameterMap();
        urlBuilder.addParameters(parameters);
        String returnUrl = urlBuilder.toString();
        logger.info("Anonymous user not allowed to see {}. Redirecting to login.", originalPath);
        RedirectResolution redirectResolution = new RedirectResolution(loginPage, true);
        redirectResolution.addParameter("returnUrl", returnUrl);
        redirectResolution.execute(request, response);
    } else {
        logger.debug("AJAX call while user disconnected");
        UrlBuilder loginUrlBuilder = new UrlBuilder(request.getLocale(), loginPage, false);
        response.setHeader(LOGIN_PAGE_HEADER, loginUrlBuilder.toString());
        new ErrorResolution(STATUS, errorMessage).execute(request, response);
    }
}

From source file:info.magnolia.voting.voters.ResponseContentTypeVoter.java

@Override
protected boolean boolVote(Object value) {
    final HttpServletResponse response;
    if (value instanceof HttpServletResponse) {
        response = (HttpServletResponse) value;
    } else {/* w  ww . jav a2s .  com*/
        if (MgnlContext.isWebContext()) {
            response = MgnlContext.getWebContext().getResponse();
        } else {
            return false;
        }
    }

    // strip the encoding off of the content type:
    final String contentType = StringUtils.substringBefore(response.getContentType(), ";");
    if (contentType == null) {
        log.warn("No content type set on the response, can't vote.");
        return false;
    }
    if (allowed.size() > 0 && !allowed.contains(contentType)) {
        return false;
    }
    if (rejected.size() > 0 && rejected.contains(contentType)) {
        return false;
    }
    return true;
}

From source file:net.hedtech.banner.filters.ZKPageFilter2.java

private void detectContentTypeFromPage(Content page, HttpServletResponse response) {
    String contentType = page.getProperty("meta.http-equiv.Content-Type");
    if (contentType != null && "text/html".equals(response.getContentType())) {
        response.setContentType(contentType);
    }/* w w w.j  a  v a2s. co m*/
}

From source file:com.adobe.acs.commons.quickly.impl.QuicklyFilter.java

@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
        final FilterChain filterChain) throws IOException, ServletException {

    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;

    if (!accepts(request)) {
        filterChain.doFilter(servletRequest, servletResponse);
        return;//  ww w.  j a  va2 s  .  com
    }

    final BufferingResponse capturedResponse = new BufferingResponse(response);

    filterChain.doFilter(request, capturedResponse);

    // Get contents
    final String contents = capturedResponse.getContents();

    if (contents != null && StringUtils.contains(response.getContentType(), "html")) {

        final int bodyIndex = contents.indexOf("</body>");
        if (bodyIndex != -1) {

            final PrintWriter printWriter = response.getWriter();

            printWriter.write(contents.substring(0, bodyIndex));
            printWriter.write(appHTML);
            printWriter.write(contents.substring(bodyIndex));

            return;
        }
    }

    if (contents != null) {
        response.getWriter().write(contents);
    }
}

From source file:com.laxser.blitz.web.instruction.TextInstruction.java

@Override
public void doRender(Invocation inv) throws Exception {
    if (StringUtils.isEmpty(text)) {
        return;/*  w  ww .j  a  v  a2s.  c  om*/
    }

    if (logger.isDebugEnabled()) {
        logger.debug("trying to render text:" + text);
    }

    HttpServletResponse response = inv.getResponse();
    String oldEncoding = response.getCharacterEncoding();
    if (StringUtils.isBlank(oldEncoding) || oldEncoding.startsWith("ISO-")) {
        String encoding = inv.getRequest().getCharacterEncoding();
        Assert.isTrue(encoding != null);
        response.setCharacterEncoding(encoding);
        if (logger.isDebugEnabled()) {
            logger.debug("set response.characterEncoding by default:" + response.getCharacterEncoding());
        }
    }

    if (response.getContentType() == null) {
        response.setContentType("text/html");
        if (logger.isDebugEnabled()) {
            logger.debug("set response content-type by default: " + response.getContentType());
        }
    }
    sendResponse(response, text);
}