1 /* 2 * Copyright (c) 1998, 2019, 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 sun.security.util; 27 28 import java.net.URI; 29 import java.net.URISyntaxException; 30 import java.security.GeneralSecurityException; 31 32 /** 33 * A utility class to expand properties embedded in a string. 34 * Strings of the form ${some.property.name} are expanded to 35 * be the value of the property. Also, the special ${/} property 36 * is expanded to be the same as file.separator. If a property 37 * is not set, a GeneralSecurityException will be thrown. 38 * 39 * @author Roland Schemers 40 */ 41 public class PropertyExpander { 42 43 44 public static class ExpandException extends GeneralSecurityException { 45 46 @java.io.Serial 47 private static final long serialVersionUID = -7941948581406161702L; 48 ExpandException(String msg)49 public ExpandException(String msg) { 50 super(msg); 51 } 52 } 53 expand(String value)54 public static String expand(String value) 55 throws ExpandException 56 { 57 return expand(value, false); 58 } 59 expand(String value, boolean encodeURL)60 public static String expand(String value, boolean encodeURL) 61 throws ExpandException 62 { 63 if (value == null) 64 return null; 65 66 int p = value.indexOf("${", 0); 67 68 // no special characters 69 if (p == -1) return value; 70 71 StringBuilder sb = new StringBuilder(value.length()); 72 int max = value.length(); 73 int i = 0; // index of last character we copied 74 75 scanner: 76 while (p < max) { 77 if (p > i) { 78 // copy in anything before the special stuff 79 sb.append(value.substring(i, p)); 80 i = p; 81 } 82 int pe = p+2; 83 84 // do not expand ${{ ... }} 85 if (pe < max && value.charAt(pe) == '{') { 86 pe = value.indexOf("}}", pe); 87 if (pe == -1 || pe+2 == max) { 88 // append remaining chars 89 sb.append(value.substring(p)); 90 break scanner; 91 } else { 92 // append as normal text 93 pe++; 94 sb.append(value.substring(p, pe+1)); 95 } 96 } else { 97 while ((pe < max) && (value.charAt(pe) != '}')) { 98 pe++; 99 } 100 if (pe == max) { 101 // no matching '}' found, just add in as normal text 102 sb.append(value.substring(p, pe)); 103 break scanner; 104 } 105 String prop = value.substring(p+2, pe); 106 if (prop.equals("/")) { 107 sb.append(java.io.File.separatorChar); 108 } else { 109 String val = System.getProperty(prop); 110 if (val != null) { 111 if (encodeURL) { 112 // encode 'val' unless it's an absolute URI 113 // at the beginning of the string buffer 114 try { 115 if (sb.length() > 0 || 116 !(new URI(val)).isAbsolute()) { 117 val = sun.net.www.ParseUtil.encodePath(val); 118 } 119 } catch (URISyntaxException use) { 120 val = sun.net.www.ParseUtil.encodePath(val); 121 } 122 } 123 sb.append(val); 124 } else { 125 throw new ExpandException( 126 "unable to expand property " + 127 prop); 128 } 129 } 130 } 131 i = pe+1; 132 p = value.indexOf("${", i); 133 if (p == -1) { 134 // no more to expand. copy in any extra 135 if (i < max) { 136 sb.append(value.substring(i, max)); 137 } 138 // break out of loop 139 break scanner; 140 } 141 } 142 return sb.toString(); 143 } 144 } 145