001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.js.rhino;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.transform.Source;
027: import javax.xml.transform.dom.DOMSource;
028: import javax.xml.transform.stream.StreamSource;
029: import javax.xml.ws.Endpoint;
030:
031: import org.w3c.dom.Node;
032:
033: import org.apache.xmlbeans.XmlCursor;
034: import org.apache.xmlbeans.XmlObject;
035:
036: import org.mozilla.javascript.Context;
037: import org.mozilla.javascript.Function;
038: import org.mozilla.javascript.Scriptable;
039: import org.mozilla.javascript.ScriptableObject;
040: import org.mozilla.javascript.Wrapper;
041:
042: public abstract class AbstractDOMProvider {
043: public static class JSDOMProviderException extends Exception {
044: public JSDOMProviderException(String reason) {
045: super (reason);
046: }
047: }
048:
049: public static final String NO_WSDL_LOCATION = "no wsdlLocation property found";
050: public static final String NO_SERVICE_NAME = "no serviceName property found";
051: public static final String NO_PORT_NAME = "no portName property found";
052: public static final String NO_TGT_NAMESPACE = "no targetNamespace property found";
053: public static final String NO_INVOKE = "no invoke property found";
054: public static final String ILLEGAL_INVOKE_TYPE = "invoke property is not a Function type";
055: public static final String NO_EP_ADDR = "no endpoint address specified";
056:
057: private Scriptable scriptScope;
058: private Scriptable webSvcProviderVar;
059: private String epAddress;
060: private boolean isBaseAddr;
061: private boolean isE4X;
062: private Function invokeFunc;
063:
064: protected AbstractDOMProvider(Scriptable scope, Scriptable wspVar,
065: String epAddr, boolean isBase, boolean e4x) {
066: scriptScope = scope;
067: webSvcProviderVar = wspVar;
068: epAddress = epAddr;
069: isBaseAddr = isBase;
070: isE4X = e4x;
071: }
072:
073: public void publish() throws Exception {
074: String addr = epAddress;
075: String wsdlLoc = null;
076: String svcNm = null;
077: String portNm = null;
078: String tgtNmspc = null;
079: String binding = null;
080: Object obj = webSvcProviderVar.get("wsdlLocation",
081: webSvcProviderVar);
082: if (obj == Scriptable.NOT_FOUND) {
083: throw new JSDOMProviderException(NO_WSDL_LOCATION);
084: }
085: if (obj instanceof String) {
086: wsdlLoc = (String) obj;
087: }
088: obj = webSvcProviderVar.get("serviceName", webSvcProviderVar);
089: if (obj == Scriptable.NOT_FOUND) {
090: throw new JSDOMProviderException(NO_SERVICE_NAME);
091: }
092: if (obj instanceof String) {
093: svcNm = (String) obj;
094: }
095: obj = webSvcProviderVar.get("portName", webSvcProviderVar);
096: if (obj == Scriptable.NOT_FOUND) {
097: throw new JSDOMProviderException(NO_PORT_NAME);
098: }
099: if (obj instanceof String) {
100: portNm = (String) obj;
101: }
102: obj = webSvcProviderVar.get("targetNamespace",
103: webSvcProviderVar);
104: if (obj == Scriptable.NOT_FOUND) {
105: throw new JSDOMProviderException(NO_TGT_NAMESPACE);
106: }
107: if (obj instanceof String) {
108: tgtNmspc = (String) obj;
109: }
110: if (addr == null) {
111: obj = webSvcProviderVar.get("EndpointAddress", scriptScope);
112: if (obj != Scriptable.NOT_FOUND && obj instanceof String) {
113: addr = (String) obj;
114: isBaseAddr = false;
115: }
116: }
117: if (addr == null) {
118: throw new JSDOMProviderException(NO_EP_ADDR);
119: }
120: if (isBaseAddr) {
121: if (addr.endsWith("/")) {
122: addr += portNm;
123: } else {
124: addr = addr + "/" + portNm;
125: }
126: }
127: obj = webSvcProviderVar.get("BindingType", scriptScope);
128: if (obj != Scriptable.NOT_FOUND && obj instanceof String) {
129: binding = (String) obj;
130: }
131: obj = webSvcProviderVar.get("invoke", webSvcProviderVar);
132: if (obj == Scriptable.NOT_FOUND) {
133: throw new JSDOMProviderException(NO_INVOKE);
134: }
135: if (obj instanceof Function) {
136: invokeFunc = (Function) obj;
137: } else {
138: throw new JSDOMProviderException(ILLEGAL_INVOKE_TYPE);
139: }
140: Endpoint ep = Endpoint.create(binding, this );
141: List<Source> metadata = new ArrayList<Source>();
142: metadata.add(new StreamSource(wsdlLoc));
143: ep.setMetadata(metadata);
144: Map<String, Object> props = new HashMap<String, Object>();
145: props.put(Endpoint.WSDL_SERVICE, new QName(tgtNmspc, svcNm));
146: props.put(Endpoint.WSDL_PORT, new QName(tgtNmspc, portNm));
147: ep.setProperties(props);
148: ep.publish(addr);
149: }
150:
151: public DOMSource invoke(DOMSource request) {
152: DOMSource response = new DOMSource();
153: Context cx = Context.enter();
154: try {
155: Scriptable scope = cx.newObject(scriptScope);
156: scope.setPrototype(scriptScope);
157: scope.setParentScope(null);
158: Node node = request.getNode();
159: Object inDoc = null;
160: if (isE4X) {
161: try {
162: XmlObject xo = XmlObject.Factory.parse(node);
163: XmlCursor cursor = xo.newCursor();
164: // strip comments out, as xmlbeans doesn't
165: // seem to like them
166: do {
167: if (cursor.isComment()) {
168: cursor.removeXml();
169: }
170: } while (cursor.toNextToken() != XmlCursor.TokenType.NONE);
171: cursor.dispose();
172: inDoc = Context.toObject(xo, scope);
173: Object[] args = { inDoc };
174: inDoc = cx.newObject(scriptScope, "XML", args);
175: } catch (Exception ex) {
176: ex.printStackTrace();
177: }
178: } else {
179: inDoc = Context.toObject(node, scope);
180: }
181: Object[] args = { inDoc };
182: Object jsResp = invokeFunc.call(cx, scope, scope, args);
183: if (isE4X) {
184: // need to check return type and throw exception
185: // if wrong type
186: Scriptable s = (Scriptable) jsResp;
187: Object out = ScriptableObject.callMethod(s,
188: "getXmlObject", Context.emptyArgs);
189: Wrapper wrapped = (Wrapper) out;
190: XmlObject xml = (XmlObject) wrapped.unwrap();
191: node = xml.getDomNode();
192: response.setNode(node.getOwnerDocument());
193: } else {
194: if (jsResp instanceof Wrapper) {
195: jsResp = ((Wrapper) jsResp).unwrap();
196: }
197: if (jsResp instanceof Node) {
198: node = (Node) jsResp;
199: response.setNode(node);
200: }
201: }
202: } catch (Exception ex) {
203: ex.printStackTrace();
204: } finally {
205: Context.exit();
206: }
207: return response;
208: }
209: }
|