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.catalog;
019:
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022:
023: import javax.wsdl.xml.WSDLLocator;
024:
025: import org.xml.sax.InputSource;
026:
027: import org.apache.cxf.resource.ExtendedURIResolver;
028: import org.apache.xml.resolver.Catalog;
029:
030: /**
031: * Resolves WSDL URIs using Apache Commons Resolver API.
032: */
033: public class CatalogWSDLLocator implements WSDLLocator {
034:
035: private String wsdlUrl;
036: private ExtendedURIResolver resolver;
037: private Catalog catalogResolver;
038: private String baseUri;
039:
040: public CatalogWSDLLocator(String wsdlUrl,
041: OASISCatalogManager catalogManager) {
042: this .wsdlUrl = wsdlUrl;
043: this .baseUri = this .wsdlUrl;
044: this .catalogResolver = catalogManager.getCatalog();
045: this .resolver = new ExtendedURIResolver();
046: }
047:
048: public InputSource getBaseInputSource() {
049: InputSource result = resolver.resolve(baseUri, null);
050: if (result == null) {
051: try {
052: String s = catalogResolver.resolveSystem(baseUri);
053: if (s != null) {
054: result = resolver.resolve(s, null);
055: }
056: } catch (MalformedURLException e) {
057: //ignore
058: } catch (IOException e) {
059: //ignore
060: }
061: }
062: baseUri = resolver.getURI();
063: return result;
064: }
065:
066: public String getBaseURI() {
067: return getBaseInputSource().getSystemId();
068: }
069:
070: public String getLatestImportURI() {
071: return resolver.getLatestImportURI();
072: }
073:
074: public InputSource getImportInputSource(String parent,
075: String importLocation) {
076: this .baseUri = parent;
077:
078: String resolvedImportLocation = null;
079: try {
080: resolvedImportLocation = this .catalogResolver
081: .resolveSystem(importLocation);
082: } catch (IOException e) {
083: throw new RuntimeException("Catalog resolution failed", e);
084: }
085:
086: InputSource in = null;
087: if (resolvedImportLocation == null) {
088: in = this .resolver.resolve(importLocation, this .baseUri);
089: } else {
090: in = this .resolver.resolve(resolvedImportLocation, null);
091: }
092:
093: // XXX: If we return null (as per javadoc), a NPE is raised in WSDL4J code.
094: // So let's return new InputSource() and let WSDL4J fail. Optionally,
095: // we can throw a similar exception as in CatalogXmlSchemaURIResolver.
096: if (in == null) {
097: in = new InputSource();
098: }
099:
100: return in;
101: }
102:
103: public void close() {
104: resolver.close();
105: }
106: }
|