1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
79
80 return RestNegotiationRegistry.get( negotiationId );
81 }
82
83
84
85
86
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
102
103
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
119
120
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
136
137
138
139
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
161
162
163
164
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
181
182
183
184
185
186 @Override
187 public void terminate( TerminateInputDocument reason )
188 throws ResourceUnknownException, ResourceUnavailableException
189 {
190 getNegotiation().terminate();
191 }
192
193
194
195
196
197
198 @Override
199 public void destroy() throws ResourceUnknownException, ResourceUnavailableException
200 {
201 RestNegotiationRegistry.remove( negotiationId );
202 }
203
204 }