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: */
019: package org.apache.axis2.deployment.resolver;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.ws.commons.schema.resolver.DefaultURIResolver;
024: import org.xml.sax.InputSource;
025:
026: import javax.wsdl.xml.WSDLLocator;
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.net.URI;
034: import java.util.zip.ZipEntry;
035: import java.util.zip.ZipInputStream;
036:
037: /**
038: * Custom WSDL locator to load schemas from zip archives
039: * Need to provide the aarFile and the baseInputStream for
040: * the base WSDL file
041: * <p/>
042: * The logic here is that we only care about the import location
043: * all imports must be relative to the META-INF folder
044: */
045: public class AARBasedWSDLLocator extends DefaultURIResolver implements
046: WSDLLocator {
047:
048: protected static final Log log = LogFactory
049: .getLog(AARBasedWSDLLocator.class);
050:
051: private File aarFile;
052: private InputStream baseInputStream;
053: private URI lastImportLocation;
054: private String baseURI;
055:
056: public AARBasedWSDLLocator(String baseURI, File zipFile,
057: InputStream baseInputStream) {
058: this .baseURI = baseURI;
059: this .baseInputStream = baseInputStream;
060: this .aarFile = zipFile;
061: }
062:
063: public InputSource getBaseInputSource() {
064: return new InputSource(baseInputStream);
065: }
066:
067: /**
068: * @param parentLocation
069: * @param importLocation
070: */
071: public InputSource getImportInputSource(String parentLocation,
072: String importLocation) {
073: lastImportLocation = URI.create(parentLocation).resolve(
074: importLocation);
075:
076: if (isAbsolute(importLocation)) {
077: return super .resolveEntity(null, importLocation,
078: parentLocation);
079: } else {
080: //we don't care about the parent location
081: ZipInputStream zin = null;
082: try {
083:
084: zin = new ZipInputStream(new FileInputStream(aarFile));
085: ZipEntry entry;
086: byte[] buf = new byte[1024];
087: int read;
088: ByteArrayOutputStream out;
089: String searchingStr = lastImportLocation.toString();
090: while ((entry = zin.getNextEntry()) != null) {
091: String entryName = entry.getName().toLowerCase();
092: if (entryName.equalsIgnoreCase(searchingStr)) {
093: out = new ByteArrayOutputStream();
094: while ((read = zin.read(buf)) > 0) {
095: out.write(buf, 0, read);
096: }
097: ByteArrayInputStream in = new ByteArrayInputStream(
098: out.toByteArray());
099: return new InputSource(in);
100: }
101: }
102: } catch (IOException e) {
103: throw new RuntimeException(e);
104: } finally {
105: try {
106: if (zin != null) {
107: zin.close();
108: }
109: } catch (IOException e) {
110: log.debug(e);
111: }
112: }
113: }
114:
115: log.info("AARBasedWSDLLocator: Unable to resolve "
116: + lastImportLocation);
117: return null;
118: }
119:
120: /**
121: * As for the zip there is no point in returning
122: * a base URI
123: */
124: public String getBaseURI() {
125: // we don't care
126: return baseURI;
127: }
128:
129: /**
130: * returns the latest import
131: */
132: public String getLatestImportURI() {
133: //we don't care about this either
134: return lastImportLocation.toString();
135: }
136:
137: public void close() {
138: //TODO: FIXME:
139: }
140: }
|