View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, 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.muse.ws.resource.faults.ResourceUnknownFault;
46  import org.apache.ws.security.WSSecurityException;
47  import org.ogf.graap.wsag.api.Agreement;
48  import org.ogf.graap.wsag.api.WsagConstants;
49  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
50  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
51  import org.ogf.graap.wsag.api.exceptions.WSAgreementException;
52  import org.ogf.graap.wsag.server.api.WsagSession;
53  import org.ogf.graap.wsag.server.engine.WsagEngine;
54  import org.ogf.graap.wsag.wsrf.WSAG4JCapability;
55  import org.ogf.schemas.graap.wsAgreement.AgreementContextType;
56  import org.ogf.schemas.graap.wsAgreement.AgreementIdDocument;
57  import org.ogf.schemas.graap.wsAgreement.AgreementRoleType;
58  import org.ogf.schemas.graap.wsAgreement.AgreementStateDocument;
59  import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateDocument;
60  import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateType;
61  import org.ogf.schemas.graap.wsAgreement.ServiceTermStateDocument;
62  import org.ogf.schemas.graap.wsAgreement.ServiceTermStateType;
63  import org.ogf.schemas.graap.wsAgreement.TemplateDocument;
64  import org.ogf.schemas.graap.wsAgreement.TermTreeType;
65  import org.ogf.schemas.graap.wsAgreement.TerminateInputDocument;
66  import org.ogf.schemas.graap.wsAgreement.TerminateOutputType;
67  import org.ogf.schemas.graap.wsAgreement.TerminateResponseDocument;
68  import org.w3c.dom.Element;
69  
70  /**
71   * Default implementation of the WS-Agreement port type.
72   * 
73   * @author Oliver Waeldrich
74   * 
75   */
76  public class AgreementCapability extends WSAG4JCapability
77  {
78  
79      @SuppressWarnings( "unused" )
80      private static Logger log = Logger.getLogger( AgreementCapability.class );
81  
82      private Agreement getAgreement() throws WSSecurityException
83      {
84          return ( (AgreementWsResource) getResource() ).getAgreement();
85      }
86  
87      private WsagSession getSession()
88      {
89          return ( (AgreementWsResource) getResource() ).getSession();
90      }
91  
92      /**
93       * @return the resource property QNames
94       */
95      public QName[] getPropertyNames()
96      {
97          return WsagConstants.WSAG_AGREEMENT_PROPERTIES;
98      }
99  
100     /**
101      * Terminates an agreement.
102      * 
103      * @param input
104      *            the input document, which may contain an optional termination reason
105      * 
106      * @return the response document, this is an empty document
107      * 
108      * @throws SoapFault
109      *             indicates a fault terminating the agreement
110      */
111     public TerminateOutputType terminate( TerminateInputDocument input ) throws SoapFault
112     {
113         try
114         {
115             WsagEngine.getWsagMessageContext().setSession( getSession() );
116 
117             TerminateResponseDocument response = TerminateResponseDocument.Factory.newInstance();
118             getAgreement().terminate( input.getTerminateInput() );
119             response.addNewTerminateResponse();
120 
121             return response.getTerminateResponse();
122 
123         }
124         catch ( Exception e )
125         {
126             throw new SoapFault( e );
127         }
128     }
129 
130     /**
131      * 
132      * @return the agreement name
133      * 
134      * @throws ResourceUnknownFault
135      *             the referenced resource is unknown
136      * 
137      * @throws ResourceUnavailableFault
138      *             the referenced resource is unavailable
139      */
140     public String getName() throws ResourceUnknownFault, ResourceUnavailableFault
141     {
142         WsagEngine.getWsagMessageContext().setSession( getSession() );
143 
144         try
145         {
146             return getAgreement().getName();
147         }
148         catch ( WSSecurityException e )
149         {
150             throw new ResourceUnavailableFault( e );
151         }
152     }
153 
154     /**
155      * 
156      * @return the agreement id
157      * 
158      * @throws ResourceUnknownFault
159      *             the referenced resource is unknown
160      * 
161      * @throws ResourceUnavailableFault
162      *             the referenced resource is unavailable
163      */
164     public Element getAgreementId() throws ResourceUnknownFault, ResourceUnavailableFault
165     {
166         try
167         {
168             WsagEngine.getWsagMessageContext().setSession( getSession() );
169 
170             String id = getAgreement().getAgreementId();
171             AgreementIdDocument doc = AgreementIdDocument.Factory.newInstance();
172             doc.setAgreementId( id );
173 
174             return toElement( doc );
175 
176         }
177         catch ( ResourceUnknownException e )
178         {
179             throw new ResourceUnknownFault( e );
180         }
181         catch ( ResourceUnavailableException e )
182         {
183             throw new ResourceUnavailableFault( e );
184         }
185         catch ( WSAgreementException e )
186         {
187             throw new ResourceUnavailableFault( e );
188         }
189         catch ( WSSecurityException e )
190         {
191             throw new ResourceUnavailableFault( e );
192         }
193     }
194 
195     /**
196      * @return the agreement context
197      * 
198      * @throws ResourceUnknownFault
199      *             the referenced resource is unknown
200      * 
201      * @throws ResourceUnavailableFault
202      *             the referenced resource is unavailable
203      */
204     public Element getContext() throws ResourceUnknownFault, ResourceUnavailableFault
205     {
206         try
207         {
208             WsagEngine.getWsagMessageContext().setSession( getSession() );
209 
210             AgreementContextType context = getAgreement().getContext();
211             if ( context == null )
212             {
213                 context = TemplateDocument.Factory.newInstance().addNewTemplate().addNewContext();
214 
215                 Calendar expire = new GregorianCalendar();
216                 expire.add( Calendar.DAY_OF_YEAR, 1 );
217 
218                 context.setExpirationTime( expire );
219                 context.setServiceProvider( AgreementRoleType.AGREEMENT_RESPONDER );
220             }
221 
222             return toElement( context );
223         }
224         catch ( ResourceUnknownException e )
225         {
226             throw new ResourceUnknownFault( e );
227         }
228         catch ( ResourceUnavailableException e )
229         {
230             throw new ResourceUnavailableFault( e );
231         }
232         catch ( WSAgreementException e )
233         {
234             throw new ResourceUnavailableFault( e );
235         }
236         catch ( WSSecurityException e )
237         {
238             throw new ResourceUnavailableFault( e );
239         }
240     }
241 
242     /**
243      * @return the agreement terms
244      * 
245      * @throws ResourceUnknownFault
246      *             the referenced resource is unknown
247      * 
248      * @throws ResourceUnavailableFault
249      *             the referenced resource is unavailable
250      */
251     public Element getTerms() throws ResourceUnknownFault, ResourceUnavailableFault
252     {
253         try
254         {
255             WsagEngine.getWsagMessageContext().setSession( getSession() );
256 
257             TermTreeType terms = getAgreement().getTerms();
258 
259             if ( terms == null )
260             {
261                 terms = TemplateDocument.Factory.newInstance().addNewTemplate().addNewTerms();
262                 terms.addNewAll();
263             }
264 
265             return toElement( terms );
266         }
267         catch ( ResourceUnknownException e )
268         {
269             throw new ResourceUnknownFault( e );
270         }
271         catch ( ResourceUnavailableException e )
272         {
273             throw new ResourceUnavailableFault( e );
274         }
275         catch ( WSAgreementException e )
276         {
277             throw new ResourceUnavailableFault( e );
278         }
279         catch ( WSSecurityException e )
280         {
281             throw new ResourceUnavailableFault( e );
282         }
283     }
284 
285     /**
286      * @return the agreement state
287      * 
288      * @throws ResourceUnknownFault
289      *             the referenced resource is unknown
290      * 
291      * @throws ResourceUnavailableFault
292      *             the referenced resource is unavailable
293      */
294     public Element getAgreementState() throws ResourceUnknownFault, ResourceUnavailableFault
295     {
296         try
297         {
298             WsagEngine.getWsagMessageContext().setSession( getSession() );
299 
300             AgreementStateDocument state = AgreementStateDocument.Factory.newInstance();
301             state.addNewAgreementState();
302 
303             if ( getAgreement().getState() != null )
304             {
305                 state.getAgreementState().set( getAgreement().getState() );
306             }
307 
308             return toElement( state );
309         }
310         catch ( ResourceUnknownException e )
311         {
312             throw new ResourceUnknownFault( e );
313         }
314         catch ( ResourceUnavailableException e )
315         {
316             throw new ResourceUnavailableFault( e );
317         }
318         catch ( WSAgreementException e )
319         {
320             throw new ResourceUnavailableFault( e );
321         }
322         catch ( WSSecurityException e )
323         {
324             throw new ResourceUnavailableFault( e );
325         }
326     }
327 
328     /**
329      * @return the agreement guarantee term states
330      * 
331      * @throws ResourceUnknownFault
332      *             the referenced resource is unknown
333      * 
334      * @throws ResourceUnavailableFault
335      *             the referenced resource is unavailable
336      */
337     public Element[] getGuaranteeTermState() throws ResourceUnknownFault, ResourceUnavailableFault
338     {
339         try
340         {
341             WsagEngine.getWsagMessageContext().setSession( getSession() );
342 
343             GuaranteeTermStateType[] currentStates = getAgreement().getGuaranteeTermStates();
344 
345             if ( currentStates == null )
346             {
347                 currentStates = new GuaranteeTermStateType[0];
348             }
349 
350             GuaranteeTermStateDocument[] states = new GuaranteeTermStateDocument[currentStates.length];
351             for ( int i = 0; i < states.length; i++ )
352             {
353                 states[i] = GuaranteeTermStateDocument.Factory.newInstance();
354                 states[i].setGuaranteeTermState( currentStates[i] );
355             }
356 
357             return toElementArray( states );
358         }
359         catch ( ResourceUnknownException e )
360         {
361             throw new ResourceUnknownFault( e );
362         }
363         catch ( ResourceUnavailableException e )
364         {
365             throw new ResourceUnavailableFault( e );
366         }
367         catch ( WSAgreementException e )
368         {
369             throw new ResourceUnavailableFault( e );
370         }
371         catch ( WSSecurityException e )
372         {
373             throw new ResourceUnavailableFault( e );
374         }
375     }
376 
377     /**
378      * @return the agreement service term states
379      * 
380      * @throws ResourceUnknownFault
381      *             the referenced resource is unknown
382      * 
383      * @throws ResourceUnavailableFault
384      *             the referenced resource is unavailable
385      */
386     public Element[] getServiceTermState() throws ResourceUnknownFault, ResourceUnavailableFault
387     {
388         try
389         {
390             WsagEngine.getWsagMessageContext().setSession( getSession() );
391 
392             ServiceTermStateType[] currentStates = getAgreement().getServiceTermStates();
393             if ( currentStates == null )
394             {
395                 currentStates = new ServiceTermStateType[0];
396             }
397 
398             ServiceTermStateDocument[] states = new ServiceTermStateDocument[currentStates.length];
399             for ( int i = 0; i < states.length; i++ )
400             {
401                 states[i] = ServiceTermStateDocument.Factory.newInstance();
402                 states[i].setServiceTermState( currentStates[i] );
403             }
404 
405             return toElementArray( states );
406 
407         }
408         catch ( ResourceUnknownException e )
409         {
410             throw new ResourceUnknownFault( e );
411         }
412         catch ( ResourceUnavailableException e )
413         {
414             throw new ResourceUnavailableFault( e );
415         }
416         catch ( WSAgreementException e )
417         {
418             throw new ResourceUnavailableFault( e );
419         }
420         catch ( WSSecurityException e )
421         {
422             throw new ResourceUnavailableFault( e );
423         }
424     }
425 
426 }