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:org.intermine.bio.web.FriendlyMineLinkController.java

/**
 * {@inheritDoc}/*w ww.  j a  v  a  2  s.  c  o  m*/
 */
@Override
public ActionForward execute(ComponentContext context, ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
    InterMineBag bag = (InterMineBag) request.getAttribute("bag");
    final InterMineAPI im = SessionMethods.getInterMineAPI(request.getSession());
    Collection<String> organismsInBag = BioUtil.getOrganisms(im.getObjectStore(), bag, false, "shortName");
    String organisms = null;
    if (!organismsInBag.isEmpty()) {
        organisms = StringUtil.join(organismsInBag, ",");
    }
    String identifierField = getIdentifierField(bag);
    String identifierList = BagHelper.getAttributesFromBag(bag, im.getObjectStore(), "", identifierField);
    request.setAttribute("identifiers", identifierList);
    if (StringUtils.isNotEmpty(organisms)) {
        request.setAttribute("organisms", organisms);
    }
    HttpSession session = request.getSession();
    ServletContext servletContext = session.getServletContext();
    final Properties webProperties = SessionMethods.getWebProperties(servletContext);
    final FriendlyMineManager linkManager = FriendlyMineManager.getInstance(im, webProperties);
    Collection<Mine> mines = linkManager.getFriendlyMines();
    request.setAttribute("mines", mines);
    return null;
}

From source file:de.interseroh.report.webapp.SessionListener.java

private ApplicationContext getApplicationContext(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
    return ctx;/*  w  ww. jav  a 2s.  com*/
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Gets the origins map from the servlet context.
 *
 * @param session An HTTP session for this web application.
 *
 * @return A map from each property to its origins.
 *//*from   w  w  w .j ava  2  s  .  c  o m*/
public static Map<String, List<String>> getPropertiesOrigins(HttpSession session) {
    return (Map<String, List<String>>) session.getServletContext().getAttribute(Constants.PROPERTIES_ORIGINS);
}

From source file:com.orchestra.portale.controller.UserRegistrationController.java

@RequestMapping(value = "/userSignIn", method = RequestMethod.POST)
public ModelAndView addUser(HttpServletRequest request, @ModelAttribute("SpringWeb") User user,
        MultipartFile avatar) {/*from  w w w  . ja  v a2 s .c  o  m*/
    ModelAndView model2 = new ModelAndView("okpage");
    User usertest = pm.findUserByUsername(user.getUsername());
    if (usertest != null && usertest.getUsername().toLowerCase().equals(user.getUsername().toLowerCase())) {
        model2.addObject("err", "Esiste gi un utente con username: " + user.getUsername());
        return model2;
    }
    //HASH PASSWORD
    user.setPassword(crypt(user.getPassword()));
    /*Create Role*/
    Role new_user_role = new Role();
    new_user_role.setRole("ROLE_USER");
    new_user_role.setUser(user);
    ArrayList<Role> new_user_roles = new ArrayList<Role>();
    new_user_roles.add(new_user_role);
    user.setRoles(new_user_roles);
    pm.saveUser(user);

    User user2 = pm.findUserByUsername(user.getUsername());

    MultipartFile file = avatar;

    try {
        byte[] bytes = file.getBytes();

        // Creating the directory to store file
        HttpSession session = request.getSession();
        ServletContext sc = session.getServletContext();

        File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
                + File.separator + user2.getId());
        if (!dir.exists())
            dir.mkdirs();

        // Create the file on server
        File serverFile = new File(dir.getAbsolutePath() + File.separator + "avatar.jpg");
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
        stream.write(bytes);
        stream.close();
    } catch (Exception e) {

    }
    model2.addObject("mess",
            "Registrazione completata con successo!<br><br><center> <a href='page?sec=home' class='btn btn-primary'>Torna alla Home</a></center> ");
    return model2;
}

From source file:com.orchestra.portale.controller.UserEditController.java

