[jboss-cvs] Picketlink SVN: r624 - federation/trunk/picketlink-fed-api/src/main/java/org/picketlink/identity/federation/api/soap.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Jan 3 16:12:07 EST 2011


Author: anil.saldhana at jboss.com
Date: 2011-01-03 16:12:06 -0500 (Mon, 03 Jan 2011)
New Revision: 624

Modified:
   federation/trunk/picketlink-fed-api/src/main/java/org/picketlink/identity/federation/api/soap/SOAPSAMLXACML.java
Log:
PLFED-142: 

Modified: federation/trunk/picketlink-fed-api/src/main/java/org/picketlink/identity/federation/api/soap/SOAPSAMLXACML.java
===================================================================
--- federation/trunk/picketlink-fed-api/src/main/java/org/picketlink/identity/federation/api/soap/SOAPSAMLXACML.java	2011-01-03 21:11:19 UTC (rev 623)
+++ federation/trunk/picketlink-fed-api/src/main/java/org/picketlink/identity/federation/api/soap/SOAPSAMLXACML.java	2011-01-03 21:12:06 UTC (rev 624)
@@ -21,28 +21,42 @@
  */
 package org.picketlink.identity.federation.api.soap;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.URL;
-import java.net.URLConnection;
 
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.bind.Unmarshaller;
+import javax.xml.soap.MessageFactory;
+import javax.xml.soap.SOAPConnection;
+import javax.xml.soap.SOAPConnectionFactory;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPMessage;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamWriter;
 
+import org.jboss.security.xacml.core.model.context.DecisionType;
+import org.jboss.security.xacml.core.model.context.RequestType;
+import org.jboss.security.xacml.core.model.context.ResultType;
 import org.picketlink.identity.federation.core.exceptions.ConfigurationException;
+import org.picketlink.identity.federation.core.exceptions.ParsingException;
 import org.picketlink.identity.federation.core.exceptions.ProcessingException;
-import org.picketlink.identity.federation.core.factories.SOAPFactory; 
-import org.picketlink.identity.federation.core.saml.v2.util.SOAPSAMLXACMLUtil;
+import org.picketlink.identity.federation.core.parsers.saml.SAMLResponseParser;
+import org.picketlink.identity.federation.core.parsers.util.StaxParserUtil;
+import org.picketlink.identity.federation.core.saml.v2.common.IDGenerator;
+import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLConstants;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
 import org.picketlink.identity.federation.core.saml.v2.util.XMLTimeUtil;
+import org.picketlink.identity.federation.core.saml.v2.writers.SAMLRequestWriter;
+import org.picketlink.identity.federation.core.util.StaxUtil;
+import org.picketlink.identity.federation.newmodel.saml.v2.assertion.AssertionType;
 import org.picketlink.identity.federation.newmodel.saml.v2.assertion.NameIDType;
+import org.picketlink.identity.federation.newmodel.saml.v2.profiles.xacml.assertion.XACMLAuthzDecisionStatementType;
 import org.picketlink.identity.federation.newmodel.saml.v2.profiles.xacml.protocol.XACMLAuthzDecisionQueryType;
-import org.picketlink.identity.federation.org.xmlsoap.schemas.soap.envelope.Body;
-import org.picketlink.identity.federation.org.xmlsoap.schemas.soap.envelope.Envelope;
-import org.picketlink.identity.federation.org.xmlsoap.schemas.soap.envelope.Fault; 
-import org.jboss.security.xacml.core.model.context.DecisionType;
-import org.jboss.security.xacml.core.model.context.RequestType;
-import org.jboss.security.xacml.core.model.context.ResultType;
+import org.picketlink.identity.federation.newmodel.saml.v2.protocol.ResponseType;
+import org.picketlink.identity.federation.org.xmlsoap.schemas.soap.envelope.Fault;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * Class that deals with sending XACML
@@ -60,34 +74,58 @@
     * @param xacmlRequest
     * @return
     * @throws ProcessingException
+    * @throws SOAPException 
+    * @throws ParsingException 
     */
