Search in sources :

Example 96 with JAXBElement

use of javax.xml.bind.JAXBElement in project ddf by codice.

the class FederationAdminServiceImplTest method testGetLocalRegistryObjects.

@Test
public void testGetLocalRegistryObjects() throws Exception {
    Metacard localMetacardOne = testMetacard;
    Metacard localMetacardTwo = testMetacard;
    List<Metacard> localMetacards = new ArrayList<>();
    localMetacards.add(localMetacardOne);
    localMetacards.add(localMetacardTwo);
    doReturn(localMetacards).when(federationAdminServiceImpl).getLocalRegistryMetacards();
    JAXBElement<RegistryPackageType> jaxbRegistryPackage = EbrimConstants.RIM_FACTORY.createRegistryPackage(getTestRegistryPackage());
    when(parser.unmarshal(any(ParserConfigurator.class), eq(JAXBElement.class), any(InputStream.class))).thenReturn(jaxbRegistryPackage);
    List<RegistryPackageType> packages = federationAdminServiceImpl.getLocalRegistryObjects();
    assertThat(packages, hasSize(2));
    verify(parser, times(2)).unmarshal(any(ParserConfigurator.class), eq(JAXBElement.class), any(InputStream.class));
}
Also used : ParserConfigurator(org.codice.ddf.parser.ParserConfigurator) Metacard(ddf.catalog.data.Metacard) RegistryPackageType(oasis.names.tc.ebxml_regrep.xsd.rim._3.RegistryPackageType) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 97 with JAXBElement

use of javax.xml.bind.JAXBElement in project ddf by codice.

the class FederationAdminServiceImplTest method testGetRegistryObjects.

@Test
public void testGetRegistryObjects() throws Exception {
    Metacard metacardOne = testMetacard;
    Metacard metacardTwo = testMetacard;
    JAXBElement<RegistryPackageType> jaxbRegistryPackage = EbrimConstants.RIM_FACTORY.createRegistryPackage(getTestRegistryPackage());
    QueryRequest request = getTestQueryRequest();
    QueryResponse response = getPopulatedTestQueryResponse(request, metacardOne, metacardTwo);
    when(catalogFramework.query(any(QueryRequest.class))).thenReturn(response);
    when(parser.unmarshal(any(ParserConfigurator.class), eq(JAXBElement.class), any(InputStream.class))).thenReturn(jaxbRegistryPackage);
    List<RegistryPackageType> regObjects = federationAdminServiceImpl.getRegistryObjects();
    assertThat(regObjects, hasSize(2));
    verify(catalogFramework).query(any(QueryRequest.class));
    verify(parser, times(2)).unmarshal(any(ParserConfigurator.class), eq(JAXBElement.class), any(InputStream.class));
}
Also used : ParserConfigurator(org.codice.ddf.parser.ParserConfigurator) Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) RegistryPackageType(oasis.names.tc.ebxml_regrep.xsd.rim._3.RegistryPackageType) InputStream(java.io.InputStream) QueryResponse(ddf.catalog.operation.QueryResponse) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 98 with JAXBElement

use of javax.xml.bind.JAXBElement in project ddf by codice.

the class TestCswEndpoint method testMarshallDescribeRecord.

/**
     * Tests to see that JAXB configuration is working
     */
