Coverage Report - org.openpermis.editor.policy.presenter.DecisionPresenter
 
Classes in this File Line Coverage Branch Coverage Complexity
DecisionPresenter
69%
76/109
26%
11/42
2.429
DecisionPresenter$1
100%
3/3
N/A
2.429
DecisionPresenter$Decision
100%
8/8
N/A
2.429
DecisionPresenter$ParameterTableModel
46%
15/32
33%
6/18
2.429
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.editor.policy.presenter;
 11  
 
 12  
 import java.awt.Color;
 13  
 import java.net.URI;
 14  
 import java.util.ArrayList;
 15  
 import java.util.Date;
 16  
 import java.util.HashSet;
 17  
 import java.util.List;
 18  
 import java.util.Set;
 19  
 import java.util.TimeZone;
 20  
 
 21  
 import javax.swing.table.DefaultTableModel;
 22  
 
 23  
 import org.jdesktop.observablecollections.ObservableCollections;
 24  
 import org.jdesktop.observablecollections.ObservableList;
 25  
 
 26  
 import org.openpermis.Subject;
 27  
 import org.openpermis.basic.PartialTime;
 28  
 import org.openpermis.basic.TimePeriod;
 29  
 import org.openpermis.editor.policy.beans.PropertyChange;
 30  
 import org.openpermis.editor.policy.gui.binding.ObservableListAdapter;
 31  
 import org.openpermis.policy.AccessDecision;
 32  
 import org.openpermis.policy.Action;
 33  
 import org.openpermis.policy.Authority;
 34  
 import org.openpermis.policy.Domain;
 35  
 import org.openpermis.policy.Role;
 36  
 import org.openpermis.policy.TimeStamp;
 37  
 import org.openpermis.policy.ParameterList.Parameter;
 38  
 import org.openpermis.policy.bean.ActionBean;
 39  
 import org.openpermis.policy.bean.AuthorityBean;
 40  
 import org.openpermis.policy.bean.PolicyBean;
 41  
 import org.openpermis.policy.bean.TargetBean;
 42  
 import org.openpermis.policy.predicate.TimeConstant;
 43  
 
 44  
 
 45  
 /**
 46  
  * Presentation model for the decision test view.
 47  
  * @since 0.3.0
 48  
  */
 49  2
 public class DecisionPresenter
 50  
         extends PartPresenter<PolicyBean>
 51  
         implements Subject
 52  
 {
 53  
 
 54  
         //---- Static
 55  
         
 56  
         /**
 57  
          * @since 0.3.0
 58  
          */
 59  120
         private static enum Decision {
 60  1
                 INCOMPLETE(Color.YELLOW),
 61  1
                 GRANTED(Color.GREEN),
 62  1
                 DENIED(Color.RED),
 63  1
                 INVALID(Color.MAGENTA);
 64  
 
 65  
                 private Color color;
 66  
                 
 67  4
                 private Decision (Color color) {
 68  4
                         this.color = color;
 69  4
                 }
 70  
         }
 71  
         
 72  
         //---- Classes
 73  
         
 74  
         @SuppressWarnings("serial")
 75  9
         class ParameterTableModel extends DefaultTableModel {
 76  
 
 77  
                 //---- Static
 78  
                 
 79  
                 private static final int THREE = 3;
 80  
                 
 81  
                 //---- State
 82  
                 
 83  9
                 private List<Object> parameters = new ArrayList<Object>();
 84  
                 
 85  9
                 private boolean triggeredByThis = false;
 86  
                 
 87  
                 //---- Methods
 88  
                 
 89  
                 public List<?> getParameters () {
 90  38
                         return this.parameters;
 91  
                 }
 92  
                 
 93  
                 //---- DefaultTableModel
 94  
                 
 95  
                 public void fireTableDataChanged () {
 96  59
                         if (!this.triggeredByThis) {
 97  59
                                 if (getAction() == null) {
 98  21
                                         return;
 99  
                                 }
 100  38
                                 this.parameters = new ArrayList<Object>(getRowCount());
 101  38
                                 for (Parameter p : getAction().getParameters()) {
 102  0
                                         this.parameters.add(parseArgument(p.getType(), null));
 103  
                                 }
 104  38
                                 super.fireTableDataChanged();
 105  
                         }
 106  38
                         this.triggeredByThis = false;
 107  38
                 }
 108  
                 
 109  
                 public int getRowCount () {
 110  56
                         if (getAction() != null) {
 111  38
                                 return getAction().getParameters().getParameterCount();
 112  
                         }
 113  18
                         return 0;
 114  
                 }
 115  
                 
 116  
                 public int getColumnCount () {
 117  0
                         return THREE;
 118  
                 }
 119  
                 
 120  
                 public boolean isCellEditable (int row, int column) {
 121  0
                         if (column < 2) {
 122  0
                                 return false;
 123  
                         }
 124  0
                         return true;
 125  
                 }
 126  
                 
 127  
                 public Object getValueAt (int row, int column) {
 128  0
                         if (column == 0) {
 129  0
                                 return getAction().getParameters().getType(row).getSimpleName();
 130  0
                         } else if (column == 1) {
 131  0
                                 return getAction().getParameters().getName(row);
 132  0
                         } else if (column == 2) {
 133  0
                                 return this.parameters.get(row).toString();
 134  
                         }
 135  0
                         return "";
 136  
                 }
 137  
                 
 138  
                 public void setValueAt (Object aValue, int row, int column) {
 139  0
                         if (column == 2) {
 140  0
                                 this.parameters.set(
 141  
                                         row,
 142  
                                         parseArgument(
 143  
                                                 getAction().getParameters().getType(row),
 144  
                                                 (String) aValue
 145  
                                         )
 146  
                                 );
 147  0
                                 this.triggeredByThis = true;
 148  0
                                 computeDecision();
 149  
                         }
 150  0
                 }
 151  
         }
 152  
         
 153  
         //---- State
 154  
         
 155  
         /**
 156  
          * @since 0.3.0
 157  
          */
 158  
         private Authority authority;
 159  
 
 160  
         /**
 161  
          * @since 0.3.0
 162  
          */
 163  
         private Domain resourceDomain;
 164  
         
 165  
         /**
 166  
          * @since 0.3.0
 167  
          */
 168  
         private ActionBean action;
 169  
         
 170  
         /**
 171  
          * @since 0.3.0
 172  
          */
 173  
         private ObservableList<Role> selectedRoles;
 174  
         
 175  
         /**
 176  
          * @since 0.3.0
 177  
          */
 178  
         private ObservableListAdapter<Role> selectedRolesListener;
 179  
         
 180  
         /**
 181  
          * @since 0.3.0
 182  
          */
 183  
         private TimeStamp evaluationTimeStamp;
 184  
 
 185  
         /**
 186  
          * @since 0.3.0
 187  
          */
 188  9
         private Decision decision = Decision.INCOMPLETE;
 189  
         
 190  
         /**
 191  
          * @since 0.3.0
 192  
          */
 193  9
         private ParameterTableModel parameterTableModel = new ParameterTableModel();
 194  
 
 195  
         /**
 196  
          * Creates a new presenter.
 197  
          * @param policy the policy whose access rules will be available for testing.
 198  
          * @param context editor meta data for the policy.
 199  
          * @since 0.3.0
 200  
          */
 201  
         public DecisionPresenter (PolicyBean policy, PolicyContext context) {
 202  9
                 super(policy, context);
 203  9
                 this.evaluationTimeStamp = new TimeStamp(new Date(), TimeZone.getDefault());
 204  9
                 createSelectedRoles();
 205  9
                 chooseDefaults();
 206  9
         }
 207  
 
 208  
         //---- Methods
 209  
         
 210  
         /**
 211  
          * @since 0.3.0
 212  
          */
 213  
         private void chooseDefaults () {
 214  9
                 chooseDefaultAuthority();
 215  9
                 chooseDefaultResourceDomain();
 216  9
                 chooseDefaultAction();
 217  9
         }
 218  
 
 219  
         /**
 220  
          * @since 0.3.0
 221  
          */
 222  
         private void chooseDefaultAuthority () {
 223  9
                 final List<AuthorityBean> authorityList = getModel().getPartsList(AuthorityBean.class);
 224  9
                 if (!authorityList.isEmpty()) {
 225  9
                         setAuthority(authorityList.get(0));
 226  
                 }
 227  9
         }
 228  
 
 229  
         /**
 230  
          * @since 0.3.0
 231  
          */
 232  
         private void chooseDefaultResourceDomain () {
 233  9
                 final List<TargetBean> targetList = getModel().getPartsList(TargetBean.class);
 234  9
                 if (!targetList.isEmpty()) {
 235  9
                         setResourceDomain(targetList.get(0).getResourceDomain());
 236  
                 }
 237  9
         }
 238  
 
 239  
         /**
 240  
          * @since 0.3.0
 241  
          */
 242  
         private void chooseDefaultAction () {
 243  9
                 final List<ActionBean> actionList = getModel().getPartsList(ActionBean.class);
 244  9
                 if (!actionList.isEmpty()) {
 245  9
                         setAction(actionList.get(0));
 246  
                 }
 247  9
         }
 248  
 
 249  
         /**
 250  
          * Gets the currently selected authority.
 251  
          * @return the selected authority.
 252  
          * @since 0.3.0
 253  
          */
 254  
         public Authority getAuthority () {
 255  66
                 return this.authority;
 256  
         }
 257  
 
 258  
         /**
 259  
          * Sets the selected authority.
 260  
          * @param authority the selected authority.
 261  
          * @since 0.3.0
 262  
          */
 263  
         public void setAuthority (Authority authority) {
 264  19
                 final Authority oldValue = this.authority;
 265  19
                 this.authority = authority;
 266  19
                 firePropertyChange("authority", oldValue, this.authority);
 267  19
                 computeDecision();
 268  19
         }
 269  
 
 270  
         /**
 271  
          * Gets the currently selected resource domain.
 272  
          * @return the selected resource domain.
 273  
          * @since 0.3.0
 274  
          */
 275  
         public Domain getResourceDomain () {
 276  98
                 return this.resourceDomain;
 277  
         }
 278  
         
 279  
         /**
 280  
          * Selects a resource domain.
 281  
          * @param domain the selected resource domain.
 282  
          * @since 0.3.0
 283  
          */
 284  
         public void setResourceDomain (Domain domain) {
 285  19
                 final Domain oldValue = this.resourceDomain;
 286  19
                 this.resourceDomain = domain;
 287  19
                 firePropertyChange("resourceDomain", oldValue, this.resourceDomain);
 288  19
                 computeDecision();
 289  19
         }
 290  
         
 291  
         /**
 292  
          * Gets the currently selected action.
 293  
          * @return the selected action.
 294  
          * @since 0.3.0
 295  
          */
 296  
         public ActionBean getAction () {
 297  280
                 return this.action;
 298  
         }
 299  
         
 300  
         /**
 301  
          * Selects an action.
 302  
          * @param action the selected action.
 303  
          * @since 0.3.0
 304  
          */
 305  
         public void setAction (ActionBean action) {
 306  17
                 final Action oldValue = this.action;
 307  17
                 this.action = action;
 308  17
                 firePropertyChange("action", oldValue, this.action);
 309  17
                 computeDecision();
 310  17
         }
 311  
 
 312  
         /**
 313  
          * @since 0.3.0
 314  
          */
 315  
         private void createSelectedRoles () {
 316  9
                 this.selectedRoles = ObservableCollections.observableList(new ArrayList<Role>());
 317  9
                 this.selectedRolesListener = new ObservableListAdapter<Role>() {
 318  
                         @Override
 319  9
                         protected void listChanged (ObservableList<Role> list) {
 320  2
                                 computeDecision();
 321  2
                         }
 322  
                 };
 323  9
                 this.selectedRoles.addObservableListListener(this.selectedRolesListener);
 324  9
         }
 325  
         
 326  
         /**
 327  
          * Gets the list where selected roles get stored.
 328  
          * @return the list of selected roles for which access decisions are evaluated.
 329  
          * @since 0.3.0
 330  
          */
 331  
         public ObservableList<Role> getSelectedRoles () {
 332  39
                 return this.selectedRoles;
 333  
         }
 334  
 
 335  
         /**
 336  
          * Gets the current evaluation time for the decision tester.
 337  
          * @return the evaluation time for the decision.
 338  
          * @since 0.3.0
 339  
          */
 340  
         public TimeStamp getEvaluationTimeStamp () {
 341  1
                 return this.evaluationTimeStamp;
 342  
         }
 343  
 
 344  
         /**
 345  
          * Selects an evaluation time for the decision tester.
 346  
          * @param timeStamp an evaluation time.
 347  
          * @since 0.3.0
 348  
          */
 349  
         public void setEvaluationTimeStamp (TimeStamp timeStamp) {
 350  2
                 final TimeStamp old = this.evaluationTimeStamp;
 351  2
                 this.evaluationTimeStamp = timeStamp;
 352  2
                 firePropertyChange("evaluationTimeStamp", old, timeStamp);
 353  2
                 computeDecision();
 354  2
         }
 355  
 
 356  
         /**
 357  
          * Gets the current decision in text form.
 358  
          * @return the decision for the current configuration.
 359  
          * @since 0.3.0
 360  
          */
 361  
         public String getDecisionText () {
 362  1
                 return this.decision.name();
 363  
         }
 364  
         
 365  
         /**
 366  
          * Gets the current decision as a colour value.
 367  
          * @return a colour value.
 368  
          * @since 0.3.0
 369  
          */
 370  
         public Color getDecisionColor () {
 371  1
                 return this.decision.color;
 372  
         }
 373  
 
 374  
         /**
 375  
          * @since 0.3.0
 376  
          */
 377  
         private void computeDecision () {
 378  59
                 final Decision oldValue = this.decision;
 379  59
                 Decision newValue = Decision.INCOMPLETE;
 380  59
                 if (!getModel().isValid(null)) {
 381  0
                         newValue = Decision.INVALID;
 382  59
                 } else if (
 383  
                         getAuthority() != null &&
 384  
                         getResourceDomain() != null &&
 385  
                         getAction() != null
 386  
                 ) {
 387  38
                         final URI resourceUri = getResourceDomain().getIdentity();
 388  38
                         final String actionName = getAction().getName();
 389  38
                         final AccessDecision ad = getModel().getAccessDecision(
 390  
                                 this,
 391  
                                 resourceUri,
 392  
                                 actionName,
 393  
                                 this.parameterTableModel.getParameters(),
 394  
                                 this.evaluationTimeStamp
 395  
                         );
 396  38
                         newValue = ad.isAccessGranted() ? Decision.GRANTED : Decision.DENIED;
 397  
                 }
 398  59
                 this.decision = newValue;
 399  59
                 firePropertyChange("decisionText", oldValue.name(), newValue.name());
 400  59
                 firePropertyChange("decisionColor", oldValue.color, newValue.color);
 401  59
                 this.parameterTableModel.fireTableDataChanged();
 402  59
         }
 403  
         
 404  
         /**
 405  
          * @since 0.3.0
 406  
          */
 407  
         public ParameterTableModel getParameterTableModel () {
 408  0
                 return this.parameterTableModel;
 409  
         }
 410  
 
 411  
         /**
 412  
          * @since 0.3.0
 413  
          */
 414  
         private Object parseArgument (Class<?> type, String valueAsString) {
 415  
                 try {
 416  0
                         if (valueAsString != null) {
 417  0
                                 if (type == Boolean.class) {
 418  0
                                         return Boolean.valueOf(valueAsString);
 419  0
                                 } else if (type == Double.class) {
 420  0
                                         return Double.valueOf(valueAsString);
 421  0
                                 } else if (type == Integer.class) {
 422  0
                                         return Integer.valueOf(valueAsString);
 423  0
                                 } else if (type == String.class) {
 424  0
                                         return valueAsString;
 425  0
                                 } else if (type == Object.class) {
 426  0
                                         return new Object();
 427  0
                                 } else if (type == TimeConstant.class) {
 428  0
                                         return new TimeConstant(new PartialTime(new Date()));
 429  
                                 }
 430  
                         }
 431  0
                 } catch (Exception e) {
 432  
                         // Problem parsing String: eat exception and have default object returned in the
 433  
                         // following lines.
 434  0
                 }
 435  0
                 if (type == Boolean.class) {
 436  0
                         return Boolean.FALSE;
 437  0
                 } else if (type == Double.class) {
 438  0
                         return Double.valueOf(0.0);
 439  0
                 } else if (type == Integer.class) {
 440  0
                         return Integer.valueOf(0);
 441  0
                 } else if (type == String.class) {
 442  0
                         return "";
 443  0
                 } else if (type == Object.class) {
 444  0
                         return new Object();
 445  0
                 } else if (type == TimeConstant.class) {
 446  0
                         return new TimeConstant(new PartialTime(new Date()));
 447  
                 }
 448  0
                 throw new IllegalStateException("Unknown argument type: " + type);
 449  
         }
 450  
 
 451  
         /**
 452  
          * @since 0.3.0
 453  
          */
 454  
         @PropertyChange(bean = PolicyBean.class)
 455  
         public void policyChanged () {
 456  0
                 computeDecision();
 457  0
         }
 458  
 
 459  
         //---- Subject
 460  
         
 461  
         /**
 462  
          * @since 0.3.0
 463  
          */
 464  
         public Set<Role> getAssignedRoles (TimeStamp timeStamp, Set<Role> roles) {
 465  37
                 final Set<Role> result = new HashSet<Role>(getSelectedRoles());
 466  37
                 result.retainAll(roles);
 467  37
                 return result;
 468  
         }
 469  
 
 470  
         /**
 471  
          * @since 0.3.0
 472  
          */
 473  
         public Set<Subject> getIssuersOf (Role role) {
 474  4
                 final Set<Subject> result = new HashSet<Subject>();
 475  4
                 result.add(getAuthority());
 476  4
                 return result;
 477  
         }
 478  
 
 479  
         /**
 480  
          * @since 0.3.0
 481  
          */
 482  
         public TimePeriod getValidityOf (Role role, Subject issuer) {
 483  2
                 return TimePeriod.INFINITE;
 484  
         }
 485  
 
 486  
         /**
 487  
          * @since 0.3.0
 488  
          */
 489  
         public URI getIdentity () {
 490  2
                 return getAuthority().getIdentity();
 491  
         }
 492  
 
 493  
         /**
 494  
          * @since 0.3.0
 495  
          */
 496  
         public boolean identityMandatory () {
 497  0
                 return false;
 498  
         }
 499  
 
 500  
 }