View Javadoc
1   /*
2    * Copyright 2013 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.adminconsole.hub;
17  
18  import java.io.StringReader;
19  import java.io.StringWriter;
20  import java.util.GregorianCalendar;
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  import javax.xml.XMLConstants;
24  import javax.xml.bind.JAXB;
25  import javax.xml.datatype.DatatypeConfigurationException;
26  import javax.xml.datatype.DatatypeFactory;
27  import javax.xml.transform.OutputKeys;
28  import javax.xml.transform.Transformer;
29  import javax.xml.transform.TransformerFactory;
30  import javax.xml.transform.stream.StreamResult;
31  import javax.xml.transform.stream.StreamSource;
32  import org.apache.commons.configuration.ConfigurationException;
33  import org.apache.juddi.api_v3.AdminSaveBusiness;
34  import org.apache.juddi.api_v3.AdminSaveBusinessWrapper;
35  import org.apache.juddi.api_v3.AdminSaveSubscriptionRequest;
36  import org.apache.juddi.api_v3.AdminSaveTModel;
37  import org.apache.juddi.api_v3.AdminSaveTModelWrapper;
38  import org.apache.juddi.api_v3.Clerk;
39  import org.apache.juddi.api_v3.ClientSubscriptionInfo;
40  import org.apache.juddi.api_v3.SaveClientSubscriptionInfo;
41  import org.apache.juddi.api_v3.SyncSubscription;
42  import org.apache.juddi.config.AppConfig;
43  import org.apache.juddi.config.Property;
44  import org.apache.juddi.v3.client.UDDIConstants;
45  import org.apache.juddi.v3.client.cryptor.XmlUtils;
46  import org.uddi.api_v3.BusinessEntity;
47  import org.uddi.api_v3.Contact;
48  import org.uddi.api_v3.FindBusiness;
49  import org.uddi.api_v3.FindQualifiers;
50  import org.uddi.api_v3.Name;
51  import org.uddi.api_v3.PersonName;
52  import org.uddi.api_v3.TModel;
53  import org.uddi.repl_v3.CommunicationGraph;
54  import org.uddi.repl_v3.Operator;
55  import org.uddi.repl_v3.OperatorStatusType;
56  import org.uddi.repl_v3.ReplicationConfiguration;
57  import org.uddi.sub_v3.CoveragePeriod;
58  import org.uddi.sub_v3.GetSubscriptionResults;
59  import org.uddi.sub_v3.Subscription;
60  import org.uddi.sub_v3.SubscriptionFilter;
61  
62  /**
63   * This class generates XML as String objects for UDDI requests. This is used
64   * from the "advanced" web pages
65   *
66   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
67   */
68  public class JUDDIRequestsAsXML {
69  
70          private static String PrettyPrintXML(String input) {
71                  if (input == null || input.length() == 0) {
72                          return "";
73                  }
74                  try {
75                          TransformerFactory transFactory = TransformerFactory.newInstance();
76                          transFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
77                          transFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
78                          Transformer transformer = transFactory.newTransformer();
79                          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
80                          transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
81                          //initialize StreamResult with File object to save to file
82                          StreamResult result = new StreamResult(new StringWriter());
83                          StreamSource source = new StreamSource(new StringReader(input.trim()));
84                          transformer.transform(source, result);
85                          String xmlString = result.getWriter().toString();
86                          return (xmlString);
87                  } catch (Exception ex) {
88                  }
89                  return null;
90          }
91  
92          public static String getSampleXML(String method) {
93                  StringWriter sw = new StringWriter();
94  
95                  if (method.equalsIgnoreCase("save_ClientSubscriptionInfo")) {
96                          SaveClientSubscriptionInfo obj = new SaveClientSubscriptionInfo();
97                          obj.getClientSubscriptionInfo().add(new ClientSubscriptionInfo());
98                          obj.getClientSubscriptionInfo().get(0).setFromClerk(new Clerk());
99                          obj.getClientSubscriptionInfo().get(0).setToClerk(new Clerk());
100                         obj.getClientSubscriptionInfo().get(0).setSubscriptionKey("key");
101 
102                         JAXB.marshal(obj, sw);
103                 }
104                 if (method.equalsIgnoreCase("invoke_SyncSubscription")) {
105                         SyncSubscription obj = new SyncSubscription();
106                         obj.getGetSubscriptionResultsList().add(new GetSubscriptionResults());
107                         obj.getGetSubscriptionResultsList().get(0).setSubscriptionKey("key");
108                         obj.getGetSubscriptionResultsList().get(0).setCoveragePeriod(new CoveragePeriod());
109                         DatatypeFactory newInstance;
110                         try {
111                                 newInstance = DatatypeFactory.newInstance();
112                                 obj.getGetSubscriptionResultsList().get(0).getCoveragePeriod().setEndPoint(newInstance.newXMLGregorianCalendar(new GregorianCalendar()));
113                                 obj.getGetSubscriptionResultsList().get(0).getCoveragePeriod().setStartPoint(newInstance.newXMLGregorianCalendar(new GregorianCalendar()));
114 
115                         } catch (DatatypeConfigurationException ex) {
116                                 Logger.getLogger(JUDDIRequestsAsXML.class.getName()).log(Level.SEVERE, null, ex);
117                         }
118                         JAXB.marshal(obj, sw);
119                 }
120                 if (method.equalsIgnoreCase("admin_SaveBusiness")) {
121                         AdminSaveBusiness obj = new AdminSaveBusiness();
122                         obj.getValues().add(new AdminSaveBusinessWrapper());
123                         obj.getValues().get(0).setPublisherID("username");
124                         obj.getValues().get(0).getBusinessEntity().add(new BusinessEntity());
125                         obj.getValues().get(0).getBusinessEntity().get(0).getName().add(new Name("Business Name", "en"));
126                         JAXB.marshal(obj, sw);
127                 }
128                 if (method.equalsIgnoreCase("admin_SaveTModel")) {
129                         AdminSaveTModel obj = new AdminSaveTModel();
130                         obj.getValues().add(new AdminSaveTModelWrapper());
131                         obj.getValues().get(0).setPublisherID("username");
132                         obj.getValues().get(0).getTModel().add(new TModel());
133                         obj.getValues().get(0).getTModel().get(0).setName(new Name("TModel Name", "en"));
134                         JAXB.marshal(obj, sw);
135                 }
136 
137                 if (method.equalsIgnoreCase("admin_SaveSubscription")) {
138                         AdminSaveSubscriptionRequest obj = new AdminSaveSubscriptionRequest();
139                         obj.setPublisherOrUsername("username");
140                         obj.getSubscriptions().add(new Subscription());
141                         obj.getSubscriptions().get(0).setSubscriptionFilter(new SubscriptionFilter());
142                         obj.getSubscriptions().get(0).setBrief(Boolean.TRUE);
143                         obj.getSubscriptions().get(0).getSubscriptionFilter().setFindBusiness(new FindBusiness());
144                         obj.getSubscriptions().get(0).getSubscriptionFilter().getFindBusiness().getName().add(new Name(UDDIConstants.WILDCARD, null));
145                         obj.getSubscriptions().get(0).getSubscriptionFilter().getFindBusiness().setFindQualifiers(new FindQualifiers());
146                         obj.getSubscriptions().get(0).getSubscriptionFilter().getFindBusiness().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
147                         obj.getSubscriptions().get(0).getSubscriptionFilter().getFindBusiness().getFindQualifiers().getFindQualifier().add(UDDIConstants.CASE_INSENSITIVE_MATCH);
148                         //obj.getSubscriptions().get(0).getSubscriptionFilter().getFindBusiness()
149                         JAXB.marshal(obj, sw);
150                 }
151 
152                 if (method.equalsIgnoreCase("set_ReplicationNodes")) {
153                         ReplicationConfiguration replicationConfiguration = new ReplicationConfiguration();
154                         replicationConfiguration.setCommunicationGraph(new CommunicationGraph());
155                         String thisnode = "NODEID";
156                         try {
157                                 thisnode = AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID);
158                         } catch (ConfigurationException ex) {
159                                 Logger.getLogger(JUDDIRequestsAsXML.class.getName()).log(Level.SEVERE, null, ex);
160                         }
161 
162                         replicationConfiguration.getCommunicationGraph().getNode().add(thisnode);
163                         Operator op = new Operator();
164                         op.setOperatorNodeID(thisnode);
165 
166                         op.setOperatorStatus(OperatorStatusType.NORMAL);
167                         String url = "http://localhost:8080/juddiv3/services/replication";
168                         try {
169                                 url = AppConfig.getConfiguration().getString(Property.DEFAULT_BASE_URL_SECURE) + "/services/replication";
170                         } catch (ConfigurationException ex) {
171                                 Logger.getLogger(JUDDIRequestsAsXML.class.getName()).log(Level.SEVERE, null, ex);
172                         }
173                         op.setSoapReplicationURL(url);
174 
175                         replicationConfiguration.getOperator().add(op);
176                         replicationConfiguration.setRegistryContact(new ReplicationConfiguration.RegistryContact());
177                         replicationConfiguration.getRegistryContact().setContact(new Contact());
178                         replicationConfiguration.getRegistryContact().getContact().getPersonName().add(new PersonName("UNKNOWN", "en"));
179 
180                         JAXB.marshal(replicationConfiguration, sw);
181                 }
182 
183                 return PrettyPrintXML(sw.toString());
184         }
185 
186         public static Object getObjectJuddi(String method, String content) {
187                 StringReader sr = new StringReader(content);
188 
189                 if (method.equalsIgnoreCase("save_ClientSubscriptionInfo")) {
190                         return XmlUtils.unmarshal(sr, SaveClientSubscriptionInfo.class);
191                 }
192                 if (method.equalsIgnoreCase("invoke_SyncSubscription")) {
193                         return XmlUtils.unmarshal(sr, SyncSubscription.class);
194                 }
195                 if (method.equalsIgnoreCase("admin_SaveBusiness")) {
196                         return XmlUtils.unmarshal(sr, AdminSaveBusiness.class);
197                 }
198                 if (method.equalsIgnoreCase("admin_SaveTModel")) {
199                         //System.out.println(content);
200                         return XmlUtils.unmarshal(sr, AdminSaveTModel.class);
201                 }
202 
203                 if (method.equalsIgnoreCase("admin_SaveSubscription")) {
204                         return JAXB.unmarshal(sr, AdminSaveSubscriptionRequest.class);
205                 }
206 
207                 if (method.equalsIgnoreCase("set_ReplicationNodes")) {
208                         return JAXB.unmarshal(sr, ReplicationConfiguration.class);
209                 }
210 
211                 return null;
212         }
213 
214 }