-   public Result send(String endpoint, String issuer, RequestType xacmlRequest) throws ProcessingException
-   {
-      throw new RuntimeException( "NYI" );/*
+   public Result send(String endpoint, String issuer, RequestType xacmlRequest) throws ProcessingException, SOAPException, ParsingException
+   { 
       try
       {
-         XACMLAuthzDecisionQueryType queryType = new XACMLAuthzDecisionQueryType();
+         String id = IDGenerator.create( "ID_" );
+         
+         XACMLAuthzDecisionQueryType queryType = new XACMLAuthzDecisionQueryType( id, JBossSAMLConstants.VERSION_2_0.get(),
+               XMLTimeUtil.getIssueInstant() );
+         
          queryType.setRequest(xacmlRequest);
          
-         //Create Issue Instant
-         queryType.setIssueInstant(XMLTimeUtil.getIssueInstant());
-         
          //Create Issuer
          NameIDType nameIDType = new NameIDType();
          nameIDType.setValue(issuer);
          queryType.setIssuer(nameIDType);
           
          
-         Envelope envelope = createEnvelope(jaxbQueryType);
          
+         
+         MessageFactory messageFactory = MessageFactory.newInstance();
+         
+         SOAPMessage soapMessage = messageFactory.createMessage();
+         
+         ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         XMLStreamWriter xmlStreamWriter = StaxUtil.getXMLStreamWriter(baos);
+
+         SAMLRequestWriter samlRequestWriter = new SAMLRequestWriter( xmlStreamWriter );
+         samlRequestWriter.write( queryType );
+         
+         Document reqDocument = DocumentUtil.getDocument( new ByteArrayInputStream( baos.toByteArray() ));
+         soapMessage.getSOAPBody().addDocument(reqDocument);
+         
+         
+         /*Envelope envelope = createEnvelope(jaxbQueryType);
+         
          JAXBElement<?> soapRequest = SOAPFactory.getObjectFactory().createEnvelope(envelope);
          
          Marshaller marshaller = SOAPSAMLXACMLUtil.getMarshaller();
          Unmarshaller unmarshaller = SOAPSAMLXACMLUtil.getUnmarshaller();
+         */
          
+         SOAPConnectionFactory connectFactory = SOAPConnectionFactory.newInstance();
+         SOAPConnection connection = connectFactory.createConnection();
          //Send it across the wire
          URL url = new URL(endpoint);
-         URLConnection conn = url.openConnection();
+         
+         SOAPMessage response = connection.call(soapMessage, url);
+         
+         /*URLConnection conn = url.openConnection();
          conn.setDoOutput(true); 
          marshaller.marshal(soapRequest, conn.getOutputStream());
          
@@ -100,20 +138,38 @@
          {
             Fault fault = (Fault) response;
             return new Result(null,fault); 
+         }*/
+         
+         NodeList nl = response.getSOAPBody().getChildNodes();
+         Node node = null;
+         
+         int length = nl != null ? nl.getLength() : 0;
+         for( int i = 0; i < length; i++ )
+         {
+            Node n = nl.item(i); 
+            String localName = n.getLocalName();
+            if( localName.contains( JBossSAMLConstants.RESPONSE.get() ))
+            {
+               node = n;
+               break;
+            }
          }
+         if( node == null )
+            throw new RuntimeException( "Did not find Response node" );
          
-         ResponseType responseType = (ResponseType) response;
-         AssertionType at = (AssertionType) responseType.getAssertionOrEncryptedAssertion().get(0);
-         XACMLAuthzDecisionStatementType xst = (XACMLAuthzDecisionStatementType) at.getStatementOrAuthnStatementOrAuthzDecisionStatement().get(0);
+
+         XMLEventReader xmlEventReader = StaxParserUtil.getXMLEventReader( DocumentUtil.getNodeAsStream( node ));
+         SAMLResponseParser samlResponseParser = new SAMLResponseParser();
+         ResponseType responseType = (ResponseType) samlResponseParser.parse(xmlEventReader);
+         
+         //ResponseType responseType = (ResponseType) response;
+         AssertionType at = (AssertionType) responseType.getAssertions().get(0).getAssertion();
+         XACMLAuthzDecisionStatementType xst = (XACMLAuthzDecisionStatementType) at.getStatements().iterator().next();
          ResultType rt = xst.getResponse().getResult().get(0);
          DecisionType dt = rt.getDecision(); 
          
          return new Result(dt,null);
-      }
-      catch (JAXBException e)
-      {
-         throw new ProcessingException(e); 
-      }
+      } 
       catch (IOException e)
       {
          throw new ProcessingException(e);
@@ -121,9 +177,9 @@
       catch (ConfigurationException e)
       {
          throw new ProcessingException(e);
-      }*/ 
+      }
    }
-   
+   /*
    private Envelope createEnvelope(JAXBElement<?> jaxbElement)
    {
       Envelope envelope = SOAPFactory.getObjectFactory().createEnvelope();
@@ -131,7 +187,7 @@
       body.getAny().add(jaxbElement); 
       envelope.setBody(body);
       return envelope;
-   } 
+   } */
    
    public static class Result
    {



More information about the jboss-cvs-commits mailing list