1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.ws.api.server; 27 28 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; 29 import com.sun.xml.internal.ws.server.ServerRtException; 30 import com.sun.xml.internal.ws.streaming.TidyXMLStreamReader; 31 import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; 32 33 import javax.xml.stream.XMLInputFactory; 34 import javax.xml.stream.XMLStreamException; 35 import javax.xml.stream.XMLStreamReader; 36 import java.io.IOException; 37 import java.io.InputStream; 38 import java.net.MalformedURLException; 39 import java.net.URL; 40 41 /** 42 * SPI that provides the source of {@link SDDocument}. 43 * 44 * <p> 45 * This abstract class could be implemented by appliations, or one of the 46 * {@link #create} methods can be used. 47 * 48 * @author Kohsuke Kawaguchi 49 */ 50 public abstract class SDDocumentSource { 51 /** 52 * Returns the {@link XMLStreamReader} that reads the document. 53 * 54 * <p> 55 * This method maybe invoked multiple times concurrently. 56 * 57 * @param xif 58 * The implementation may choose to use this object when it wants to 59 * create a new parser (or it can just ignore this parameter completely.) 60 * @return 61 * The caller is responsible for closing the reader to avoid resource leak. 62 * 63 * @throws XMLStreamException 64 * if something goes wrong while creating a parser. 65 * @throws IOException 66 * if something goes wrong trying to read the document. 67 */ 68 public abstract XMLStreamReader read(XMLInputFactory xif) throws IOException, XMLStreamException; 69 70 /** 71 * Returns the {@link XMLStreamReader} that reads the document. 72 * 73 * <p> 74 * This method maybe invoked multiple times concurrently. 75 * 76 * @return 77 * The caller is responsible for closing the reader to avoid resource leak. 78 * 79 * @throws XMLStreamException 80 * if something goes wrong while creating a parser. 81 * @throws IOException 82 * if something goes wrong trying to read the document. 83 */ 84 public abstract XMLStreamReader read() throws IOException, XMLStreamException; 85 86 /** 87 * System ID of this document. 88 */ 89 public abstract URL getSystemId(); 90 91 /** 92 * Creates {@link SDDocumentSource} from an URL. 93 */ 94 public static SDDocumentSource create(final URL url) { 95 return new SDDocumentSource() { 96 private final URL systemId = url; 97 98 public XMLStreamReader read(XMLInputFactory xif) throws IOException, XMLStreamException { 99 InputStream is = url.openStream(); 100 return new TidyXMLStreamReader( 101 xif.createXMLStreamReader(systemId.toExternalForm(),is), is); 102 } 103 104 public XMLStreamReader read() throws IOException, XMLStreamException { 105 InputStream is = url.openStream(); 106 return new TidyXMLStreamReader( 107 XMLStreamReaderFactory.create(systemId.toExternalForm(),is,false), is); 108 } 109 110 public URL getSystemId() { 111 return systemId; 112 } 113 }; 114 } 115 116 /** 117 * Creates {@link SDDocumentSource} from resource path using resolvingClass to read the resource. 118 * Required for Jigsaw runtime. 119 * 120 * @param resolvingClass class used to read resource 121 * @param path resource path 122 */ 123 public static SDDocumentSource create(final Class resolvingClass, final String path) { 124 return new SDDocumentSource() { 125 126 public XMLStreamReader read(XMLInputFactory xif) throws IOException, XMLStreamException { 127 InputStream is = inputStream(); 128 return new TidyXMLStreamReader(xif.createXMLStreamReader(path,is), is); 129 } 130 131 public XMLStreamReader read() throws IOException, XMLStreamException { 132 InputStream is = inputStream(); 133 return new TidyXMLStreamReader(XMLStreamReaderFactory.create(path,is,false), is); 134 } 135 136 public URL getSystemId() { 137 try { 138 return new URL("file://" + path); 139 } catch (MalformedURLException e) { 140 return null; 141 } 142 } 143 144 private InputStream inputStream() throws IOException { 145 java.lang.reflect.Module module = resolvingClass.getModule(); 146 if (module != null) { 147 InputStream stream = module.getResourceAsStream(path); 148 if (stream != null) { 149 return stream; 150 } 151 } 152 throw new ServerRtException("cannot.load.wsdl", path); 153 } 154 155 }; 156 } 157 158 /** 159 * Creates a {@link SDDocumentSource} from {@link XMLStreamBuffer}. 160 */ 161 public static SDDocumentSource create(final URL systemId, final XMLStreamBuffer xsb) { 162 return new SDDocumentSource() { 163 public XMLStreamReader read(XMLInputFactory xif) throws XMLStreamException { 164 return xsb.readAsXMLStreamReader(); 165 } 166 167 public XMLStreamReader read() throws XMLStreamException { 168 return xsb.readAsXMLStreamReader(); 169 } 170 171 public URL getSystemId() { 172 return systemId; 173 } 174 }; 175 } 176 }