KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpEndpoint


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http;
18
19 import java.net.URI JavaDoc;
20
21 import javax.jbi.component.ComponentLifeCycle;
22 import javax.jbi.servicedesc.ServiceEndpoint;
23 import javax.wsdl.Binding;
24 import javax.wsdl.Definition;
25 import javax.wsdl.Port;
26 import javax.wsdl.PortType;
27 import javax.wsdl.Service;
28 import javax.wsdl.extensions.ExtensibilityElement;
29 import javax.wsdl.extensions.http.HTTPAddress;
30 import javax.xml.namespace.QName JavaDoc;
31
32 import org.apache.servicemix.common.ExchangeProcessor;
33 import org.apache.servicemix.http.processors.ConsumerProcessor;
34 import org.apache.servicemix.http.processors.ProviderProcessor;
35 import org.apache.servicemix.http.tools.PortTypeDecorator;
36 import org.apache.servicemix.jbi.security.auth.AuthenticationService;
37 import org.apache.servicemix.jbi.security.keystore.KeystoreManager;
38 import org.apache.servicemix.soap.SoapEndpoint;
39
40 import com.ibm.wsdl.extensions.http.HTTPAddressImpl;
41
42 /**
43  *
44  * @author gnodet
45  * @version $Revision: 434058 $
46  * @org.apache.xbean.XBean element="endpoint"
47  * description="An http endpoint"
48  *
49  */

