@Test public void testParse() throws Exception { assertValid("a: assertValid("b: assertValid("c: assertValid("d: assertValid("e: assertValid("f: assertValid("g: assertValid("abc-123: assertValid("1.2.3.4", null, "1.2.3.4", null, null, null); assertValid("1.2.3.4:8888", null, "1.2.3.4", 8888, null, null); assertValid("1.2.3.4:123", null, "1.2.3.4", 123, null, null); assertValid("uuu@1.2.3.4", null, "1.2.3.4", null, "uuu", null); assertValid("uuu@1.2.3.4:123", null, "1.2.3.4", 123, "uuu", null); assertValid("uuu:ppp@1.2.3.4", null, "1.2.3.4", null, "uuu", "ppp"); assertValid("uuu:ppp@1.2.3.4:123", null, "1.2.3.4", 123, "uuu", "ppp"); assertInvalid(""); assertInvalid("x: assertInvalid("uuu@ppp@1.2.3.4"); assertInvalid("uuu:ppp@1.2.3.4@12323"); assertInvalid("uuu@ppp:1.2.3.4@12323"); }
public Target parse(String target) throws Exception { Target result = null; try { URI uri = new URI(contains(target, ": result = new Target(); result.setProvider(StringUtils.equals(uri.getScheme(), DUMMY_PROTOCOL) ? null : uri.getScheme()); result.setHost(uri.getHost()); result.setPort(uri.getPort() == -1 ? null : uri.getPort()); String userInfo = uri.getUserInfo(); if (isNotBlank(userInfo)) { if (contains(userInfo, ":")) { String[] split = userInfo.split(":"); result.setUsername(split[0]); result.setPassword(split[1]); } else { result.setUsername(userInfo); } } if (isNotBlank(uri.getRawPath())) { result.setDestination(length(uri.getRawPath()) > 1 ? removeStart(uri.getRawPath(), "/") : uri.getRawPath()); } if (isNotBlank(uri.getQuery())) { for (String pair: StringUtils.split(uri.getQuery(), "&")) { String[] split = StringUtils.split(pair, "=", 2); if (split.length > 0) { String key = split[0]; if (isNotBlank(key)) { String value = null; if (split.length > 1) { if (isNotBlank(split[1])) { try { value = URLDecoder.decode(split[1], UTF_8.toString()); } catch (UnsupportedEncodingException ex) { Output.error("UTF-8 unknown"); } } } result.getParameter().put(key, value); } } } } if (isBlank(result.getHost())) { result = null; } } catch (URISyntaxException uex) { } return result; }
TargetParser { public Target parse(String target) throws Exception { Target result = null; try { URI uri = new URI(contains(target, ": result = new Target(); result.setProvider(StringUtils.equals(uri.getScheme(), DUMMY_PROTOCOL) ? null : uri.getScheme()); result.setHost(uri.getHost()); result.setPort(uri.getPort() == -1 ? null : uri.getPort()); String userInfo = uri.getUserInfo(); if (isNotBlank(userInfo)) { if (contains(userInfo, ":")) { String[] split = userInfo.split(":"); result.setUsername(split[0]); result.setPassword(split[1]); } else { result.setUsername(userInfo); } } if (isNotBlank(uri.getRawPath())) { result.setDestination(length(uri.getRawPath()) > 1 ? removeStart(uri.getRawPath(), "/") : uri.getRawPath()); } if (isNotBlank(uri.getQuery())) { for (String pair: StringUtils.split(uri.getQuery(), "&")) { String[] split = StringUtils.split(pair, "=", 2); if (split.length > 0) { String key = split[0]; if (isNotBlank(key)) { String value = null; if (split.length > 1) { if (isNotBlank(split[1])) { try { value = URLDecoder.decode(split[1], UTF_8.toString()); } catch (UnsupportedEncodingException ex) { Output.error("UTF-8 unknown"); } } } result.getParameter().put(key, value); } } } } if (isBlank(result.getHost())) { result = null; } } catch (URISyntaxException uex) { } return result; } }
TargetParser { public Target parse(String target) throws Exception { Target result = null; try { URI uri = new URI(contains(target, ": result = new Target(); result.setProvider(StringUtils.equals(uri.getScheme(), DUMMY_PROTOCOL) ? null : uri.getScheme()); result.setHost(uri.getHost()); result.setPort(uri.getPort() == -1 ? null : uri.getPort()); String userInfo = uri.getUserInfo(); if (isNotBlank(userInfo)) { if (contains(userInfo, ":")) { String[] split = userInfo.split(":"); result.setUsername(split[0]); result.setPassword(split[1]); } else { result.setUsername(userInfo); } } if (isNotBlank(uri.getRawPath())) { result.setDestination(length(uri.getRawPath()) > 1 ? removeStart(uri.getRawPath(), "/") : uri.getRawPath()); } if (isNotBlank(uri.getQuery())) { for (String pair: StringUtils.split(uri.getQuery(), "&")) { String[] split = StringUtils.split(pair, "=", 2); if (split.length > 0) { String key = split[0]; if (isNotBlank(key)) { String value = null; if (split.length > 1) { if (isNotBlank(split[1])) { try { value = URLDecoder.decode(split[1], UTF_8.toString()); } catch (UnsupportedEncodingException ex) { Output.error("UTF-8 unknown"); } } } result.getParameter().put(key, value); } } } } if (isBlank(result.getHost())) { result = null; } } catch (URISyntaxException uex) { } return result; }  }
TargetParser { public Target parse(String target) throws Exception { Target result = null; try { URI uri = new URI(contains(target, ": result = new Target(); result.setProvider(StringUtils.equals(uri.getScheme(), DUMMY_PROTOCOL) ? null : uri.getScheme()); result.setHost(uri.getHost()); result.setPort(uri.getPort() == -1 ? null : uri.getPort()); String userInfo = uri.getUserInfo(); if (isNotBlank(userInfo)) { if (contains(userInfo, ":")) { String[] split = userInfo.split(":"); result.setUsername(split[0]); result.setPassword(split[1]); } else { result.setUsername(userInfo); } } if (isNotBlank(uri.getRawPath())) { result.setDestination(length(uri.getRawPath()) > 1 ? removeStart(uri.getRawPath(), "/") : uri.getRawPath()); } if (isNotBlank(uri.getQuery())) { for (String pair: StringUtils.split(uri.getQuery(), "&")) { String[] split = StringUtils.split(pair, "=", 2); if (split.length > 0) { String key = split[0]; if (isNotBlank(key)) { String value = null; if (split.length > 1) { if (isNotBlank(split[1])) { try { value = URLDecoder.decode(split[1], UTF_8.toString()); } catch (UnsupportedEncodingException ex) { Output.error("UTF-8 unknown"); } } } result.getParameter().put(key, value); } } } } if (isBlank(result.getHost())) { result = null; } } catch (URISyntaxException uex) { } return result; }  Target parse(String target); }
TargetParser { public Target parse(String target) throws Exception { Target result = null; try { URI uri = new URI(contains(target, ": result = new Target(); result.setProvider(StringUtils.equals(uri.getScheme(), DUMMY_PROTOCOL) ? null : uri.getScheme()); result.setHost(uri.getHost()); result.setPort(uri.getPort() == -1 ? null : uri.getPort()); String userInfo = uri.getUserInfo(); if (isNotBlank(userInfo)) { if (contains(userInfo, ":")) { String[] split = userInfo.split(":"); result.setUsername(split[0]); result.setPassword(split[1]); } else { result.setUsername(userInfo); } } if (isNotBlank(uri.getRawPath())) { result.setDestination(length(uri.getRawPath()) > 1 ? removeStart(uri.getRawPath(), "/") : uri.getRawPath()); } if (isNotBlank(uri.getQuery())) { for (String pair: StringUtils.split(uri.getQuery(), "&")) { String[] split = StringUtils.split(pair, "=", 2); if (split.length > 0) { String key = split[0]; if (isNotBlank(key)) { String value = null; if (split.length > 1) { if (isNotBlank(split[1])) { try { value = URLDecoder.decode(split[1], UTF_8.toString()); } catch (UnsupportedEncodingException ex) { Output.error("UTF-8 unknown"); } } } result.getParameter().put(key, value); } } } } if (isBlank(result.getHost())) { result = null; } } catch (URISyntaxException uex) { } return result; }  Target parse(String target);  }