package eu.stork.documentservice.utils;
/*
 * This work is Open Source and licensed by the European Commission under the
 * conditions of the European Public License v1.1 
 *  
 * (http://www.osor.eu/eupl/european-union-public-licence-eupl-v.1.1); 
 * 
 * any use of this file implies acceptance of the conditions of this license. 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 */


import org.apache.log4j.Logger;
import org.bouncycastle.util.encoders.UrlBase64;
import org.opensaml.xml.ConfigurationException;

import eu.stork.peps.auth.commons.IPersonalAttributeList;
import eu.stork.peps.auth.commons.PEPSUtil;

import eu.stork.peps.auth.commons.PersonalAttribute;
import eu.stork.peps.auth.commons.STORKAttrQueryRequest;

//import eu.stork.peps.auth.cpeps.AUCPEPS;
import eu.stork.peps.auth.engine.STORKSAMLEngine;
import eu.stork.peps.exceptions.STORKSAMLEngineException;

/**
 * This Action will be invoked by file transfer in order to validate saml
 * 
 * @author Advania
 * 
 * @version $Revision: $, $Date: $
 * 
 */
public final class GetDSSFileAction {
	/**
	 * Unique identifier. 
	 */
	private static final long serialVersionUID = 6233180247896844849L;
	/**
	 * Logger object.
	 */
	private static final Logger LOG = Logger.getLogger(GetDSSFileAction.class.getName());
//	public String SAMLRequest;
//	public String docId;
	private static final String SAML_INSTANCE = "VIDP";

	public static String processDocRequest(String samlRequest)
	{		
		String result = "";
	    //final Map<String, String> parameters = getHttpRequestParameters();	
		STORKAttrQueryRequest request;
		try {
			request = processDocRequest_(samlRequest);
			final IPersonalAttributeList pal = request.getPersonalAttributeList();
		    final PersonalAttribute sdoc = pal.get("docRequest");
		    if (sdoc != null)
		    {
		    	String docId = sdoc.getValue().get(0);//remove countries from beginning
		    	docId = docId.substring(docId.indexOf("/")+1);
				docId = docId.substring(docId.indexOf('/')+1);
				return docId.replace("/", "");
		    }
		} catch (final STORKSAMLEngineException e) {
			e.printStackTrace();
		}	   		
		return null;
	}
		
	private static STORKAttrQueryRequest processDocRequest_(String samlRequest) 
		throws STORKSAMLEngineException{

			// fetch the samlToken from the request
			final byte[] samlToken =  UrlBase64.decode(samlRequest);			
			//System.out.println("SAML Engine DokumentService init");
			try {
				Bootstrap.bootstrap();
			} catch (ConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			final STORKSAMLEngine engine = STORKSAMLEngine.getInstance(SAML_INSTANCE);
		      
			final STORKAttrQueryRequest attrData= engine.validateSTORKAttrQueryRequest(samlToken);
			//SAMLRequest = new String(samlToken);
			LOG.trace("Processing doc request done. SAML: " + new String(samlToken));
			return attrData;											
	  }	
}