src/share/jaxws_classes/com/sun/xml/internal/ws/util/JAXWSUtils.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.util;
aoqi@0 27
aoqi@0 28 import java.util.UUID;
aoqi@0 29 import java.util.regex.Pattern;
aoqi@0 30 import java.net.URL;
aoqi@0 31 import java.net.MalformedURLException;
aoqi@0 32 import java.net.URI;
aoqi@0 33 import java.net.URISyntaxException;
aoqi@0 34 import java.io.File;
aoqi@0 35 import java.io.IOException;
aoqi@0 36
aoqi@0 37 import javax.xml.namespace.QName;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * @author Vivek Pandey
aoqi@0 41 *
aoqi@0 42 * Wrapper utility class to be used from the generated code or run time.
aoqi@0 43 */
aoqi@0 44 public final class JAXWSUtils {
aoqi@0 45 public static String getUUID(){
aoqi@0 46 return UUID.randomUUID().toString();
aoqi@0 47 }
aoqi@0 48
aoqi@0 49
aoqi@0 50
aoqi@0 51 public static String getFileOrURLName(String fileOrURL) {
aoqi@0 52 try{
aoqi@0 53 try {
aoqi@0 54 return escapeSpace(new URL(fileOrURL).toExternalForm());
aoqi@0 55 } catch (MalformedURLException e) {
aoqi@0 56 return new File(fileOrURL).getCanonicalFile().toURL().toExternalForm();
aoqi@0 57 }
aoqi@0 58 } catch (Exception e) {
aoqi@0 59 // try it as an URL
aoqi@0 60 return fileOrURL;
aoqi@0 61 }
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 public static URL getFileOrURL(String fileOrURL) throws IOException {
aoqi@0 65 try {
aoqi@0 66 URL url = new URL(fileOrURL);
aoqi@0 67 String scheme = String.valueOf(url.getProtocol()).toLowerCase();
aoqi@0 68 if (scheme.equals("http") || scheme.equals("https"))
aoqi@0 69 return new URL(url.toURI().toASCIIString());
aoqi@0 70 return url;
aoqi@0 71 } catch (URISyntaxException e) {
aoqi@0 72 return new File(fileOrURL).toURL();
aoqi@0 73 } catch (MalformedURLException e) {
aoqi@0 74 return new File(fileOrURL).toURL();
aoqi@0 75 }
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 public static URL getEncodedURL(String urlStr) throws MalformedURLException {
aoqi@0 79 URL url = new URL(urlStr);
aoqi@0 80 String scheme = String.valueOf(url.getProtocol()).toLowerCase();
aoqi@0 81 if (scheme.equals("http") || scheme.equals("https")) {
aoqi@0 82 try {
aoqi@0 83 return new URL(url.toURI().toASCIIString());
aoqi@0 84 } catch (URISyntaxException e) {
aoqi@0 85 MalformedURLException malformedURLException = new MalformedURLException(e.getMessage());
aoqi@0 86 malformedURLException.initCause(e);
aoqi@0 87 throw malformedURLException;
aoqi@0 88 }
aoqi@0 89 }
aoqi@0 90 return url;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 private static String escapeSpace( String url ) {
aoqi@0 94 // URLEncoder didn't work.
aoqi@0 95 StringBuilder buf = new StringBuilder();
aoqi@0 96 for (int i = 0; i < url.length(); i++) {
aoqi@0 97 // TODO: not sure if this is the only character that needs to be escaped.
aoqi@0 98 if (url.charAt(i) == ' ')
aoqi@0 99 buf.append("%20");
aoqi@0 100 else
aoqi@0 101 buf.append(url.charAt(i));
aoqi@0 102 }
aoqi@0 103 return buf.toString();
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 public static String absolutize(String name) {
aoqi@0 107 // absolutize all the system IDs in the input,
aoqi@0 108 // so that we can map system IDs to DOM trees.
aoqi@0 109 try {
aoqi@0 110 URL baseURL = new File(".").getCanonicalFile().toURL();
aoqi@0 111 return new URL(baseURL, name).toExternalForm();
aoqi@0 112 } catch( IOException e) {
aoqi@0 113 //ignore
aoqi@0 114 }
aoqi@0 115 return name;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 /**
aoqi@0 119 * Checks if the system ID is absolute.
aoqi@0 120 */
aoqi@0 121 @SuppressWarnings("ResultOfObjectAllocationIgnored")
aoqi@0 122 public static void checkAbsoluteness(String systemId) {
aoqi@0 123 // we need to be able to handle system IDs like "urn:foo", which java.net.URL can't process,
aoqi@0 124 // but OTOH we also need to be able to process system IDs like "file://a b c/def.xsd",
aoqi@0 125 // which java.net.URI can't process. So for now, let's fail only if both of them fail.
aoqi@0 126 // eventually we need a proper URI class that works for us.
aoqi@0 127 try {
aoqi@0 128 new URL(systemId);
aoqi@0 129 } catch( MalformedURLException mue) {
aoqi@0 130 try {
aoqi@0 131 new URI(systemId);
aoqi@0 132 } catch (URISyntaxException e) {
aoqi@0 133 throw new IllegalArgumentException("system ID '"+systemId+"' isn't absolute",e);
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 /*
aoqi@0 139 * To match, both QNames must have the same namespace and the local
aoqi@0 140 * part of the target must match the local part of the 'pattern'
aoqi@0 141 * QName, which may contain wildcard characters.
aoqi@0 142 */
aoqi@0 143 public static boolean matchQNames(QName target, QName pattern) {
aoqi@0 144 if ((target == null) || (pattern == null)) {
aoqi@0 145 // if no service or port is in descriptor
aoqi@0 146 return false;
aoqi@0 147 }
aoqi@0 148 if (pattern.getNamespaceURI().equals(target.getNamespaceURI())) {
aoqi@0 149 String regex = pattern.getLocalPart().replaceAll("\\*", ".*");
aoqi@0 150 return Pattern.matches(regex, target.getLocalPart());
aoqi@0 151 }
aoqi@0 152 return false;
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 }

mercurial