ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.util; ohair@286: ohair@286: import java.util.UUID; ohair@286: import java.util.regex.Pattern; ohair@286: import java.net.URL; ohair@286: import java.net.MalformedURLException; ohair@286: import java.net.URI; ohair@286: import java.net.URISyntaxException; ohair@286: import java.io.File; ohair@286: import java.io.IOException; ohair@286: ohair@286: import javax.xml.namespace.QName; ohair@286: ohair@286: /** ohair@286: * @author Vivek Pandey ohair@286: * ohair@286: * Wrapper utility class to be used from the generated code or run time. ohair@286: */ ohair@286: public final class JAXWSUtils { ohair@286: public static String getUUID(){ ohair@286: return UUID.randomUUID().toString(); ohair@286: } ohair@286: ohair@286: ohair@286: ohair@286: public static String getFileOrURLName(String fileOrURL) { ohair@286: try{ ohair@286: try { ohair@286: return escapeSpace(new URL(fileOrURL).toExternalForm()); ohair@286: } catch (MalformedURLException e) { ohair@286: return new File(fileOrURL).getCanonicalFile().toURL().toExternalForm(); ohair@286: } ohair@286: } catch (Exception e) { ohair@286: // try it as an URL ohair@286: return fileOrURL; ohair@286: } ohair@286: } ohair@286: ohair@286: public static URL getFileOrURL(String fileOrURL) throws IOException { ohair@286: try { ohair@286: URL url = new URL(fileOrURL); ohair@286: String scheme = String.valueOf(url.getProtocol()).toLowerCase(); ohair@286: if (scheme.equals("http") || scheme.equals("https")) ohair@286: return new URL(url.toURI().toASCIIString()); ohair@286: return url; ohair@286: } catch (URISyntaxException e) { ohair@286: return new File(fileOrURL).toURL(); ohair@286: } catch (MalformedURLException e) { ohair@286: return new File(fileOrURL).toURL(); ohair@286: } ohair@286: } ohair@286: ohair@286: public static URL getEncodedURL(String urlStr) throws MalformedURLException { ohair@286: URL url = new URL(urlStr); ohair@286: String scheme = String.valueOf(url.getProtocol()).toLowerCase(); ohair@286: if (scheme.equals("http") || scheme.equals("https")) { ohair@286: try { ohair@286: return new URL(url.toURI().toASCIIString()); ohair@286: } catch (URISyntaxException e) { ohair@286: MalformedURLException malformedURLException = new MalformedURLException(e.getMessage()); ohair@286: malformedURLException.initCause(e); ohair@286: throw malformedURLException; ohair@286: } ohair@286: } ohair@286: return url; ohair@286: } ohair@286: ohair@286: private static String escapeSpace( String url ) { ohair@286: // URLEncoder didn't work. alanb@368: StringBuilder buf = new StringBuilder(); ohair@286: for (int i = 0; i < url.length(); i++) { ohair@286: // TODO: not sure if this is the only character that needs to be escaped. ohair@286: if (url.charAt(i) == ' ') ohair@286: buf.append("%20"); ohair@286: else ohair@286: buf.append(url.charAt(i)); ohair@286: } ohair@286: return buf.toString(); ohair@286: } ohair@286: ohair@286: public static String absolutize(String name) { ohair@286: // absolutize all the system IDs in the input, ohair@286: // so that we can map system IDs to DOM trees. ohair@286: try { ohair@286: URL baseURL = new File(".").getCanonicalFile().toURL(); ohair@286: return new URL(baseURL, name).toExternalForm(); alanb@368: } catch( IOException e) { alanb@368: //ignore ohair@286: } ohair@286: return name; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Checks if the system ID is absolute. ohair@286: */ alanb@368: @SuppressWarnings("ResultOfObjectAllocationIgnored") ohair@286: public static void checkAbsoluteness(String systemId) { ohair@286: // we need to be able to handle system IDs like "urn:foo", which java.net.URL can't process, ohair@286: // but OTOH we also need to be able to process system IDs like "file://a b c/def.xsd", ohair@286: // which java.net.URI can't process. So for now, let's fail only if both of them fail. ohair@286: // eventually we need a proper URI class that works for us. ohair@286: try { ohair@286: new URL(systemId); alanb@368: } catch( MalformedURLException mue) { ohair@286: try { ohair@286: new URI(systemId); alanb@368: } catch (URISyntaxException e) { ohair@286: throw new IllegalArgumentException("system ID '"+systemId+"' isn't absolute",e); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: /* ohair@286: * To match, both QNames must have the same namespace and the local ohair@286: * part of the target must match the local part of the 'pattern' ohair@286: * QName, which may contain wildcard characters. ohair@286: */ ohair@286: public static boolean matchQNames(QName target, QName pattern) { ohair@286: if ((target == null) || (pattern == null)) { ohair@286: // if no service or port is in descriptor ohair@286: return false; ohair@286: } ohair@286: if (pattern.getNamespaceURI().equals(target.getNamespaceURI())) { ohair@286: String regex = pattern.getLocalPart().replaceAll("\\*", ".*"); ohair@286: return Pattern.matches(regex, target.getLocalPart()); ohair@286: } ohair@286: return false; ohair@286: } ohair@286: ohair@286: }