Example usage for javax.servlet.http HttpSession getServletContext

List of usage examples for javax.servlet.http HttpSession getServletContext

Introduction

In this page you can find the example usage for javax.servlet.http HttpSession getServletContext.

Prototype

public ServletContext getServletContext();

Source Link

Document

Returns the ServletContext to which this session belongs.

Usage

From source file:com.doculibre.constellio.filters.LocalRequestFilter.java

private boolean isFileRequest(ServletRequest request) {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpSession httpSession = httpRequest.getSession();
    ServletContext servletContext = httpSession.getServletContext();
    String contextPath = httpRequest.getContextPath();
    String requestURI = httpRequest.getRequestURI();
    String contextRelativePath = requestURI.substring(contextPath.length());
    String possibleFilePath = servletContext.getRealPath(contextRelativePath);
    return new File(possibleFilePath).exists();
}

From source file:dto.Image.java

public String execute() {

    try {/*from ww  w  . j a va 2s .  co  m*/

        HttpSession srvltSession = this.servletRequest.getSession();
        ServletContext srvlContxt = srvltSession.getServletContext();// .getServletContext()
        String filePath = srvlContxt.getRealPath("/").concat("userimages");
        System.out.println("Image Location:" + filePath);
        File destFile = new File(filePath, this.imageUploadFileName);
        FileUtils.copyFile(this.imageUpload, destFile);

    } catch (Exception e) {
        System.err.println(e);
        return ERROR;
    }
    return SUCCESS;

}

From source file:com.nec.harvest.servlet.listener.HarvestSessionListener.java

@Override
public void sessionDestroyed(HttpSessionEvent event) {
    activeSessions--;/*  www .  j  a v  a  2  s . c  om*/

    // ?
    HttpSession session = event.getSession();
    Assert.notNull(session, "No HttpSession Specified");

    ServletContext ctx = session.getServletContext();
    ctx.removeAttribute(Constants.USER_LOGGED_IN_LASTTIME);
    ctx.removeAttribute(Constants.SESS_ORGANIZATION_CODE);
    ctx.removeAttribute(Constants.SESS_BUSINESS_DAY);

    if (SecurityContextHolder.getContext().getAuthentication() != null) {
        // Remove from LRU Cache
        AuthenticatedUserDetails.removeUserPrincipal();

        // Empty authentication
        SecurityContextHolder.getContext().setAuthentication(null);
    }

    // ??????
    logger.info("A HttpSession [{}] is going to be destroyed", session.getId());
}

From source file:org.molasdin.wbase.jsf.spring.web.SpringBeansRewireListener.java

@Override
public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
    HttpSession session = httpSessionEvent.getSession();
    WebApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(session.getServletContext());
    AutowireCapableBeanFactory bf = ctx.getAutowireCapableBeanFactory();
    for (String entry : IteratorUtils.asIterable(IteratorUtils.asIterator(session.getAttributeNames()))) {
        if (bf.containsBean(entry)) {
            Object bean = session.getAttribute(entry);
            bean = bf.configureBean(bean, entry);
            session.setAttribute(entry, bean);
        }//from  ww w  .j a va  2s .  com
    }

}

From source file:springapp.web.controller.theme.DownloadController.java

public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) throws Exception {

    String templateIdString = httpServletRequest.getParameter(ApplicationConstants.TEMPLATE_ID);

    byte templateId = null != templateIdString ? Byte.parseByte(templateIdString) : 0;
    String templateName = templateId == 0 ? "blue" : "gray";

    String shemaNameParam = httpServletRequest.getParameter(ApplicationConstants.NEW_SCHEMA_NAME);
    String newSchemaName = (null != shemaNameParam) && (!"".equals(shemaNameParam)) ? shemaNameParam
            : "default";

    String versionString = httpServletRequest.getParameter("version");
    String version = (null == versionString) ? "3.2" : versionString;

    HttpSession session = httpServletRequest.getSession();

    ServletContext servletContext = session.getServletContext();
    WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    //String resourcesPath = (String)context.getBean("resourcesPath");

    ResourcesHolder currentSchema = (ResourcesHolder) session
            .getAttribute(ApplicationConstants.CURRENT_SCHEMA_ATTRIBUTE_NAME/*+version*/);
    String resourcesPath = currentSchema.getResourcesPath();
    ResourcesLoader loader = loaderFactory.getResourcesLoader(currentSchema, context);

    httpServletResponse.setContentType("application/zip");

    httpServletResponse.setHeader("Pragma", "no-cache");
    httpServletResponse.setHeader("Cache-Control", "no-cache");
    httpServletResponse.setHeader("Expires", "-1");
    httpServletResponse.setHeader("Content-Disposition",
            "attachment; filename=\"xtheme-" + newSchemaName + ".zip\";");
    //httpServletResponse.setContentLength(zipOS.size());
    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
    ZipOutputStream zipOS = new ZipOutputStream(servletOutputStream);

    zipOS = loader.outForZip(currentSchema, resourcesPath, zipOS, context, newSchemaName, templateName,
            version);//from  w ww  .j  a  v a  2 s. c om

    zipOS.flush();
    zipOS.close();
    servletOutputStream.flush();
    servletOutputStream.close();

    logger.info("DownloadController processed! ");

    return null;
}

