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.ws.security.sts.provider.operation.IssueDelegateTest.java

@Test
public void testIssueDelegateWithInvalidCert() throws CertificateException {
    IssueDelegate id = new IssueDelegate();
    assertNotNull(id);//from   w  w w . ja  v  a 2s  . c  om

    // CertificateFactory certificateFactory =
    // CertificateFactory.getInstance("X.509");
    // X509Certificate x509Certificate = null;
    // try {
    // x509Certificate =
    // (X509Certificate)certificateFactory.generateCertificate(new
    // ByteArrayInputStream(Base64.decodeBase64(CERT_DATA.getBytes())));
    // } catch (CertificateException e) {
    // e.printStackTrace();
    // }
    // JAXBElement<X509Certificate> jX509Certificate = new
    // JAXBElement<X509Certificate>(QName.valueOf("X509Certificate"),
    // X509Certificate.class, x509Certificate);

    JAXBElement<byte[]> jX509Certificate = new JAXBElement<byte[]>(QName.valueOf("X509Certificate"),
            byte[].class, 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);

    EasyMock.expect(requestMock.getAny()).andStubReturn(Arrays.asList((Object) jUseKeyType));
    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) {

    }

    verify(requestMock);
}

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

@Test
public void testIssueDelegateWithInvalidCert2() throws CertificateException {
    IssueDelegate id = new IssueDelegate();
    assertNotNull(id);/*from  w w w.j a va2s .  co m*/

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate x509Certificate = (X509Certificate) certificateFactory
            .generateCertificate(new ByteArrayInputStream(Base64.decodeBase64(CERT_DATA.getBytes())));
    JAXBElement<X509Certificate> jX509Certificate = new JAXBElement<X509Certificate>(
            QName.valueOf("X509Certificate"), X509Certificate.class, x509Certificate);

    // JAXBElement<byte[]> jX509Certificate = new
    // JAXBElement<byte[]>(QName.valueOf("X509Certificate"), byte[].class,
    // 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);

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

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

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

    id.setPasswordCallback(passwordCallbackMock);

    try {
        id.issue(requestMock);
        fail("CertificateException should be thrown");
    } catch (Exception e) {
    }

    verify(requestMock);
}

From source file:org.apache.falcon.lifecycle.engine.oozie.utils.OozieBuilderUtils.java

public static Path marshalDefaultConfig(Cluster cluster, WORKFLOWAPP workflowapp, Properties properties,
        Path outPath) throws FalconException {
    QName workflowQName = new org.apache.falcon.oozie.workflow.ObjectFactory().createWorkflowApp(workflowapp)
            .getName();/*from   w  w w  .  j a v a2s . co m*/
    org.apache.falcon.oozie.workflow.CONFIGURATION config = getWorkflowConfig(properties);
    JAXBElement<org.apache.falcon.oozie.workflow.CONFIGURATION> configJaxbElement = new JAXBElement(
            new QName(workflowQName.getNamespaceURI(), "configuration", workflowQName.getPrefix()),
            org.apache.falcon.oozie.workflow.CONFIGURATION.class, config);

    Path defaultConfigPath = new Path(outPath, "config-default.xml");
    return marshal(cluster, configJaxbElement, CONFIG_JAXB_CONTEXT, defaultConfigPath);
}

From source file:org.apache.falcon.oozie.OozieOrchestrationWorkflowBuilder.java

protected Path marshal(Cluster cluster, WORKFLOWAPP workflowapp, CONFIGURATION config, Path outPath)
        throws FalconException {
    QName workflowQName = new org.apache.falcon.oozie.workflow.ObjectFactory().createWorkflowApp(workflowapp)
            .getName();/*from  w w  w . ja  v  a  2  s.c o  m*/
    JAXBElement<CONFIGURATION> configJaxbElement = new JAXBElement(
            new QName(workflowQName.getNamespaceURI(), "configuration", workflowQName.getPrefix()),
            CONFIGURATION.class, config);

    return marshal(cluster, configJaxbElement, OozieUtils.CONFIG_JAXB_CONTEXT,
            new Path(outPath, "config-default.xml"));
}

