Session.java

  1. /*
  2.  * CNR - IIT (2015-2016)
  3.  *
  4.  * @authors Fabio Bindi and Filippo Lauria
  5.  */
  6. package it.cnr.iit.ucs.sessionmanager;

  7. import com.j256.ormlite.dao.ForeignCollection;
  8. import com.j256.ormlite.field.DataType;
  9. import com.j256.ormlite.field.DatabaseField;
  10. import com.j256.ormlite.field.ForeignCollectionField;
  11. import com.j256.ormlite.table.DatabaseTable;

  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.logging.Logger;

  15. /**
  16.  * Creates Session objects. It configures OrmLite annotations to persist these
  17.  * objects in the sessions table of the database.
  18.  *
  19.  * @author Fabio Bindi, Filippo Lauria and Antonio La Marra
  20.  */
  21. @DatabaseTable(tableName = "sessions")
  22. public final class Session implements SessionInterface {

  23.     private static final Logger log = Logger.getLogger(Session.class.getName());

  24.     public static final String ID_FIELD_NAME = "id";
  25.     public static final String POLICYSET_FIELD_NAME = "policy_set";
  26.     public static final String ORIGINALREQUEST_FIELD_NAME = "original_request";
  27.     public static final String STATUS_FIELD_NAME = "status";
  28.     public static final String PEPURI_FIELD_NAME = "pep_uri";
  29.     public static final String MYIP_FIELD = "myip";

  30.     @DatabaseField(id = true, columnName = ID_FIELD_NAME)
  31.     private String id;

  32.     @DatabaseField(columnName = POLICYSET_FIELD_NAME, dataType = DataType.LONG_STRING)
  33.     private String policySet;

  34.     @DatabaseField(columnName = ORIGINALREQUEST_FIELD_NAME, dataType = DataType.LONG_STRING)
  35.     private String originalRequest;

  36.     @DatabaseField(columnName = STATUS_FIELD_NAME)
  37.     private String status;

  38.     @DatabaseField(columnName = PEPURI_FIELD_NAME)
  39.     private String pepURI;

  40.     @DatabaseField(columnName = MYIP_FIELD)
  41.     private String myIP;

  42.     @ForeignCollectionField(eager = true, columnName = OnGoingAttribute.ATTRIBUTEID_FIELD)
  43.     private ForeignCollection<OnGoingAttribute> onGoingAttributes;

  44.     public Session() {

  45.     }

  46.     /**
  47.      * @param id              session ID
  48.      * @param policySet       policy containing all the mutable attributes needed during the session
  49.      * @param originalRequest original XAML request containing information about the subject/object
  50.      * @param status          status of the session
  51.      * @param pepURI          URI of the PEP that started the session
  52.      */
  53.     public Session(String id, String policySet, String originalRequest, String status, String pepURI,
  54.                    String myIP) {
  55.         this.id = id;
  56.         this.policySet = policySet;
  57.         this.originalRequest = originalRequest;
  58.         this.status = status;
  59.         this.pepURI = pepURI;
  60.         this.myIP = myIP;
  61.     }

  62.     /**
  63.      * Retrieves session ID
  64.      *
  65.      * @return session ID
  66.      */
  67.     @Override
  68.     public String getId() {
  69.         return id;
  70.     }

  71.     /**
  72.      * Sets the session ID
  73.      *
  74.      * @param id session ID to be set
  75.      */
  76.     public void setId(String id) {
  77.         this.id = id;
  78.     }

  79.     /**
  80.      * Retrieves the PolicySet
  81.      *
  82.      * @return policySet
  83.      */
  84.     @Override
  85.     public String getPolicySet() {
  86.         return policySet;
  87.     }

  88.     /**
  89.      * Sets the policySet
  90.      *
  91.      * @param policySet policySet to set
  92.      */
  93.     public void setPolicySet(String policySet) {
  94.         this.policySet = policySet;
  95.     }

  96.     /**
  97.      * Retrieves the originalRequest
  98.      *
  99.      * @return originalRequest
  100.      */
  101.     @Override
  102.     public String getOriginalRequest() {
  103.         return originalRequest;
  104.     }

  105.     /**
  106.      * Sets the originalRequest
  107.      *
  108.      * @param originalRequest originalRequest to be set
  109.      */
  110.     public void setOriginalRequest(String originalRequest) {
  111.         this.originalRequest = originalRequest;
  112.     }

  113.     /**
  114.      * Retrieves the status of the session
  115.      *
  116.      * @return status
  117.      */
  118.     @Override
  119.     public String getStatus() {
  120.         return status;
  121.     }

  122.     /**
  123.      * Sets the status of the session
  124.      *
  125.      * @param status status to be set
  126.      */
  127.     public void setStatus(String status) {
  128.         this.status = status;
  129.     }

  130.     @Override
  131.     public boolean isStatus(String status) {
  132.         return this.status.equalsIgnoreCase(status);
  133.     }

  134.     /**
  135.      * Sets the URI of the PEP
  136.      *
  137.      * @param pepURI URI of the PEP to be set
  138.      */
  139.     public void setPepURI(String pepURI) {
  140.         this.pepURI = pepURI;
  141.     }

  142.     public void setMyIP(String ip) {
  143.         this.myIP = ip;
  144.     }

  145.     /**
  146.      * Retrieves the list of mutable attributes of the session
  147.      *
  148.      * @return list of mutable attributes
  149.      */
  150.     @Override
  151.     public List<OnGoingAttributesInterface> getOnGoingAttributes() {
  152.         List<OnGoingAttributesInterface> list = new ArrayList<>();
  153.         for (OnGoingAttribute onGoingAttribute : onGoingAttributes) {
  154.             list.add(onGoingAttribute);
  155.         }
  156.         return list;
  157.     }

  158.     /**
  159.      * Retrieves the list of mutable attributes of the session
  160.      *
  161.      * @return list of mutable attributes
  162.      */
  163.     public List<OnGoingAttributesInterface> getOnGoingAttribute() {
  164.         List<OnGoingAttributesInterface> list = new ArrayList<>();
  165.         for (OnGoingAttributesInterface onGoingAttribute : onGoingAttributes) {
  166.             list.add(onGoingAttribute);
  167.         }
  168.         return list;
  169.     }

  170.     /**
  171.      * Retrieves the list of mutable attributes of the session
  172.      *
  173.      * @return list of mutable attributes
  174.      */
  175.     public ForeignCollection<OnGoingAttribute> getOnGoingAttributesAsForeign() {
  176.         return onGoingAttributes;
  177.     }

  178.     @Override
  179.     public String toString() {
  180.         StringBuilder stringBuilder = new StringBuilder();
  181.         stringBuilder.append("SessionId = ").append(id).append("\nPolicySet = ").append(policySet).append("\n");
  182.         stringBuilder.append("OriginalRequest = ").append(originalRequest).append("\n");
  183.         stringBuilder.append("Status = ").append(status).append("\npepURI = ").append(pepURI).append("\n");
  184.         if (onGoingAttributes != null) {
  185.             stringBuilder.append("OnGoingAttributes = ");
  186.             try {
  187.                 for (OnGoingAttribute a : onGoingAttributes) {
  188.                     stringBuilder.append(a.toString()).append(" ");
  189.                 }
  190.             } catch (NullPointerException e) {
  191.                 log.warning("Error null ongoing attributes");
  192.             }
  193.         }
  194.         return stringBuilder.toString();
  195.     }

  196.     @Override
  197.     public String getPepId() {
  198.         return pepURI;
  199.     }

  200.     @Override
  201.     public String getIP() {
  202.         return myIP;
  203.     }

  204.     @Override
  205.     public void setRequest(String request) {
  206.         this.originalRequest = request;
  207.     }

  208. }