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.samples.pendingagreement;
36
37 import javax.xml.namespace.QName;
38
39 import org.apache.log4j.Logger;
40 import org.ogf.graap.wsag.api.Agreement;
41 import org.ogf.graap.wsag.api.AgreementOffer;
42 import org.ogf.graap.wsag.api.client.AgreementClient;
43 import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
44 import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
45 import org.ogf.graap.wsag.api.security.SecurityProperties;
46 import org.ogf.graap.wsag.api.types.AgreementOfferType;
47 import org.ogf.graap.wsag.client.remote.RemoteAgreementFactoryClientImpl;
48 import org.ogf.graap.wsag.server.actions.AbstractCreateAgreementAction;
49 import org.ogf.graap.wsag.server.actions.ActionInitializationException;
50 import org.ogf.graap.wsag.server.api.AgreementAcceptanceFactory;
51 import org.ogf.graap.wsag.server.api.AgreementAcceptanceListener;
52 import org.ogf.graap.wsag.server.api.IAgreementFactoryContext;
53 import org.ogf.graap.wsag.server.engine.WsagEngine;
54 import org.ogf.schemas.graap.wsAgreement.AgreementStateDefinition;
55 import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
56 import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
57 import org.w3.x2005.x08.addressing.EndpointReferenceDocument;
58 import org.w3.x2005.x08.addressing.EndpointReferenceType;
59
60
61
62
63
64
65
66
67
68 public class CreatePendingAgreementWithNotificationAction extends AbstractCreateAgreementAction
69 {
70
71 private static final Logger LOG = Logger.getLogger( CreatePendingAgreementWithNotificationAction.class );
72
73
74
75
76
77
78 public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
79 {
80 IAgreementFactoryContext factoryContext = getHandlerContext().getFactoryContext();
81 AgreementAcceptanceFactory acceptanceFactory =
82 (AgreementAcceptanceFactory) factoryContext.get( AgreementAcceptanceFactory.AGREEMENT_ACCEPTANCE_FACTORY );
83
84 if ( acceptanceFactory == null )
85 {
86 throw new AgreementFactoryException( "Acceptance not supported." );
87 }
88 else
89 {
90
91
92
93 final PendingAgreement agreement = new PendingAgreement( offer );
94
95 AgreementAcceptanceListener acceptanceListener = new AgreementAcceptanceListener()
96 {
97
98 public void reject()
99 {
100 try
101 {
102 agreement.getState().setState( AgreementStateDefinition.REJECTED );
103 }
104 catch ( Exception e )
105 {
106 LOG.error( "Error while setting agreement state." );
107 }
108 }
109
110 public void accept()
111 {
112 try
113 {
114 agreement.getState().setState( AgreementStateDefinition.COMPLETE );
115 }
116 catch ( Exception e )
117 {
118 LOG.error( "Error while setting agreement state." );
119 }
120 }
121 };
122
123 EndpointReferenceType acceptanceEPR =
124 acceptanceFactory.registerAgreementAccetanceListener( acceptanceListener );
125
126
127
128
129
130 final QName eprQName = EndpointReferenceDocument.type.getDocumentElementName();
131 ServiceDescriptionTermType sdt = offer.getTerms().getAll().getServiceDescriptionTermArray( 0 );
132 EndpointReferenceType epr = (EndpointReferenceType) sdt.selectChildren( eprQName )[0];
133
134 try
135 {
136 SecurityProperties securityProperties = new SecurityProperties( WsagEngine.getLoginContext() );
137
138 AgreementFactoryClient factory =
139 new RemoteAgreementFactoryClientImpl( epr, securityProperties );
140
141 AgreementTemplateType template = factory.getTemplate( "SAMPLE4-PENDING-AGREEMENT", "1" );
142 AgreementClient agreementClient =
143 factory.createPendingAgreement( new AgreementOfferType( template ), acceptanceEPR );
144
145 if ( LOG.isInfoEnabled() )
146 {
147 LOG.info( "Created agreement client: " + agreementClient.getEndpoint().xmlText() );
148 }
149 }
150 catch ( Exception e )
151 {
152 LOG.error( "Failed to create the pending agreement. Reason: " + e.getMessage() );
153
154 if ( LOG.isDebugEnabled() )
155 {
156 LOG.debug( e );
157 }
158
159 throw new AgreementFactoryException( e );
160 }
161
162
163
164
165
166 return agreement;
167
168 }
169 }
170
171
172
173
174
175
176 public void initialize() throws ActionInitializationException
177 {
178
179 }
180
181 }