View Javadoc
1   /* 
2    * Copyright (c) 2012, 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.server.rest;
36  
37  import javax.ws.rs.Path;
38  import javax.ws.rs.PathParam;
39  
40  import org.apache.xmlbeans.QNameSet;
41  import org.apache.xmlbeans.XmlObject;
42  import org.ogf.graap.wsag.api.Negotiation;
43  import org.ogf.graap.wsag.api.exceptions.NegotiationException;
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.rest.RestNegotiation;
47  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
48  import org.ogf.schemas.graap.wsAgreement.negotiation.AdvertiseInputDocument;
49  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiateInputDocument;
50  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiateOutputDocument;
51  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
52  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferType;
53  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationPropertiesDocument;
54  import org.ogf.schemas.graap.wsAgreement.negotiation.TerminateInputDocument;
55  
56  /**
57   * @author owaeld
58   * 
59   */
60  @Path( "/factories/{factoryId}/Negotiations/{negotiationId}" )
61  public class RestNegotiationFacade
62      implements RestNegotiation
63  {
64      private final String factoryId;
65  
66      private final String negotiationId;
67  
68      public RestNegotiationFacade( @PathParam( "factoryId" ) String factoryId,
69                                    @PathParam( "negotiationId" ) String negotiationId )
70      {
71          this.factoryId = factoryId;
72          this.negotiationId = negotiationId;
73      }
74  
75      private Negotiation getNegotiation()
76      {
77          /*
78           * each negotiation has a global unique id, no need to use the factory id here
79           */
80          return RestNegotiationRegistry.get( negotiationId );
81      }
82  
83      /*
84       * (non-Javadoc)
85       * 
86       * @see org.ogf.graap.wsag.api.rest.RestNegotiation#getNegotiationContext()
87       */
88      @Override
89      public NegotiationPropertiesDocument getNegotiationContext()
90          throws ResourceUnknownException, ResourceUnavailableException
91      {
92          NegotiationContextType context = getNegotiation().getNegotiationContext();
93  
94          NegotiationPropertiesDocument properties = NegotiationPropertiesDocument.Factory.newInstance();
95          properties.addNewNegotiationProperties().addNewNegotiationContext().set( context );
96  
97          return properties;
98      }
99  
100     /*
101      * (non-Javadoc)
102      * 
103      * @see org.ogf.graap.wsag.api.rest.RestNegotiation#getNegotiableTemplates()
104      */
105     @Override
106     public NegotiationPropertiesDocument getNegotiableTemplates()
107         throws ResourceUnknownException, ResourceUnavailableException
108     {
109         AgreementTemplateType[] templates = getNegotiation().getNegotiableTemplates();
110 
111         NegotiationPropertiesDocument properties = NegotiationPropertiesDocument.Factory.newInstance();
112         properties.addNewNegotiationProperties().setNegotiableTemplateArray( templates );
113 
114         return properties;
115     }
116 
117     /*
118      * (non-Javadoc)
119      * 
120      * @see org.ogf.graap.wsag.api.rest.RestNegotiation#getNegotiationOffers()
121      */
122     @Override
123     public NegotiationPropertiesDocument getNegotiationOffers()
124         throws ResourceUnknownException, ResourceUnavailableException
125     {
126         NegotiationOfferType[] offers = getNegotiation().getNegotiationOffers();
127 
128         NegotiationPropertiesDocument properties = NegotiationPropertiesDocument.Factory.newInstance();
129         properties.addNewNegotiationProperties().setNegotiationOfferArray( offers );
130 
131         return properties;
132     }
133 
134     /*
135      * (non-Javadoc)
136      * 
137      * @see
138      * org.ogf.graap.wsag.api.rest.RestNegotiation#negotiate(org.ogf.schemas.graap.wsAgreement.negotiation
139      * .NegotiateInputDocument)
140      */
141     @Override
142     public NegotiateOutputDocument negotiate( NegotiateInputDocument input )
143         throws NegotiationException, ResourceUnknownException, ResourceUnavailableException
144     {
145         NegotiationOfferType[] offers = input.getNegotiateInput().getNegotiationOfferArray();
146         QNameSet others =
147             QNameSet.forWildcardNamespaceString( "##other",
148                 NegotiateInputDocument.type.getDocumentElementName().getNamespaceURI() );
149         XmlObject[] noncritical = input.getNegotiateInput().selectChildren( others );
150 
151         NegotiationOfferType[] counterOffers = getNegotiation().negotiate( offers, noncritical );
152 
153         NegotiateOutputDocument output = NegotiateOutputDocument.Factory.newInstance();
154         output.addNewNegotiateOutput().setNegotiationCounterOfferArray( counterOffers );
155 
156         return output;
157     }
158 
159     /*
160      * (non-Javadoc)
161      * 
162      * @see
163      * org.ogf.graap.wsag.api.rest.RestNegotiation#advertise(org.ogf.schemas.graap.wsAgreement.negotiation
164      * .AdvertiseInputDocument)
165      */
166     @Override
167     public void advertise( AdvertiseInputDocument input )
168         throws NegotiationException, ResourceUnknownException, ResourceUnavailableException
169     {
170         NegotiationOfferType[] offers = input.getAdvertiseInput().getNegotiationOfferArray();
171         QNameSet others =
172             QNameSet.forWildcardNamespaceString( "##other",
173                 NegotiateInputDocument.type.getDocumentElementName().getNamespaceURI() );
174         XmlObject[] noncritical = input.getAdvertiseInput().selectChildren( others );
175 
176         getNegotiation().advertise( offers, noncritical );
177     }
178 
179     /*
180      * (non-Javadoc)
181      * 
182      * @see
183      * org.ogf.graap.wsag.api.rest.RestNegotiation#terminate(org.ogf.schemas.graap.wsAgreement.negotiation
184      * .TerminateInputDocument)
185      */
186     @Override
187     public void terminate( TerminateInputDocument reason )
188         throws ResourceUnknownException, ResourceUnavailableException
189     {
190         getNegotiation().terminate();
191     }
192 
193     /*
194      * (non-Javadoc)
195      * 
196      * @see org.ogf.graap.wsag.api.rest.RestNegotiation#destroy()
197      */
198     @Override
199     public void destroy() throws ResourceUnknownException, ResourceUnavailableException
200     {
201         RestNegotiationRegistry.remove( negotiationId );
202     }
203 
204 }