From source file:org.apache.juddi.jaxb.EntityCreator.java

/**
 * Only use this class for debugging purposes. Output may not be valid XML
 * @param obj/*from  ww  w.  ja  v a2s .c o  m*/
 * @param thePackage
 * @throws JAXBException
 * @deprecated
 */
@Deprecated
public static void outputEntity(Object obj, String thePackage) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(thePackage);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(
            new JAXBElement<Object>(new javax.xml.namespace.QName("uri", "local"), Object.class, obj),
            System.out);

}

From source file:org.apache.juddi.mapping.MappingModelToApi.java

private static void mapModelKeyDataValue(List<KeyDataValue> modelKeyDataValueList,
        List<Object> parentKeyDataList) {
    for (KeyDataValue modelKeyDataValue : modelKeyDataValueList) {
        String tagName = modelKeyDataValue.getKeyDataName();
        String dataType = modelKeyDataValue.getKeyDataType();
        byte[] contentBytes = modelKeyDataValue.getKeyDataValueBytes();
        String contentStr = modelKeyDataValue.getKeyDataValueString();
        Object contents = contentBytes != null ? contentBytes : contentStr;

        if (contents == null) {
            List<Object> childKeyDataList = new ArrayList<Object>();
            if (dataType.equals(X509DataType.class.getSimpleName())) {
                X509DataType x509DataType = new X509DataType();
                mapModelKeyDataValue(modelKeyDataValue.getKeyDataValueList(), childKeyDataList);
                x509DataType.getX509IssuerSerialOrX509SKIOrX509SubjectName().addAll(childKeyDataList);
                JAXBElement dataJAXB = new JAXBElement(new QName("http://www.w3.org/2000/09/xmldsig#", tagName),
                        X509DataType.class, x509DataType);
                parentKeyDataList.add(dataJAXB);
            } else if (dataType.equals(RetrievalMethodType.class.getSimpleName())) {
                RetrievalMethodType retrievalMethodType = new RetrievalMethodType();
                TransformsType transformsType = new TransformsType();
                for (KeyDataValue retrievalMethodKDV : modelKeyDataValue.getKeyDataValueList()) {
                    if (retrievalMethodKDV.getKeyDataName().equals("Transform")) {
                        TransformType tType = new TransformType();
                        tType.setAlgorithm(retrievalMethodKDV.getKeyDataValueString());

                        for (KeyDataValue transformContentKDV : retrievalMethodKDV.getKeyDataValueList()) {
                            String type = transformContentKDV.getKeyDataType();
                            byte[] xformBytes = transformContentKDV.getKeyDataValueBytes();
                            Object transformObject = convertDataToTransformContent(type, xformBytes);
                            tType.getContent().add(transformObject);
                        }/* www .  j ava 2s  . co m*/

                        transformsType.getTransform().add(tType);
                    } else if (retrievalMethodKDV.getKeyDataName().equals("Type")) {
                        retrievalMethodType.setType(retrievalMethodKDV.getKeyDataValueString());
                    } else if (retrievalMethodKDV.getKeyDataName().equals("URI")) {
                        retrievalMethodType.setURI(retrievalMethodKDV.getKeyDataValueString());
                    } else {
                        throw new RuntimeException(
                                "Unrecognized key data type: " + retrievalMethodKDV.getKeyDataType());
                    }
                }
                if (transformsType.getTransform() != null && !transformsType.getTransform().isEmpty()) {
                    retrievalMethodType.setTransforms(transformsType);
                }
                JAXBElement dataJAXB = new JAXBElement(new QName("http://www.w3.org/2000/09/xmldsig#", tagName),
                        RetrievalMethodType.class, retrievalMethodType);
                parentKeyDataList.add(dataJAXB);
            } else if (dataType.equals(PGPDataType.class.getSimpleName())) {
                PGPDataType pgpDataType = new PGPDataType();

                for (KeyDataValue pgpDataKDV : modelKeyDataValue.getKeyDataValueList()) {
                    String pgpDataTagName = pgpDataKDV.getKeyDataName();
                    Object pgpDataContents = pgpDataKDV.getKeyDataValueBytes() != null
                            ? pgpDataKDV.getKeyDataValueBytes()
                            : pgpDataKDV.getKeyDataValueString();
                    Class pgpDataClazz = pgpDataContents.getClass();
                    JAXBElement jaxb = new JAXBElement(
                            new QName("http://www.w3.org/2000/09/xmldsig#", pgpDataTagName), pgpDataClazz,
                            pgpDataContents);
                    pgpDataType.getContent().add(jaxb);
                }

                JAXBElement dataJAXB = new JAXBElement(new QName("http://www.w3.org/2000/09/xmldsig#", tagName),
                        PGPDataType.class, pgpDataType);
                parentKeyDataList.add(dataJAXB);
            } else if (dataType.equals(SPKIDataType.class.getSimpleName())) {
                SPKIDataType spkiDataType = new SPKIDataType();

                for (KeyDataValue spkiDataKDV : modelKeyDataValue.getKeyDataValueList()) {
                    String spkiDataTagName = spkiDataKDV.getKeyDataName();
                    Object spkiDataContents = spkiDataKDV.getKeyDataValueBytes() != null
                            ? spkiDataKDV.getKeyDataValueBytes()
                            : spkiDataKDV.getKeyDataValueString();
                    Class spkiDataClazz = spkiDataContents.getClass();
                    JAXBElement jaxb = new JAXBElement(
                            new QName("http://www.w3.org/2000/09/xmldsig#", spkiDataTagName), spkiDataClazz,
                            spkiDataContents);
                    spkiDataType.getSPKISexpAndAny().add(jaxb);
                }

                JAXBElement dataJAXB = new JAXBElement(new QName("http://www.w3.org/2000/09/xmldsig#", tagName),
                        SPKIDataType.class, spkiDataType);
                parentKeyDataList.add(dataJAXB);
            } else if (dataType.equals(KeyValueType.class.getSimpleName())) {
                KeyValueType keyValueType = new KeyValueType();
                for (KeyDataValue keyValueDataKDV : modelKeyDataValue.getKeyDataValueList()) {
                    for (KeyDataValue keyValueDataChildKDV : keyValueDataKDV.getKeyDataValueList()) {
                        String keyDataDataType = keyValueDataChildKDV.getKeyDataType();
                        if (keyDataDataType.equals("DSAKeyValueType")) {
                            DSAKeyValueType dsaKeyValueType = new DSAKeyValueType();
                            for (KeyDataValue dsaKDV : keyValueDataChildKDV.getKeyDataValueList()) {
                                if ("G".equals(dsaKDV.getKeyDataName())) {
                                    dsaKeyValueType.setG(dsaKDV.getKeyDataValueBytes());
                                } else if ("P".equals(dsaKDV.getKeyDataName())) {
                                    dsaKeyValueType.setP(dsaKDV.getKeyDataValueBytes());
                                } else if ("Q".equals(dsaKDV.getKeyDataName())) {
                                    dsaKeyValueType.setQ(dsaKDV.getKeyDataValueBytes());
                                } else if ("Y".equals(dsaKDV.getKeyDataName())) {
                                    dsaKeyValueType.setY(dsaKDV.getKeyDataValueBytes());
                                } else if ("J".equals(dsaKDV.getKeyDataName())) {
                                    dsaKeyValueType.setJ(dsaKDV.getKeyDataValueBytes());
                                } else if ("Seed".equals(dsaKDV.getKeyDataName())) {
                                    dsaKeyValueType.setSeed(dsaKDV.getKeyDataValueBytes());
                                } else if ("PgenCounter".equals(dsaKDV.getKeyDataName())) {
                                    dsaKeyValueType.setPgenCounter(dsaKDV.getKeyDataValueBytes());
                                } else {
                                    throw new RuntimeException(
                                            "Unrecognized dsa type: " + dsaKDV.getKeyDataName());
                                }
                            }
                            JAXBElement jaxb = new JAXBElement(
                                    new QName("http://www.w3.org/2000/09/xmldsig#",
                                            keyValueDataChildKDV.getKeyDataName()),
                                    DSAKeyValueType.class, dsaKeyValueType);
                            keyValueType.getContent().add(jaxb);
                        } else if (keyDataDataType.equals("RSAKeyValueType")) {
                            RSAKeyValueType rsaKeyValueType = new RSAKeyValueType();
                            for (KeyDataValue rsaKDV : keyValueDataChildKDV.getKeyDataValueList()) {
                                if ("Exponent".equals(rsaKDV.getKeyDataName())) {
                                    rsaKeyValueType.setExponent(rsaKDV.getKeyDataValueBytes());
                                } else if ("Modulus".equals(rsaKDV.getKeyDataName())) {
                                    rsaKeyValueType.setModulus(rsaKDV.getKeyDataValueBytes());
                                } else {
                                    throw new RuntimeException(
                                            "Unrecognized dsa type: " + rsaKDV.getKeyDataName());
                                }
                            }
                            JAXBElement jaxb = new JAXBElement(
                                    new QName("http://www.w3.org/2000/09/xmldsig#",
                                            keyValueDataChildKDV.getKeyDataName()),
                                    RSAKeyValueType.class, rsaKeyValueType);
                            keyValueType.getContent().add(jaxb);
                        } else {
                            throw new RuntimeException("Unrecognized element: " + keyDataDataType);
                        }
                    }
                }
                JAXBElement dataJAXB = new JAXBElement(new QName("http://www.w3.org/2000/09/xmldsig#", tagName),
                        KeyValueType.class, keyValueType);
                parentKeyDataList.add(dataJAXB);
            } else {
                throw new RuntimeException("Unrecognized type: " + dataType);
            }
        } else {
            if (dataType != null && dataType.equals(X509IssuerSerialType.class.getSimpleName())) {
                X509IssuerSerialType x509IssuerSerialType = new X509IssuerSerialType();
                x509IssuerSerialType.setX509IssuerName(contentStr);
                x509IssuerSerialType.setX509SerialNumber(new BigInteger(contentBytes));
                JAXBElement dataJAXB = new JAXBElement(new QName("http://www.w3.org/2000/09/xmldsig#", tagName),
                        X509IssuerSerialType.class, x509IssuerSerialType);
                parentKeyDataList.add(dataJAXB);
            } else {
                JAXBElement dataJAXB = new JAXBElement(new QName("http://www.w3.org/2000/09/xmldsig#", tagName),
                        contents.getClass(), contents);
                parentKeyDataList.add(dataJAXB);
            }
        }
    }
}

