Project

General

Profile

Download (9.21 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.model.common;
11

    
12
import java.io.File;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.InputStreamReader;
16
import java.io.Reader;
17
import java.net.URL;
18

    
19
import javax.wsdl.xml.WSDLLocator;
20

    
21
import org.apache.commons.logging.Log;
22
import org.apache.commons.logging.LogFactory;
23
import org.xml.sax.InputSource;
24

    
25
import com.ibm.wsdl.util.StringUtils;
26

    
27
/**
28
 * WSDLLocator based almost totally upon WSIFWSDLLocatorImpl by Owen Burroughs
29
 * Created primarily because locating the required wsdl documents in the classpath
30
 * seems like a safer thing than using a file path and relying on users remembering to
31
 * copy the correct wsdl files into the correct places.
32
 *
33
 * @author ben.clark
34
 * @author Owen Burroughs
35
 */
36
public class LSIDWSDLLocator implements WSDLLocator {
37
	private static Log log = LogFactory.getLog(LSIDWSDLLocator.class);
38

    
39
	 private Reader baseReader = null;
40
	 private Reader importReader = null;
41
	 private String contextURI = null;
42
	 private String wsdlLocation = null;
43
	 private String documentBase = null;
44
	 private String importBase = null;
45
	 private ClassLoader loader = null;
46

    
47
	 public LSIDWSDLLocator(String ctxt, String wsdlURI, ClassLoader cl) {
48
		contextURI = ctxt;
49
		wsdlLocation = wsdlURI;
50
		loader = cl;
51
	}
52

    
53
	 public LSIDWSDLLocator(String docBase, Reader reader, ClassLoader cl) {
54
		 documentBase = docBase;
55
		 baseReader = reader;
56
		 loader = cl;
57
	}
58

    
59
	/**
60
	 * @see javax.wsdl.xml.WSDLLocator#getBaseReader()
61
	 */
62
	public Reader getBaseReader() {
63
		if (baseReader == null) {
64
			try {
65
				URL url = null;
66
				URL contextURL = (contextURI != null) ? StringUtils.getURL(
67
						null, contextURI) : null;
68
				if (loader != null) {
69
					InputStream in = null;
70
					try {
71
						if (contextURL != null) {
72
                            url = new URL(contextURL, wsdlLocation);
73
                        } else {
74
							if (wsdlLocation.indexOf(":") == -1) {
75
                                url = new URL("file", null, wsdlLocation);
76
                            } else {
77
                                url = new URL(wsdlLocation);
78
                            }
79
						}
80
						String wsdlRelativeLocation = url.getPath();
81
						if (wsdlRelativeLocation.startsWith("/")) {
82
                            wsdlRelativeLocation = wsdlRelativeLocation
83
									.substring(1);
84
                        }
85
						in = loader
86
								.getResourceAsStream(wsdlRelativeLocation);
87
						baseReader = new InputStreamReader(in);
88
					} catch (Exception exc) {
89
					}
90
				}
91
				if (baseReader == null) {
92
					url = StringUtils.getURL(contextURL, wsdlLocation);
93
					baseReader = new InputStreamReader(StringUtils
94
							.getContentAsInputStream(url));
95
				}
96
				if (url != null) {
97
                    documentBase = url.toString();
98
                }
99
			} catch (Exception e) {
100
				documentBase = wsdlLocation;
101
			}
102
		}
103

    
104
		return baseReader;
105
	}
106

    
107
	/**
108
	 * @see javax.wsdl.xml.WSDLLocator#getBaseURI()
109
	 */
110
	@Override
111
    public String getBaseURI() {
112
		return documentBase;
113
	}
114

    
115
	/**
116
	 * used to read imports as a document is parsed
117
	 *
118
	 * @see javax.wsdl.xml.WSDLLocator#getImportReader(String, String)
119
	 */
120
	public Reader getImportReader(String base, String relativeLocation) {
121
		// Reset importReader if finding import within import
122
		importReader = null;
123
		boolean triedSU = false;
124
		try {
125
			// If a ClassLoader was used to load the base
126
			// document, chances
127
			// are we need to use it to find the import.
128
			URL url = null;
129
			if (loader != null) {
130
				if (relativeLocation.startsWith("/")
131
						|| relativeLocation.startsWith("\\")) {
132
					// Relative location has been specified from a root dir.
133
					// However,
134
					// using a ClassLoader, root dirs don't mean anything.
135
					relativeLocation = relativeLocation.substring(1,relativeLocation.length());
136
					InputStream in = loader.getResourceAsStream(relativeLocation);
137
					importReader = new InputStreamReader(in);
138
				} else if (relativeLocation.indexOf("://") != -1) {
139
					// This is a fully specified URL of some kind so don't
140
					// use the
141
					// ClassLoader to find the import.
142
					triedSU = true;
143
					url = StringUtils.getURL(null, relativeLocation);
144
					importReader = new InputStreamReader(StringUtils
145
							.getContentAsInputStream(url));
146
				} else {
147
					// Import location has been specified relative to the
148
					// base document
149
					// and so we can to try to form the complete path to it.
150
					if (base != null) {
151
						int i = base.lastIndexOf("/");
152
						if (i == -1) {
153
							i = base.lastIndexOf("\\");
154
						}
155
						if (i != -1) {
156
							String path = base.substring(0, i + 1);
157
							String resolvedPath = path + relativeLocation;
158
							if (relativeLocation.startsWith("..")) {
159
								resolvedPath = resolvePath(path,
160
										relativeLocation);
161
							}
162
							if (resolvedPath == null) {
163
								throw new Exception("Invalid Path");
164
							}
165

    
166
							// Make sure that resolved path starts with
167
							// file:
168
							if (resolvedPath.startsWith("file:")) {
169
								url = new URL(null, resolvedPath);
170
							} else {
171
								url = new URL(null, "file:" + resolvedPath);
172
							}
173
						} else {
174
							url = new URL(null, "file:" + base + File.separator + relativeLocation);
175
						}
176
						InputStream in = loader.getResourceAsStream(url.getPath());
177
						importReader = new InputStreamReader(in);
178
					} else {
179
						url = new URL(null, "file:" + relativeLocation);
180
						InputStream in = loader.getResourceAsStream(url.getPath());
181
						importReader = new InputStreamReader(in);
182
					}
183
				}
184
			} else {
185
				triedSU = true;
186
				URL contextURL = (base != null) ? StringUtils.getURL(null,
187
						base) : null;
188
				url = StringUtils.getURL(contextURL, relativeLocation);
189
				importReader = new InputStreamReader(StringUtils
190
						.getContentAsInputStream(url));
191
			}
192
			importBase = (url == null) ? relativeLocation : url.toString();
193
		} catch (Exception e) {
194
            log.error(e.toString());
195
            log.error(e.getMessage());
196
			// If we have not tried using a non-ClassLoader route, try it
197
			// now
198
			// as a last resort.
199
			if (!triedSU) {
200
				try {
201
					URL contextURL = (base != null) ? StringUtils.getURL(
202
							null, base) : null;
203
					URL url = StringUtils.getURL(contextURL,
204
							relativeLocation);
205
					importReader = new InputStreamReader(StringUtils
206
							.getContentAsInputStream(url));
207
					importBase = (url == null) ? relativeLocation : url
208
							.toString();
209
				} catch (Exception e2) {
210
					 log.error(e2.toString());
211
					 log.error("Cannot find " + importBase + " so setting importBase to unknownImportURI");
212
					// we can't find the import so set a temporary value for
213
					// the import URI. This is
214
					// necessary to avoid a NullPointerException in
215
					// WSDLReaderImpl
216
					importBase = "unknownImportURI";
217
				}
218
			} else {
219
				log.error("Cannot find " + importBase + " so setting importBase to unknownImportURI");
220
				// we can't find the import so set a temporary value for the
221
				// import URI. This is
222
				// necessary to avoid a NullPointerException in
223
				// WSDLReaderImpl
224
				importBase = "unknownImportURI";
225
			}
226
		}
227

    
228
		return importReader;
229
	}
230

    
231
	/**
232
	 * Resolve a path when the relative location begins with ..
233
	 */
234
	private String resolvePath(String ba, String rel) {
235
		StringBuffer sb = new StringBuffer(rel);
236
		int dd = 0;
237
		while (sb.length() > 0) {
238
			if (sb.length() > 3 && sb.charAt(0) == '.'
239
					&& sb.charAt(1) == '.'
240
					&& (sb.charAt(2) == '/' || sb.charAt(2) == '\\')) {
241
				dd++;
242
				sb.delete(0, 3);
243
			} else {
244
				break;
245
			}
246
		}
247
		StringBuffer sb2 = new StringBuffer(ba);
248
		int j = sb2.length() - 1;
249
		int found = 0;
250
		for (int k = j; k >= 0; k--) {
251
			if (k != j && (sb2.charAt(k) == '/' || sb2.charAt(k) == '\\')) {
252
				found++;
253
			}
254
			if (found < dd) {
255
				sb2.deleteCharAt(k);
256
			} else {
257
				break;
258
			}
259
		}
260
		if (found + 1 < dd) {
261
            return null;
262
        }
263
		return sb2.toString() + sb.toString();
264
	}
265

    
266
	/**
267
	 * @see javax.wsdl.xml.WSDLLocator#getLatestImportURI()
268
	 */
269
	@Override
270
    public String getLatestImportURI() {
271
		return importBase;
272
	}
273

    
274
	/**
275
	 * @see javax.wsdl.xml.WSDLLocator#getBaseInputSource()
276
	 */
277
	@Override
278
    public InputSource getBaseInputSource() {
279
		return new InputSource(getBaseReader());
280
	}
281

    
282
	/**
283
	 * @see javax.wsdl.xml.WSDLLocator#getImportInputSource(String, String)
284
	 */
285
	@Override
286
    public InputSource getImportInputSource(String arg0, String arg1) {
287
		return new InputSource(getImportReader(arg0, arg1));
288
	}
289

    
290
	@Override
291
    public void close() {
292
		if (baseReader != null) {
293
            try {
294
				baseReader.close();
295
			} catch (IOException e) {
296
				// TODO Auto-generated catch block
297
				e.printStackTrace();
298
			}
299
        }
300
		if (importReader != null) {
301
            try {
302
				importReader.close();
303
			} catch (IOException e) {
304
				// TODO Auto-generated catch block
305
				e.printStackTrace();
306
			}
307
        }
308
	}
309

    
310
}
(39-39/58)