MarshallUtilities.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.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.InputStream;
  24. import java.util.List;

  25. import org.herasaf.xacml.core.SyntaxException;
  26. import org.herasaf.xacml.core.WritingException;
  27. import org.herasaf.xacml.core.context.RequestMarshaller;
  28. import org.herasaf.xacml.core.context.ResponseMarshaller;
  29. import org.herasaf.xacml.core.context.impl.RequestType;
  30. import org.herasaf.xacml.core.context.impl.ResponseType;
  31. import org.herasaf.xacml.core.context.impl.ResultType;
  32. import org.herasaf.xacml.core.policy.Evaluatable;
  33. import org.herasaf.xacml.core.policy.PolicyMarshaller;
  34. import org.herasaf.xacml.core.simplePDP.initializers.InitializerExecutor;


  35. /**
  36.  * ResultCombining
  37.  *
  38.  * @author Bussu Giovanni (bussu@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class MarshallUtilities {

  43.     private static boolean runInitialized = false;
  44.     private static synchronized void _runInitializers(){
  45.         if(runInitialized==false){
  46.             InitializerExecutor.runInitializers(); //Inizializza gli unmarshaller
  47.             runInitialized = true;
  48.         }
  49.     }
  50.     public static void runInitializers(){
  51.         if(runInitialized==false){
  52.             _runInitializers();
  53.         }
  54.     }

  55.     public static List<ResultType> unmarshallResult(byte[] res) throws SyntaxException {

  56.         runInitializers();
  57.        
  58.         InputStream inputStream = null;
  59.         try{
  60.             inputStream = new ByteArrayInputStream(res);
  61.             ResponseType response = ResponseMarshaller.unmarshal(inputStream);
  62.             return response.getResults();
  63.         } finally {
  64.             if(inputStream != null) {
  65.                 try {
  66.                     inputStream.close();
  67.                 } catch(Exception e) {}
  68.             }
  69.         }
  70.     }

  71.     public static Evaluatable unmarshallPolicy(byte[] res) throws SyntaxException {

  72.         runInitializers();
  73.        
  74.         InputStream inputStream = null;
  75.         try{
  76.             inputStream = new ByteArrayInputStream(res);
  77.             Evaluatable response = PolicyMarshaller.unmarshal(inputStream);
  78.             return response;
  79.         } finally {
  80.             if(inputStream != null) {
  81.                 try {
  82.                     inputStream.close();
  83.                 } catch(Exception e) {}
  84.             }
  85.         }
  86.     }

  87.    
  88.     public static byte[] marshallRequest(XacmlRequest request) throws SecurityException {
  89.         return marshallRequest(request.getXacmlRequest());
  90.     }
  91.     public static byte[] marshallRequest(RequestType request) throws SecurityException {
  92.        
  93.         runInitializers();
  94.        
  95.         ByteArrayOutputStream baos = null;
  96.         try{

  97.             baos = new ByteArrayOutputStream();
  98.             RequestMarshaller.marshal(request, baos);

  99.             return baos.toByteArray();
  100.         } catch (WritingException e) {
  101.             throw new SecurityException(e);
  102.         } finally {
  103.             if(baos != null) {
  104.                 try{
  105.                     baos.flush();
  106.                     baos.close();
  107.                 } catch (Exception e) {}
  108.             }
  109.         }
  110.     }


  111. }