Search in sources :

Example 86 with JAXBContext

use of javax.xml.bind.JAXBContext in project midpoint by Evolveum.

the class DescriptorLoader method loadData.

public void loadData(MidPointApplication application) {
    LOGGER.debug("Loading data from descriptor files.");
    try (InputStream baseInput = application.getServletContext().getResourceAsStream(baseFileName);
        InputStream customInput = application.getServletContext().getResourceAsStream(customFileName)) {
        if (baseInput == null) {
            LOGGER.error("Couldn't find " + baseFileName + " file, can't load application menu and other stuff.");
        }
        JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<DescriptorType> element = (JAXBElement) unmarshaller.unmarshal(baseInput);
        DescriptorType descriptor = element.getValue();
        LOGGER.debug("Loading menu bar from " + baseFileName + " .");
        DescriptorType customDescriptor = null;
        if (customInput != null) {
            element = (JAXBElement) unmarshaller.unmarshal(customInput);
            customDescriptor = element.getValue();
        }
        scanPackagesForPages(descriptor.getPackagesToScan(), application);
        if (customDescriptor != null) {
            scanPackagesForPages(customDescriptor.getPackagesToScan(), application);
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("loaded:\n{}", debugDump(1));
        }
    } catch (Exception ex) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't process application descriptor", ex);
    }
}
Also used : InputStream(java.io.InputStream) DescriptorType(com.evolveum.midpoint.xml.ns._public.gui.admin_1.DescriptorType) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 87 with JAXBContext

use of javax.xml.bind.JAXBContext in project libresonic by Libresonic.

the class SonosService method getUsername.

private String getUsername() {
    MessageContext messageContext = context.getMessageContext();
    if (messageContext == null || !(messageContext instanceof WrappedMessageContext)) {
        LOG.error("Message context is null or not an instance of WrappedMessageContext.");
        return null;
    }
    Message message = ((WrappedMessageContext) messageContext).getWrappedMessage();
    List<Header> headers = CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
    if (headers != null) {
        for (Header h : headers) {
            Object o = h.getObject();
            // Unwrap the node using JAXB
            if (o instanceof Node) {
                JAXBContext jaxbContext;
                try {
                    // TODO: Check performance
                    jaxbContext = new JAXBDataBinding(Credentials.class).getContext();
                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                    o = unmarshaller.unmarshal((Node) o);
                } catch (JAXBException e) {
                    // failed to get the credentials object from the headers
                    LOG.error("JAXB error trying to unwrap credentials", e);
                }
            }
            if (o instanceof Credentials) {
                Credentials c = (Credentials) o;
                // Note: We're using the username as session ID.
                String username = c.getSessionId();
                if (username == null) {
                    LOG.debug("No session id in credentials object, get from login");
                    username = c.getLogin().getUsername();
                }
                return username;
            } else {
                LOG.error("No credentials object");
            }
        }
    } else {
        LOG.error("No headers found");
    }
    return null;
}
Also used : Message(org.apache.cxf.message.Message) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Header(org.apache.cxf.headers.Header) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) MessageContext(javax.xml.ws.handler.MessageContext) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 88 with JAXBContext

use of javax.xml.bind.JAXBContext in project midpoint by Evolveum.

the class AbstractTestForExchangeConnector method unmarshallResource.

protected static <T> T unmarshallResource(String path) throws JAXBException, FileNotFoundException {
    JAXBContext jc = ModelClientUtil.instantiateJaxbContext();
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    InputStream is = null;
    JAXBElement<T> element = null;
    try {
        is = AbstractTestForExchangeConnector.class.getClassLoader().getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException("System resource " + path + " was not found");
        }
        element = (JAXBElement<T>) unmarshaller.unmarshal(is);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (element == null) {
        return null;
    }
    return element.getValue();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 89 with JAXBContext

use of javax.xml.bind.JAXBContext in project uPortal by Jasig.

the class GsaSearchTest method test.

@Test
public void test() throws JAXBException, IOException {
    Resource resource = applicationContext.getResource("classpath:/org/apereo/portal/portlets/search/gsa/gsa1.xml");
    JAXBContext jc = JAXBContext.newInstance(GsaResults.class);
    Unmarshaller u = jc.createUnmarshaller();
    GsaResults result = (GsaResults) u.unmarshal(resource.getInputStream());
    assert result.getSpellingSuggestion().size() == 1;
}
Also used : Resource(org.springframework.core.io.Resource) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 90 with JAXBContext

use of javax.xml.bind.JAXBContext in project uPortal by Jasig.

the class GsaSearchTest method test2.

@Test
public void test2() throws JAXBException, IOException {
    Resource resource = applicationContext.getResource("classpath:/org/apereo/portal/portlets/search/gsa/gsa2.xml");
    JAXBContext jc = JAXBContext.newInstance(GsaResults.class);
    Unmarshaller u = jc.createUnmarshaller();
    GsaResults result = (GsaResults) u.unmarshal(resource.getInputStream());
    assert result.getDirectoryLinks().size() == 2;
}
Also used : Resource(org.springframework.core.io.Resource) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)1150 Unmarshaller (javax.xml.bind.Unmarshaller)558 Marshaller (javax.xml.bind.Marshaller)371 JAXBException (javax.xml.bind.JAXBException)330 Test (org.junit.Test)226 File (java.io.File)161 InputStream (java.io.InputStream)150 StringWriter (java.io.StringWriter)138 IOException (java.io.IOException)105 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 StringReader (java.io.StringReader)74 StreamSource (javax.xml.transform.stream.StreamSource)73 JAXBElement (javax.xml.bind.JAXBElement)72 ArrayList (java.util.ArrayList)58 URL (java.net.URL)55 ByteArrayInputStream (java.io.ByteArrayInputStream)50 Schema (javax.xml.validation.Schema)48 SchemaFactory (javax.xml.validation.SchemaFactory)48 OutputStream (java.io.OutputStream)42 BaseTest (org.orcid.core.BaseTest)39