View Javadoc
1   /*
2    * Copyright 2014 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  package org.apache.juddi.api.impl;
17  
18  import java.util.List;
19  import javax.jws.WebService;
20  import javax.persistence.EntityManager;
21  import javax.persistence.EntityTransaction;
22  import javax.xml.ws.Holder;
23  import org.apache.commons.configuration.ConfigurationException;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.config.AppConfig;
27  import org.apache.juddi.config.PersistenceManager;
28  import org.apache.juddi.config.Property;
29  import org.apache.juddi.v3.client.mapping.MapUDDIv2Tov3;
30  import org.apache.juddi.v3.client.mapping.MapUDDIv3Tov2;
31  import org.uddi.api_v2.AddPublisherAssertions;
32  import org.uddi.api_v2.AssertionStatusReport;
33  import org.uddi.api_v2.AuthToken;
34  import org.uddi.api_v2.BindingDetail;
35  import org.uddi.api_v2.BusinessDetail;
36  import org.uddi.api_v2.DeleteBinding;
37  import org.uddi.api_v2.DeleteBusiness;
38  import org.uddi.api_v2.DeletePublisherAssertions;
39  import org.uddi.api_v2.DeleteService;
40  import org.uddi.api_v2.DeleteTModel;
41  import org.uddi.api_v2.DiscardAuthToken;
42  import org.uddi.api_v2.DispositionReport;
43  import org.uddi.api_v2.GetAssertionStatusReport;
44  import org.uddi.api_v2.GetAuthToken;
45  import org.uddi.api_v2.GetPublisherAssertions;
46  import org.uddi.api_v2.GetRegisteredInfo;
47  import org.uddi.api_v2.PublisherAssertions;
48  import org.uddi.api_v2.RegisteredInfo;
49  import org.uddi.api_v2.Result;
50  import org.uddi.api_v2.SaveBinding;
51  import org.uddi.api_v2.SaveBusiness;
52  import org.uddi.api_v2.SaveService;
53  import org.uddi.api_v2.SaveTModel;
54  import org.uddi.api_v2.ServiceDetail;
55  import org.uddi.api_v2.SetPublisherAssertions;
56  import org.uddi.api_v2.TModelDetail;
57  import org.uddi.api_v2.Truncated;
58  import org.uddi.api_v3.CompletionStatus;
59  import org.uddi.api_v3.PublisherAssertion;
60  import org.uddi.v2_service.Publish;
61  import org.uddi.v3_service.DispositionReportFaultMessage;
62  
63  /**
64   * UDDI v2 Implementation for the Publish service. This implementation is
65   * basically a wrapper and API translator that translates and forwards the
66   * request to our UDDIv3 Publish implementation<br><br>
67   * This class is a BETA feature and is largely untested. Please report any
68   * issues
69   *
70   * @author <a href="mailto:alexoree.apache.org">Alex O'Ree</a>
71   * @since 3.2
72   */
73  @WebService(serviceName = "Publish", targetNamespace = "urn:uddi-org:publication_v2",
74       endpointInterface = "org.uddi.v2_service.Publish")
75  public class UDDIv2PublishImpl implements Publish {
76  
77          public UDDIv2PublishImpl() {
78                  logger.warn("This implementation of UDDIv2 Publish service " + UDDIv2PublishImpl.class.getCanonicalName() + " is considered BETA. Please"
79                       + " report any issues to https://issues.apache.org/jira/browse/JUDDI");
80          }
81  
82          static String nodeId = null;
83  
84          private static String getNodeID() {
85                  try {
86                          nodeId = AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID);
87                  } catch (ConfigurationException ex) {
88                          logger.warn(ex.getMessage());
89                          nodeId = "JUDDI_v3";
90                  }
91                  return nodeId;
92          }
93          private static Log logger = LogFactory.getLog(UDDIv2PublishImpl.class);
94          static UDDIPublicationImpl publishService = new UDDIPublicationImpl();
95          static UDDISecurityImpl securityService = new UDDISecurityImpl();
96  
97          @Override
98          public DispositionReport addPublisherAssertions(AddPublisherAssertions body) throws org.uddi.v2_service.DispositionReport {
99                  try {
100                         publishService.addPublisherAssertions(MapUDDIv2Tov3.MapAddPublisherAssertions(body));
101                 } catch (DispositionReportFaultMessage ex) {
102                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
103                 }
104                 return getSuccessMessage();
105         }
106 
107         @Override
108         public DispositionReport deleteBinding(DeleteBinding body) throws org.uddi.v2_service.DispositionReport {
109                 try {
110                         publishService.deleteBinding(MapUDDIv2Tov3.MapDeleteBinding(body));
111                 } catch (DispositionReportFaultMessage ex) {
112                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
113                 }
114                 return getSuccessMessage();
115         }
116 
117         @Override
118         public DispositionReport deleteBusiness(DeleteBusiness body) throws org.uddi.v2_service.DispositionReport {
119                 try {
120                         publishService.deleteBusiness(MapUDDIv2Tov3.MapDeleteBusiness(body));
121                 } catch (DispositionReportFaultMessage ex) {
122                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
123                 }
124                 return getSuccessMessage();
125         }
126 
127         @Override
128         public DispositionReport deletePublisherAssertions(DeletePublisherAssertions body) throws org.uddi.v2_service.DispositionReport {
129                 try {
130                         publishService.deletePublisherAssertions(MapUDDIv2Tov3.MapDeletePublisherAssertion(body));
131                 } catch (DispositionReportFaultMessage ex) {
132                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
133                 }
134                 return getSuccessMessage();
135         }
136 
137         @Override
138         public DispositionReport deleteService(DeleteService body) throws org.uddi.v2_service.DispositionReport {
139                 try {
140                         publishService.deleteService(MapUDDIv2Tov3.MapDeleteService(body));
141                 } catch (DispositionReportFaultMessage ex) {
142                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
143                 }
144                 return getSuccessMessage();
145         }
146 
147         @Override
148         public DispositionReport deleteTModel(DeleteTModel body) throws org.uddi.v2_service.DispositionReport {
149                 try {
150                         publishService.deleteTModel(MapUDDIv2Tov3.MapDeleteTModel(body));
151                 } catch (DispositionReportFaultMessage ex) {
152                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
153                 }
154                 return getSuccessMessage();
155         }
156 
157         @Override
158         public DispositionReport discardAuthToken(DiscardAuthToken body) throws org.uddi.v2_service.DispositionReport {
159                 try {
160                         securityService.discardAuthToken(new org.uddi.api_v3.DiscardAuthToken(body.getAuthInfo()));
161                 } catch (DispositionReportFaultMessage ex) {
162                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
163                 }
164                 return getSuccessMessage();
165         }
166 
167         @Override
168         public AssertionStatusReport getAssertionStatusReport(GetAssertionStatusReport body) throws org.uddi.v2_service.DispositionReport {
169                 try {
170 
171                         return MapUDDIv3Tov2.MapAssertionStatusReport(publishService.getAssertionStatusReport(body.getAuthInfo(), MapUDDIv2Tov3.MapCompletionStatus(body.getCompletionStatus())));
172                 } catch (DispositionReportFaultMessage ex) {
173                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
174                 }
175         }
176 
177         @Override
178         public AuthToken getAuthToken(GetAuthToken body) throws org.uddi.v2_service.DispositionReport {
179                 try {
180                         org.uddi.api_v3.GetAuthToken r = new org.uddi.api_v3.GetAuthToken();
181                         r.setCred(body.getCred());
182                         r.setUserID(body.getUserID());
183                         org.uddi.api_v3.AuthToken authToken = securityService.getAuthToken(r);
184                         AuthToken ret = new AuthToken();
185                         ret.setAuthInfo(authToken.getAuthInfo());
186                         ret.setGeneric("2.0");
187                         ret.setOperator(getNodeID());
188                         return ret;
189                 } catch (DispositionReportFaultMessage ex) {
190                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
191                 }
192         }
193 
194         @Override
195         public PublisherAssertions getPublisherAssertions(GetPublisherAssertions body) throws org.uddi.v2_service.DispositionReport {
196                 try {
197                         return MapUDDIv3Tov2.MapPublisherAssertions(publishService.getPublisherAssertions(body.getAuthInfo()), getNodeID(),getUsername(body.getAuthInfo()));
198                 } catch (DispositionReportFaultMessage ex) {
199                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
200                 }
201         }
202 
203         @Override
204         public RegisteredInfo getRegisteredInfo(GetRegisteredInfo body) throws org.uddi.v2_service.DispositionReport {
205                 try {
206                         return MapUDDIv3Tov2.MapRegisteredInfo(publishService.getRegisteredInfo(MapUDDIv2Tov3.MapGetRegisteredInfo(body)), getNodeID());
207                 } catch (DispositionReportFaultMessage ex) {
208                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
209                 }
210 
211         }
212 
213         @Override
214         public BindingDetail saveBinding(SaveBinding body) throws org.uddi.v2_service.DispositionReport {
215                 try {
216                         return MapUDDIv3Tov2.MapBindingDetail(publishService.saveBinding(MapUDDIv2Tov3.MapSaveBinding(body)), getNodeID());
217                 } catch (DispositionReportFaultMessage ex) {
218                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
219                 }
220         }
221 
222         @Override
223         public BusinessDetail saveBusiness(SaveBusiness body) throws org.uddi.v2_service.DispositionReport {
224                 try {
225                         return MapUDDIv3Tov2.MapBusinessDetail(publishService.saveBusiness(MapUDDIv2Tov3.MapSaveBusiness(body)), getNodeID());
226                 } catch (DispositionReportFaultMessage ex) {
227                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
228                 }
229         }
230 
231         @Override
232         public ServiceDetail saveService(SaveService body) throws org.uddi.v2_service.DispositionReport {
233                 try {
234                         return MapUDDIv3Tov2.MapServiceDetail(publishService.saveService(MapUDDIv2Tov3.MapSaveService(body)), getNodeID());
235                 } catch (DispositionReportFaultMessage ex) {
236                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
237                 }
238         }
239 
240         @Override
241         public TModelDetail saveTModel(SaveTModel body) throws org.uddi.v2_service.DispositionReport {
242                 try {
243                         return MapUDDIv3Tov2.MapTModelDetail(publishService.saveTModel(MapUDDIv2Tov3.MapSaveTModel(body)), getNodeID());
244                 } catch (DispositionReportFaultMessage ex) {
245                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
246                 }
247         }
248 
249         private String getUsername(String authinfo) {
250                 String user = "N/A";
251 
252                 EntityManager em = PersistenceManager.getEntityManager();
253                 EntityTransaction tx = em.getTransaction();
254                 try {
255 
256                         tx.begin();
257                         user = publishService.getEntityPublisher(em, authinfo).getAuthorizedName();
258                         tx.commit();
259                 } catch (Exception ex) {
260                         logger.error(ex);
261                 } finally {
262                         if (tx.isActive()) {
263                                 tx.rollback();
264                         }
265                         em.close();
266                 }
267                 return user;
268         }
269 
270         @Override
271         public PublisherAssertions setPublisherAssertions(SetPublisherAssertions body) throws org.uddi.v2_service.DispositionReport {
272                 try {
273                         Holder<List<PublisherAssertion>> req = new Holder<List<PublisherAssertion>>();
274                         req.value = MapUDDIv2Tov3.MapSetPublisherAssertions(body);
275                         publishService.setPublisherAssertions(body.getAuthInfo(), req);
276 
277                         return MapUDDIv3Tov2.MapPublisherAssertions(req.value, getNodeID(), getUsername(body.getAuthInfo()));
278                 } catch (DispositionReportFaultMessage ex) {
279                         throw MapUDDIv3Tov2.MapException(ex, getNodeID());
280                 }
281         }
282 
283         private DispositionReport getSuccessMessage() {
284                 DispositionReport r = new DispositionReport();
285                 r.setGeneric("2.0");
286                 r.setTruncated(Truncated.FALSE);
287                 Result x = new Result();
288                 r.setOperator(getNodeID());
289                 r.getResult().add(x);
290                 return r;
291         }
292 
293 }