50 public class HttpEndpoint extends SoapEndpoint {
51
52     protected ExtensibilityElement binding;
53     protected String JavaDoc locationURI;
54     protected SslParameters ssl;
55     protected String JavaDoc authMethod;
56     protected String JavaDoc soapAction;
57     protected BasicAuthCredentials basicAuthentication;
58     
59     /**
60      * @return the soapAction
61      */

62     public String JavaDoc getSoapAction() {
63         return soapAction;
64     }
65
66     /**
67      * @param soapAction the soapAction to set
68      */

69     public void setSoapAction(String JavaDoc soapAction) {
70         this.soapAction = soapAction;
71     }
72
73     /**
74      * @return the authMethod
75      */

76     public String JavaDoc getAuthMethod() {
77         return authMethod;
78     }
79
80     /**
81      * @param authMethod the authMethod to set
82      */

83     public void setAuthMethod(String JavaDoc authMethod) {
84         this.authMethod = authMethod;
85     }
86
87     /**
88      * @return Returns the ssl.
89      */

90     public SslParameters getSsl() {
91         return ssl;
92     }
93
94     /**
95      * @param ssl The ssl to set.
96      */

97     public void setSsl(SslParameters ssl) {
98         this.ssl = ssl;
99     }
100
101     public ExtensibilityElement getBinding() {
102         return binding;
103     }
104
105     public void setBinding(ExtensibilityElement binding) {
106         this.binding = binding;
107     }
108
109     public String JavaDoc getLocationURI() {
110         return locationURI;
111     }
112
113     public void setLocationURI(String JavaDoc locationUri) {
114         this.locationURI = locationUri;
115     }
116
117     /**
118      * Authentication parameters used for provider endpoints using BASIC
119      * authentication.
120      *
121      * @return Returns the basicAuthentication.
122      */

123     public BasicAuthCredentials getBasicAuthentication() {
124         return basicAuthentication;
125     }
126
127     /**
128      * @param basicAuthentication The basicAuthentication to set.
129      */

130     public void setBasicAuthentication(BasicAuthCredentials basicAuthCredentials) {
131         this.basicAuthentication = basicAuthCredentials;
132     }
133     
134     /**
135      * @org.apache.xbean.Property alias="role"
136      * @param role
137      */

138     public void setRoleAsString(String JavaDoc role) {
139         super.setRoleAsString(role);
140     }
141
142     protected PortType getTargetPortType(Definition def) {
143         PortType portType = null;
144         // If the WSDL description only contain one PortType, use it
145
if (def.getServices().size() == 0 && def.getPortTypes().size() == 1) {
146             if (logger.isDebugEnabled()) {
147                 logger.debug("WSDL only defines a PortType, using this one");
148             }
149             portType = (PortType) def.getPortTypes().values().iterator().next();
150         } else if (targetInterfaceName != null) {
151             portType = def.getPortType(targetInterfaceName);
152             if (portType == null && logger.isDebugEnabled()) {
153                 logger.debug("PortType for targetInterfaceName could not be found");
154             }
155         } else if (targetService != null && targetEndpoint != null) {
156             Service svc = def.getService(targetService);
157             Port port = (svc != null) ? svc.getPort(targetEndpoint) : null;
158             portType = (port != null) ? port.getBinding().getPortType() : null;
159             if (portType == null && logger.isDebugEnabled()) {
160                 logger.debug("PortType for targetService/targetEndpoint could not be found");
161             }
162         } else if (targetService != null) {
163             Service svc = def.getService(targetService);
164             if (svc != null && svc.getPorts().size() == 1) {
165                 Port port = (Port) svc.getPorts().values().iterator().next();
166                 portType = (port != null) ? port.getBinding().getPortType() : null;
167             }
168             if (portType == null && logger.isDebugEnabled()) {
169                 logger.debug("Service for targetService could not be found");
170             }
171         } else if (interfaceName != null) {
172             portType = def.getPortType(interfaceName);
173             if (portType == null && logger.isDebugEnabled()) {
174                 logger.debug("Service for targetInterfaceName could not be found");
175             }
176         } else {
177             Service svc = def.getService(service);
178             Port port = (svc != null) ? svc.getPort(endpoint) : null;
179             portType = (port != null && port.getBinding() != null) ? port.getBinding().getPortType() : null;
180             if (portType == null && logger.isDebugEnabled()) {
181                 logger.debug("Port for service/endpoint could not be found");
182             }
183         }
184         return portType;
185     }
186     
187     protected void overrideDefinition(Definition def) throws Exception JavaDoc {
188         PortType portType = getTargetPortType(def);
189         if (portType != null) {
190             QName JavaDoc[] names = (QName JavaDoc[]) def.getPortTypes().keySet().toArray(new QName JavaDoc[0]);
191             for (int i = 0; i < names.length; i++) {
192                 if (!names[i].equals(portType.getQName())) {
193                     def.removePortType(names[i]);
194                 }
195             }
196             names = (QName JavaDoc[]) def.getServices().keySet().toArray(new QName JavaDoc[0]);
197             for (int i = 0; i < names.length; i++) {
198                 def.removeService(names[i]);
199             }
200             names = (QName JavaDoc[]) def.getBindings().keySet().toArray(new QName JavaDoc[0]);
201             for (int i = 0; i < names.length; i++) {
202                 def.removeBinding(names[i]);
203             }
204             String JavaDoc location = getLocationURI();
205             if (!location.endsWith("/")) {
206                 location += "/";
207             }
208             HttpLifeCycle lf = (HttpLifeCycle) getServiceUnit().getComponent().getLifeCycle();
209             if (lf.getConfiguration().isManaged()) {
210                 // TODO: need to find the port of the web server
211
location = "http://localhost" + lf.getConfiguration().getMapping() + new URI JavaDoc(location).getPath();
212             }
213             if (portType.getQName().getNamespaceURI().equals(service.getNamespaceURI())) {
214                 if (isSoap()) {
215                     PortTypeDecorator.decorate(
216                             def,
217                             portType,
218                             location,
219                             endpoint + "Binding",
220                             service.getLocalPart(),
221                             endpoint);
222                     definition = def;
223                 } else {
224                     Binding binding = def.createBinding();
225                     binding.setPortType(portType);
226                     binding.setQName(new QName JavaDoc(service.getNamespaceURI(), endpoint + "Binding"));
227                     binding.setUndefined(false);
228                     def.addBinding(binding);
229                     Port port = def.createPort();
230                     port.setName(endpoint);
231                     port.setBinding(binding);
232                     HTTPAddress address = new HTTPAddressImpl();
233                     address.setLocationURI(location);
234                     port.addExtensibilityElement(address);
235                     def.addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/");
236                     Service svc = def.createService();
237                     svc.setQName(service);
238                     svc.addPort(port);
239                     def.addService(svc);
240                     definition = def;
241                 }
242             } else {
243                 definition = PortTypeDecorator.createImportDef(def, service.getNamespaceURI(), "porttypedef.wsdl");
244                 PortTypeDecorator.decorate(
245                         definition,
246                         portType,
247                         location,
248                         endpoint + "Binding",
249                         service.getLocalPart(),
250                         endpoint);
251             }
252         }
253     }
254     
255     protected ExchangeProcessor createProviderProcessor() {
256         return new ProviderProcessor(this);
257     }
258
259     protected ExchangeProcessor createConsumerProcessor() {
260         return new ConsumerProcessor(this);
261     }
262
263     protected ServiceEndpoint createExternalEndpoint() {
264         return new HttpExternalEndpoint(this);
265     }
266
267     public AuthenticationService getAuthenticationService() {
268         ComponentLifeCycle lf = getServiceUnit().getComponent().getLifeCycle();
269         return ((HttpLifeCycle) lf).getAuthenticationService();
270     }
271
272     public KeystoreManager getKeystoreManager() {
273         ComponentLifeCycle lf = getServiceUnit().getComponent().getLifeCycle();
274         return ((HttpLifeCycle) lf).getKeystoreManager();
275     }
276
277
278
279 }
280
Popular Tags