That fuck shit the fascists are using
at master 80 lines 2.5 kB view raw
1/* 2 * Copyright (C) 2015 Open Whisper Systems 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18package org.tm.archive.util; 19 20import java.net.URISyntaxException; 21import java.net.URLDecoder; 22import java.util.HashMap; 23import java.util.Map; 24 25public class Rfc5724Uri { 26 27 private final String uri; 28 private final String schema; 29 private final String path; 30 private final Map<String, String> queryParams; 31 32 public Rfc5724Uri(String uri) throws URISyntaxException { 33 this.uri = uri; 34 this.schema = parseSchema(); 35 this.path = parsePath(); 36 this.queryParams = parseQueryParams(); 37 } 38 39 private String parseSchema() throws URISyntaxException { 40 String[] parts = uri.split(":"); 41 42 if (parts.length < 1 || parts[0].isEmpty()) throw new URISyntaxException(uri, "invalid schema"); 43 else return parts[0]; 44 } 45 46 private String parsePath() throws URISyntaxException { 47 String[] parts = uri.split("\\?")[0].split(":", 2); 48 49 if (parts.length < 2 || parts[1].isEmpty()) throw new URISyntaxException(uri, "invalid path"); 50 else return parts[1]; 51 } 52 53 private Map<String, String> parseQueryParams() throws URISyntaxException { 54 Map<String, String> queryParams = new HashMap<>(); 55 if (uri.split("\\?").length < 2) { 56 return queryParams; 57 } 58 59 for (String keyValue : uri.split("\\?")[1].split("&")) { 60 String[] parts = keyValue.split("="); 61 62 if (parts.length == 1) queryParams.put(parts[0], ""); 63 else queryParams.put(parts[0], URLDecoder.decode(parts[1])); 64 } 65 66 return queryParams; 67 } 68 69 public String getSchema() { 70 return schema; 71 } 72 73 public String getPath() { 74 return path; 75 } 76 77 public Map<String, String> getQueryParams() { 78 return queryParams; 79 } 80}