|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.networknt.schema.utils; |
| 17 | + |
| 18 | +import java.io.UnsupportedEncodingException; |
| 19 | +import java.net.IDN; |
| 20 | +import java.net.URI; |
| 21 | +import java.net.URLEncoder; |
| 22 | + |
| 23 | +import com.networknt.schema.AbsoluteIri; |
| 24 | + |
| 25 | +/** |
| 26 | + * Utility functions for AbsoluteIri. |
| 27 | + */ |
| 28 | +public class AbsoluteIris { |
| 29 | + /** |
| 30 | + * Converts an IRI to a URI. |
| 31 | + * |
| 32 | + * @param iri the IRI to convert |
| 33 | + * @return the URI string |
| 34 | + */ |
| 35 | + public static String toUri(AbsoluteIri iri) { |
| 36 | + String iriString = iri.toString(); |
| 37 | + boolean ascii = isAscii(iriString); |
| 38 | + if (ascii) { |
| 39 | + int index = iriString.indexOf('?'); |
| 40 | + if (index == -1) { |
| 41 | + return iriString; |
| 42 | + } |
| 43 | + String rest = iriString.substring(0, index + 1); |
| 44 | + String query = iriString.substring(index + 1); |
| 45 | + StringBuilder result = new StringBuilder(rest); |
| 46 | + handleQuery(result, query); |
| 47 | + return result.toString(); |
| 48 | + } |
| 49 | + String[] parts = iriString.split(":"); // scheme + rest |
| 50 | + if (parts.length == 2) { |
| 51 | + StringBuilder result = new StringBuilder(parts[0]); |
| 52 | + result.append(":"); |
| 53 | + |
| 54 | + String rest = parts[1]; |
| 55 | + if (rest.startsWith("//")) { |
| 56 | + rest = rest.substring(2); |
| 57 | + result.append("//"); |
| 58 | + } else if (rest.startsWith("/")) { |
| 59 | + rest = rest.substring(1); |
| 60 | + result.append("/"); |
| 61 | + } |
| 62 | + String[] query = rest.split("\\?"); // rest ? query |
| 63 | + String[] restParts = query[0].split("/"); |
| 64 | + for (int x = 0; x < restParts.length; x++) { |
| 65 | + String p = restParts[x]; |
| 66 | + if (x == 0) { |
| 67 | + // Domain |
| 68 | + if (isAscii(p)) { |
| 69 | + result.append(p); |
| 70 | + } else { |
| 71 | + result.append(unicodeToASCII(p)); |
| 72 | + } |
| 73 | + } else { |
| 74 | + result.append(p); |
| 75 | + } |
| 76 | + if (x != restParts.length - 1) { |
| 77 | + result.append("/"); |
| 78 | + } |
| 79 | + } |
| 80 | + if (query[0].endsWith("/")) { |
| 81 | + result.append("/"); |
| 82 | + } |
| 83 | + if (query.length == 2) { |
| 84 | + // handle query string |
| 85 | + result.append("?"); |
| 86 | + handleQuery(result, query[1]); |
| 87 | + } |
| 88 | + |
| 89 | + return URI.create(result.toString()).toASCIIString(); |
| 90 | + } |
| 91 | + return iriString; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Determine if a string is US ASCII. |
| 96 | + * |
| 97 | + * @param value to test |
| 98 | + * @return true if ASCII |
| 99 | + */ |
| 100 | + static boolean isAscii(String value) { |
| 101 | + return value.codePoints().allMatch(ch -> ch < 0x7F); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Ensures that the query parameters are properly URL encoded. |
| 106 | + * |
| 107 | + * @param result the string builder to add to |
| 108 | + * @param query the query string |
| 109 | + */ |
| 110 | + static void handleQuery(StringBuilder result, String query) { |
| 111 | + String[] queryParts = query.split("&"); |
| 112 | + for (int y = 0; y < queryParts.length; y++) { |
| 113 | + String queryPart = queryParts[y]; |
| 114 | + |
| 115 | + String[] nameValue = queryPart.split("="); |
| 116 | + try { |
| 117 | + result.append(URLEncoder.encode(nameValue[0], "UTF-8")); |
| 118 | + if (nameValue.length == 2) { |
| 119 | + result.append("="); |
| 120 | + result.append(URLEncoder.encode(nameValue[1], "UTF-8")); |
| 121 | + } |
| 122 | + } catch (UnsupportedEncodingException e) { |
| 123 | + throw new IllegalArgumentException(e); |
| 124 | + } |
| 125 | + if (y != queryParts.length - 1) { |
| 126 | + result.append("&"); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // The following routines are from apache commons validator routines |
| 132 | + // DomainValidator |
| 133 | + static String unicodeToASCII(final String input) { |
| 134 | + try { |
| 135 | + final String ascii = IDN.toASCII(input); |
| 136 | + if (IDNBUGHOLDER.IDN_TOASCII_PRESERVES_TRAILING_DOTS) { |
| 137 | + return ascii; |
| 138 | + } |
| 139 | + final int length = input.length(); |
| 140 | + if (length == 0) { // check there is a last character |
| 141 | + return input; |
| 142 | + } |
| 143 | + // RFC3490 3.1. 1) |
| 144 | + // Whenever dots are used as label separators, the following |
| 145 | + // characters MUST be recognized as dots: U+002E (full stop), U+3002 |
| 146 | + // (ideographic full stop), U+FF0E (fullwidth full stop), U+FF61 |
| 147 | + // (halfwidth ideographic full stop). |
| 148 | + final char lastChar = input.charAt(length - 1);// fetch original last char |
| 149 | + switch (lastChar) { |
| 150 | + case '\u002E': // "." full stop |
| 151 | + case '\u3002': // ideographic full stop |
| 152 | + case '\uFF0E': // fullwidth full stop |
| 153 | + case '\uFF61': // halfwidth ideographic full stop |
| 154 | + return ascii + "."; // restore the missing stop |
| 155 | + default: |
| 156 | + return ascii; |
| 157 | + } |
| 158 | + } catch (final IllegalArgumentException e) { // input is not valid |
| 159 | + return input; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static class IDNBUGHOLDER { |
| 164 | + private static final boolean IDN_TOASCII_PRESERVES_TRAILING_DOTS = keepsTrailingDot(); |
| 165 | + |
| 166 | + private static boolean keepsTrailingDot() { |
| 167 | + final String input = "a."; // must be a valid name |
| 168 | + return input.equals(IDN.toASCII(input)); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments