use of org.codice.ddf.parser.ParserException in project ddf by codice.
the class UsernameTokenValidator method validateToken.
/**
* Validate a Token using the given TokenValidatorParameters.
*/
public TokenValidatorResponse validateToken(TokenValidatorParameters tokenParameters) {
LOGGER.debug("Validating UsernameToken");
if (parser == null) {
throw new IllegalStateException("XMLParser must be configured.");
}
if (failedLoginDelayer == null) {
throw new IllegalStateException("Failed Login Delayer must be configured");
}
STSPropertiesMBean stsProperties = tokenParameters.getStsProperties();
Crypto sigCrypto = stsProperties.getSignatureCrypto();
CallbackHandler callbackHandler = stsProperties.getCallbackHandler();
RequestData requestData = new RequestData();
requestData.setSigVerCrypto(sigCrypto);
WSSConfig wssConfig = WSSConfig.getNewInstance();
requestData.setWssConfig(wssConfig);
requestData.setCallbackHandler(callbackHandler);
TokenValidatorResponse response = new TokenValidatorResponse();
ReceivedToken validateTarget = tokenParameters.getToken();
validateTarget.setState(ReceivedToken.STATE.INVALID);
response.setToken(validateTarget);
if (!validateTarget.isUsernameToken()) {
return response;
}
//
// Turn the JAXB UsernameTokenType into a DOM Element for validation
//
UsernameTokenType usernameTokenType = (UsernameTokenType) validateTarget.getToken();
JAXBElement<UsernameTokenType> tokenType = new JAXBElement<>(QNameConstants.USERNAME_TOKEN, UsernameTokenType.class, usernameTokenType);
Document doc = DOMUtils.createDocument();
Element rootElement = doc.createElement("root-element");
List<String> ctxPath = new ArrayList<>(1);
ctxPath.add(UsernameTokenType.class.getPackage().getName());
Element usernameTokenElement = null;
ParserConfigurator configurator = parser.configureParser(ctxPath, UsernameTokenValidator.class.getClassLoader());
try {
parser.marshal(configurator, tokenType, rootElement);
usernameTokenElement = (Element) rootElement.getFirstChild();
} catch (ParserException ex) {
LOGGER.info("Unable to parse username token", ex);
return response;
}
//
try {
boolean allowNamespaceQualifiedPasswordTypes = requestData.isAllowNamespaceQualifiedPasswordTypes();
UsernameToken ut = new UsernameToken(usernameTokenElement, allowNamespaceQualifiedPasswordTypes, new BSPEnforcer());
// The parsed principal is set independent whether validation is successful or not
response.setPrincipal(new CustomTokenPrincipal(ut.getName()));
if (ut.getPassword() == null) {
failedLoginDelayer.delay(ut.getName());
return response;
}
Credential credential = new Credential();
credential.setUsernametoken(ut);
//Only this section is new, the rest is copied from the apache class
Set<Map.Entry<String, Validator>> entries = validators.entrySet();
for (Map.Entry<String, Validator> entry : entries) {
try {
entry.getValue().validate(credential, requestData);
validateTarget.setState(ReceivedToken.STATE.VALID);
break;
} catch (WSSecurityException ex) {
LOGGER.debug("Unable to validate user against {}" + entry.getKey(), ex);
}
}
if (ReceivedToken.STATE.INVALID.equals(validateTarget.getState())) {
failedLoginDelayer.delay(ut.getName());
return response;
}
//end new section
Principal principal = createPrincipal(ut.getName(), ut.getPassword(), ut.getPasswordType(), ut.getNonce(), ut.getCreated());
response.setPrincipal(principal);
response.setTokenRealm(null);
validateTarget.setState(ReceivedToken.STATE.VALID);
validateTarget.setPrincipal(principal);
} catch (WSSecurityException ex) {
LOGGER.debug("Unable to validate token.", ex);
}
return response;
}
use of org.codice.ddf.parser.ParserException in project ddf by codice.
the class MetacardMarshaller method getRegistryPackageAsXml.
/**
* Converts the RegistryPackageType into an xml string
*
* @param registryPackage Registry package to convert
* @return Ebrim xml string
* @throws ParserException
*/
public String getRegistryPackageAsXml(RegistryPackageType registryPackage) throws ParserException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
JAXBElement<RegistryPackageType> registryObjectTypeJAXBElement = EbrimConstants.RIM_FACTORY.createRegistryPackage(registryPackage);
parser.marshal(marshalConfigurator, registryObjectTypeJAXBElement, outputStream);
return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ParserException("Error parsing registry package to ebrim xml", e);
}
}
use of org.codice.ddf.parser.ParserException in project ddf by codice.
the class GeometryTransformer method transform.
public BinaryContent transform(Attribute attribute) throws CatalogTransformerException {
ParserConfigurator parserConfigurator = getParserConfigurator().setHandler(new DefaultValidationEventHandler());
try {
ByteArrayOutputStream os = new ByteArrayOutputStream(BUFFER_SIZE);
getParser().marshal(parserConfigurator, GeometryAdapter.marshalFrom(attribute), os);
ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());
return new BinaryContentImpl(bais, MIME_TYPE);
} catch (ParserException e) {
throw new CatalogTransformerException("Failed to marshall geometry data", e);
}
}
use of org.codice.ddf.parser.ParserException in project ddf by codice.
the class XmlParser method unmarshal.
private <T> T unmarshal(ParserConfigurator configurator, Function<Unmarshaller, T> func) throws ParserException {
JAXBContext jaxbContext = getContext(configurator.getContextPath(), configurator.getClassLoader());
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(configurator.getClassLoader());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
if (configurator.getAdapter() != null) {
unmarshaller.setAdapter(configurator.getAdapter());
}
if (configurator.getHandler() != null) {
unmarshaller.setEventHandler(configurator.getHandler());
}
for (Map.Entry<String, Object> propRow : configurator.getProperties().entrySet()) {
unmarshaller.setProperty(propRow.getKey(), propRow.getValue());
}
return func.apply(unmarshaller);
} catch (RuntimeException | JAXBException e) {
LOGGER.debug("Error unmarshalling ", e);
throw new ParserException("Error unmarshalling", e);
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
}
use of org.codice.ddf.parser.ParserException in project ddf by codice.
the class XmlParser method getContext.
private JAXBContext getContext(List<String> contextPath, ClassLoader loader) throws ParserException {
String joinedPath = CTX_JOINER.join(contextPath);
JAXBContext jaxbContext;
try {
jaxbContext = jaxbContextCache.get(new CacheKey(joinedPath, loader));
} catch (ExecutionException e) {
LOGGER.info("Unable to create JAXB context using context path: {}", joinedPath, e);
throw new ParserException("Unable to create XmlParser", e.getCause());
}
return jaxbContext;
}
Aggregations