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.client.wsrf;
36  
37  import java.util.Properties;
38  
39  import org.apache.xmlbeans.XmlObject;
40  import org.ogf.graap.wsag.api.AgreementOffer;
41  import org.ogf.graap.wsag.api.WsagConstants;
42  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
43  import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
44  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
45  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
46  import org.ogf.graap.wsag.api.security.ISecurityProperties;
47  import org.ogf.graap.wsag.client.api.AgreementClient;
48  import org.ogf.graap.wsag.client.api.AgreementFactoryClient;
49  import org.ogf.graap.wsag.client.api.NegotiationClient;
50  import org.ogf.graap.wsag.client.api.RemoteClient;
51  import org.ogf.graap.wsag.client.wsrf.impl.WsrfAgreementFactoryService;
52  import org.ogf.graap.wsag.client.wsrf.impl.WsrfAgreementRegistryService;
53  import org.ogf.graap.wsag.client.wsrf.impl.WsrfResourceClient;
54  import org.ogf.schemas.graap.wsAgreement.AgreementFactoryPropertiesDocument;
55  import org.ogf.schemas.graap.wsAgreement.AgreementFactoryPropertiesType;
56  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
57  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
58  import org.w3.x2005.x08.addressing.EndpointReferenceDocument;
59  import org.w3.x2005.x08.addressing.EndpointReferenceType;
60  import org.w3c.dom.Element;
61  
62  /**
63   * RemoteAgreementFactoryClientImpl
64   * 
65   * @author Oliver Waeldrich
66   * 
67   */
68  public class WsrfAgreementFactoryClient extends RemoteClient
69      implements AgreementFactoryClient
70  {
71  
72      //
73      // client for invoking WS-Agreement factory methods
74      //
75      private final WsrfAgreementFactoryService factoryClient;
76  
77      //
78      // client for invoking WSSRF service group methods
79      //
80      private final WsrfAgreementRegistryService registryClient;
81  
82      /**
83       * Creates a new agreement factory client for a given endpoint using the given security properties.
84       * 
85       * @param epr
86       *            the agreement factory endpoint
87       * 
88       * @param securityProperties
89       *            the client security properties
90       */
91      public WsrfAgreementFactoryClient( EndpointReferenceType epr, Properties properties,
92                                         ISecurityProperties securityProperties )
93      {
94          super( epr, properties, securityProperties );
95          try
96          {
97              //
98              // generate the endpoint references for the registry service
99              //
100             String factoryURI = epr.getAddress().getStringValue();
101             String registryURI =
102                 factoryURI.substring( 0, factoryURI.indexOf( WsagConstants.AGREEMENT_FACTORY_SERVICE_URI ) )
103                     + WsagConstants.AGREEMENT_REGISTRY_SERVICE_URI;
104 
105             EndpointReferenceDocument registryEPR = EndpointReferenceDocument.Factory.newInstance();
106             registryEPR.addNewEndpointReference().addNewAddress().setStringValue( registryURI );
107             registryEPR.getEndpointReference().addNewReferenceParameters().set( epr.getReferenceParameters() );
108 
109             //
110             // initialize the service clients
111             //
112             factoryClient = new WsrfAgreementFactoryService( epr, getProperties(), getSecurityProperties() );
113 
114             registryClient =
115                 new WsrfAgreementRegistryService( registryEPR.getEndpointReference(), getProperties(),
116                     getSecurityProperties() );
117         }
118         catch ( Exception e )
119         {
120             throw new RuntimeException( e );
121         }
122 
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public AgreementFactoryClient clone() throws CloneNotSupportedException
130     {
131         return new WsrfAgreementFactoryClient( getEndpoint(), getProperties(),
132             getSecurityProperties().clone() );
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     public RemoteClient getWebServiceClient()
139     {
140         return this;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     public EndpointReferenceType getEndpoint()
147     {
148         return getRemoteReference();
149     }
150 
151     /*
152      * (non-Javadoc)
153      * 
154      * @see org.ogf.graap.wsag.client.api.AgreementFactoryClient#getRemoteClient()
155      */
156     public RemoteClient getRemoteClient()
157     {
158         return this;
159     }
160 
161     /**
162      * Returns the templates supported by this factory.
163      * 
164      * {@inheritDoc}
165      */
166     public AgreementTemplateType[] getTemplates()
167         throws ResourceUnknownException, ResourceUnavailableException
168     {
169         return factoryClient.getTemplates();
170     }
171 
172     /**
173      * Creates a new agreement instance.
174      * 
175      * @param offer
176      *            the offer to create the agreement for
177      * 
178      * @return the created agreement
179      * 
180      * @throws AgreementFactoryException
181      *             the agreement offer was rejected by the factory
182      * 
183      * @throws ResourceUnavailableException
184      *             the requested agreement factory resource is unavailable
185      * 
186      * @throws ResourceUnknownException
187      *             the requested agreement factory resource is unknown
188      */
189     public AgreementClient createAgreement( AgreementOffer offer )
190         throws AgreementFactoryException, ResourceUnknownException, ResourceUnavailableException
191     {
192         return factoryClient.createAgreement( offer );
193     }
194 
195     /**
196      * Creates a new pending agreement instance.
197      * 
198      * {@inheritDoc}
199      */
200     public AgreementClient createPendingAgreement( AgreementOffer offer )
201         throws AgreementFactoryException, ResourceUnknownException, ResourceUnavailableException
202     {
203         return factoryClient.createPendingAgreement( offer );
204     }
205 
206     /**
207      * Creates a new pending agreement instance.
208      * 
209      * {@inheritDoc}
210      */
211     public AgreementClient createPendingAgreement( AgreementOffer offer, EndpointReferenceType acceptanceEPR )
212         throws AgreementFactoryException, ResourceUnknownException, ResourceUnavailableException
213     {
214         return factoryClient.createPendingAgreement( offer, acceptanceEPR );
215     }
216 
217     /**
218      * Initiates a Negotiation.
219      * 
220      * {@inheritDoc}
221      */
222     public NegotiationClient initiateNegotiation( NegotiationContextType context )
223         throws NegotiationFactoryException, ResourceUnknownException, ResourceUnavailableException
224     {
225         return factoryClient.initiateNegotiation( context );
226     }
227 
228     /**
229      * {@inheritDoc}
230      */
231     public String getResourceId() throws ResourceUnknownException, ResourceUnavailableException
232     {
233         return factoryClient.getResourceId();
234     }
235 
236     /**
237      * 
238      * @return a template with the given name and id, or null if no appropriate template was found.
239      * 
240      *         {@inheritDoc}
241      */
242     public AgreementTemplateType getTemplate( String name, String id )
243         throws ResourceUnknownException, ResourceUnavailableException
244     {
245         if ( name == null )
246         {
247             return null;
248         }
249 
250         id = ( id != null ) ? id : "";
251 
252         AgreementTemplateType[] templates = getTemplates();
253 
254         for ( int i = 0; i < templates.length; i++ )
255         {
256             if ( name.equalsIgnoreCase( templates[i].getName() ) )
257             {
258 
259                 String actualId =
260                     ( templates[i].getTemplateId() != null ) ? templates[i].getTemplateId() : "";
261 
262                 if ( id.equalsIgnoreCase( actualId ) )
263                 {
264                     return templates[i];
265                 }
266             }
267         }
268 
269         return null;
270     }
271 
272     /**
273      * Lists all agreements of this factory.
274      * 
275      * @return all agreement clients known by this factory
276      * 
277      * @throws ResourceUnavailableException
278      *             the requested agreement factory resource is unavailable
279      * 
280      * @throws ResourceUnknownException
281      *             the requested agreement factory resource is unknown
282      */
283     public AgreementClient[] listAgreements() throws ResourceUnknownException, ResourceUnavailableException
284     {
285         return registryClient.listAgreements();
286     }
287 
288     /**
289      * Turns trace on/off.
290      * 
291      * @param trace
292      *            turns trace on/off.
293      */
294     @Override
295     public void setTrace( boolean trace )
296     {
297         factoryClient.getWebServiceClient().setTrace( trace );
298         registryClient.getWebServiceClient().setTrace( trace );
299     }
300 
301     /**
302      * @param properties
303      *            The properties to set.
304      */
305     @Override
306     public void setProperties( Properties properties )
307     {
308         super.setProperties( properties );
309         factoryClient.getWebServiceClient().setProperties( properties );
310         registryClient.getWebServiceClient().setProperties( properties );
311     }
312 
313     /**
314      * @return Returns the agreement factory client.
315      */
316     public WsrfAgreementFactoryService getAgreementFactoryClient()
317     {
318         return factoryClient;
319     }
320 
321     /**
322      * @return Returns the agreement registry client.
323      */
324     public WsrfAgreementRegistryService getAgreementRegistryClient()
325     {
326         return registryClient;
327     }
328 
329     /**
330      * {@inheritDoc}
331      * 
332      * @see org.ogf.graap.wsag.client.api.AgreementFactoryClient#getResourceProperties()
333      */
334     public AgreementFactoryPropertiesType getResourceProperties()
335         throws ResourceUnknownException, ResourceUnavailableException
336     {
337         try
338         {
339             WsrfResourceClient wsrf = getAgreementFactoryClient().getWebServiceClient();
340             Element doc = wsrf.getResourcePropertyDocument();
341             AgreementFactoryPropertiesDocument propertiesDoc =
342                 (AgreementFactoryPropertiesDocument) XmlObject.Factory.parse( doc );
343             return propertiesDoc.getAgreementFactoryProperties();
344         }
345         catch ( Exception e )
346         {
347             throw new ResourceUnavailableException( "Failed to parse server response.", e );
348         }
349     }
350 
351 }