private static String getFirstParameterValue(CaseInsensitiveParamWrapper params, String paramName) { List<String> paramValues = params.getQueryParameters(paramName); return (paramValues.isEmpty()) ? null : paramValues.get(0); }
public static MailTo parse(Uri uri) throws NullPointerException, IllegalArgumentException { if (uri == null || uri.toString() == null) { throw new NullPointerException("Argument 'uri' must not be null"); } if (!isMailTo(uri)) { throw new IllegalArgumentException("Not a mailto scheme"); } String schemaSpecific = uri.getSchemeSpecificPart(); int end = schemaSpecific.indexOf('?'); if (end == -1) { end = schemaSpecific.length(); } CaseInsensitiveParamWrapper params = new CaseInsensitiveParamWrapper(Uri.parse("foo://bar?" + uri.getEncodedQuery())); // Extract the recipient's email address from the mailto URI if there's one. String recipient = Uri.decode(schemaSpecific.substring(0, end)); List<String> toList = params.getQueryParameters(TO); if (recipient.length() != 0) { toList.add(0, recipient); } List<String> ccList = params.getQueryParameters(CC); List<String> bccList = params.getQueryParameters(BCC); Address[] toAddresses = toAddressArray(toList); Address[] ccAddresses = toAddressArray(ccList); Address[] bccAddresses = toAddressArray(bccList); String subject = getFirstParameterValue(params, SUBJECT); String body = getFirstParameterValue(params, BODY); return new MailTo(toAddresses, ccAddresses, bccAddresses, subject, body); }
@Test public void testCaseInsensitiveParamWrapper() { Uri uri = Uri.parse("scheme://authority?a=one&b=two&c=three"); CaseInsensitiveParamWrapper caseInsensitiveParamWrapper = new CaseInsensitiveParamWrapper(uri); List<String> result = caseInsensitiveParamWrapper.getQueryParameters("b"); assertThat(Collections.singletonList("two"), is(result)); }
@Test public void testCaseInsensitiveParamWrapper_withoutQueryParameters() { Uri uri = Uri.parse("scheme://authority"); CaseInsensitiveParamWrapper caseInsensitiveParamWrapper = new CaseInsensitiveParamWrapper(uri); List<String> result = caseInsensitiveParamWrapper.getQueryParameters("name"); assertThat(Collections.<String>emptyList(), is(result)); } }
@Test public void testCaseInsensitiveParamWrapper_multipleMatchingQueryParameters() { Uri uri = Uri.parse("scheme://authority?xname=one&name=two&Name=Three&NAME=FOUR"); CaseInsensitiveParamWrapper caseInsensitiveParamWrapper = new CaseInsensitiveParamWrapper(uri); List<String> result = caseInsensitiveParamWrapper.getQueryParameters("name"); assertThat(Arrays.asList("two", "Three", "FOUR"), is(result)); }