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.impl;
36  
37  import java.util.Calendar;
38  import java.util.GregorianCalendar;
39  
40  import javax.xml.namespace.QName;
41  
42  import org.apache.log4j.Logger;
43  import org.apache.muse.ws.addressing.soap.SoapFault;
44  import org.apache.muse.ws.resource.faults.ResourceUnavailableFault;
45  import org.apache.xmlbeans.XmlObject;
46  import org.apache.xmlbeans.XmlOptions;
47  import org.ogf.graap.wsag.api.Negotiation;
48  import org.ogf.graap.wsag.api.WsagConstants;
49  import org.ogf.graap.wsag.api.exceptions.NegotiationException;
50  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
51  import org.ogf.graap.wsag.api.exceptions.WSAgreementException;
52  import org.ogf.graap.wsag.wsrf.WSAG4JCapability;
53  import org.ogf.schemas.graap.wsAgreement.AgreementFactoryPropertiesDocument;
54  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
55  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextDocument;
56  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
57  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferType;
58  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.AdvertiseInputDocument;
59  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.AdvertiseInputType;
60  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.AdvertiseOutputDocument;
61  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.NegotiateInputDocument;
62  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.NegotiateInputType;
63  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.NegotiateOutputDocument;
64  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.NegotiationPropertiesDocument;
65  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.TerminateInputDocument;
66  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.TerminateResponseDocument;
67  import org.w3c.dom.Element;
68  
69  /**
70   * Default implementation of the WS-Agreement Negotiation port type.
71   * 
72   * @author hrasheed
73   * 
74   */
75  public class NegotiationCapability extends WSAG4JCapability
76  {
77  
78      private static final Logger LOG = Logger.getLogger( NegotiationCapability.class );
79  
80      /**
81       * @return the resource property QNames
82       */
83      public QName[] getPropertyNames()
84      {
85          return WsagConstants.WSAG_NEGOTIATION_PROPERTIES;
86      }
87  
88      private Negotiation getNegotiation()
89      {
90          return ( (NegotiationWsResource) getResource() ).getNegotiation();
91      }
92  
93      /**
94       * Returns the context of this negotiation instance.
95       * 
96       * @return the context of this negotiation instance
97       * 
98       * @throws ResourceUnavailableFault
99       *             the negotiation resource is not available
100      */
101     public Element getNegotiationContext() throws ResourceUnavailableFault
102     {
103 
104         try
105         {
106 
107             NegotiationContextType context = getNegotiation().getNegotiationContext();
108 
109             if ( context == null )
110             {
111                 context = NegotiationContextDocument.Factory.newInstance().addNewNegotiationContext();
112                 Calendar expire = new GregorianCalendar();
113                 expire.add( Calendar.DAY_OF_YEAR, 1 );
114                 context.setExpirationTime( expire );
115             }
116 
117             NegotiationPropertiesDocument propsDoc = NegotiationPropertiesDocument.Factory.newInstance();
118             propsDoc.addNewNegotiationProperties().setNegotiationContext( context );
119 
120             // NegotiationContextDocument _contextDoc = NegotiationContextDocument.Factory.newInstance();
121             // _contextDoc.setNegotiationContext(context);
122 
123             return toElement( propsDoc.getNegotiationProperties().getNegotiationContext() );
124         }
125         catch ( Exception e )
126         {
127             if ( LOG.isDebugEnabled() )
128             {
129                 LOG.debug( e );
130             }
131             throw new ResourceUnavailableFault( e );
132         }
133     }
134 
135     /**
136      * Returns the negotiable templates supported by this negotiation instance.
137      * 
138      * @return the negotiable templates
139      * 
140      * @throws ResourceUnavailableFault
141      *             the negotiation resource is not available
142      */
143     public Element[] getNegotiableTemplate() throws ResourceUnavailableFault
144     {
145         // if the resource router is not initialized we return an empty template array
146         if ( !getResource().getResourceManager().hasBeenInitialized()
147             || getResource().getResourceManager().hasBeenShutdown() )
148         {
149             return new Element[] {};
150         }
151 
152         try
153         {
154 
155             AgreementTemplateType[] templates = getNegotiation().getNegotiableTemplates();
156 
157             AgreementFactoryPropertiesDocument doc = AgreementFactoryPropertiesDocument.Factory.newInstance();
158             doc.addNewAgreementFactoryProperties().setTemplateArray( templates );
159 
160             // assure that we convert xml-elements
161             return toElementArray( doc.getAgreementFactoryProperties().getTemplateArray() );
162         }
163         catch ( ResourceUnavailableException e )
164         {
165             throw new ResourceUnavailableFault( e );
166         }
167         catch ( WSAgreementException e )
168         {
169             throw new ResourceUnavailableFault( e );
170         }
171         catch ( Exception e )
172         {
173             LOG.error( "Luke, I am your father.", e );
174 
175             String message = "Illegal state in GetTemplates method. Error: " + e.getMessage();
176             LOG.error( message );
177 
178             throw new ResourceUnavailableFault( message, e );
179         }
180     }
181 
182     /**
183      * Returns a history of exchanged negotiation offers.
184      * 
185      * @return the negotiation offers history
186      * 
187      * @throws ResourceUnavailableFault
188      *             the negotiation resource is not available
189      * 
190      * @todo not implemented yet
191      */
192     public Element[] getNegotiationOffer() throws ResourceUnavailableFault
193     {
194 
195         try
196         {
197 
198             NegotiationOfferType[] currentOffers = getNegotiation().getNegotiationOffers();
199 
200             if ( currentOffers == null )
201             {
202                 currentOffers = new NegotiationOfferType[0];
203             }
204 
205             /*
206              * NegotiationOfferDocument[] negotiated_offers = new
207              * NegotiationOfferDocument[current_offers.length]; for (int i = 0; i < negotiated_offers.length;
208              * i++) { negotiated_offers[i] = NegotiationOfferDocument.Factory.newInstance();
209              * negotiated_offers[i].setNegotiationOffer(current_offers[i]); }
210              */
211 
212             NegotiationPropertiesDocument propsDoc = NegotiationPropertiesDocument.Factory.newInstance();
213             propsDoc.addNewNegotiationProperties().setNegotiationOfferArray( currentOffers );
214 
215             return toElementArray( propsDoc.getNegotiationProperties().getNegotiationOfferArray() );
216         }
217         catch ( Exception e )
218         {
219             if ( LOG.isDebugEnabled() )
220             {
221                 LOG.debug( e );
222             }
223             throw new ResourceUnavailableFault( e );
224         }
225     }
226 
227     /**
228      * Negotiates a set of offers and creates c set of counter offers.
229      * 
230      * @param in
231      *            the input document
232      * 
233      * @return the response document
234      * 
235      * @throws SoapFault
236      *             generic SOAP fault
237      */
238     public Element negotiate( NegotiateInputDocument in ) throws SoapFault
239     {
240 
241         try
242         {
243 
244             NegotiateInputType input = in.getNegotiateInput();
245             NegotiationOfferType[] quotes = input.getNegotiationOfferArray();
246             XmlObject[] ncExtensions = new XmlObject[0];
247 
248             NegotiationOfferType[] negotiatedOffers = getNegotiation().negotiate( quotes, ncExtensions );
249 
250             if ( negotiatedOffers == null )
251             {
252                 negotiatedOffers = new NegotiationOfferType[0];
253             }
254 
255             NegotiateOutputDocument output = NegotiateOutputDocument.Factory.newInstance();
256             output.addNewNegotiateOutput().setNegotiationCounterOfferArray( negotiatedOffers );
257 
258             return toElement( output );
259         }
260         catch ( NegotiationException e )
261         {
262             if ( LOG.isDebugEnabled() )
263             {
264                 LOG.debug( e );
265             }
266             throw new SoapFault( e );
267         }
268         catch ( Exception e )
269         {
270             if ( LOG.isDebugEnabled() )
271             {
272                 LOG.debug( "failed to process negotiation request" );
273                 LOG.debug( in.xmlText( new XmlOptions().setSavePrettyPrint() ) );
274                 LOG.debug( e );
275             }
276             throw new ResourceUnavailableFault( e );
277         }
278     }
279 
280     /**
281      * Advertises a set of negotiation offers to a negotiation participator.
282      * 
283      * @param in
284      *            the input document
285      * 
286      * @return the response document
287      * 
288      * @throws SoapFault
289      *             generic SOAP fault
290      */
291     public Element advertise( AdvertiseInputDocument in ) throws SoapFault
292     {
293 
294         try
295         {
296 
297             AdvertiseInputType input = in.getAdvertiseInput();
298 
299             NegotiationOfferType[] quotes = input.getNegotiationOfferArray();
300             XmlObject[] ncExtensions = new XmlObject[0];
301 
302             getNegotiation().advertise( quotes, ncExtensions );
303 
304             AdvertiseOutputDocument output = AdvertiseOutputDocument.Factory.newInstance();
305             output.addNewAdvertiseOutput();
306 
307             return toElement( output );
308         }
309         catch ( NegotiationException e )
310         {
311             if ( LOG.isDebugEnabled() )
312             {
313                 LOG.debug( e );
314             }
315             throw new SoapFault( e );
316         }
317         catch ( Exception e )
318         {
319             if ( LOG.isDebugEnabled() )
320             {
321                 LOG.debug( e );
322             }
323             throw new ResourceUnavailableFault( e );
324         }
325     }
326 
327     /**
328      * Terminates a negotiation process.
329      * 
330      * @param in
331      *            the termination reason
332      * 
333      * @return the termination response document
334      * 
335      * @throws SoapFault
336      *             generic SOAP fault
337      */
338     public Element terminate( TerminateInputDocument in ) throws SoapFault
339     {
340 
341         try
342         {
343 
344             getNegotiation().terminate();
345 
346             TerminateResponseDocument output = TerminateResponseDocument.Factory.newInstance();
347             output.addNewTerminateResponse();
348 
349             return toElement( output );
350         }
351         catch ( Exception e )
352         {
353             throw new SoapFault( e );
354         }
355     }
356 }