1 package org.objectweb.celtix.js.rhino; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 import javax.xml.namespace.QName ; 9 import javax.xml.transform.Source ; 10 import javax.xml.transform.dom.DOMSource ; 11 import javax.xml.transform.stream.StreamSource ; 12 import javax.xml.ws.Endpoint; 13 14 import org.w3c.dom.Node ; 15 16 import org.apache.xmlbeans.XmlObject; 17 18 import org.mozilla.javascript.Context; 19 import org.mozilla.javascript.Function; 20 import org.mozilla.javascript.Scriptable; 21 import org.mozilla.javascript.ScriptableObject; 22 import org.mozilla.javascript.Wrapper; 23 24 25 public abstract class AbstractDOMProvider { 26 public static class JSDOMProviderException extends Exception { 27 public JSDOMProviderException(String reason) { 28 super(reason); 29 } 30 } 31 32 public static final String NO_WSDL_LOCATION = "no wsdlLocation property found"; 33 public static final String NO_SERVICE_NAME = "no serviceName property found"; 34 public static final String NO_PORT_NAME = "no portName property found"; 35 public static final String NO_TGT_NAMESPACE = "no targetNamespace property found"; 36 public static final String NO_INVOKE = "no invoke property found"; 37 public static final String ILLEGAL_INVOKE_TYPE = "invoke property is not a Function type"; 38 public static final String NO_EP_ADDR = "no endpoint address specified"; 39 40 private Scriptable scriptScope; 41 private Scriptable webSvcProviderVar; 42 private String epAddress; 43 private boolean isBaseAddr; 44 private boolean isE4X; 45 private Function invokeFunc; 46 47 protected AbstractDOMProvider(Scriptable scope, 48 Scriptable wspVar, String epAddr, 49 boolean isBase, boolean e4x) { 50 scriptScope = scope; 51 webSvcProviderVar = wspVar; 52 epAddress = epAddr; 53 isBaseAddr = isBase; 54 isE4X = e4x; 55 } 56 57 public void publish() throws Exception { 58 String addr = epAddress; 59 String wsdlLoc = null; 60 String svcNm = null; 61 String portNm = null; 62 String tgtNmspc = null; 63 String binding = null; 64 Object obj = webSvcProviderVar.get("wsdlLocation", webSvcProviderVar); 65 if (obj == Scriptable.NOT_FOUND) { 66 throw new JSDOMProviderException(NO_WSDL_LOCATION); 67 } 68 if (obj instanceof String ) { 69 wsdlLoc = (String )obj; 70 } 71 obj = webSvcProviderVar.get("serviceName", webSvcProviderVar); 72 if (obj == Scriptable.NOT_FOUND) { 73 throw new JSDOMProviderException(NO_SERVICE_NAME); 74 } 75 if (obj instanceof String ) { 76 svcNm = (String )obj; 77 } 78 obj = webSvcProviderVar.get("portName", webSvcProviderVar); 79 if (obj == Scriptable.NOT_FOUND) { 80 throw new JSDOMProviderException(NO_PORT_NAME); 81 } 82 if (obj instanceof String ) { 83 portNm = (String )obj; 84 } 85 obj = webSvcProviderVar.get("targetNamespace", webSvcProviderVar); 86 if (obj == Scriptable.NOT_FOUND) { 87 throw new JSDOMProviderException(NO_TGT_NAMESPACE); 88 } 89 if (obj instanceof String ) { 90 tgtNmspc = (String )obj; 91 } 92 if (addr == null) { 93 obj = webSvcProviderVar.get("EndpointAddress", scriptScope); 94 if (obj != Scriptable.NOT_FOUND && obj instanceof String ) { 95 addr = (String )obj; 96 isBaseAddr = false; 97 } 98 } 99 if (addr == null) { 100 throw new JSDOMProviderException(NO_EP_ADDR); 101 } 102 if (isBaseAddr) { 103 if (addr.endsWith("/")) { 104 addr += portNm; 105 } else { 106 addr = addr + "/" + portNm; 107 } 108 } 109 obj = webSvcProviderVar.get("BindingType", scriptScope); 110 if (obj != Scriptable.NOT_FOUND && obj instanceof String ) { 111 binding = (String )obj; 112 } 113 obj = webSvcProviderVar.get("invoke", webSvcProviderVar); 114 if (obj == Scriptable.NOT_FOUND) { 115 throw new JSDOMProviderException(NO_INVOKE); 116 } 117 if (obj instanceof Function) { 118 invokeFunc = (Function)obj; 119 } else { 120 throw new JSDOMProviderException(ILLEGAL_INVOKE_TYPE); 121 } 122 Endpoint ep = Endpoint.create(binding, this); 123 List <Source > metadata = new ArrayList <Source >(); 124 metadata.add(new StreamSource (wsdlLoc)); 125 ep.setMetadata(metadata); 126 Map <String , Object > props = new HashMap <String , Object >(); 127 props.put(Endpoint.WSDL_SERVICE, new QName (tgtNmspc, svcNm)); 128 props.put(Endpoint.WSDL_PORT, new QName (tgtNmspc, portNm)); 129 ep.setProperties(props); 130 ep.publish(addr); 131 } 132 133 public DOMSource invoke(DOMSource request) { 134 DOMSource response = new DOMSource (); 135 Context cx = Context.enter(); 136 try { 137 Scriptable scope = cx.newObject(scriptScope); 138 scope.setPrototype(scriptScope); 139 scope.setParentScope(null); 140 Node node = request.getNode(); 141 Object inDoc = null; 142 if (isE4X) { 143 try { 144 Object xo = XmlObject.Factory.parse(node); 145 inDoc = Context.toObject(xo, scope); 146 Object [] args = {inDoc}; 147 inDoc = cx.newObject(scriptScope, "XML", args); 148 } catch (Exception ex) { 149 ex.printStackTrace(); 150 } 151 } else { 152 inDoc = Context.toObject(node, scope); 153 } 154 Object [] args = {inDoc}; 155 Object jsResp = invokeFunc.call(cx, scope, scope, args); 156 if (isE4X) { 157 Scriptable s = (Scriptable)jsResp; 160 Object out = ScriptableObject.callMethod(s, 161 "getXmlObject", 162 Context.emptyArgs); 163 Wrapper wrapped = (Wrapper)out; 164 XmlObject xml = (XmlObject)wrapped.unwrap(); 165 node = xml.getDomNode(); 166 response.setNode(node.getOwnerDocument()); 167 } else { 168 if (jsResp instanceof Wrapper) { 169 jsResp = ((Wrapper)jsResp).unwrap(); 170 } 171 if (jsResp instanceof Node ) { 172 node = (Node )jsResp; 173 response.setNode(node); 174 } 175 } 176 } catch (Exception ex) { 177 ex.printStackTrace(); 178 } finally { 179 Context.exit(); 180 } 181 return response; 182 } 183 } 184 | Popular Tags |