This project has retired. For details please refer to its Attic page.
RequestHandler xref
View Javadoc
1   /*
2    * Copyright 2001-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  package org.apache.juddi.v3.client.transport.wrapper;
17  
18  import java.io.StringWriter;
19  import java.lang.reflect.Method;
20  import java.rmi.Remote;
21  import java.util.List;
22  import javax.xml.XMLConstants;
23  
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.parsers.ParserConfigurationException;
27  import javax.xml.transform.Transformer;
28  import javax.xml.transform.TransformerException;
29  import javax.xml.transform.TransformerFactory;
30  import javax.xml.transform.dom.DOMSource;
31  import javax.xml.transform.stream.StreamResult;
32  import javax.xml.ws.Holder;
33  
34  import org.apache.juddi.jaxb.JAXBMarshaller;
35  import org.uddi.api_v3.AssertionStatusItem;
36  import org.uddi.api_v3.AssertionStatusReport;
37  import org.uddi.api_v3.CompletionStatus;
38  import org.uddi.api_v3.DispositionReport;
39  import org.uddi.api_v3.GetAssertionStatusReport;
40  import org.uddi.api_v3.GetPublisherAssertions;
41  import org.uddi.api_v3.PublisherAssertion;
42  import org.uddi.api_v3.PublisherAssertions;
43  import org.uddi.api_v3.PublisherAssertionsResponse;
44  import org.uddi.api_v3.SetPublisherAssertions;
45  import org.uddi.v3_service.DispositionReportFaultMessage;
46  import org.w3c.dom.Document;
47  import org.w3c.dom.Element;
48  import org.w3c.dom.Node;
49  
50  /**
51   * @author Tom Cunningham (tcunning@apache.org)
52   * @author Kurt Stam (kurt.stam@redhat.com)
53   */
54  public class RequestHandler {
55    // private reference to the webapp's logger.
56          //private static Log log = LogFactory.getLog(RequestHandler.class);
57  
58          // XML Document Builder
59          private static DocumentBuilder docBuilder = null;
60  
61          private volatile String version;
62          private volatile String operation;
63  
64          private volatile Remote portType;
65          private volatile String methodName;
66          private volatile Class<?> operationClass;
67          private static TransformerFactory transFactory = null;
68  
69          /**
70           * Grab the local name of the UDDI request element from the UDDI
71           * Request. If a value isn't returned (either null or an empty String is
72           * returned) then throw a FatalError exception. This is probably a
73           * configuration problem related to the XML Parser that jUDDI is using.
74           *
75           * @param uddiReq
76           * @return the operation name
77           * @throws Exception
78           */
79          public String getOperation(Element uddiReq) throws Exception {
80                  if (uddiReq == null) {
81                          throw new UnsupportedOperationException("UDDI Request is null");
82                  }
83  
84                  String operation = uddiReq.getLocalName();
85                  if ((operation == null) || (operation.trim().length() == 0)) {
86                          throw new UnsupportedOperationException("operation " + operation + " not supported");
87                  }
88                  setOperation(operation);
89                  return operation;
90          }
91  
92          /**
93           * Grab the generic attribute value (version value). If one isn't
94           * specified or the value specified is not "2.0" then throw an exception
95           * (this value must be specified for all UDDI requests and currently
96           * only version 2.0 UDDI requests are supported).
97           *
98           * @param uddiReq
99           * @return version
100          * @throws Exception
101          */
102         public String getVersion(Element uddiReq, String operation) throws Exception {
103                 String version = uddiReq.getAttribute("generic");
104                 if ((version == null) || ("".equals(version))) {
105                         if ("urn:uddi-org:api_v3".equals(uddiReq.getNamespaceURI())) {
106                                 version = "3.0";
107                         }
108                 }
109                 if (!"3.0".equals(version)) {
110                         throw new UnsupportedOperationException("version needs to be 3.0");
111                 }
112                 setVersion(version);
113                 return version;
114         }
115 
116         public static synchronized String getText(Element element) throws TransformerException {
117                 if (transFactory == null) {
118                         transFactory = TransformerFactory.newInstance();
119                         transFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
120                         transFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
121                 }
122                 Transformer trans = transFactory.newTransformer();
123                 StringWriter sw = new StringWriter();
124                 trans.transform(new DOMSource(element), new StreamResult(sw));
125                 return sw.toString();
126         }
127 
128         @SuppressWarnings("unchecked")
129         public Node invoke(Element uddiReq) throws Exception {
130                 Node response = null;
131     // Create a new 'temp' XML element to use as a container 
132                 // in which to marshal the UDDI response data into.
133                 DocumentBuilder docBuilder = getDocumentBuilder();
134                 Document document = docBuilder.newDocument();
135                 Element element = document.createElement("temp");
136                 try {
137       // Lookup the appropriate XML handler.  Throw an 
138                         // UnsupportedException if one could not be located.
139                         //String reqString = getText(uddiReq);
140                         //Object uddiReqObj = JAXBMarshaller.unmarshallFromString(reqString, "org.uddi.api_v3");
141                         Object uddiReqObj = JAXBMarshaller.unmarshallFromElement(uddiReq, "org.uddi.api_v3");
142                         Object result = null;
143                         if (operationClass.equals(GetAssertionStatusReport.class)) {
144                                 GetAssertionStatusReport getAssertionStatusReport = (GetAssertionStatusReport) uddiReqObj;
145                                 Method method = portType.getClass().getMethod(methodName, String.class, CompletionStatus.class);
146                                 result = method.invoke(portType, getAssertionStatusReport.getAuthInfo(), getAssertionStatusReport.getCompletionStatus());
147                                 AssertionStatusReport assertionStatusReport = new AssertionStatusReport();
148                                 assertionStatusReport.getAssertionStatusItem().addAll((List<AssertionStatusItem>) result);
149                                 result = assertionStatusReport;
150                         } else if (operationClass.equals(SetPublisherAssertions.class)) {
151                                 SetPublisherAssertions setPublisherAssertions = (SetPublisherAssertions) uddiReqObj;
152                                 Method method = portType.getClass().getMethod(methodName, String.class, Holder.class);
153                                 Holder<List<PublisherAssertion>> holder = new Holder<List<PublisherAssertion>>(setPublisherAssertions.getPublisherAssertion());
154                                 result = method.invoke(portType, setPublisherAssertions.getAuthInfo(), holder);
155                                 PublisherAssertions assertions = new PublisherAssertions();
156                                 if (holder.value != null) {
157                                         assertions.getPublisherAssertion().addAll(holder.value);
158                                 }
159                                 result = assertions;
160                         } else if (operationClass.equals(GetPublisherAssertions.class)) {
161                                 GetPublisherAssertions getPublisherAssertions = (GetPublisherAssertions) uddiReqObj;
162                                 Method method = portType.getClass().getMethod(methodName, String.class);
163                                 result = method.invoke(portType, getPublisherAssertions.getAuthInfo());
164                                 List<PublisherAssertion> assertionList = (List<PublisherAssertion>) result;
165                                 PublisherAssertionsResponse publisherAssertionsResponse = new PublisherAssertionsResponse();
166                                 if (assertionList != null) {
167                                         publisherAssertionsResponse.getPublisherAssertion().addAll(assertionList);
168                                 }
169                                 result = publisherAssertionsResponse;
170                         } else {
171                                 Method method = portType.getClass().getMethod(methodName, operationClass);
172                                 result = method.invoke(portType, (Object) uddiReqObj);
173                         }
174 
175       // Lookup the appropriate response handler which will
176                         // be used to marshal the UDDI object into the appropriate 
177                         // xml format.
178                         /*
179                          IHandler responseHandler = maker.lookup(uddiResObj.getClass().getName());
180                          if (responseHandler == null)
181                          throw new FatalErrorException("The response object " +
182                          "type is unknown: " +uddiResObj.getClass().getName());
183                          */
184       // Lookup the appropriate response handler and marshal 
185                         // the juddi object into the appropriate xml format (we 
186                         // only support UDDI v2.0 at this time).  Attach the
187                         // results to the body of the SOAP response.
188                         if (result != null) {
189                                 JAXBMarshaller.marshallToElement(result, "org.uddi.api_v3", element);
190        // Grab a reference to the 'temp' element's
191                                 // only child here (this has the effect of
192                                 // discarding the temp element) and append 
193                                 // this child to the soap response body
194                                 document.appendChild(element.getFirstChild());
195                         }
196                         response = document;
197                 } catch (Exception e) {
198                         DispositionReport dr = DispositionReportFaultMessage.getDispositionReport(e);
199                         if (dr != null) {
200                                 JAXBMarshaller.marshallToElement(dr, "org.uddi.api_v3", element);
201                                 document.appendChild(element.getFirstChild());
202                                 response = document;
203                         } else {
204                                 throw e;
205                         }
206                         //log.error(e.getMessage(),e);
207                 }
208                 return response;
209         }
210 
211         /**
212          *
213          */
214         private DocumentBuilder getDocumentBuilder() {
215                 if (docBuilder == null) {
216                         docBuilder = createDocumentBuilder();
217                 }
218                 return docBuilder;
219         }
220 
221         /**
222          *
223          */
224         private synchronized DocumentBuilder createDocumentBuilder() {
225                 if (docBuilder != null) {
226                         return docBuilder;
227                 }
228 
229                 try {
230                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
231                         factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
232                         factory.setNamespaceAware(true);
233                         //factory.setValidating(true);
234 
235                         docBuilder = factory.newDocumentBuilder();
236                 } catch (ParserConfigurationException pcex) {
237                         pcex.printStackTrace();
238                 }
239 
240                 return docBuilder;
241         }
242 
243         public String getOperation() {
244                 return operation;
245         }
246 
247         public void setOperation(String operation) {
248                 this.operation = operation;
249         }
250 
251         public Remote getPortType() {
252                 return portType;
253         }
254 
255         public void setPortType(Remote portType) {
256                 this.portType = portType;
257         }
258 
259         public String getMethodName() {
260                 return methodName;
261         }
262 
263         public void setMethodName(String methodName) {
264                 this.methodName = methodName;
265         }
266 
267         public Class<?> getOperationClass() {
268                 return operationClass;
269         }
270 
271         public void setOperationClass(Class<?> operationClass) {
272                 this.operationClass = operationClass;
273         }
274 
275         public String getVersion() {
276                 return version;
277         }
278 
279         public void setVersion(String version) {
280                 this.version = version;
281         }
282 
283 }