/** * get a encrypter cookie by name */ public String getEncryptedCookie(String name) throws PragmatachException { try { if ((null != name) && (name.length() > 0)) { final Hashtable<String, String> cookies = getCookies(); if (null != cookies) { final String k = cookies.get(name); if (null != k) { return decryptCookie(k); } } } return null; } catch (final Exception e) { throw new PragmatachException("Exception in getCookie", e); } }
/** * clear all cookies */ public void clearAll() throws PragmatachException { final Cookie[] cookies = httpServletRequest.getCookies(); if ((null != cookies) && (cookies.length > 0)) { for (final Cookie cookie : cookies) { removeCookie(cookie.getName()); } } }
/** * ctor */ public Request(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Route.HttpMethod method, ServletConfig servletConfig) { this.httpServletRequest = httpServletRequest; this.servletConfig = servletConfig; this.method = method; headers = new Headers(httpServletRequest, httpServletResponse); cookies = new Cookies(httpServletRequest, httpServletResponse); }
/** * get a cookie by name */ public String getCookie(String name) throws PragmatachException { try { if ((null != name) && (name.length() > 0)) { final Hashtable<String, String> cookies = getCookies(); if (null != cookies) { return cookies.get(name); } } return null; } catch (final Exception e) { throw new PragmatachException("Exception in getCookie", e); } }
/** * decrypt cookie */ private String decryptCookie(String cookie) throws Exception { final String key = getCryptoKey(); if (null != key) { return AES.decrypt(cookie, key); } else { return cookie; } }
/** * set a cookie */ public void setEncryptedCookie(String name, String value) throws PragmatachException { try { final Cookie cookie = new Cookie(name, encryptCookie(value)); cookie.setMaxAge(SECONDS_PER_YEAR); cookie.setPath("/"); httpServletResponse.addCookie(cookie); } catch (final Exception e) { throw new PragmatachException("Exception in setCookie", e); } } }
@Route(uri = "/pragmatach/admin/cookies/clearCookies", method = HttpMethod.post) public Response clearCookies() throws PragmatachException { getRequest().getCookies().clearAll(); return super.render(); }
@Route(uri = "/pragmatach/admin/login/doLogin", method = HttpMethod.post) public Response doLogin() throws PragmatachException { /* * valid u/p? */ if ((null != username) && (username.length() > 0) && (null != password) && (password.length() > 0)) { /* * check that it matches */ if ((username.compareTo(getConfigurationParameter("pragmatach.adminapp.username")) == 0) && (password.compareTo(getConfigurationParameter("pragmatach.adminapp.password")) == 0)) { /* * set session state */ this.getSessionScopedController(AdminUserController.class).setUsername(username); /* * set cookie */ getRequest().getCookies().setEncryptedCookie(BaseAdminController.USERID, username); getRequest().getCookies().setEncryptedCookie(BaseAdminController.PASSWORD, password); /* * go to main page */ return super.forward("/pragmatach/admin/"); } } return super.forward("/pragmatach/admin/login"); }
final String userId = getRequest().getCookies().getEncryptedCookie(USERID); final String password = getRequest().getCookies().getEncryptedCookie(PASSWORD); if ((null != userId) && (null != password)) {
@Route(uri = "/pragmatach/admin/cookies") public Response render() throws PragmatachException { cookies = getRequest().getCookies().getCookies(); return super.render(); }
/** * encrypt cookie */ private String encryptCookie(String cookie) throws Exception { final String key = getCryptoKey(); if (null != key) { return AES.encrypt(cookie, key); } else { return cookie; } }
/** * log out */ @Route(uri = "pragmatach/admin/logout") public Response logout() throws PragmatachException { try { /* * get the user controller */ final AdminUserController adminUserController = this.getSessionScopedController(AdminUserController.class); /* * remove the login session state */ adminUserController.setUsername(null); /* * remove the cookies */ getRequest().getCookies().removeCookie(BaseAdminController.USERID); getRequest().getCookies().removeCookie(BaseAdminController.PASSWORD); /* * to login screen */ return super.forward("/pragmatach/admin/"); } catch (final Exception e) { throw new PragmatachException("Exception in logout", e); } } }