View Javadoc
1   /*
2    * Copyright 2001-2009 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.juddi.v3.client.transport;
18  
19  import java.util.Map;
20  import java.util.Properties;
21  
22  import javax.xml.ws.BindingProvider;
23  
24  import org.apache.commons.configuration.ConfigurationException;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.juddi.v3.client.JUDDIApiService;
28  import org.apache.juddi.v3.client.UDDIService;
29  import org.apache.juddi.v3.client.config.Property;
30  import org.apache.juddi.v3.client.config.UDDIClient;
31  import org.apache.juddi.v3.client.config.UDDIClientContainer;
32  import org.apache.juddi.v3.client.cryptor.CryptorFactory;
33  import org.apache.juddi.v3_service.JUDDIApiPortType;
34  import org.uddi.v3_service.UDDICustodyTransferPortType;
35  import org.uddi.v3_service.UDDIInquiryPortType;
36  import org.uddi.v3_service.UDDIPublicationPortType;
37  import org.uddi.v3_service.UDDISecurityPortType;
38  import org.uddi.v3_service.UDDISubscriptionListenerPortType;
39  import org.uddi.v3_service.UDDISubscriptionPortType;
40  
41  public class JAXWSTransport extends Transport {
42  
43          private static Log logger = LogFactory.getLog(JAXWSTransport.class);
44  
45          String nodeName = null;
46          String clientName = null;
47          UDDIInquiryPortType inquiryService = null;
48          UDDISecurityPortType securityService = null;
49          UDDIPublicationPortType publishService = null;
50          UDDISubscriptionPortType subscriptionService = null;
51          UDDISubscriptionListenerPortType subscriptionListenerService = null;
52          UDDICustodyTransferPortType custodyTransferService = null;
53          JUDDIApiPortType publisherService = null;
54  
55          public JAXWSTransport() {
56                  super();
57                  this.nodeName = Transport.DEFAULT_NODE_NAME;
58          }
59  
60          public JAXWSTransport(String nodeName) {
61                  super();
62                  this.nodeName = nodeName;
63          }
64  
65          public JAXWSTransport(String clientName, String nodeName) {
66                  super();
67                  this.clientName = clientName;
68                  this.nodeName = nodeName;
69          }
70  
71          public UDDIInquiryPortType getUDDIInquiryService(String endpointURL) throws TransportException {
72                  try {
73                          if (inquiryService == null) {
74                                  if (endpointURL == null) {
75                                          UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
76                                          endpointURL = client.getClientConfig().getUDDINode(nodeName).getInquiryUrl();
77                                  }
78                                  UDDIService service = new UDDIService();
79                                  inquiryService = service.getUDDIInquiryPort();
80                          }
81                          Map<String, Object> requestContext = ((BindingProvider) inquiryService).getRequestContext();
82                          if (endpointURL != null) {
83                          
84                                  requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
85                          }
86                          setCredentials(requestContext);
87                  } catch (Exception e) {
88                          throw new TransportException(e.getMessage(), e);
89                  }
90                  return inquiryService;
91          }
92  
93          public UDDISecurityPortType getUDDISecurityService(String endpointURL) throws TransportException {
94                  try {
95                          if (securityService == null) {
96                                  if (endpointURL == null) {
97                                          UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
98                                          endpointURL = client.getClientConfig().getUDDINode(nodeName).getSecurityUrl();
99                                  }
100                                 UDDIService service = new UDDIService();
101                                 securityService = service.getUDDISecurityPort();
102                         }
103                         if (endpointURL != null) {
104                                 if (endpointURL.toLowerCase().startsWith("http:")){
105                                         logger.warn("You should consider using a secure protocol (https) when sending your password!");
106                                 }
107                                 Map<String, Object> requestContext = ((BindingProvider) securityService).getRequestContext();
108                                 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
109                                 setCredentials(requestContext);
110                         }
111                 } catch (Exception e) {
112                         throw new TransportException(e.getMessage(), e);
113                 }
114                 return securityService;
115         }
116 
117         public UDDIPublicationPortType getUDDIPublishService(String endpointURL) throws TransportException {
118                 try {
119                         if (publishService == null) {
120 
121                                 if (endpointURL == null) {
122                                         UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
123                                         endpointURL = client.getClientConfig().getUDDINode(nodeName).getPublishUrl();
124                                 }
125                                 UDDIService service = new UDDIService();
126                                 publishService = service.getUDDIPublicationPort();
127                         }
128                         Map<String, Object> requestContext = ((BindingProvider) publishService).getRequestContext();
129                         if (endpointURL != null)
130                                 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
131                         setCredentials(requestContext);
132                 } catch (Exception e) {
133                         throw new TransportException(e.getMessage(), e);
134                 }
135                 return publishService;
136         }
137 
138         public UDDISubscriptionPortType getUDDISubscriptionService(String endpointURL) throws TransportException {
139                 try {
140                         if (subscriptionService == null) {
141                                 if (endpointURL == null) {
142                                         UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
143                                         endpointURL = client.getClientConfig().getUDDINode(nodeName).getSubscriptionUrl();
144                                 }
145                                 UDDIService service = new UDDIService();
146                                 subscriptionService = service.getUDDISubscriptionPort();
147                         }
148                         if (endpointURL != null) {
149                                 Map<String, Object> requestContext = ((BindingProvider) subscriptionService).getRequestContext();
150                                 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
151                                 setCredentials(requestContext);
152                         }
153                 } catch (Exception e) {
154                         throw new TransportException(e.getMessage(), e);
155                 }
156                 return subscriptionService;
157         }
158 
159         public UDDISubscriptionListenerPortType getUDDISubscriptionListenerService(String endpointURL) throws TransportException {
160                 try {
161                         if (subscriptionListenerService == null) {
162                                 if (endpointURL == null) {
163                                         UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
164                                         endpointURL = client.getClientConfig().getUDDINode(nodeName).getSubscriptionListenerUrl();
165                                 }
166                                 UDDIService service = new UDDIService();
167                                 subscriptionListenerService = service.getUDDISubscriptionListenerPort();
168                         }
169                         if (endpointURL != null) {
170                                 Map<String, Object> requestContext = ((BindingProvider) subscriptionListenerService).getRequestContext();
171                                 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
172                                 setCredentials(requestContext);
173                         }
174                 } catch (Exception e) {
175                         throw new TransportException(e.getMessage(), e);
176                 }
177                 return subscriptionListenerService;
178         }
179 
180         public UDDICustodyTransferPortType getUDDICustodyTransferService(String endpointURL) throws TransportException {
181                 try {
182                         if (custodyTransferService == null) {
183                                 if (endpointURL == null) {
184                                         UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
185                                         endpointURL = client.getClientConfig().getUDDINode(nodeName).getCustodyTransferUrl();
186                                 }
187                                 UDDIService service = new UDDIService();
188                                 custodyTransferService = service.getUDDICustodyPort();
189                         }
190                         if (endpointURL != null) {
191                                 Map<String, Object> requestContext = ((BindingProvider) custodyTransferService).getRequestContext();
192                                 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
193                                 setCredentials(requestContext);
194                         }
195                 } catch (Exception e) {
196                         throw new TransportException(e.getMessage(), e);
197                 }
198                 return custodyTransferService;
199         }
200 
201         /**
202          * This is a jUDDI specific API
203          */
204         public JUDDIApiPortType getJUDDIApiService(String endpointURL) throws TransportException {
205                 try {
206                         if (publisherService == null) {
207                                 if (endpointURL == null) {
208                                         UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
209                                         endpointURL = client.getClientConfig().getUDDINode(nodeName).getJuddiApiUrl();
210                                 }
211                                 JUDDIApiService service = new JUDDIApiService();
212                                 publisherService = (JUDDIApiPortType) service.getPort(JUDDIApiPortType.class);
213                         }
214                         if (endpointURL != null) {
215                                 Map<String, Object> requestContext = ((BindingProvider) publisherService).getRequestContext();
216                                 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
217                                 setCredentials(requestContext);
218                         }
219                 } catch (Exception e) {
220                         throw new TransportException(e.getMessage(), e);
221                 }
222                 return publisherService;
223         }
224 
225         public String getNodeName() {
226                 return nodeName;
227         }
228 
229         public void setNodeName(String nodeName) {
230                 this.nodeName = nodeName;
231         }
232 
233         /**
234          * Sets the credentials on the RequestContext if the services are
235          * protected by Basic Authentication. The username and password are
236          * obtained from the uddi.xml.
237          *
238          * @param requestContext
239          * @throws ConfigurationException
240          */
241         private void setCredentials(Map<String, Object> requestContext) throws ConfigurationException {
242                 UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
243                 Properties properties = client.getClientConfig().getUDDINode(nodeName).getProperties();
244                 if (properties != null) {
245                         String username = null;
246                         String password = null;
247                         if (properties.containsKey(Property.BASIC_AUTH_USERNAME)) {
248                                 username = properties.getProperty(Property.BASIC_AUTH_USERNAME);
249                         }
250                         if (properties.containsKey(Property.BASIC_AUTH_PASSWORD)) {
251                                 password = properties.getProperty(Property.BASIC_AUTH_PASSWORD);
252                         }
253                         String cipher = null;
254                         boolean isEncrypted = false;
255                         if (properties.containsKey(Property.BASIC_AUTH_PASSWORD_CP)) {
256                                 cipher = properties.getProperty(Property.BASIC_AUTH_PASSWORD_CP);
257                         }
258                         if (properties.containsKey(Property.BASIC_AUTH_PASSWORD_IS_ENC)) {
259                                 isEncrypted = Boolean.parseBoolean(properties.getProperty(Property.BASIC_AUTH_PASSWORD_IS_ENC));
260                         }
261                         if (username != null && password != null) {
262                                 requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
263                                 if (isEncrypted) {
264                                         try {
265                                                 requestContext.put(BindingProvider.PASSWORD_PROPERTY, CryptorFactory.getCryptor(cipher).decrypt(password));
266                                         } catch (Exception ex) {
267                                                 logger.error("Unable to decrypt password!", ex);
268                                         }
269                                 } else {
270                                         requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
271                                 }
272                         }
273                 }
274         }
275 
276 }