Example usage for javax.servlet.http HttpSession putValue

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

Introduction

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

Prototype

@Deprecated
public void putValue(String name, Object value);

Source Link

Usage

From source file:org.codehaus.wadi.web.TestHttpSession.java

public void testPutValue(Manager manager) {
    HttpSession session = ((WADIHttpSession) manager.create(null)).getWrapper();
    assertTrue(events.size() == 1); // sessionCreated
    events.clear();//ww w  .  j  av a 2 s. co m

    String key = "foo";
    Object val = new Listener();
    session.putValue(key, val);
    assertTrue(events.size() == 2); // valueBound, attributeAdded
    {
        Pair pair = (Pair) events.get(0);
        assertTrue(pair != null);
        assertTrue(pair.getType().equals("valueBound"));
        HttpSessionEvent e = pair.getEvent();
        assertTrue(session == e.getSession());
        HttpSessionBindingEvent be = (HttpSessionBindingEvent) e;
        assertTrue(be.getName() == key);
        assertTrue(be.getValue() == val);
    }
    {
        Pair pair = (Pair) events.get(1);
        assertTrue(pair != null);
        assertTrue(pair.getType().equals("attributeAdded"));
        HttpSessionEvent e = pair.getEvent();
        assertTrue(session == e.getSession());
        HttpSessionBindingEvent be = (HttpSessionBindingEvent) e;
        assertTrue(be.getName() == key);
        assertTrue(be.getValue() == val);
    }
    events.clear();
    assertTrue(events.size() == 0);
}

From source file:org.codehaus.wadi.web.TestHttpSession.java

public void testPutValueNull(Manager manager) {
    HttpSession session = ((WADIHttpSession) manager.create(null)).getWrapper();
    assertTrue(events.size() == 1); // sessionCreated
    String key = "foo";
    Object val = new Listener();
    session.setAttribute(key, val);
    assertTrue(events.size() == 3); // valueBound, attributeAdded
    events.clear();/*from  w ww.  j  av a  2s .  c om*/

    session.putValue(key, null);
    assertTrue(events.size() == 2); // valueUnBound, attributeRemoved

    {
        Pair pair = (Pair) events.get(0);
        assertTrue(pair != null);
        assertTrue(pair.getType().equals("valueUnbound"));
        HttpSessionEvent e = pair.getEvent();
        assertTrue(session == e.getSession());
        HttpSessionBindingEvent be = (HttpSessionBindingEvent) e;
        assertTrue(be.getName() == key);
        assertTrue(be.getValue() == val);
    }
    {
        Pair pair = (Pair) events.get(1);
        assertTrue(pair != null);
        assertTrue(pair.getType().equals("attributeRemoved"));
        HttpSessionEvent e = pair.getEvent();
        assertTrue(session == e.getSession());
        HttpSessionBindingEvent be = (HttpSessionBindingEvent) e;
        assertTrue(be.getName() == key);
        assertTrue(be.getValue() == val);
    }
    events.clear();
    assertTrue(events.size() == 0);
    assertTrue(session.getAttribute(key) == null);
}

From source file:org.codehaus.wadi.web.TestHttpSession.java

public void testReplaceValue(Manager manager) {
    HttpSession session = ((WADIHttpSession) manager.create(null)).getWrapper();
    String key = "foo";
    Object oldVal = new Listener();
    Object newVal = new Listener();
    session.setAttribute(key, oldVal);/*ww w  .  j  ava  2 s  . c o m*/
    events.clear();

    session.putValue(key, newVal);
    {
        Pair pair = (Pair) events.remove(0);
        assertTrue(pair != null);
        assertTrue(pair.getType().equals("valueUnbound"));
        HttpSessionEvent e = pair.getEvent();
        assertTrue(session == e.getSession());
        HttpSessionBindingEvent be = (HttpSessionBindingEvent) e;
        assertTrue(be.getName() == key);
        assertTrue(be.getValue() == oldVal);
    }
    {
        Pair pair = (Pair) events.remove(0);
        assertTrue(pair != null);
        assertTrue(pair.getType().equals("valueBound"));
        HttpSessionEvent e = pair.getEvent();
        assertTrue(session == e.getSession());
        HttpSessionBindingEvent be = (HttpSessionBindingEvent) e;
        assertTrue(be.getName() == key);
        assertTrue(be.getValue() == newVal);
    }
    {
        Pair pair = (Pair) events.remove(0);
        assertTrue(pair != null);
        assertTrue(pair.getType().equals("attributeReplaced"));
        HttpSessionEvent e = pair.getEvent();
        assertTrue(session == e.getSession());
        HttpSessionBindingEvent be = (HttpSessionBindingEvent) e;
        assertTrue(be.getName() == key);
        assertTrue(be.getValue() == oldVal);
    }
    assertTrue(session.getValue(key) == newVal);
    assertTrue(events.size() == 0);
}

