XacmlRequest.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.xacml;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.herasaf.xacml.core.context.impl.ActionType;
  24. import org.herasaf.xacml.core.context.impl.AttributeType;
  25. import org.herasaf.xacml.core.context.impl.AttributeValueType;
  26. import org.herasaf.xacml.core.context.impl.EnvironmentType;
  27. import org.herasaf.xacml.core.context.impl.ObjectFactory;
  28. import org.herasaf.xacml.core.context.impl.RequestType;
  29. import org.herasaf.xacml.core.context.impl.ResourceType;
  30. import org.herasaf.xacml.core.context.impl.SubjectType;
  31. import org.herasaf.xacml.core.dataTypeAttribute.impl.StringDataTypeAttribute;


  32. /**
  33.  * XacmlRequest
  34.  *
  35.  * @author Andrea Poli (poli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class XacmlRequest {

  40.     private ObjectFactory factory;
  41.     private RequestType xacmlRequest;
  42.    
  43.     public RequestType getXacmlRequest() {
  44.         return this.xacmlRequest;
  45.     }

  46.     public XacmlRequest(){
  47.         this.factory = new ObjectFactory();
  48.         this.xacmlRequest = this.factory.createRequestType();
  49.         MarshallUtilities.runInitializers();
  50.     }
  51.    
  52.     public void createAction(){
  53.         if(this.xacmlRequest.getAction()==null){
  54.             ActionType action =  this.factory.createActionType();  
  55.             this.xacmlRequest.setAction(action);
  56.         }
  57.     }
  58.     public void addAction(String azione){
  59.         this.addActionAttribute("urn:oasis:names:tc:xacml:1.0:action:action-id", azione);
  60.     }
  61.     public void addActionAttribute(String attributeName, String attributeValue){
  62.         this.addActionAttribute(createAttribute(attributeName, attributeValue));
  63.     }
  64.     public void addActionAttribute(String attributeName, List<String> attributeValues){
  65.         this.addActionAttribute(createAttribute(attributeName, attributeValues));
  66.     }
  67.     public void addActionAttribute(AttributeType attribute){
  68.         this.createAction();
  69.         this.xacmlRequest.getAction().getAttributes().add(attribute);
  70.     }
  71.    
  72.    
  73.     public void addSubject(String subject){
  74.         this.addSubjectAttribute("urn:oasis:names:tc:xacml:1.0:subject:subject-id", subject);
  75.     }
  76.     public void createSubject(){
  77.         if(this.xacmlRequest.getSubjects().isEmpty()){
  78.             SubjectType subject = this.factory.createSubjectType();
  79.             this.xacmlRequest.getSubjects().add(subject);
  80.         }
  81.     }
  82.     public void addSubjectAttribute(String attributeName, String attributeValue){
  83.         this.addSubjectAttribute(createAttribute(attributeName, attributeValue));
  84.     }
  85.     public void addSubjectAttribute(String attributeName, List<String> attributeValues){
  86.         this.addSubjectAttribute(createAttribute(attributeName, attributeValues));
  87.     }
  88.     public void addSubjectAttribute(AttributeType attribute){
  89.         createSubject();
  90.         this.xacmlRequest.getSubjects().get(0).getAttributes().add(attribute);
  91.     }
  92.    
  93.    
  94.     public void createEnvironment(){
  95.         if(this.xacmlRequest.getEnvironment()==null){
  96.             EnvironmentType action = this.factory.createEnvironmentType();  
  97.             this.xacmlRequest.setEnvironment(action);
  98.         }
  99.     }
  100.     public void addEnvironmentAttribute(String attributeName, String attributeValue){
  101.         this.addEnvironmentAttribute(createAttribute(attributeName, attributeValue));
  102.     }
  103.     public void addEnvironmentAttribute(String attributeName, List<String> attributeValues){
  104.         this.addEnvironmentAttribute(createAttribute(attributeName, attributeValues));
  105.     }
  106.     public void addEnvironmentAttribute(AttributeType attribute){
  107.         createEnvironment();
  108.         this.xacmlRequest.getEnvironment().getAttributes().add(attribute);
  109.     }
  110.    
  111.    
  112.    
  113.     public void createResource(){
  114.         if(this.xacmlRequest.getResources().isEmpty()){
  115.             ResourceType resource = this.factory.createResourceType();
  116.             this.xacmlRequest.getResources().add(resource);
  117.         }
  118.     }
  119.     public void addResourceAttribute(String attributeName, String attributeValue){
  120.         this.addResourceAttribute(createAttribute(attributeName, attributeValue));
  121.     }
  122.     public void addResourceAttribute(String attributeName, List<String> attributeValues){
  123.         this.addResourceAttribute(createAttribute(attributeName, attributeValues));
  124.     }
  125.     public void addResourceAttribute(AttributeType attribute){
  126.         createResource();
  127.         this.xacmlRequest.getResources().get(0).getAttributes().add(attribute);
  128.     }
  129.    
  130.    
  131.    
  132.     private AttributeType createAttribute(String name, List<String> values) {

  133.         AttributeType attribute = this.factory.createAttributeType();
  134.        
  135.         for(String value: values) {
  136.             AttributeValueType value1 = new AttributeValueType();
  137.             value1.getContent().add(value);
  138.             attribute.getAttributeValues().add(value1);
  139.         }

  140.         attribute.setAttributeId(name);
  141.         attribute.setDataType(new StringDataTypeAttribute());      
  142.         return attribute;
  143.        
  144.     }

  145.     private AttributeType createAttribute(String name, String value) {

  146.         List<String> lst = new ArrayList<>();
  147.         lst.add(value);
  148.         return createAttribute(name, lst);
  149.        
  150.     }
  151. }