1 /*
2 * Copyright (c) 1997, 2012, 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.messaging.saaj.util;
27
28 // Imported from: org.apache.xerces.util
29 // Needed to work around differences in JDK1.2 and 1.3 and deal with userInfo
30
31 import java.io.IOException;
32 import java.io.Serializable;
33
34
35 /**********************************************************************
36 * A class to represent a Uniform Resource Identifier (URI). This class
37 * is designed to handle the parsing of URIs and provide access to
38 * the various components (scheme, host, port, userinfo, path, query
39 * string and fragment) that may constitute a URI.
40 * <p>
41 * Parsing of a URI specification is done according to the URI
42 * syntax described in <a href="http://www.ietf.org/rfc/rfc2396.txt?number=2396">RFC 2396</a>.
43 * Every URI consists of a scheme, followed by a colon (':'), followed by a scheme-specific
44 * part. For URIs that follow the "generic URI" syntax, the scheme-
45 * specific part begins with two slashes ("//") and may be followed
46 * by an authority segment (comprised of user information, host, and
47 * port), path segment, query segment and fragment. Note that RFC 2396
48 * no longer specifies the use of the parameters segment and excludes
49 * the "user:password" syntax as part of the authority segment. If
50 * "user:password" appears in a URI, the entire user/password string
51 * is stored as userinfo.
52 * <p>
53 * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
54 * the entire scheme-specific part is treated as the "path" portion
55 * of the URI.
56 * <p>
57 * Note that, unlike the java.net.URL class, this class does not provide
58 * any built-in network access functionality nor does it provide any
59 * scheme-specific functionality (for example, it does not know a
60 * default port for a specific scheme). Rather, it only knows the
61 * grammar and basic set of operations that can be applied to a URI.
62 *
63 * @version
64 *
65 **********************************************************************/
66 public class JaxmURI implements Serializable {
67
68 /*******************************************************************
69 * MalformedURIExceptions are thrown in the process of building a URI
70 * or setting fields on a URI when an operation would result in an
71 * invalid URI specification.
72 *
73 ********************************************************************/
74 public static class MalformedURIException extends IOException {
75
76 /******************************************************************
77 * Constructs a <code>MalformedURIException</code> with no specified
78 * detail message.
79 ******************************************************************/
80 public MalformedURIException() {
81 super();
82 }
83
84 /*****************************************************************
1089 throw new MalformedURIException(
1090 "Fragment cannot be set when path is null!");
1091 }
1092 else if (!isURIString(p_fragment)) {
1093 throw new MalformedURIException(
1094 "Fragment contains invalid character!");
1095 }
1096 else {
1097 m_fragment = p_fragment;
1098 }
1099 }
1100
1101 /**
1102 * Determines if the passed-in Object is equivalent to this URI.
1103 *
1104 * @param p_test the Object to test for equality.
1105 *
1106 * @return true if p_test is a URI with all values equal to this
1107 * URI, false otherwise
1108 */
1109 public boolean equals(Object p_test) {
1110 if (p_test instanceof JaxmURI) {
1111 JaxmURI testURI = (JaxmURI) p_test;
1112 if (((m_scheme == null && testURI.m_scheme == null) ||
1113 (m_scheme != null && testURI.m_scheme != null &&
1114 m_scheme.equals(testURI.m_scheme))) &&
1115 ((m_userinfo == null && testURI.m_userinfo == null) ||
1116 (m_userinfo != null && testURI.m_userinfo != null &&
1117 m_userinfo.equals(testURI.m_userinfo))) &&
1118 ((m_host == null && testURI.m_host == null) ||
1119 (m_host != null && testURI.m_host != null &&
1120 m_host.equals(testURI.m_host))) &&
1121 m_port == testURI.m_port &&
1122 ((m_path == null && testURI.m_path == null) ||
1123 (m_path != null && testURI.m_path != null &&
1124 m_path.equals(testURI.m_path))) &&
1125 ((m_queryString == null && testURI.m_queryString == null) ||
1126 (m_queryString != null && testURI.m_queryString != null &&
1127 m_queryString.equals(testURI.m_queryString))) &&
1128 ((m_fragment == null && testURI.m_fragment == null) ||
1129 (m_fragment != null && testURI.m_fragment != null &&
1130 m_fragment.equals(testURI.m_fragment)))) {
1131 return true;
1132 }
1133 }
1134 return false;
1135 }
1136
1137 public int hashCode() {
1138 // No members safe to use, just default to a constant.
1139 return 153214;
1140 }
1141
1142 /**
1143 * Get the URI as a string specification. See RFC 2396 Section 5.2.
1144 *
1145 * @return the URI string specification
1146 */
1147 public String toString() {
1148 StringBuilder uriSpecString = new StringBuilder();
1149
1150 if (m_scheme != null) {
1151 uriSpecString.append(m_scheme);
1152 uriSpecString.append(':');
1153 }
1154 uriSpecString.append(getSchemeSpecificPart());
1155 return uriSpecString.toString();
1156 }
1157
1158 /**
1159 * Get the indicator as to whether this URI uses the "generic URI"
1160 * syntax.
1161 *
1162 * @return true if this URI uses the "generic URI" syntax, false
1163 * otherwise
1164 */
1165 public boolean isGenericURI() {
1166 // presence of the host (whether valid or empty) means
1167 // double-slashes which means generic uri
1168 return (m_host != null);
1169 }
1170
1171 /**
1172 * Determine whether a scheme conforms to the rules for a scheme name.
1173 * A scheme is conformant if it starts with an alphanumeric, and
1174 * contains only alphanumerics, '+','-' and '.'.
1175 *
1176 * @return true if the scheme is conformant, false otherwise
1177 */
1178 public static boolean isConformantSchemeName(String p_scheme) {
1179 if (p_scheme == null || p_scheme.trim().length() == 0) {
1180 return false;
1181 }
1182
1183 if (!isAlpha(p_scheme.charAt(0))) {
1184 return false;
1185 }
1186
1187 char testChar;
1188 for (int i = 1; i < p_scheme.length(); i++) {
1189 testChar = p_scheme.charAt(i);
1190 if (!isAlphanum(testChar) &&
1191 SCHEME_CHARACTERS.indexOf(testChar) == -1) {
1192 return false;
1193 }
1194 }
1195
1196 return true;
1197 }
1198
1199 /**
1200 * Determine whether a string is syntactically capable of representing
1201 * a valid IPv4 address or the domain name of a network host. A valid
1202 * IPv4 address consists of four decimal digit groups separated by a
1203 * '.'. A hostname consists of domain labels (each of which must
1204 * begin and end with an alphanumeric but may contain '-') separated
1205 & by a '.'. See RFC 2396 Section 3.2.2.
1206 *
1207 * @return true if the string is a syntactically valid IPv4 address
1208 * or hostname
1209 */
1210 public static boolean isWellFormedAddress(String p_address) {
1211 if (p_address == null) {
1212 return false;
1213 }
1214
1215 String address = p_address.trim();
1216 int addrLength = address.length();
1217 if (addrLength == 0 || addrLength > 255) {
1218 return false;
1219 }
1220
1221 if (address.startsWith(".") || address.startsWith("-")) {
1222 return false;
1223 }
1224
1225 // rightmost domain label starting with digit indicates IP address
|
1 /*
2 * Copyright (c) 1997, 2017, 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.messaging.saaj.util;
27
28 // Imported from: org.apache.xerces.util
29 // Needed to work around differences in JDK1.2 and 1.3 and deal with userInfo
30
31 import java.io.IOException;
32 import java.io.Serializable;
33
34
35 /**********************************************************************
36 * A class to represent a Uniform Resource Identifier (URI). This class
37 * is designed to handle the parsing of URIs and provide access to
38 * the various components (scheme, host, port, userinfo, path, query
39 * string and fragment) that may constitute a URI.
40 * <p>
41 * Parsing of a URI specification is done according to the URI
42 * syntax described in <a href="http://www.ietf.org/rfc/rfc2396.txt?number=2396">
43 * RFC 2396</a>. Every URI consists
44 * of a scheme, followed by a colon (':'), followed by a scheme-specific
45 * part. For URIs that follow the "generic URI" syntax, the scheme-
46 * specific part begins with two slashes ("//") and may be followed
47 * by an authority segment (comprised of user information, host, and
48 * port), path segment, query segment and fragment. Note that RFC 2396
49 * no longer specifies the use of the parameters segment and excludes
50 * the "user:password" syntax as part of the authority segment. If
51 * "user:password" appears in a URI, the entire user/password string
52 * is stored as userinfo.
53 * <p>
54 * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
55 * the entire scheme-specific part is treated as the "path" portion
56 * of the URI.
57 * <p>
58 * Note that, unlike the java.net.URL class, this class does not provide
59 * any built-in network access functionality nor does it provide any
60 * scheme-specific functionality (for example, it does not know a
61 * default port for a specific scheme). Rather, it only knows the
62 * grammar and basic set of operations that can be applied to a URI.
63 *
64 **********************************************************************/
65 public class JaxmURI implements Serializable {
66
67 /*******************************************************************
68 * MalformedURIExceptions are thrown in the process of building a URI
69 * or setting fields on a URI when an operation would result in an
70 * invalid URI specification.
71 *
72 ********************************************************************/
73 public static class MalformedURIException extends IOException {
74
75 /******************************************************************
76 * Constructs a <code>MalformedURIException</code> with no specified
77 * detail message.
78 ******************************************************************/
79 public MalformedURIException() {
80 super();
81 }
82
83 /*****************************************************************
1088 throw new MalformedURIException(
1089 "Fragment cannot be set when path is null!");
1090 }
1091 else if (!isURIString(p_fragment)) {
1092 throw new MalformedURIException(
1093 "Fragment contains invalid character!");
1094 }
1095 else {
1096 m_fragment = p_fragment;
1097 }
1098 }
1099
1100 /**
1101 * Determines if the passed-in Object is equivalent to this URI.
1102 *
1103 * @param p_test the Object to test for equality.
1104 *
1105 * @return true if p_test is a URI with all values equal to this
1106 * URI, false otherwise
1107 */
1108 @Override
1109 public boolean equals(Object p_test) {
1110 if (p_test instanceof JaxmURI) {
1111 JaxmURI testURI = (JaxmURI) p_test;
1112 if (((m_scheme == null && testURI.m_scheme == null) ||
1113 (m_scheme != null && testURI.m_scheme != null &&
1114 m_scheme.equals(testURI.m_scheme))) &&
1115 ((m_userinfo == null && testURI.m_userinfo == null) ||
1116 (m_userinfo != null && testURI.m_userinfo != null &&
1117 m_userinfo.equals(testURI.m_userinfo))) &&
1118 ((m_host == null && testURI.m_host == null) ||
1119 (m_host != null && testURI.m_host != null &&
1120 m_host.equals(testURI.m_host))) &&
1121 m_port == testURI.m_port &&
1122 ((m_path == null && testURI.m_path == null) ||
1123 (m_path != null && testURI.m_path != null &&
1124 m_path.equals(testURI.m_path))) &&
1125 ((m_queryString == null && testURI.m_queryString == null) ||
1126 (m_queryString != null && testURI.m_queryString != null &&
1127 m_queryString.equals(testURI.m_queryString))) &&
1128 ((m_fragment == null && testURI.m_fragment == null) ||
1129 (m_fragment != null && testURI.m_fragment != null &&
1130 m_fragment.equals(testURI.m_fragment)))) {
1131 return true;
1132 }
1133 }
1134 return false;
1135 }
1136
1137 @Override
1138 public int hashCode() {
1139 // No members safe to use, just default to a constant.
1140 return 153214;
1141 }
1142
1143 /**
1144 * Get the URI as a string specification. See RFC 2396 Section 5.2.
1145 *
1146 * @return the URI string specification
1147 */
1148 @Override
1149 public String toString() {
1150 StringBuilder uriSpecString = new StringBuilder();
1151
1152 if (m_scheme != null) {
1153 uriSpecString.append(m_scheme);
1154 uriSpecString.append(':');
1155 }
1156 uriSpecString.append(getSchemeSpecificPart());
1157 return uriSpecString.toString();
1158 }
1159
1160 /**
1161 * Get the indicator as to whether this URI uses the "generic URI"
1162 * syntax.
1163 *
1164 * @return true if this URI uses the "generic URI" syntax, false
1165 * otherwise
1166 */
1167 public boolean isGenericURI() {
1168 // presence of the host (whether valid or empty) means
1169 // double-slashes which means generic uri
1170 return (m_host != null);
1171 }
1172
1173 /**
1174 * Determine whether a scheme conforms to the rules for a scheme name.
1175 * A scheme is conformant if it starts with an alphanumeric, and
1176 * contains only alphanumerics, '+','-' and '.'.
1177 *
1178 * @param p_scheme scheme name
1179 *
1180 * @return true if the scheme is conformant, false otherwise
1181 */
1182 public static boolean isConformantSchemeName(String p_scheme) {
1183 if (p_scheme == null || p_scheme.trim().length() == 0) {
1184 return false;
1185 }
1186
1187 if (!isAlpha(p_scheme.charAt(0))) {
1188 return false;
1189 }
1190
1191 char testChar;
1192 for (int i = 1; i < p_scheme.length(); i++) {
1193 testChar = p_scheme.charAt(i);
1194 if (!isAlphanum(testChar) &&
1195 SCHEME_CHARACTERS.indexOf(testChar) == -1) {
1196 return false;
1197 }
1198 }
1199
1200 return true;
1201 }
1202
1203 /**
1204 * Determine whether a string is syntactically capable of representing
1205 * a valid IPv4 address or the domain name of a network host. A valid
1206 * IPv4 address consists of four decimal digit groups separated by a
1207 * '.'. A hostname consists of domain labels (each of which must
1208 * begin and end with an alphanumeric but may contain '-') separated
1209 * by a '.'. See RFC 2396 Section 3.2.2.
1210 *
1211 * @param p_address address
1212 *
1213 * @return true if the string is a syntactically valid IPv4 address
1214 * or hostname
1215 */
1216 public static boolean isWellFormedAddress(String p_address) {
1217 if (p_address == null) {
1218 return false;
1219 }
1220
1221 String address = p_address.trim();
1222 int addrLength = address.length();
1223 if (addrLength == 0 || addrLength > 255) {
1224 return false;
1225 }
1226
1227 if (address.startsWith(".") || address.startsWith("-")) {
1228 return false;
1229 }
1230
1231 // rightmost domain label starting with digit indicates IP address
|