From source file:org.apache.pluto.driver.services.container.EventProviderImpl.java

/**
 * Register an event, which should be fired within that request
 * //from w  w w. j a v a 2 s.  co m
 * @param qname
 * @param value
 * @throws {@link IllegalArgumentException}
 */
public void registerToFireEvent(QName qname, Serializable value) throws IllegalArgumentException {
    if (isDeclaredAsPublishingEvent(qname)) {

        if (value != null && !isValueInstanceOfDefinedClass(qname, value))
            throw new IllegalArgumentException("Payload has not the right class");

        try {

            if (value == null) {
                savedEvents.addEvent(new EventImpl(qname, value));
            } else if (!(value instanceof Serializable)) {
                throw new IllegalArgumentException("Object payload must implement Serializable");
            } else {

                Writer out = new StringWriter();

                Class clazz = value.getClass();

                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                try {
                    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
                    JAXBContext jc = JAXBContext.newInstance(clazz);

                    Marshaller marshaller = jc.createMarshaller();

                    JAXBElement<Serializable> element = new JAXBElement<Serializable>(qname, clazz, value);
                    marshaller.marshal(element, out);
                    // marshaller.marshal(value, out);
                } finally {
                    Thread.currentThread().setContextClassLoader(cl);
                }

                if (out != null) {
                    savedEvents.addEvent(new EventImpl(qname, (Serializable) out.toString()));
                } else {
                    savedEvents.addEvent(new EventImpl(qname, value));
                }
            }
        } catch (JAXBException e) {
            // maybe there is no valid jaxb binding
            // TODO wsrp:eventHandlingFailed
            LOG.error("Event handling failed", e);
        } catch (FactoryConfigurationError e) {
            LOG.warn(e);
        }
    }
}

