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.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
71
72
73
74
75 public class NegotiationCapability extends WSAG4JCapability
76 {
77
78 private static final Logger LOG = Logger.getLogger( NegotiationCapability.class );
79
80
81
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
95
96
97
98
99
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
121
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
137
138
139
140
141
142
143 public Element[] getNegotiableTemplate() throws ResourceUnavailableFault
144 {
145
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
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
184
185
186
187
188
189
190
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
207
208
209
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
229
230
231
232
233
234
235
236
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
282
283
284
285
286
287
288
289
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
329
330
331
332
333
334
335
336
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 }