Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *Добавил в класс Request:*
- public static final String JUNIT_SESSION_ID_COOKIE_NAME = "JUNIT_ID";
- @Override
- public HttpSession getSession()
- {
- log("getSession");
- return getSession(true);
- }
- @Override
- public HttpSession getSession(boolean create)
- {
- log("getSession: " + create);
- SessionManager manager = SessionManager.getInstance();
- Cookie cookie = getCookie(); //finding cookie
- HttpSession result = null;
- if(cookie!=null){ //cookie found
- result = manager.getSession(cookie.getValue()); //finding session with id = cookie's value
- if(result==null && create) { //session not found, but must be created
- result = new SessionImpl();
- manager.putSession(result.getId(), result); //saving session
- cookie.setValue(result.getId());
- addCookie(cookie); //saving session's id in cookie
- }
- }
- else if(create) { ////cookie not found, but session must be created
- result = new SessionImpl();
- manager.putSession(result.getId(), result); //saving session
- cookie = new Cookie(JUNIT_SESSION_ID_COOKIE_NAME, result.getId()); //creation cookie
- cookies.addCookieField(JUNIT_SESSION_ID_COOKIE_NAME);
- addCookie(cookie);
- }
- result = manager.checkTimeOut(result);
- return result;
- }
- private void addCookie(Cookie newCookie) {
- Cookie[] cs = cookies.getCookies();
- for(Cookie c : cs){
- if(c.getName().equals(newCookie.getName())){
- c = newCookie;
- break;
- }
- }
- cookies.setCookies(cs);
- }
- private Cookie getCookie() {
- Cookie cookie = null;
- Cookie[] cookies = this.getCookies();
- if(cookies!=null){
- for (Cookie next : cookies) {
- if (next.getName().equals(JUNIT_SESSION_ID_COOKIE_NAME)) {
- cookie = next;
- break;
- }
- }
- }
- return cookie;
- }
- *Изменил метод service в классе Context:*
- public static void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
- ctx_.callServlets(req, resp);
- if(resp instanceof HttpServletResponse && req instanceof HttpServletRequest){
- Cookie[] cookies = ((HttpServletRequest) req).getCookies();
- for(Cookie cookie : cookies)
- ((HttpServletResponse)resp).addCookie(cookie);
- }
- }
- *Упростил класс SessionManager:*
- package nginx.unit.session;
- import javax.servlet.http.HttpSession;
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- public class SessionManager implements ActionListener {
- private static SessionManager ourInstance = new SessionManager();
- public static SessionManager getInstance() {
- return ourInstance;
- }
- private SessionManager() {
- cleaner.start();
- }
- private int timeOut = 60_000;
- private Timer cleaner = new Timer(timeOut, this); //removes "timeouted" session every $timeOut milliseconds
- private Map<String, HttpSession> map = new HashMap<>();
- public void invalidate(SessionImpl session) {
- map.remove(session.getId());
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- long now = new Date().getTime();
- map.values().removeIf(httpSession -> httpSession.getCreationTime() + timeOut > now);
- }
- public int getTimeOut() {
- return timeOut;
- }
- public void setTimeOut(int timeOut) {
- this.timeOut = timeOut;
- cleaner = new Timer(timeOut, this);
- cleaner.start();
- }
- public HttpSession getSession(String value) {
- return map.get(value);
- }
- public void putSession(String id, HttpSession session) {
- map.put(id,session);
- }
- public HttpSession checkTimeOut(HttpSession result) {
- if(result!=null && result.getCreationTime() + timeOut < new Date().getTime()){
- map.remove(result);
- result = null;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment