public <T> T remove(String name) { T t = get(name); httpSession.removeAttribute(name); return t; }
public Map<String, Object> getAll() { Map<String, Object> all = new HashMap<>(); Enumeration<String> names = getNames(); while (names.hasMoreElements() ) { String name = names.nextElement(); if (FLASH.equalsIgnoreCase(name)) { continue; } all.put(name, get(name)); } return all; }
public void recreateSession() { if (session == null) { return; } // preserve the session data Map<String, Object> values = new HashMap<>(); Enumeration<String> names = getSession().getNames(); while (names.hasMoreElements()) { String name = names.nextElement(); values.put(name, getSession().get(name)); } // preserve the flash data Flash flash = session.getFlash(); // create a new session resetSession(); // restore the session data for (Map.Entry<String, Object> entry : values.entrySet()) { getSession().put(entry.getKey(), entry.getValue()); } // restore the flash instance if (flash != null) { getSession().put("flash", flash); } }
@Override public Object get(PippoWebContext pippoWebContext, String name) { return getSession(pippoWebContext).get(name); }
public Flash getFlash() { Flash flash = get(FLASH); if (flash == null) { put(FLASH, flash = new Flash()); } return flash; }
@Override @SuppressWarnings("unchecked") public <T> T getSession(String name) { return hasSession() ? (T) request.getSession().get(name) : null; }
/** * Renders a template to a {@link String}. * * @param templateName * @param model */ public String renderToString(String templateName, Map<String, Object> model) { if (templateEngine == null) { throw new PippoRuntimeException("You must set a template engine in your application"); } // merge the model passed with the locals data model.putAll(getLocals()); // add session (if exists) to model Session session = Session.get(); if (session != null) { model.put("session", session); } // render the template using the merged model StringWriter stringWriter = new StringWriter(); templateEngine.renderResource(templateName, model, stringWriter); return stringWriter.toString(); }