Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement JAXBElement.

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:org.apache.cxf.systest.jaxrs.JAXRS20ClientServerBookTest.java

@Test
public void testJAXBElementBookCollection() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/jaxbelementxmlrootcollections";
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(address);

    Book b1 = new Book("CXF in Action", 123L);
    Book b2 = new Book("CXF Rocks", 124L);
    List<JAXBElement<Book>> books = new ArrayList<JAXBElement<Book>>();
    books.add(new JAXBElement<Book>(new QName("bookRootElement"), Book.class, b1));
    books.add(new JAXBElement<Book>(new QName("bookRootElement"), Book.class, b2));

    GenericEntity<List<JAXBElement<Book>>> collectionEntity = new GenericEntity<List<JAXBElement<Book>>>(
            books) {/*from   w  w w .  ja  va 2 s  .  c  o  m*/
    };
    GenericType<List<JAXBElement<Book>>> genericResponseType = new GenericType<List<JAXBElement<Book>>>() {
    };

    List<JAXBElement<Book>> books2 = target.request().accept("application/xml")
            .post(Entity.entity(collectionEntity, "application/xml"), genericResponseType);

    assertNotNull(books2);
    assertNotSame(books, books2);
    assertEquals(2, books2.size());
    Book b11 = books.get(0).getValue();
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    Book b22 = books.get(1).getValue();
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegate() {
    IssueDelegate id = new IssueDelegate();
    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias(keySignAlias);
    certificateVerifierConfig.setKeySignPwd(keySignPwd);
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd(storePwd);
    id.setCertificateVerifierConfig(certificateVerifierConfig);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn("joe");
    EasyMock.expect(passwordCallbackMock.resetPassword()).andReturn("joespassword");
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) tokenType));

    EasyMock.replay(requestMock);/*w w w .  ja va 2  s  .  c  o  m*/

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    id.issue(requestMock);

    verify(requestMock);
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void TestIssueDelegateWrongUsername() {
    IssueDelegate id = new IssueDelegate();
    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias(keySignAlias);
    certificateVerifierConfig.setKeySignPwd(keySignPwd);
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd(storePwd);
    id.setCertificateVerifierConfig(certificateVerifierConfig);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn("joexxx");
    EasyMock.expect(passwordCallbackMock.resetPassword()).andReturn("joespassword");
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) tokenType));

    EasyMock.replay(requestMock);/*from   www.  jav a2s . co m*/

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    try {
        id.issue(requestMock);
        fail("STSException should be thrown");
    } catch (STSException e) {
        // expected
    } finally {
        verify(requestMock);
    }
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWrongPassword() {
    IssueDelegate id = new IssueDelegate();
    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias(keySignAlias);
    certificateVerifierConfig.setKeySignPwd(keySignPwd);
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd(storePwd);
    id.setCertificateVerifierConfig(certificateVerifierConfig);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn("joe");
    EasyMock.expect(passwordCallbackMock.resetPassword()).andReturn("joespasswordxxx");
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) tokenType));

    EasyMock.replay(requestMock);//from   w w w . j  a v a 2 s.co  m

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    try {
        id.issue(requestMock);

        fail("STSException should be thrown");
    } catch (STSException e) {
        // expected 
    } finally {
        verify(requestMock);
    }
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWrongSignKey() {
    IssueDelegate id = new IssueDelegate();
    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias(keySignAlias);
    certificateVerifierConfig.setKeySignPwd("xxx");
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd(storePwd);
    id.setCertificateVerifierConfig(certificateVerifierConfig);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn("joe");
    EasyMock.expect(passwordCallbackMock.resetPassword()).andReturn("joespassword");
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) tokenType));

    EasyMock.replay(requestMock);//from   w  w  w. ja v a2  s. com

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    try {
        id.issue(requestMock);
        fail("STSException should be thrown");
    } catch (STSException e) {
        // expected 
    } finally {
        verify(requestMock);
    }
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWrongSignAlias() {
    IssueDelegate id = new IssueDelegate();
    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias("xxx");
    certificateVerifierConfig.setKeySignPwd(keySignPwd);
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd(storePwd);
    id.setCertificateVerifierConfig(certificateVerifierConfig);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn("joe");
    EasyMock.expect(passwordCallbackMock.resetPassword()).andReturn("joespassword");
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) tokenType));

    EasyMock.replay(requestMock);//from w  w  w . j  a  va2s .c o m

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    try {
        id.issue(requestMock);

        fail("STSException should be thrown");
    } catch (STSException e) {
        // expected 
    } finally {
        verify(requestMock);
    }
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWithCert() throws CertificateException {
    IssueDelegate id = new IssueDelegate();
    assertNotNull(id);//from w  ww.j ava 2 s. c  o  m
    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias(keySignAlias);
    certificateVerifierConfig.setKeySignPwd(keySignPwd);
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd(storePwd);
    certificateVerifierConfig.setVerifySelfSignedCert(true);
    id.setCertificateVerifierConfig(certificateVerifierConfig);
    JAXBElement<byte[]> jX509Certificate = new JAXBElement<byte[]>(QName.valueOf("X509Certificate"),
            byte[].class, Base64.decodeBase64(CERT_DATA.getBytes()));

    X509DataType x509DataType = new X509DataType();
    x509DataType.getX509IssuerSerialOrX509SKIOrX509SubjectName().add(jX509Certificate);
    JAXBElement<X509DataType> jX509DataType = new JAXBElement<X509DataType>(QName.valueOf("X509Data"),
            X509DataType.class, x509DataType);

    KeyInfoType keyInfoType = new KeyInfoType();
    keyInfoType.getContent().add(jX509DataType);
    JAXBElement<KeyInfoType> jKeyInfoType = new JAXBElement<KeyInfoType>(QName.valueOf("KeyInfo"),
            KeyInfoType.class, keyInfoType);

    UseKeyType useKeyType = new UseKeyType();
    useKeyType.setAny(jKeyInfoType);
    JAXBElement<UseKeyType> jUseKeyType = new JAXBElement<UseKeyType>(QName.valueOf("UseKey"), UseKeyType.class,
            useKeyType);

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny())
            .andStubReturn(Arrays.asList((Object) jUseKeyType, (Object) tokenType));
    EasyMock.replay(requestMock);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn(null);
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    id.issue(requestMock);

    verify(requestMock);
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWithCertWithWrongStorePass() throws CertificateException {
    IssueDelegate id = new IssueDelegate();

    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias(keySignAlias);
    certificateVerifierConfig.setKeySignPwd(keySignPwd);
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd("xxx");
    id.setCertificateVerifierConfig(certificateVerifierConfig);
    JAXBElement<byte[]> jX509Certificate = new JAXBElement<byte[]>(QName.valueOf("X509Certificate"),
            byte[].class, Base64.decodeBase64(CERT_DATA.getBytes()));

    X509DataType x509DataType = new X509DataType();
    x509DataType.getX509IssuerSerialOrX509SKIOrX509SubjectName().add(jX509Certificate);
    JAXBElement<X509DataType> jX509DataType = new JAXBElement<X509DataType>(QName.valueOf("X509Data"),
            X509DataType.class, x509DataType);

    KeyInfoType keyInfoType = new KeyInfoType();
    keyInfoType.getContent().add(jX509DataType);
    JAXBElement<KeyInfoType> jKeyInfoType = new JAXBElement<KeyInfoType>(QName.valueOf("KeyInfo"),
            KeyInfoType.class, keyInfoType);

    UseKeyType useKeyType = new UseKeyType();
    useKeyType.setAny(jKeyInfoType);/*w w w .  j a v  a 2 s .  c  o  m*/
    JAXBElement<UseKeyType> jUseKeyType = new JAXBElement<UseKeyType>(QName.valueOf("UseKey"), UseKeyType.class,
            useKeyType);

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny())
            .andStubReturn(Arrays.asList((Object) jUseKeyType, (Object) tokenType));
    EasyMock.replay(requestMock);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn(null);
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    try {
        id.issue(requestMock);
        fail("STSException should be thrown");
    } catch (STSException e) {
        // expected
    } finally {
        verify(requestMock);
    }
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWithCertWithoutTokenProvidersAndTokenType() throws CertificateException {
    IssueDelegate id = new IssueDelegate();

    CertificateVerifierConfig certificateVerifierConfig = new CertificateVerifierConfig();
    certificateVerifierConfig.setTrustCertAliases(Arrays.asList("myclientkey"));
    certificateVerifierConfig.setKeySignAlias(keySignAlias);
    certificateVerifierConfig.setKeySignPwd(keySignPwd);
    certificateVerifierConfig.setStorePath(storePath);
    certificateVerifierConfig.setStorePwd(storePwd);
    id.setCertificateVerifierConfig(certificateVerifierConfig);
    JAXBElement<byte[]> jX509Certificate = new JAXBElement<byte[]>(QName.valueOf("X509Certificate"),
            byte[].class, Base64.decodeBase64(CERT_DATA.getBytes()));

    X509DataType x509DataType = new X509DataType();
    x509DataType.getX509IssuerSerialOrX509SKIOrX509SubjectName().add(jX509Certificate);
    JAXBElement<X509DataType> jX509DataType = new JAXBElement<X509DataType>(QName.valueOf("X509Data"),
            X509DataType.class, x509DataType);

    KeyInfoType keyInfoType = new KeyInfoType();
    keyInfoType.getContent().add(jX509DataType);
    JAXBElement<KeyInfoType> jKeyInfoType = new JAXBElement<KeyInfoType>(QName.valueOf("KeyInfo"),
            KeyInfoType.class, keyInfoType);

    UseKeyType useKeyType = new UseKeyType();
    useKeyType.setAny(jKeyInfoType);//w w  w . j a  v  a2s .co  m
    JAXBElement<UseKeyType> jUseKeyType = new JAXBElement<UseKeyType>(QName.valueOf("UseKey"), UseKeyType.class,
            useKeyType);

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) jUseKeyType));
    EasyMock.replay(requestMock);

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn(null);
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    List<TokenProvider> tps = Collections.emptyList();
    id.setTokenProviders(tps);

    try {
        id.issue(requestMock);
        fail("STSException should be thrown");
    } catch (STSException e) {
        // expected
    } finally {
        verify(requestMock);
    }
}

From source file:org.apache.cxf.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWithoutCertAndUserToken() throws CertificateException {
    IssueDelegate id = new IssueDelegate();

    JAXBElement<String> tokenType = new JAXBElement<String>(
            new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class,
            "urn:oasis:names:tc:SAML:1.0:assertion");

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) tokenType));
    EasyMock.replay(requestMock);/*w  ww .j  a v a2  s .c  o m*/

    EasyMock.expect(passwordCallbackMock.resetUsername()).andReturn(null);
    EasyMock.expect(passwordCallbackMock.resetPassword()).andReturn(null);
    EasyMock.replay(passwordCallbackMock);

    id.setPasswordCallback(passwordCallbackMock);

    TokenProvider tp1 = new Saml1TokenProvider();
    TokenProvider tp2 = new Saml2TokenProvider();
    id.setTokenProviders(Arrays.asList(tp1, tp2));

    try {
        id.issue(requestMock);

        fail("STSException should be thrown");
    } catch (STSException e) {
        // expected
    } finally {
        verify(requestMock);
    }
}