1 /* 2 * Copyright (C) 2023 The Android Open Source Project 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 17 package com.android.cts.verifier; 18 19 import android.util.Log; 20 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 import org.xmlpull.v1.XmlPullParserFactory; 24 25 import java.io.File; 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.io.InputStreamReader; 30 import java.util.HashMap; 31 import java.util.Map; 32 33 /** 34 * Reads URL replacement configurations from the give config file, and provides the method to do the 35 * URL replacement. 36 */ 37 public class UrlReplacement { 38 private static final String LOG_TAG = UrlReplacement.class.getName(); 39 40 private static final String NS = null; 41 private static final String DYNAMIC_CONFIG_URL_TAG = "dynamicConfigUrl"; 42 private static final String URL_REPLACEMENT_TAG = "urlReplacement"; 43 private static final String ENTRY_TAG = "entry"; 44 private static final String REPLACEMENT_TAG = "replacement"; 45 private static final String URL_ATTR = "url"; 46 private static final String URL_REPLACEMENT_FILE_PATH = "/sdcard/UrlReplacement.xml"; 47 private static final Map<String, String> REPLACEMENT_MAP = new HashMap<>(); 48 49 static { 50 File urlReplacementConfigFile = new File(URL_REPLACEMENT_FILE_PATH); 51 if (urlReplacementConfigFile.exists()) { Log.i(LOG_TAG, String.format("Parsing URL replacement config file %s ...", URL_REPLACEMENT_FILE_PATH))52 Log.i(LOG_TAG, String.format("Parsing URL replacement config file %s ...", 53 URL_REPLACEMENT_FILE_PATH)); 54 try { 55 InputStream inputStream = new FileInputStream(urlReplacementConfigFile); 56 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); parser.setInput(new InputStreamReader(inputStream))57 parser.setInput(new InputStreamReader(inputStream)); parser.nextTag()58 parser.nextTag(); 59 parser.require(XmlPullParser.START_TAG, NS, URL_REPLACEMENT_TAG)60 parser.require(XmlPullParser.START_TAG, NS, URL_REPLACEMENT_TAG); parser.nextTag()61 parser.nextTag(); 62 parser.require(XmlPullParser.START_TAG, NS, DYNAMIC_CONFIG_URL_TAG)63 parser.require(XmlPullParser.START_TAG, NS, DYNAMIC_CONFIG_URL_TAG); 64 // Ignore the DynamicConfig server URL. parser.nextText()65 parser.nextText(); parser.require(XmlPullParser.END_TAG, NS, DYNAMIC_CONFIG_URL_TAG)66 parser.require(XmlPullParser.END_TAG, NS, DYNAMIC_CONFIG_URL_TAG); 67 68 while (parser.nextTag() == XmlPullParser.START_TAG) { parser.require(XmlPullParser.START_TAG, NS, ENTRY_TAG)69 parser.require(XmlPullParser.START_TAG, NS, ENTRY_TAG); 70 String key = parser.getAttributeValue(NS, URL_ATTR); parser.nextTag()71 parser.nextTag(); parser.require(XmlPullParser.START_TAG, NS, REPLACEMENT_TAG)72 parser.require(XmlPullParser.START_TAG, NS, REPLACEMENT_TAG); 73 String value = parser.nextText(); parser.require(XmlPullParser.END_TAG, NS, REPLACEMENT_TAG)74 parser.require(XmlPullParser.END_TAG, NS, REPLACEMENT_TAG); parser.nextTag()75 parser.nextTag(); parser.require(XmlPullParser.END_TAG, NS, ENTRY_TAG)76 parser.require(XmlPullParser.END_TAG, NS, ENTRY_TAG); 77 if (key != null && value != null) { REPLACEMENT_MAP.put(key, value)78 REPLACEMENT_MAP.put(key, value); 79 } 80 } 81 parser.require(XmlPullParser.END_TAG, NS, URL_REPLACEMENT_TAG)82 parser.require(XmlPullParser.END_TAG, NS, URL_REPLACEMENT_TAG); 83 Log.i(LOG_TAG, String.format("Parsed URL replacement config: %s", REPLACEMENT_MAP))84 Log.i(LOG_TAG, String.format("Parsed URL replacement config: %s", 85 REPLACEMENT_MAP)); 86 } catch (XmlPullParserException | IOException e) { 87 Log.w(LOG_TAG, String.format("Failed to parse URL replacement config file: %s.", 88 e.getMessage())); 89 e.printStackTrace(); 90 } 91 } 92 } 93 94 /** 95 * Replaces the URLs that existed in the key set of {@code REPLACEMENT_MAP} with the 96 * corresponding value, and returns the final result. 97 */ getFinalUri(String originalUrl)98 public static String getFinalUri(String originalUrl) { 99 String url = originalUrl; 100 for (Map.Entry<String, String> entry : REPLACEMENT_MAP.entrySet()) { 101 url = url.replace(entry.getKey(), entry.getValue()); 102 } 103 return url; 104 } 105 } 106