package ir.piana.dev.server.session;

import ir.piana.dev.secure.crypto.CryptoAttribute;
import ir.piana.dev.secure.crypto.CryptoMaker;
import ir.piana.dev.server.role.RoleType;

import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.NewCookie;
import java.security.KeyPair;
import java.util.*;

/**
 * @author Mohammad Rahmati, 4/18/2017 4:30 PM
 */
public class Session {
    private String sessionName;
    private KeyPair keyPair;
    private RoleType roleType;
    private String sessionKey;
    private Map<String, String> sessionMap;

    Session(String sessionName,
            KeyPair keyPair,
            RoleType roleType) {
        this(sessionName, keyPair, roleType, null);
    }

    Session(String sessionName,
            KeyPair keyPair,
            RoleType roleType,
            String sessionKey) {
        this.sessionName = sessionName;
        this.keyPair = keyPair;
        this.roleType = roleType;
        this.sessionKey = sessionKey;
        this.sessionMap = new LinkedHashMap<>();
    }

    public KeyPair getKeyPair() {
        return keyPair;
    }

    void setKeyPair(KeyPair keyPair) {
        this.keyPair = keyPair;
    }

    public String getSessionKey() {
        return sessionKey;
    }

    void setSessionKey(String sessionKey) {
        this.sessionKey = sessionKey;
    }

    public RoleType getRoleType() {
        return roleType;
    }

    public void setRoleType(RoleType roleType) {
        this.roleType = roleType;
    }

    public byte[] getPublicKeyBytes() {
        return keyPair.getPublic().getEncoded();
    }

    public void set(String key, String value) {
        this.sessionMap.put(key, value);
    }

    public String get(String key) {
        return this.sessionMap.get(key);
    }

    public String remove(String key) {
        return this.sessionMap.remove(key);
    }

    public void clear() {
        this.sessionMap.clear();
    }

    public byte[] decrypt(byte[] rawMessage)
            throws Exception {
        return CryptoMaker.decrypt(rawMessage,
                keyPair.getPrivate(),
                CryptoAttribute.RSA);
    }
}

@@ -176,75 +176,8 @@
 e;%0A%0A
-import javax.ws.rs.core.Cookie;%0Aimport javax.ws.rs.core.NewCookie;%0A
 impo
@@ -457,22 +457,64 @@
 tring%3E s
-ession
+tringMap;%0A    private Map%3CString, Object%3E object
 Map;%0A%0A  
@@ -947,22 +947,69 @@
   this.s
-ession
+tringMap = new LinkedHashMap%3C%3E();%0A        this.object
 Map = ne
@@ -1611,16 +1611,22 @@
 void set
+String
 (String 
@@ -1656,30 +1656,29 @@
       this.s
-ession
+tring
 Map.put(key,
@@ -1714,16 +1714,22 @@
 ring get
+String
 (String 
@@ -1752,30 +1752,29 @@
 eturn this.s
-ession
+tring
 Map.get(key)
@@ -1806,16 +1806,22 @@
 g remove
+String
 (String 
@@ -1844,30 +1844,29 @@
 eturn this.s
-ession
+tring
 Map.remove(k
@@ -1898,16 +1898,22 @@
 id clear
+String
 () %7B%0A   
@@ -1923,22 +1923,377 @@
   this.s
-ession
+tringMap.clear();%0A    %7D%0A%0A    public void setObject(String key, Object value) %7B%0A        this.objectMap.put(key, value);%0A    %7D%0A%0A    public Object getObject(String key) %7B%0A        return this.objectMap.get(key);%0A    %7D%0A%0A    public Object removeObject(String key) %7B%0A        return this.objectMap.remove(key);%0A    %7D%0A%0A    public void clearObject() %7B%0A        this.object
 Map.clea

add object map to session