View Javadoc
1   /*
2    * Copyright 2001-2010 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.wrapper;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.StringWriter;
21  import java.util.HashMap;
22  import javax.xml.XMLConstants;
23  
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.TransformerFactory;
27  import javax.xml.transform.dom.DOMSource;
28  import javax.xml.transform.stream.StreamResult;
29  
30  import org.apache.juddi.v3.client.ClassUtil;
31  import org.apache.juddi.v3.client.config.UDDIClient;
32  import org.apache.juddi.v3.client.config.UDDIClientContainer;
33  import org.apache.juddi.v3.client.transport.Transport;
34  import org.uddi.api_v3.FindBinding;
35  import org.uddi.api_v3.FindBusiness;
36  import org.uddi.api_v3.FindRelatedBusinesses;
37  import org.uddi.api_v3.FindService;
38  import org.uddi.api_v3.FindTModel;
39  import org.uddi.api_v3.GetBindingDetail;
40  import org.uddi.api_v3.GetBusinessDetail;
41  import org.uddi.api_v3.GetOperationalInfo;
42  import org.uddi.api_v3.GetServiceDetail;
43  import org.uddi.api_v3.GetTModelDetail;
44  import org.uddi.v3_service.UDDIInquiryPortType;
45  import org.w3c.dom.Document;
46  import org.w3c.dom.Element;
47  import org.w3c.dom.Node;
48  
49  /**
50   * @author Tom Cunningham (tcunning@apache.org)
51   */
52  public class UDDIInquiryService {
53  	
54  	private final static String DEFAULT_NODE_NAME = "default";
55  
56  	private String clientName = null;
57  	private String nodeName = null;
58  	private HashMap<String, Handler> operations = null;
59  
60  	public UDDIInquiryService() {
61  		super();
62  		clientName = System.getProperty("org.apache.juddi.v3.client.name");
63  		nodeName    = System.getProperty("org.apache.juddi.v3.client.node.name",DEFAULT_NODE_NAME);
64  		operations = new HashMap<String, Handler>();
65  		operations.put("find_business", new Handler("findBusiness", FindBusiness.class));
66  		operations.put("find_service", new Handler("findService", FindService.class));
67  		operations.put("find_binding", new Handler("findBinding", FindBinding.class));
68  		operations.put("find_tModel", new Handler ("findTModel", FindTModel.class));
69  		operations.put("find_relatedBusinesses", new Handler("findRelatedBusinesses", FindRelatedBusinesses.class));
70  		operations.put("get_businessDetail", new Handler("getBusinessDetail", GetBusinessDetail.class));
71  		operations.put("get_serviceDetail", new Handler("getServiceDetail", GetServiceDetail.class));
72  		operations.put("get_bindingDetail", new Handler("getBindingDetail", GetBindingDetail.class));
73  		operations.put("get_tModelDetail", new Handler("getTModelDetail", GetTModelDetail.class));
74  		operations.put("get_operationalInfo", new Handler("getOperationalInfo", GetOperationalInfo.class));
75  	}
76  
77  
78  
79  	//Verify that the appropriate endpoint was targeted for
80  	// this service request.  The validateRequest method will
81  	// throw an UnsupportedOperationException if anything's amiss.
82  	public void validateRequest(String operation)
83  	    throws UnsupportedOperationException
84  	{
85  	    if ((operation == null) || (operation.trim().length() == 0))
86  	    	throw new UnsupportedOperationException("operation " + operation + " not supported");
87  	}
88  
89  	public Node inquire(Element uddiReq) throws Exception {
90  	    return inquire(uddiReq, nodeName, clientName);
91  	}
92  	
93  	public Node inquire(Element uddiReq, String nodeName, String clientName) throws Exception {
94  	    UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
95  	    String clazz = client.getClientConfig().getUDDINode(nodeName).getProxyTransport();
96              Class<?> transportClass = ClassUtil.forName(clazz,this.getClass());
97              Transport transport = (Transport) transportClass.getConstructor(String.class, String.class).newInstance(clientName, nodeName);
98              UDDIInquiryPortType inquiry = transport.getUDDIInquiryService();
99  
100 	    //new RequestHandler on it's own thread
101 	    RequestHandler requestHandler = new RequestHandler();
102 	    requestHandler.setPortType(inquiry);
103 
104 	    String operation = requestHandler.getOperation(uddiReq);
105 	    Handler opHandler = operations.get(operation);
106 	    if (opHandler == null) {
107 	        throw new IllegalArgumentException("Operation not found: " + operation);
108 	    }
109 	    requestHandler.setMethodName(opHandler.getMethodName());
110 	    requestHandler.setOperationClass(opHandler.getParameter());
111 
112 	    @SuppressWarnings("unused")
113 	    String version   = requestHandler.getVersion(uddiReq,operation);
114 	    validateRequest(operation);
115 	    return requestHandler.invoke(uddiReq);
116 	}
117 
118 	public String inquire(UDDIInquiryPortType inquiry, String request) throws Exception {
119 	    java.io.InputStream sbis = new ByteArrayInputStream(request.getBytes());
120 	    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
121             dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
122 	    dbf.setNamespaceAware(true);
123 	    dbf.setValidating(false);
124 	    DocumentBuilder db = dbf.newDocumentBuilder();
125 	    Document doc = db.parse(sbis);
126 	    Element reqElem = doc.getDocumentElement();
127 
128 	    RequestHandler requestHandler = new RequestHandler();
129 	    requestHandler.setPortType(inquiry);
130 
131 	    String operation = reqElem.getTagName().toString();
132 	    Handler opHandler = operations.get(operation);
133             if (opHandler == null) {
134                 throw new IllegalArgumentException("Operation not found: " + operation);
135             }
136 
137 	    requestHandler.setMethodName(opHandler.getMethodName());
138 	    requestHandler.setOperationClass(opHandler.getParameter());
139 
140 	    Node n = requestHandler.invoke(reqElem);
141 
142 	    StringWriter sw = new StringWriter();
143             TransformerFactory factory = TransformerFactory.newInstance();
144             factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
145             factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
146             Transformer t = factory.newTransformer();
147             
148 	    t.transform(new DOMSource(n), new StreamResult(sw));
149 	    return sw.toString();
150 	}
151 }