AuthorizationHeaderParser { public static Result parse(String pAuthInfo) { StringTokenizer stok = new StringTokenizer(pAuthInfo); String method = stok.nextToken(); if (!HttpServletRequest.BASIC_AUTH.equalsIgnoreCase(method)) { throw new IllegalArgumentException("Only BasicAuthentication is supported"); } String b64Auth = stok.nextToken(); String auth = new String(Base64Util.decode(b64Auth)); int p = auth.indexOf(':'); String user; String password; boolean valid; if (p != -1) { user = auth.substring(0, p); password = auth.substring(p+1); valid = true; } else { valid = false; user = null; password = null; } return new Result(method,user,password,valid); } private  AuthorizationHeaderParser(); static Result parse(String pAuthInfo); static final String JOLOKIA_ALTERNATE_AUTHORIZATION_HEADER; }
@Test public void testParseAuthorizationPositive() throws Exception { AuthorizationHeaderParser.Result result = AuthorizationHeaderParser.parse("Basic cm9sYW5kOnMhY3IhdA=="); assertEquals(result.getUser(),"roland"); assertEquals(result.getPassword(),"s!cr!t"); assertTrue(result.isValid()); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*BasicAuthentication.*") public void testParseAuthorizationWrongMethod() throws Exception { AuthorizationHeaderParser.parse("Digest cm9sYW5kOnMhY3IhdA=="); }
@Test public void testParseAuthorizationInvalidFormat() throws Exception { AuthorizationHeaderParser.Result result = AuthorizationHeaderParser.parse("Basic cm9sYAZ5kOnMhA="); assertFalse(result.isValid()); }