From source file:net.jforum.core.tags.ImportFileTag.java

/**
 * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
 *///from  w  ww. j av a  2s  .c om
@Override
public void doTag() throws JspException, IOException {
    // check the URL
    if (StringUtils.isEmpty(url))
        throw new NullAttributeException("import", "url");

    String jsp = this.getFile(url);

    ServletRequest request = this.request();
    ServletResponse respose = this.response();
    HttpSession session = ((HttpServletRequest) request).getSession();
    ServletContext servletContext = session.getServletContext();

    String jspPath = servletContext.getRealPath(jsp);
    File jspFile = new File(jspPath);
    if (!jspFile.exists())
        return;

    respose.flushBuffer();
    RequestDispatcher rd = this.pageContext().getRequest().getRequestDispatcher(jsp);
    try {
        // include the resource, using our custom wrapper
        ImportResponseWrapper irw = new ImportResponseWrapper((HttpServletResponse) respose);
        irw.setCharacterEncoding(charEncoding);
        rd.include(request, irw);
        // disallow inappropriate response codes per JSTL spec
        if (irw.getStatus() < 200 || irw.getStatus() > 299) {
            throw new JspTagException(irw.getStatus() + " " + jsp);
        }

        // recover the response String from our wrapper
        pageContext().getOut().print(irw.getString());
    } catch (ServletException e) {
        e.printStackTrace();
    }
}

From source file:org.callistasoftware.netcare.web.controller.SecurityController.java

@RequestMapping("/login")
public String login(@RequestParam(value = "guid", required = false) final String guid, final HttpSession sc,
        final Model m) {
    getLog().info("Display login page");

    if ((WebUtil.isProfileActive(sc.getServletContext(), "prod")
            || WebUtil.isProfileActive(sc.getServletContext(), "qa")) && guid != null) {
        m.addAttribute("guid", guid);
        return "redirect:/netcare/setup";
    }//  w w  w.ja  v a  2s. c om

    if (WebUtil.isProfileActive(sc.getServletContext(), "prod") && guid == null) {
        return "redirect:/netcare/security/denied";
    }

    if (WebUtil.isProfileActive(sc.getServletContext(), "qa")) {
        return "redirect:/netcare/mvk/token/forPatient";
    }

    if (WebUtil.isProfileActive(sc.getServletContext(), "test")) {
        return "login";
    }

    throw new RuntimeException("Could not determine application mode.");
}

From source file:org.hdiv.AbstractHDIVTestCase.java

protected final void setUp() throws Exception {

    String[] files = { "/org/hdiv/config/hdiv-core-applicationContext.xml", "/org/hdiv/config/hdiv-config.xml",
            "/org/hdiv/config/hdiv-validations.xml", "/org/hdiv/config/applicationContext-test.xml",
            "/org/hdiv/config/applicationContext-extra.xml" };

    if (this.applicationContext == null) {
        this.applicationContext = new ClassPathXmlApplicationContext(files);
    }/*from w w w .  j  a  v  a  2s . co  m*/

    // Servlet API mock
    HttpServletRequest request = (MockHttpServletRequest) this.applicationContext.getBean("mockRequest");
    HttpSession httpSession = request.getSession();
    ServletContext servletContext = httpSession.getServletContext();
    HDIVUtil.setHttpServletRequest(request);

    // Put Spring context on ServletContext
    StaticWebApplicationContext webApplicationContext = new StaticWebApplicationContext();
    webApplicationContext.setServletContext(servletContext);
    webApplicationContext.setParent(this.applicationContext);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            webApplicationContext);

    // Initialize config
    this.config = (HDIVConfig) this.applicationContext.getBean("config");

    InitListener initListener = new InitListener();
    // Initialize ServletContext
    ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);
    initListener.contextInitialized(servletContextEvent);
    // Initialize HttpSession
    HttpSessionEvent httpSessionEvent = new HttpSessionEvent(httpSession);
    initListener.sessionCreated(httpSessionEvent);
    // Initialize request
    ServletRequestEvent requestEvent = new ServletRequestEvent(servletContext, request);
    initListener.requestInitialized(requestEvent);

    if (log.isDebugEnabled()) {
        log.debug("Hdiv test context initialized");
    }

    onSetUp();
}

