Search in sources :

Example 36 with Unmarshaller

use of javax.xml.bind.Unmarshaller 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 37 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class OrcidValidationJaxbContextResolver method getContext.

@Override
public Unmarshaller getContext(Class<?> type) {
    try {
        String apiVersion = getApiVersion();
        String schemaFilenamePrefix = getSchemaFilenamePrefix(type, apiVersion);
        Unmarshaller unmarshaller = getJAXBContext(apiVersion).createUnmarshaller();
        // the controller
        if (OrcidMessage.class.equals(type) || org.orcid.jaxb.model.record_rc3.WorkBulk.class.equals(type) || org.orcid.jaxb.model.record_rc4.WorkBulk.class.equals(type) || org.orcid.jaxb.model.record_v2.WorkBulk.class.equals(type)) {
            return unmarshaller;
        }
        if (schemaFilenamePrefix != null) {
            Schema schema = getSchema(schemaFilenamePrefix, apiVersion);
            unmarshaller.setSchema(schema);
            unmarshaller.setEventHandler(new OrcidValidationHandler());
        }
        return unmarshaller;
    } catch (JAXBException e) {
        throw new WebApplicationException(getResponse(e));
    } catch (SAXException e) {
        throw new WebApplicationException(getResponse(e));
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 38 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class JaxbOrcidMessageUtil method getPublicOrcidMessage.

public static OrcidMessage getPublicOrcidMessage() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(PACKAGE);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (OrcidMessage) unmarshaller.unmarshal(JaxbOrcidMessageUtil.class.getResourceAsStream(ORCID_PUBLIC_FULL_XML));
}
Also used : OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 39 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class MemberV1Test method unmarshallFromPath.

public OrcidMessage unmarshallFromPath(String path) throws Exception {
    try (Reader reader = new InputStreamReader(getClass().getResourceAsStream(path))) {
        JAXBContext context = JAXBContext.newInstance(OrcidProfile.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (OrcidMessage) unmarshaller.unmarshal(reader);
    } catch (Exception e) {
        throw new RuntimeException("Error reading file from classpath", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 40 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class NotificationsTest method unmarshall.

public NotificationPermission unmarshall(Reader reader) {
    try {
        JAXBContext context = JAXBContext.newInstance(NotificationPermission.class.getPackage().getName());
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (NotificationPermission) unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) NotificationPermission(org.orcid.jaxb.model.notification.permission_rc3.NotificationPermission) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

Unmarshaller (javax.xml.bind.Unmarshaller)292 JAXBContext (javax.xml.bind.JAXBContext)240 JAXBException (javax.xml.bind.JAXBException)97 InputStream (java.io.InputStream)91 Test (org.junit.Test)79 StringReader (java.io.StringReader)40 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 File (java.io.File)33 InputSource (org.xml.sax.InputSource)22 IOException (java.io.IOException)21 JAXBElement (javax.xml.bind.JAXBElement)18 Marshaller (javax.xml.bind.Marshaller)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParserFactory (javax.xml.parsers.SAXParserFactory)13 XMLInputFactory (javax.xml.stream.XMLInputFactory)13 XMLStreamException (javax.xml.stream.XMLStreamException)13 XMLStreamReader (javax.xml.stream.XMLStreamReader)13 Schema (javax.xml.validation.Schema)13