ResultUtilities.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.io.ByteArrayOutputStream;
  22. import java.util.List;

  23. import org.herasaf.xacml.core.context.impl.DecisionType;
  24. import org.herasaf.xacml.core.context.impl.ResultType;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.xml.JaxbUtils;
  27. import org.slf4j.Logger;


  28. /**
  29.  * ResultCombining
  30.  *
  31.  * @author Bussu Giovanni (bussu@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class ResultUtilities {


  36.     public static List<ResultType> evaluate(XacmlRequest xacmlRequest, Logger log, String policyKey, PolicyDecisionPoint pdp) throws UtilsException{
  37.         List<ResultType> results = null;
  38.         try {    
  39.             log.debug("evaluete XACMLRequest (idPolicy:"+policyKey+") ... ");
  40.             results = pdp.evaluate(xacmlRequest);
  41.             StringBuilder bf = new StringBuilder();
  42.             bf.append("----XACML Results (idPolicy:"+policyKey+") begin ---\n");
  43.             for(ResultType result: results) {
  44.                 bf.append("Decision: "+result.getDecision().toString()+"\n");
  45.             }
  46.             bf.append("----XACML Results end ---");
  47.             log.debug(bf.toString());
  48.             return results;
  49.         } catch(Exception e) {
  50.             throw new UtilsException(e.getMessage(),e);
  51.         }
  52.     }
  53.    
  54.     public static String toRawString(List<ResultType> results) throws UtilsException{
  55.         try{
  56.             StringBuilder bfPolicy = new StringBuilder();
  57.             for (int i = 0; i < results.size(); i++) {
  58.                 ResultType res = results.get(i);
  59.                 if(bfPolicy.length()>0){
  60.                     bfPolicy.append("\n");
  61.                 }
  62.                 bfPolicy.append("Result["+(i+1)+"]: ");
  63.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  64.                 JaxbUtils.objToXml(bout, res.getClass(), res);
  65.                 bout.flush();
  66.                 bout.close();
  67.                 bfPolicy.append(bout.toString());
  68.             }
  69.             return bfPolicy.toString();
  70.         }catch(Throwable e){
  71.             throw new UtilsException("Serializzazione risposta non riuscita",e);
  72.         }
  73.     }
  74.    
  75.     public static String toString(List<ResultType> results, DecisionType decision) throws UtilsException{
  76.         try{
  77.             StringBuilder bf = new StringBuilder();
  78.             for (int i = 0; i < results.size(); i++) {
  79.                 ResultType res = results.get(i);
  80.                
  81.                 boolean check = false;
  82.                 if(DecisionType.DENY.equals(decision)) {
  83.                     check = DecisionType.DENY.equals(res.getDecision());
  84.                 }
  85.                 else{
  86.                     check = DecisionType.DENY.equals(res.getDecision()) || DecisionType.INDETERMINATE.equals(res.getDecision()) || DecisionType.NOT_APPLICABLE.equals(res.getDecision());
  87.                 }
  88.                
  89.                 if(check) {
  90.                     if(bf.length()>0){
  91.                         bf.append(" - ");
  92.                     }
  93.                     if(results.size()>1){
  94.                         bf.append("(");
  95.                     }
  96.                     bf.append("result-"+(i+1)+" "+res.getDecision().name());
  97.                     if( res.getStatus() != null ){
  98.                         if(res.getStatus().getStatusCode() != null){
  99.                             bf.append(" code:").append(res.getStatus().getStatusCode().getValue());
  100.                         }
  101.                         if(res.getStatus().getStatusMessage() != null){
  102.                             bf.append(" ").append(res.getStatus().getStatusMessage());
  103.                         }
  104.                     }
  105.                     if(results.size()>1){
  106.                         bf.append(")");
  107.                     }
  108.                    
  109.                 }
  110.             }
  111.             return bf.toString();
  112.         }catch(Throwable e){
  113.             throw new UtilsException("Serializzazione risposta non riuscita",e);
  114.         }
  115.     }

  116. }