Coverage Report - org.openpermis.xacml.io.v2.BasicXacmlWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicXacmlWriter
95%
39/41
100%
16/16
2.667
 
 1  
 /*
 2  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 3  
  * All rights reserved.
 4  
  * 
 5  
  * Licensed under the Open Permis License which accompanies this distribution, 
 6  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 7  
  */
 8  
 package org.openpermis.xacml.io.v2;
 9  
 
 10  
 import java.io.IOException;
 11  
 import java.io.StringWriter;
 12  
 import java.util.Set;
 13  
 
 14  
 
 15  
 import org.dom4j.Document;
 16  
 import org.dom4j.DocumentHelper;
 17  
 import org.dom4j.Element;
 18  
 import org.dom4j.io.OutputFormat;
 19  
 import org.dom4j.io.XMLWriter;
 20  
 
 21  
 import org.openpermis.policy.AccessDecision;
 22  
 import org.openpermis.xacml.io.XacmlStatus;
 23  
 import org.openpermis.xacml.io.XacmlWriter;
 24  
 
 25  
 
 26  
 /**
 27  
  * Basic implementation of a xacml writer.
 28  
  * @since 0.4.0
 29  
  */
 30  
 public class BasicXacmlWriter implements XacmlWriter {
 31  
 
 32  
         //---- Static
 33  
         
 34  
         /**
 35  
          * @since 0.4.0
 36  
          */
 37  
         public static final String XACML_CONTEXT_NAMESPACE = 
 38  
                 "urn:oasis:names:tc:xacml:2.0:context:schema:os";
 39  
         
 40  
         /**
 41  
          * @since 0.4.0
 42  
          */
 43  
         public static final String XACML_POLICY_NAMESPACE = 
 44  
                 "urn:oasis:names:tc:xacml:2.0:policy:schema:os";
 45  
         
 46  
         //---- Constructors
 47  
         
 48  
         /**
 49  
          * Creates a new basic response writer.
 50  
          * @since 0.4.0
 51  
          */
 52  8
         public BasicXacmlWriter () {
 53  
                 // Nop.
 54  8
         }
 55  
         
 56  
         //---- Methods
 57  
 
 58  
         /**
 59  
          * Formats the document.
 60  
          * @param document the document to format.
 61  
          * @return a formatted {@link String}. 
 62  
          * @since 0.4.0
 63  
          */
 64  
         private String formatXml (Document document) throws IOException {
 65  8
                 final OutputFormat format = OutputFormat.createPrettyPrint();
 66  8
                 final StringWriter out = new StringWriter();
 67  8
                 final XMLWriter writer = new XMLWriter(out, format);
 68  8
                 writer.write(document);
 69  8
                 return out.getBuffer().toString();
 70  
         }
 71  
         
 72  
         /**
 73  
          * Adds the input decision to the parent.
 74  
          * @param parent the parent element.
 75  
          * @param inputDecision an {@link AccessDecision}.
 76  
          * @since 0.4.0
 77  
          */
 78  
         private void addDecision (Element parent, AccessDecision inputDecision) {
 79  8
                 final Element decision = parent.addElement("Decision");
 80  8
                 if (inputDecision == null) {
 81  2
                         decision.addText("Indeterminate");
 82  6
                 } else if (inputDecision.isAccessGranted()) {
 83  5
                         decision.addText("Permit");
 84  
                 } else {
 85  1
                         decision.addText("Deny");
 86  
                 }
 87  8
         }
 88  
         
 89  
         /**
 90  
          * Adds the obligations of the input decision to the parent.
 91  
          * @param parent the parent element.
 92  
          * @param inputDecision an {@link AccessDecision}.
 93  
          * @note All obligations have to be returned as one list, there is no way to group the 
 94  
          * obligations into sets in a xacml response (This is permis specific).
 95  
          * @since 0.4.0
 96  
          */
 97  
         private void addObligations (Element parent, AccessDecision inputDecision) {
 98  8
                 if (inputDecision != null && !inputDecision.getObligations().isEmpty()) {
 99  1
                         final Element obligations = parent.addElement("Obligations");
 100  1
                         obligations.addNamespace("xmlns", XACML_POLICY_NAMESPACE);
 101  
                         
 102  1
                         for (Set<String> obligationSet : inputDecision.getObligations()) {
 103  1
                                 for (String obligationId : obligationSet) {
 104  2
                                         final Element obligation = obligations.addElement("Obligation");
 105  2
                                         obligation.addAttribute("ObligationId", obligationId);
 106  2
                                         obligation.addAttribute("FulfillOn", "Permit");
 107  2
                                 }
 108  
                         }
 109  
                 }        
 110  8
         }
 111  
         
 112  
         /**
 113  
          * Adds the status to the parent.
 114  
          * @param parent the parent element.
 115  
          * @param inputStatus a {@link XacmlStatus}.
 116  
          * @since 0.4.0
 117  
          */
 118  
         private void addStatus (Element parent, XacmlStatus inputStatus) {
 119  8
                 if (inputStatus != null) {
 120  4
                         final Element status = parent.addElement("Status");
 121  4
                         status.addElement("StatusCode").addAttribute("Value", inputStatus.getCode());
 122  4
                         if (inputStatus.getMessage() != null) {
 123  2
                                 status.addElement("StatusMessage").addText(inputStatus.getMessage());
 124  
                         }
 125  
                 }
 126  8
         }
 127  
         
 128  
         //---- ResponseWriter
 129  
         
 130  
         /**
 131  
          * @since 0.4.0
 132  
          */
 133  
         public String writeResponse (AccessDecision inputDecision, XacmlStatus inputStatus) {
 134  8
                 final Document document = DocumentHelper.createDocument();
 135  8
                 document.setXMLEncoding("UTF-8");
 136  
                 
 137  8
                 final Element response = document.addElement("Response");
 138  8
                 response.addNamespace("xmlns", XACML_CONTEXT_NAMESPACE);
 139  
                 
 140  8
                 final Element result = response.addElement("Result");
 141  
                 
 142  8
                 addDecision(result, inputDecision);
 143  8
                 addStatus(result, inputStatus);
 144  8
                 addObligations(result, inputDecision);
 145  
                 
 146  
                 try {
 147  8
                         return formatXml(document);
 148  0
                 } catch (IOException e) {
 149  0
                         return document.asXML();
 150  
                 }
 151  
         }
 152  
 
 153  
 }