001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.http.endpoints;
018:
019: import java.io.IOException;
020:
021: import javax.jbi.management.DeploymentException;
022: import javax.jbi.servicedesc.ServiceEndpoint;
023: import javax.wsdl.Definition;
024: import javax.wsdl.Port;
025: import javax.wsdl.Service;
026: import javax.wsdl.WSDLException;
027: import javax.wsdl.extensions.soap.SOAPAddress;
028: import javax.wsdl.extensions.soap12.SOAP12Address;
029: import javax.xml.namespace.QName;
030:
031: import org.w3c.dom.Element;
032:
033: import org.apache.servicemix.common.DefaultComponent;
034: import org.apache.servicemix.common.ServiceUnit;
035: import org.apache.servicemix.soap.api.Policy;
036: import org.apache.servicemix.soap.util.DomUtil;
037: import org.apache.servicemix.soap.wsdl.BindingFactory;
038: import org.apache.servicemix.soap.wsdl.WSDLUtils;
039: import org.apache.servicemix.soap.wsdl.validator.WSIBPValidator;
040: import org.apache.woden.WSDLFactory;
041: import org.apache.woden.WSDLReader;
042: import org.apache.woden.types.NCName;
043: import org.apache.woden.wsdl20.Description;
044: import org.apache.woden.wsdl20.Endpoint;
045: import org.apache.woden.wsdl20.xml.DescriptionElement;
046: import org.springframework.core.io.Resource;
047:
048: /**
049: *
050: * @author gnodet
051: * @since 3.2
052: * @org.apache.xbean.XBean element="soap-provider"
053: */
054: public class HttpSoapProviderEndpoint extends HttpProviderEndpoint {
055:
056: private Resource wsdl;
057: private boolean useJbiWrapper = true;
058: private boolean validateWsdl = true;
059: private Policy[] policies;
060:
061: public HttpSoapProviderEndpoint() {
062: super ();
063: }
064:
065: public HttpSoapProviderEndpoint(DefaultComponent component,
066: ServiceEndpoint endpoint) {
067: super (component, endpoint);
068: }
069:
070: public HttpSoapProviderEndpoint(ServiceUnit serviceUnit,
071: QName service, String endpoint) {
072: super (serviceUnit, service, endpoint);
073: }
074:
075: public Resource getWsdl() {
076: return wsdl;
077: }
078:
079: public void setWsdl(Resource wsdl) {
080: this .wsdl = wsdl;
081: }
082:
083: public boolean isValidateWsdl() {
084: return validateWsdl;
085: }
086:
087: public void setValidateWsdl(boolean validateWsdl) {
088: this .validateWsdl = validateWsdl;
089: }
090:
091: public boolean isUseJbiWrapper() {
092: return useJbiWrapper;
093: }
094:
095: public void setUseJbiWrapper(boolean useJbiWrapper) {
096: this .useJbiWrapper = useJbiWrapper;
097: }
098:
099: public Policy[] getPolicies() {
100: return policies;
101: }
102:
103: public void setPolicies(Policy[] policies) {
104: this .policies = policies;
105: }
106:
107: @Override
108: public void validate() throws DeploymentException {
109: if (wsdl == null) {
110: throw new DeploymentException("wsdl property must be set");
111: }
112: HttpSoapProviderMarshaler marshaler = new HttpSoapProviderMarshaler();
113: try {
114: description = DomUtil.parse(wsdl.getInputStream());
115: Element elem = description.getDocumentElement();
116: if (WSDLUtils.WSDL1_NAMESPACE
117: .equals(elem.getNamespaceURI())) {
118: validateWsdl1(marshaler);
119: } else if (WSDLUtils.WSDL2_NAMESPACE.equals(elem
120: .getNamespaceURI())) {
121: validateWsdl2(marshaler);
122: } else {
123: throw new DeploymentException(
124: "Unrecognized wsdl namespace: "
125: + elem.getNamespaceURI());
126: }
127: marshaler.setUseJbiWrapper(useJbiWrapper);
128: marshaler.setPolicies(policies);
129: setMarshaler(marshaler);
130: } catch (DeploymentException e) {
131: throw e;
132: } catch (Exception e) {
133: throw new DeploymentException("Unable to read WSDL from: "
134: + wsdl, e);
135: }
136: super .validate();
137: }
138:
139: protected void validateWsdl1(HttpSoapProviderMarshaler marshaler)
140: throws WSDLException, IOException, DeploymentException {
141: Definition def = WSDLUtils.createWSDL11Reader().readWSDL(
142: wsdl.getURL().toString());
143: if (validateWsdl) {
144: WSIBPValidator validator = new WSIBPValidator(def);
145: if (!validator.isValid()) {
146: throw new DeploymentException(
147: "WSDL is not WS-I BP compliant: "
148: + validator.getErrors());
149: }
150: }
151: Service svc;
152: if (getService() != null) {
153: svc = def.getService(getService());
154: if (svc == null) {
155: throw new DeploymentException(
156: "Could not find service '" + getService()
157: + "' in wsdl");
158: }
159: } else if (def.getServices().size() == 1) {
160: svc = (Service) def.getServices().values().iterator()
161: .next();
162: setService(svc.getQName());
163: } else {
164: throw new DeploymentException(
165: "If service is not set, the WSDL must contain a single service definition");
166: }
167: Port port;
168: if (getEndpoint() != null) {
169: port = svc.getPort(getEndpoint());
170: if (port == null) {
171: throw new DeploymentException("Cound not find port '"
172: + getEndpoint() + "' "
173: + "in wsdl for service '" + getService() + "'");
174: }
175: } else if (svc.getPorts().size() == 1) {
176: port = (Port) svc.getPorts().values().iterator().next();
177: setEndpoint(port.getName());
178: } else {
179: throw new DeploymentException(
180: "If endpoint is not set, the WSDL service '"
181: + getService() + "' "
182: + "must contain a single port definition");
183: }
184: SOAPAddress sa11 = WSDLUtils.getExtension(port,
185: SOAPAddress.class);
186: SOAP12Address sa12 = WSDLUtils.getExtension(port,
187: SOAP12Address.class);
188: if (sa11 != null) {
189: marshaler.setBaseUrl(sa11.getLocationURI());
190: } else if (sa12 != null) {
191: marshaler.setBaseUrl(sa12.getLocationURI());
192: } else {
193: throw new DeploymentException(
194: "No SOAP address defined on port '"
195: + port.getName() + "'");
196: }
197: description = WSDLUtils.getWSDL11Factory().newWSDLWriter()
198: .getDocument(def);
199: marshaler.setBinding(BindingFactory.createBinding(port));
200: }
201:
202: protected void validateWsdl2(HttpSoapProviderMarshaler marshaler)
203: throws org.apache.woden.WSDLException, IOException,
204: DeploymentException {
205: WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
206: DescriptionElement descElement = reader.readWSDL(wsdl.getURL()
207: .toString());
208: Description desc = descElement.toComponent();
209: org.apache.woden.wsdl20.Service svc;
210: if (getService() != null) {
211: svc = desc.getService(getService());
212: if (svc == null) {
213: throw new DeploymentException(
214: "Could not find service '" + getService()
215: + "' in wsdl");
216: }
217: } else if (desc.getServices().length == 1) {
218: svc = desc.getServices()[0];
219: setService(svc.getName());
220: } else {
221: throw new DeploymentException(
222: "If service is not set, the WSDL must contain a single service definition");
223: }
224: Endpoint endpoint;
225: if (getEndpoint() != null) {
226: endpoint = svc.getEndpoint(new NCName(getEndpoint()));
227: if (endpoint == null) {
228: throw new DeploymentException(
229: "Cound not find endpoint '" + getEndpoint()
230: + "' in wsdl for service '"
231: + getService() + "'");
232: }
233: } else if (svc.getEndpoints().length == 1) {
234: endpoint = svc.getEndpoints()[0];
235: setEndpoint(endpoint.getName().toString());
236: } else {
237: throw new DeploymentException(
238: "If endpoint is not set, the WSDL service '"
239: + getService() + "' "
240: + "must contain a single port definition");
241: }
242: marshaler.setBinding(BindingFactory.createBinding(endpoint));
243: }
244:
245: }
|