blob: 00e3067a5be0cc050e359d2f63ceb54166f87945 [file] [log] [blame]
Michael Hanle25dea22015-09-24 19:37:56 +02001package de.ids_mannheim.korap.security;
2
3import de.ids_mannheim.korap.utils.IPNetMask;
4import de.ids_mannheim.korap.utils.TimeUtils;
5import lombok.Getter;
6
7import java.net.UnknownHostException;
8
9/**
10 * @author hanl
11 * @date 09/01/2014
12 */
13@Getter
14public class PolicyContext {
15
16 // refers to a specific ip location
17 private String ipmask = "";
18 // this context is not like an environmental property (e.g. morning hours/ evening hours), but specifies absolute time
19 // parameters (e.g. from 10.04.2014 9:00 till 14..04.2014 active for testing).
20 // if the containing parameter do not meet, the policy will be deactivated. if no parameter where specified, the policy
21 // remains active
22 // specifies a start time for the policy to be activated
23 private long start = 0L;
24 // specifies a time up to which the policy stays active
25 private long end = 0L;
26
27
Michael Hanl8abaf9e2016-05-23 16:46:35 +020028 public PolicyContext () {
Michael Hanle25dea22015-09-24 19:37:56 +020029 start = TimeUtils.getNow().getMillis();
30 }
31
Michael Hanl8abaf9e2016-05-23 16:46:35 +020032
33 public PolicyContext setIPMask (String ip) {
Michael Hanle25dea22015-09-24 19:37:56 +020034 this.ipmask = ip;
35 return this;
36 }
37
Michael Hanl8abaf9e2016-05-23 16:46:35 +020038
39 public PolicyContext setExpirationTime (long limit) {
Michael Hanle25dea22015-09-24 19:37:56 +020040 this.end = limit;
41 return this;
42 }
43
Michael Hanl8abaf9e2016-05-23 16:46:35 +020044
45 public PolicyContext setEnableTime (long start) {
Michael Hanle25dea22015-09-24 19:37:56 +020046 this.start = start;
47 return this;
48 }
49
Michael Hanl8abaf9e2016-05-23 16:46:35 +020050
51 protected boolean isActive (String ipaddress) {
Michael Hanle25dea22015-09-24 19:37:56 +020052 if (ipaddress == null)
53 return false;
54 if (noMask())
55 return true;
56 IPNetMask mask;
57 try {
58 mask = IPNetMask.getIPMask(this.ipmask);
59 boolean f = mask.matches(ipaddress);
60 return f;
Michael Hanl8abaf9e2016-05-23 16:46:35 +020061 }
62 catch (UnknownHostException e) {
Michael Hanle25dea22015-09-24 19:37:56 +020063 e.printStackTrace();
64 return false;
65 }
66 }
67
Michael Hanl8abaf9e2016-05-23 16:46:35 +020068
69 protected boolean noMask () {
Michael Hanle25dea22015-09-24 19:37:56 +020070 return ipmask == null || ipmask.isEmpty();
71 }
72
Michael Hanl8abaf9e2016-05-23 16:46:35 +020073
Michael Hanle25dea22015-09-24 19:37:56 +020074 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020075 public String toString () {
76 return "PolicyContext{" + ", ipmask='" + ipmask + '\'' + ", start="
77 + start + ", end=" + end + '}';
Michael Hanle25dea22015-09-24 19:37:56 +020078 }
79}