From source file:org.hdiv.hateoas.jackson.AbstractHDIVTestCase.java

protected void setUp() throws Exception {

    String[] files = { "/org/hdiv/config/hdiv-core-applicationContext.xml", "/org/hdiv/config/hdiv-config.xml",
            "/org/hdiv/config/hdiv-validations.xml", "/org/hdiv/config/applicationContext-test.xml",
            "/org/hdiv/config/applicationContext-extra.xml" };

    if (this.applicationContext == null) {
        this.applicationContext = new ClassPathXmlApplicationContext(files);
    }/*w  w w.java  2  s.c o  m*/

    // Servlet API mock
    HttpServletRequest request = (MockHttpServletRequest) this.applicationContext.getBean("mockRequest");
    HttpSession httpSession = request.getSession();
    ServletContext servletContext = httpSession.getServletContext();
    HDIVUtil.setHttpServletRequest(request);

    // Put Spring context on ServletContext
    StaticWebApplicationContext webApplicationContext = new StaticWebApplicationContext();
    webApplicationContext.setServletContext(servletContext);
    webApplicationContext.setParent(this.applicationContext);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            webApplicationContext);

    // Initialize config
    this.config = (HDIVConfig) this.applicationContext.getBean("config");

    InitListener initListener = new InitListener();
    // Initialize ServletContext
    ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);
    initListener.contextInitialized(servletContextEvent);
    // Initialize HttpSession
    HttpSessionEvent httpSessionEvent = new HttpSessionEvent(httpSession);
    initListener.sessionCreated(httpSessionEvent);
    // Initialize request
    ServletRequestEvent requestEvent = new ServletRequestEvent(servletContext, request);
    initListener.requestInitialized(requestEvent);

    if (log.isDebugEnabled()) {
        log.debug("Hdiv test context initialized");
    }

    onSetUp();
}

From source file:springapp.web.controller.theme.ResourceController.java

public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) throws Exception {
    String thisControllerUrl = httpServletResponse.encodeURL(httpServletRequest.getRequestURI());
    HttpSession session = httpServletRequest.getSession();

    ServletContext servletContext = session.getServletContext();
    WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    //String resourcesPath = (String)context.getBean("resourcesPath");

    ResourcesHolder currentSchema = (ResourcesHolder) session
            .getAttribute(ApplicationConstants.CURRENT_SCHEMA_ATTRIBUTE_NAME);

    String resourcesPath = currentSchema.getResourcesPath();
    String resourcePath = httpServletRequest.getParameter("resourcePath");
    boolean b = null != resourcePath;
    Integer hashCode = null;//w  w  w. j  a v  a2 s  .  c  om
    if (b)
        try {
            hashCode = new Integer(Integer.parseInt(resourcePath));
        } catch (NumberFormatException e) {
            hashCode = new Integer(resourcePath.hashCode());
        }
    //ResourcesHolder resource = currentSchema.findResourceByPath(resourcePath);
    ResourcesHolder resource = currentSchema.findResourceByPathHashCode(hashCode);

    ResourcesLoader loader = loaderFactory.getResourcesLoader(resource, context);
    OutputStream outputStream = loader.outForWeb(resource, thisControllerUrl, resourcesPath);

    if (outputStream instanceof ByteArrayOutputStream) {
        ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) outputStream;
        if (resource instanceof CSSHolderImpl)
            httpServletResponse.setContentType("text/css");
        else if (resource instanceof GIFHolderImpl)
            httpServletResponse.setContentType("image/gif");
        else if (resource instanceof PNGHolderImpl)
            httpServletResponse.setContentType("image/png");

        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        httpServletResponse.setHeader("Expires", "-1");
        httpServletResponse.setContentLength(byteArrayOutputStream.size());

        ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
        byteArrayOutputStream.writeTo(servletOutputStream);
        servletOutputStream.flush();
        servletOutputStream.close();
    }
    logger.info("ResourceController ! ");

    return null;
}