From source file:org.apache.wink.itest.contextresolver.ContextTest.java

public void testUserContextProvider() throws Exception {
    HttpClient httpClient = new HttpClient();

    User user = new User();
    user.setUserName("joedoe@example.com");
    JAXBElement<User> element = new JAXBElement<User>(new QName("http://jaxb.context.tests", "user"),
            User.class, user);
    JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
    StringWriter sw = new StringWriter();
    Marshaller m = context.createMarshaller();
    m.marshal(element, sw);//w  w w.  j  av a  2 s.co  m
    PostMethod postMethod = new PostMethod(getBaseURI());
    try {
        postMethod.setRequestEntity(new ByteArrayRequestEntity(sw.toString().getBytes(), "text/xml"));
        httpClient.executeMethod(postMethod);
        assertEquals(204, postMethod.getStatusCode());
    } finally {
        postMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/joedoe@example.com");
    try {
        httpClient.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        Unmarshaller u = context.createUnmarshaller();
        element = u.unmarshal(new StreamSource(getMethod.getResponseBodyAsStream()), User.class);
        assertNotNull(element);
        user = element.getValue();
        assertNotNull(user);
        assertEquals("joedoe@example.com", user.getUserName());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apereo.portal.portlet.container.EventProviderImpl.java

@Override
public Event createEvent(QName qname, Serializable value) throws IllegalArgumentException {
    if (this.isDeclaredAsPublishingEvent(qname)) {
        if (value != null && !this.isValueInstanceOfDefinedClass(qname, value)) {
            throw new IllegalArgumentException("Payload class (" + value.getClass().getCanonicalName()
                    + ") does not have the right class, check your defined event types in portlet.xml.");
        }/*from  ww  w .j  av a  2  s .c om*/

        if (value == null) {
            return new EventImpl(qname);
        }

        try {
            final Thread currentThread = Thread.currentThread();
            final ClassLoader cl = currentThread.getContextClassLoader();
            final Writer out = new StringWriter();
            final Class clazz = value.getClass();
            try {
                currentThread.setContextClassLoader(this.portletClassLoader);
                final JAXBContext jc = JAXBContext.newInstance(clazz);
                final Marshaller marshaller = jc.createMarshaller();
                final JAXBElement<Serializable> element = new JAXBElement<Serializable>(qname, clazz, value);
                marshaller.marshal(element, out);
            } finally {
                currentThread.setContextClassLoader(cl);
            }
            return new EventImpl(qname, out.toString());
        } catch (JAXBException e) {
            // maybe there is no valid jaxb binding
            // TODO throw exception?
            logger.error("Event handling failed", e);
        } catch (FactoryConfigurationError e) {
            // TODO throw exception?
            logger.warn(e.getMessage(), e);
        }
    }
    return null;
}

From source file:org.artificer.server.atom.workspaces.AbstractWorkspaceTest.java

/**
 * Marshalls the app service to XML./*from  www  . j  a v a 2  s .  c  o  m*/
 * @param appService
 */
public static String marshall(AppService appService) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(AppService.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    StringWriter writer = new StringWriter();
    JAXBElement<AppService> element = new JAXBElement<AppService>(new QName("", "app:service", "app"),
            AppService.class, appService);
    marshaller.marshal(element, writer);
    return writer.toString();
}