From source file:org.codehaus.wadi.web.TestHttpSession.java

public void testNullName(Manager manager) {
    HttpSession session = ((WADIHttpSession) manager.create(null)).getWrapper();
    try {//from w w  w  .  j a  v a 2 s .c  om
        session.setAttribute(null, "a");
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
    try {
        session.getAttribute(null);
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
    try {
        session.removeAttribute(null);
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
    try {
        session.putValue(null, "a");
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
    try {
        session.getValue(null);
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
    try {
        session.removeValue(null);
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    }
}

From source file:org.energy_home.jemma.ah.webui.energyathome.ekitchen.EnergyAtHome.java

public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {

    // we need http scheme!
    if (enableHttps && !request.getScheme().equals("https")) {
        try {/*from w ww  . j  a v a2s.  com*/
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        } catch (IOException e) {
            // do nothing
        }
        return false;
    }

    String queryString = request.getRequestURI();

    if (queryString.equals(applicationWebAlias + "/conf")
            || (queryString.equals(applicationWebAlias + "/conf/"))) {
        response.sendRedirect(applicationWebAlias + "/conf/index.html");
        return true;
    } else if (queryString.equals(applicationWebAlias) || (queryString.equals(applicationWebAlias + "/"))) {
        response.sendRedirect(applicationWebAlias + "/index.html");
        return true;
    }

    if (enableSecurity) {
        if (useBasic) {
            String auth = request.getHeader("Authorization");

            if (auth == null)
                return failAuthorization(request, response);

            StringTokenizer tokens = new StringTokenizer(auth);
            String authscheme = tokens.nextToken();

            if (!authscheme.equals("Basic"))
                return failAuthorization(request, response);

            String base64credentials = tokens.nextToken();
            String credentials = new String(Base64.decode(base64credentials.getBytes()));
            int colon = credentials.indexOf(':');
            String userid = credentials.substring(0, colon);
            String password = credentials.substring(colon + 1);
            Authorization subject = null;

            try {
                subject = login(request, userid, password);
            } catch (LoginException e) {
                return failAuthorization(request, response);
            }

            request.setAttribute(HttpContext.REMOTE_USER, userid);
            request.setAttribute(HttpContext.AUTHENTICATION_TYPE, authscheme);
            request.setAttribute(HttpContext.AUTHORIZATION, subject);
        } else {
            HttpSession session = request.getSession(true);
            if (queryString.startsWith(applicationWebAlias + "/conf")) {
                // this is a restricted area so performs login

                String a = request.getMethod();
                String submit = request.getParameter("submit");
                if (submit != null) {
                    String username = request.getParameter("username");
                    String password = request.getParameter("password");
                    if (!allowUser(username, password)) {
                        return redirectToLoginPage(request, response);
                    } else {
                        session.putValue("logon.isDone", username);
                        try {
                            String target = (String) session.getValue("login.target");
                            if (target != null)
                                response.sendRedirect(target);
                            else {
                                response.sendRedirect(applicationWebAlias + "/conf/index.html");
                            }
                        } catch (Exception ignored) {
                            return false;
                        }
                    }
                } else {
                    if (queryString.equals(applicationWebAlias + "/conf/login.html")) {
                        return true;
                    } else {
                        //                     session.putValue("login.target", HttpUtils.getRequestURL(request).toString());
                        session.putValue("login.target", applicationWebAlias + "/conf/index.html");
                        Object done = session.getValue("logon.isDone");
                        if (done == null) {
                            if (request.getMethod().equals("GET")) {
                                return redirectToLoginPage(request, response);
                            } else {
                                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                                return false;
                            }
                        }

                    }
                }
            }
        }
    }

    if (request.getRequestURI().endsWith(".png")) {
        response.setHeader("Cache-Control", "public, max-age=10000");
    }

    /*if (request.getRequestURI().endsWith(".js")) {
       response.setHeader("Pragma", "no-cache");
       response.setHeader("Cache-Control", "no-store");
       response.setHeader("Cache-Control", "public, max-age=0");
    }*/

    // response.addHeader(HttpServletResponse, arg1)

    return true;
}