/** * Parses the contents of the {@code OnBehalfOf} element and returns a {@code Principal} * representing the identity on behalf of which the request was made. * * @param onBehalfOf the type that represents the {@code OnBehalfOf} element. * @return a {@code Principal} representing the extracted identity, or {@code null} if the * contents of the {@code OnBehalfOf} element could not be parsed. */ public static Principal getOnBehalfOfPrincipal(OnBehalfOfType onBehalfOf) { // if OnBehalfOfType contains a username token, return this username in the form of a principal. UsernameTokenType usernameToken = null; List<Object> theList = onBehalfOf.getAny(); for (Object content : theList) { if (content instanceof UsernameTokenType) usernameToken = (UsernameTokenType) content; else if (content instanceof JAXBElement) { JAXBElement<?> element = (JAXBElement<?>) content; if (element.getName().getLocalPart().equalsIgnoreCase("UsernameToken")) usernameToken = (UsernameTokenType) element.getValue(); } } /* * Object content = onBehalfOf.getAny(); if (content instanceof UsernameTokenType) usernameToken = (UsernameTokenType) * content; else if (content instanceof JAXBElement) { JAXBElement<?> element = (JAXBElement<?>) content; if * (element.getName().getLocalPart().equalsIgnoreCase("UsernameToken")) usernameToken = (UsernameTokenType) * element.getValue(); } */ if (usernameToken != null && usernameToken.getUsername() != null) { final String username = usernameToken.getUsername().getValue(); return new Principal() { public String getName() { return username; } }; } logger.debug("Unable to parse the contents of the OnBehalfOfType: " + onBehalfOf.getAny()); return null; }
/** * Creates a {@code OnBehalfOfType} instance that contains a {@code UsernameTokenType}. * * @param username a {@code String} that represents the username of the {@code UsernameTokenType}. * @param id an optional {@code String} that uniquely identifies the {@code UsernameTokenType}. * @return the constructed {@code OnBehalfOfType} instance. */ public static OnBehalfOfType createOnBehalfOfWithUsername(String username, String id) { AttributedString attrString = new AttributedString(); attrString.setValue(username); UsernameTokenType usernameToken = new UsernameTokenType(); usernameToken.setId(id); usernameToken.setUsername(attrString); // create the OnBehalfOfType and set the UsernameTokenType. OnBehalfOfType onBehalfOf = new OnBehalfOfType(); onBehalfOf.add(usernameToken); return onBehalfOf; }