View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.wsrf;
36  
37  import java.util.Set;
38  
39  import javax.security.auth.x500.X500Principal;
40  import javax.xml.namespace.QName;
41  
42  import org.apache.muse.ws.addressing.soap.SoapFault;
43  import org.apache.muse.ws.resource.impl.SimpleWsResource;
44  import org.apache.xmlbeans.XmlObject;
45  import org.ogf.graap.wsag.server.engine.WsagEngine;
46  import org.ogf.graap.wsag4j.types.engine.ServerIdentityDocument;
47  import org.w3.x2005.x08.addressing.EndpointReferenceType;
48  import org.w3.x2005.x08.addressing.MetadataType;
49  import org.w3c.dom.Node;
50  
51  /**
52   * Abstract implementation of a WSRF resource. This class implements basic mechanisms to use XmlBeans EPRs
53   * with a WSRF resource. Moreover, the server identity is added automatically to EPR meta data when the WSRF
54   * resource is initialized. This is done for convenience when clients want to delegate trust to a service that
55   * is implemented as WSRF resource.
56   * 
57   * @author Oliver Waeldrich
58   * 
59   */
60  public abstract class AbstractWsResource extends SimpleWsResource
61  {
62  
63      private boolean initialized = false;
64  
65      /**
66       * 
67       * @return the QName of the implemented port type
68       */
69      public abstract QName getInterfaceName();
70  
71      /**
72       * Sets the EPR for this WS-Resource and adds the server identity to the meta data of the EPR.
73       * 
74       * @param epr
75       *            the endpoint reference of this resource
76       */
77      public void setEndpointReference( EndpointReferenceType epr )
78      {
79          setEndpointReference( XmlUtils.convertEndpointToMuseEPR( epr ) );
80      }
81  
82      /**
83       * {@inheritDoc}
84       * 
85       * @see org.apache.muse.ws.resource.impl.SimpleWsResource#initialize()
86       */
87      public void initialize() throws SoapFault
88      {
89          //
90          // first add the server identity to the EPR
91          //
92          EndpointReferenceType epr = XmlUtils.convertMuseEPRToEndpoint( getEndpointReference() );
93          addServerIdentityToEPR( epr );
94          setEndpointReference( XmlUtils.convertEndpointToMuseEPR( epr ) );
95  
96          //
97          // now initialize the WS resource
98          //
99          super.initialize();
100 
101         initialized = true;
102     }
103 
104     private void addServerIdentityToEPR( EndpointReferenceType epr )
105     {
106         if ( !epr.isSetMetadata() )
107         {
108             epr.addNewMetadata();
109         }
110 
111         MetadataType meta = epr.getMetadata();
112         XmlObject[] identities = meta.selectChildren( ServerIdentityDocument.type.getDocumentElementName() );
113         for ( int i = 0; i < identities.length; i++ )
114         {
115             meta.getDomNode().removeChild( identities[i].getDomNode() );
116         }
117 
118         Set<X500Principal> principals =
119             WsagEngine.getLoginContext().getSubject().getPrincipals( X500Principal.class );
120         if ( !principals.isEmpty() )
121         {
122             X500Principal principal = principals.iterator().next();
123 
124             ServerIdentityDocument id = ServerIdentityDocument.Factory.newInstance();
125             id.setServerIdentity( principal.getName() );
126             Node imported =
127                 meta.getDomNode().getOwnerDocument().importNode( id.getDomNode().getFirstChild(), true );
128             meta.getDomNode().appendChild( imported );
129         }
130     }
131 
132     /**
133      * Returns true only after the resource initialization process is complete. By default an additional
134      * schema validation is performed where the properties of the agreement are accessed.
135      * 
136      * @return the initialized
137      */
138     public boolean isInitialized()
139     {
140         return initialized;
141     }
142 }