Merged in brasseld/openesb-components (pull request #8)

Access to imported WSDL from concrete WSDL through Load Balancer
master
David Brassely 2013-03-20 16:51:55 +01:00
commit 6306792bd3
2 changed files with 348 additions and 281 deletions

View File

@ -3,6 +3,7 @@
*/
package com.sun.jbi.httpsoapbc;
import com.ibm.wsdl.Constants;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.InetAddress;
@ -57,21 +58,16 @@ public class WsdlQueryHelper {
private String lbHost;
private String lbPort;
private Definition definition;
// private Definition definition;
private boolean isWsdl;
private String localhostFQDN;
private Document doc;
private ByteBuffer byteBuffer;
/**
* this is the port on which http-bc is running
*/
String httpbcPort;
private CoyoteRequest request;
private static Messages mMessages = Messages.getMessages(WsdlQueryHelper.class);
private static Logger mLog = Messages.getLogger(WsdlQueryHelper.class);
@ -95,6 +91,7 @@ public class WsdlQueryHelper {
localhostFQDN = InetAddress.getLocalHost().getCanonicalHostName().toString();
/*
if (isWsdl) {
WSDLFactoryImpl wsdlFactory = new WSDLFactoryImpl();
WSDLReader reader = (WSDLReader) wsdlFactory.newWSDLReader();
@ -104,6 +101,7 @@ public class WsdlQueryHelper {
this.definition = reader.readWSDL("", source);
} else {
* */
DocumentBuilderFactory builderF = DocumentBuilderFactory.newInstance();
builderF.setNamespaceAware(true);
@ -111,7 +109,7 @@ public class WsdlQueryHelper {
doc = builder.parse(new ByteArrayInputStream(def.array()));
}
//}
} catch (Exception e) {
if (mLog.isLoggable(Level.FINE)) {
@ -121,14 +119,13 @@ public class WsdlQueryHelper {
}
public WsdlQueryHelper(CoyoteRequest request, int rPort, Definition def, boolean wsdl) {
public WsdlQueryHelper(CoyoteRequest request, int rPort, ByteArrayOutputStream baos, boolean wsdl) {
this.isWsdl = wsdl;
this.request = request;
this.definition = def;
// this.definition = def;
lbHost = request.getServerName();
lbPort = Integer.toString(request.getServerPort());
@ -137,7 +134,14 @@ public class WsdlQueryHelper {
try {
localhostFQDN = InetAddress.getLocalHost().getCanonicalHostName().toString();
} catch (UnknownHostException e) {
DocumentBuilderFactory builderF = DocumentBuilderFactory.newInstance();
builderF.setNamespaceAware(true);
DocumentBuilder builder = builderF.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(baos.toByteArray()));
} catch (Exception e) {
if (mLog.isLoggable(Level.FINE)) {
mLog.log(Level.FINE, "the proxy host may be unknown.", e);
}
@ -145,6 +149,7 @@ public class WsdlQueryHelper {
}
/*
public Definition getServiceDescriptor() {
try {
@ -158,7 +163,7 @@ public class WsdlQueryHelper {
}
}
return definition;
}
}*/
public ByteBuffer getServiceDescriptorAsByteBuffer() {
@ -166,11 +171,56 @@ public class WsdlQueryHelper {
* check if the request is a direct request, return the byte-buffer if
* true
*/
if (isDirectRequest()) {
return byteBuffer;
}
if (isWsdl) {
try {
updateImportDom(doc.getDocumentElement());
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Exception : " + ex);
if (mLog.isLoggable(Level.FINE)) {
mLog.log(Level.FINE, "unable to modify the wsdl.", ex);
}
}
} else {// this is xsd
try {
updateSchemaDom(doc.getDocumentElement());
} catch (Exception e) {
if (mLog.isLoggable(Level.FINE)) {
mLog.log(Level.FINE, "unable to modify the xsd.", e);
}
}
}
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String encoding = doc.getXmlEncoding();
Transformer trans = TransformerFactory.newInstance().newTransformer();
//trans.setOutputProperty(OutputKeys.ENCODING, encoding);
if (encoding == null) {
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
} else {
trans.setOutputProperty(OutputKeys.ENCODING, encoding);
}
//changes ends here
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
trans.setOutputProperty(OutputKeys.METHOD, "xml");
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, false ? "yes" : "no");
trans.transform(new DOMSource(doc), new StreamResult(baos));
return ByteBuffer.wrap(baos.toByteArray());
} catch (Exception e) {
if (mLog.isLoggable(Level.FINE)) {
mLog.log(Level.FINE, "not able to transform.", e);
}
}
/*
if (isWsdl) {
try {
WSDLFactory wsdlFactory = (WSDLFactory) WSDLFactory.newInstance();
@ -212,6 +262,7 @@ public class WsdlQueryHelper {
}
}
}
*/
return null;
}
@ -246,7 +297,7 @@ public class WsdlQueryHelper {
}
@SuppressWarnings("unchecked")
public void modifyWSDL(Definition def) throws Exception {
private void modifyWSDL(Definition def) throws Exception {
// Search all wsdl imports for the resourceName
Iterator importLists = def.getImports().values().iterator();
@ -431,6 +482,18 @@ public class WsdlQueryHelper {
return result;
}
private void updateImportDom(Element el) throws Exception {
Element tempEl = DOMUtils.getFirstChildElement(el);
for (; tempEl != null; tempEl = DOMUtils.getNextSiblingElement(tempEl)) {
QName tempElType = QNameUtils.newQName(tempEl);
if (Constants.Q_ELEM_IMPORT.equals(tempElType)) {
updateImportDomReference(tempEl);
}
}
}
private void updateSchemaDom(Element el) throws Exception {
Element tempEl = DOMUtils.getFirstChildElement(el);
@ -444,6 +507,19 @@ public class WsdlQueryHelper {
}
}
private void updateImportDomReference(Element tempEl) throws Exception {
String locationURI = DOMUtils.getAttribute(tempEl, Constants.ATTR_LOCATION);
if (locationURI != null) {
if (mLog.isLoggable(Level.FINE)) {
mLog.log(Level.FINE, "nlocationURI = " + locationURI);
}
String result = getResultUrl(locationURI);
setAttribute(tempEl, Constants.ATTR_LOCATION, result);
}
}
private void updateSchemaDomReference(Element tempEl) throws Exception {
String locationURI = DOMUtils.getAttribute(tempEl, SchemaConstants.ATTR_SCHEMA_LOCATION);
if (locationURI != null) {
@ -468,13 +544,4 @@ public class WsdlQueryHelper {
private String getlocalhostFQDN() {
return localhostFQDN;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

View File

@ -23,7 +23,7 @@ import com.ibm.wsdl.extensions.schema.SchemaImpl;
*/
public class WsdlQueryHelperTest extends TestCase {
private CoyoteRequest req = new DummyCoyoteRequest();
private CoyoteRequest req = new WsdlQueryHelperTest.DummyCoyoteRequest();
@Override
@ -50,7 +50,7 @@ public class WsdlQueryHelperTest extends TestCase {
System.out.println(new String(baos.toByteArray()));
WsdlQueryHelper helper = new WsdlQueryHelper(req, 9080, def, true);
WsdlQueryHelper helper = new WsdlQueryHelper(req, 9080, baos, true);
ByteBuffer buffer = helper.getServiceDescriptorAsByteBuffer();
System.out.println(new String(buffer.array()));