@Test
public void testMarshallDescribeRecord() {
    DescribeRecordResponseType response = new DescribeRecordResponseType();
    List<SchemaComponentType> schemas = new ArrayList<>();
    SchemaComponentType schemaComponentType = new SchemaComponentType();
    schemas.add(schemaComponentType);
    response.setSchemaComponent(schemas);
    JAXBContext context;
    try {
        context = JAXBContext.newInstance("net.opengis.cat.csw.v_2_0_2:net.opengis.filter.v_1_1_0:net.opengis.gml.v_3_1_1");
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        StringWriter sw = new StringWriter();
        JAXBElement<DescribeRecordResponseType> wrappedResponse = new JAXBElement<>(cswQnameOutPutSchema, DescribeRecordResponseType.class, response);
        marshaller.marshal(wrappedResponse, sw);
        LOGGER.info("Response: {}", sw.toString());
    } catch (JAXBException e) {
        fail("Could not marshall message, Error: " + e.getMessage());
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) SchemaComponentType(net.opengis.cat.csw.v_2_0_2.SchemaComponentType) StringWriter(java.io.StringWriter) DescribeRecordResponseType(net.opengis.cat.csw.v_2_0_2.DescribeRecordResponseType) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 99 with JAXBElement

use of javax.xml.bind.JAXBElement in project ddf by codice.

the class TestCswEndpoint method testPostGetRecordsValidElementSetNames.

@Test
public void testPostGetRecordsValidElementSetNames() throws CswException {
    GetRecordsType grr = createDefaultPostRecordsRequest();
    QueryType query = new QueryType();
    JAXBElement<QueryType> jaxbQuery = new JAXBElement<>(cswQnameOutPutSchema, QueryType.class, query);
    ElementSetNameType elsnt = new ElementSetNameType();
    elsnt.setValue(ElementSetType.BRIEF);
    query.setElementSetName(elsnt);
    grr.setAbstractQuery(jaxbQuery);
    csw.getRecords(grr);
}
Also used : GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) ElementSetNameType(net.opengis.cat.csw.v_2_0_2.ElementSetNameType) JAXBElement(javax.xml.bind.JAXBElement) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Test(org.junit.Test)

Example 100 with JAXBElement

use of javax.xml.bind.JAXBElement 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;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) UsernameToken(org.apache.wss4j.dom.message.token.UsernameToken) Document(org.w3c.dom.Document) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) WSSConfig(org.apache.wss4j.dom.engine.WSSConfig) JAASUsernameTokenValidator(org.apache.wss4j.dom.validate.JAASUsernameTokenValidator) RequestData(org.apache.wss4j.dom.handler.RequestData) ReceivedToken(org.apache.cxf.sts.request.ReceivedToken) ParserException(org.codice.ddf.parser.ParserException) Credential(org.apache.wss4j.dom.validate.Credential) UsernameTokenType(org.apache.cxf.ws.security.sts.provider.model.secext.UsernameTokenType) BSPEnforcer(org.apache.wss4j.common.bsp.BSPEnforcer) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) JAXBElement(javax.xml.bind.JAXBElement) ParserConfigurator(org.codice.ddf.parser.ParserConfigurator) Crypto(org.apache.wss4j.common.crypto.Crypto) STSPropertiesMBean(org.apache.cxf.sts.STSPropertiesMBean) TokenValidatorResponse(org.apache.cxf.sts.token.validator.TokenValidatorResponse) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Validator(org.apache.wss4j.dom.validate.Validator) JAASUsernameTokenValidator(org.apache.wss4j.dom.validate.JAASUsernameTokenValidator) TokenValidator(org.apache.cxf.sts.token.validator.TokenValidator) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) Principal(java.security.Principal)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)650 QName (javax.xml.namespace.QName)194 Element (org.w3c.dom.Element)124 RequestSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType)102 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)95 ArrayList (java.util.ArrayList)93 MessageImpl (org.apache.cxf.message.MessageImpl)92 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)90 Test (org.junit.Test)87 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)86 StaticSTSProperties (org.apache.cxf.sts.StaticSTSProperties)83 Crypto (org.apache.wss4j.common.crypto.Crypto)82 JAXBException (javax.xml.bind.JAXBException)81 PasswordCallbackHandler (org.apache.cxf.sts.common.PasswordCallbackHandler)77 JAXBContext (javax.xml.bind.JAXBContext)75 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)72 Unmarshaller (javax.xml.bind.Unmarshaller)65 ServiceMBean (org.apache.cxf.sts.service.ServiceMBean)61 StaticService (org.apache.cxf.sts.service.StaticService)61 RequestSecurityTokenResponseCollectionType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseCollectionType)58