View Javadoc

1   /*
2    * Copyright  2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.apache.kandula.wscoor;
18  
19  import javax.xml.namespace.QName;
20  
21  import org.apache.axiom.om.OMAbstractFactory;
22  import org.apache.axiom.om.OMElement;
23  import org.apache.axiom.om.OMNamespace;
24  import org.apache.axiom.soap.SOAPFactory;
25  import org.apache.axis2.AxisFault;
26  import org.apache.axis2.addressing.EndpointReference;
27  import org.apache.axis2.context.OperationContext;
28  import org.apache.axis2.wsdl.WSDLConstants;
29  import org.apache.kandula.Constants;
30  import org.apache.kandula.coordinator.Coordinator;
31  import org.apache.kandula.faults.AbstractKandulaException;
32  import org.apache.kandula.storage.StorageFactory;
33  import org.apache.kandula.utility.EndpointReferenceFactory;
34  
35  /***
36   * @author <a href="mailto:thilina@apache.org"> Thilina Gunarathne </a>
37   */
38  
39  public class RegistrationPortTypeRawXMLSkeleton {
40  	private OperationContext opContext;
41  
42  	public void setOperationContext(OperationContext opContext) {
43  		this.opContext = opContext;
44  	}
45  
46  	public OMElement registerOperation(OMElement request) throws AxisFault {
47  
48  		String protocolIdentifier;
49  		EndpointReference participantEPR;
50  		String activityId;
51  		StorageFactory.getInstance().setConfigurationContext(
52  				opContext.getServiceContext().getConfigurationContext());
53  		/*
54  		 * Extracting data from the received message
55  		 */
56  		protocolIdentifier = request.getFirstChildWithName(
57  				new QName("ProtocolIdentifier")).getText();
58  		OMElement participantEPRElement = request
59  				.getFirstChildWithName(new QName("ParticipantProtocolService"));
60  		// Extracting the participant EPR
61  		participantEPR = EndpointReferenceFactory
62  				.endpointFromOM(participantEPRElement);
63  
64  		OMElement header = opContext.getMessageContext(
65  				WSDLConstants.MESSAGE_LABEL_IN_VALUE).getEnvelope().getHeader();
66  		activityId = header.getFirstChildWithName(
67  				Constants.TRANSACTION_ID_PARAMETER).getText();
68  		/*
69  		 * Registering the participant for the activity for the given protocol
70  		 */
71  		try {
72  			Coordinator coordinator = new Coordinator();
73  			EndpointReference epr = coordinator.registerParticipant(activityId,
74  					protocolIdentifier, participantEPR);
75  			SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
76  			OMNamespace wsCoor = factory.createOMNamespace(Constants.WS_COOR,
77  					"wscoor");
78  			OMElement responseEle = factory.createOMElement("RegisterResponse",
79  					wsCoor);
80  			responseEle.addChild(toOM(epr));
81  			return responseEle;
82  		} catch (AbstractKandulaException e) {
83  			AxisFault fault = new AxisFault(e);
84  			fault.setFaultCode(e.getFaultCode());
85  			throw fault;
86  		}
87  	}
88  
89  	/***
90  	 * Serializes an EndpointRefrence to OM Nodes
91  	 */
92  	private OMElement toOM(EndpointReference epr) {
93  		SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
94  		OMNamespace wsCoor = factory.createOMNamespace(
95  				org.apache.kandula.Constants.WS_COOR, "wscoor");
96  		OMElement protocolService = factory.createOMElement(
97  				"CoordinatorProtocolService", wsCoor);
98  		OMElement coordinatorProtocolService = factory.createOMElement(
99  				"CoordinatorProtocolService", wsCoor);
100 		EndpointReferenceFactory.endpointToOM(epr, coordinatorProtocolService,
101 				factory);
102 		protocolService.addChild(coordinatorProtocolService);
103 		return protocolService;
104 	}
105 }