@RequestMapping("userEditProfile")
@Secured("ROLE_USER")
public ModelAndView editUser(HttpServletRequest request) {
    ModelAndView model = new ModelAndView("userEditProfile", "command", new User());
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    User user = pm.findUserByUsername(auth.getName());

    HttpSession session = request.getSession();
    ServletContext sc = session.getServletContext();
    File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
            + File.separator + user.getId() + File.separator + "avatar.jpg");
    if (dir.exists()) {
        model.addObject("avatar", "./dist/user/img/" + user.getId() + "/avatar.jpg");
    } else {/*from   ww  w.  j  a  v a2  s  .  c  om*/

        model.addObject("avatar", "./dist/img/default_avatar.png");
    }

    model.addObject("user", user);

    return model;

}

From source file:edu.lternet.pasta.portal.DesktopCleanerServlet.java

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)/*from   w w  w.  jav a2s.  c o  m*/
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession httpSession = request.getSession();
    ServletContext servletContext = httpSession.getServletContext();
    String dataPath = servletContext.getRealPath(HarvesterServlet.DESKTOP_DATA_DIR);
    String ttlString = TTL_IN_MINUTES; // number of minutes for desktop data to live

    if (ttlString != null && !ttlString.isEmpty()) {
        Long ttl = Long.valueOf(ttlString) * 60000L; // Convert minutes to ms
        cleanExpiredData(dataPath, ttl);
    }

}

From source file:org.apache.struts2.showcase.chat.ChatSessionListener.java

public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    WebApplicationContext context = WebApplicationContextUtils
            .getWebApplicationContext(session.getServletContext());
    if (context != null) {
        User user = (User) session.getAttribute(ChatInterceptor.CHAT_USER_SESSION_KEY);
        if (user != null) {
            ChatService service = (ChatService) context.getBean("chatService");
            service.logout(user.getName());

            _log.info("session expired, logged user [" + user.getName() + "] out");
        }// w  ww .jav a  2 s .c  o m
    }
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Return the ReportObjects Map from the session or create and return it if it doesn't exist.
 *
 * @param session the HttpSession to get the ReportObjects Map from
 * @return the (possibly new) ReportObjects Map
 *//*www  .  j  a va 2s .c o  m*/
public static ReportObjectFactory getReportObjects(HttpSession session) {
    ServletContext servletContext = session.getServletContext();
    ReportObjectFactory reportObjects = (ReportObjectFactory) servletContext
            .getAttribute(Constants.REPORT_OBJECT_CACHE);

    // Build map from object id to ReportObject
    if (reportObjects == null) {
        InterMineAPI im = getInterMineAPI(session);
        WebConfig webConfig = getWebConfig(servletContext);
        Properties webProperties = getWebProperties(servletContext);
        reportObjects = new ReportObjectFactory(im, webConfig, webProperties);
        servletContext.setAttribute(Constants.REPORT_OBJECT_CACHE, reportObjects);
    }

    return reportObjects;
}

From source file:com.feilong.controller.TestController.java

@RequestMapping("/helloworld1")
public String test1(HttpServletRequest request, HttpSession session, HttpServletResponse response) {

    session.setAttribute("name", "?");
    request.setAttribute("name", "");
    session.getServletContext().setAttribute("name", "?");

    return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/index";
}

From source file:leon.ssi.util.session.AppSessionListener.java

/**
 * set time out//from  w w w.j  a va2s .  c  o  m
 */
public void sessionCreated(HttpSessionEvent se) {
    HttpSession session = null;
    try {
        session = se.getSession();
        // get value
        ServletContext context = session.getServletContext();
        String timeoutValue = context.getInitParameter("sessionTimeout");
        int timeout = Integer.valueOf(timeoutValue);
        // set value
        session.setMaxInactiveInterval(timeout);
        logger.info("session max inactive interval has been set to " + timeout + " seconds.");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}