| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.security; |
| 2 | |
| 3 | import de.ids_mannheim.korap.utils.IPNetMask; |
| 4 | import de.ids_mannheim.korap.utils.TimeUtils; |
| 5 | import lombok.Getter; |
| 6 | |
| 7 | import java.net.UnknownHostException; |
| 8 | |
| 9 | /** |
| 10 | * @author hanl |
| 11 | * @date 09/01/2014 |
| 12 | */ |
| 13 | @Getter |
| 14 | public 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 28 | public PolicyContext () { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 29 | start = TimeUtils.getNow().getMillis(); |
| 30 | } |
| 31 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 32 | |
| 33 | public PolicyContext setIPMask (String ip) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 34 | this.ipmask = ip; |
| 35 | return this; |
| 36 | } |
| 37 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 38 | |
| 39 | public PolicyContext setExpirationTime (long limit) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 40 | this.end = limit; |
| 41 | return this; |
| 42 | } |
| 43 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 44 | |
| 45 | public PolicyContext setEnableTime (long start) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 46 | this.start = start; |
| 47 | return this; |
| 48 | } |
| 49 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 50 | |
| 51 | protected boolean isActive (String ipaddress) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 52 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 61 | } |
| 62 | catch (UnknownHostException e) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 63 | e.printStackTrace(); |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 68 | |
| 69 | protected boolean noMask () { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 70 | return ipmask == null || ipmask.isEmpty(); |
| 71 | } |
| 72 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 73 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 74 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 75 | public String toString () { |
| 76 | return "PolicyContext{" + ", ipmask='" + ipmask + '\'' + ", start=" |
| 77 | + start + ", end=" + end + '}'; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 78 | } |
| 79 | } |