Search in sources :

Example 11 with JAXBElement

use of javax.xml.bind.JAXBElement in project jersey by jersey.

the class JaxbTest method testXmlType.

@Test
public void testXmlType() {
    JaxbXmlType t1 = target().path("jaxb/JAXBElement").request().get(JaxbXmlType.class);
    JAXBElement<JaxbXmlType> e = new JAXBElement<JaxbXmlType>(new QName("jaxbXmlRootElement"), JaxbXmlType.class, t1);
    JaxbXmlType t2 = target().path("jaxb/XmlType").request("application/xml").post(xml(e), JaxbXmlType.class);
    assertEquals(t1, t2);
}
Also used : QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 12 with JAXBElement

use of javax.xml.bind.JAXBElement in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method testSupports.

private void testSupports() throws Exception {
    assertTrue("Jaxb2Marshaller does not support Flights class", marshaller.supports(Flights.class));
    assertTrue("Jaxb2Marshaller does not support Flights generic type", marshaller.supports((Type) Flights.class));
    assertFalse("Jaxb2Marshaller supports FlightType class", marshaller.supports(FlightType.class));
    assertFalse("Jaxb2Marshaller supports FlightType type", marshaller.supports((Type) FlightType.class));
    Method method = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
    assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>", marshaller.supports(method.getGenericReturnType()));
    marshaller.setSupportJaxbElementClass(true);
    JAXBElement<FlightType> flightTypeJAXBElement = new JAXBElement<>(new QName("http://springframework.org", "flight"), FlightType.class, new FlightType());
    assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>", marshaller.supports(flightTypeJAXBElement.getClass()));
    assertFalse("Jaxb2Marshaller supports class not in context path", marshaller.supports(DummyRootElement.class));
    assertFalse("Jaxb2Marshaller supports type not in context path", marshaller.supports((Type) DummyRootElement.class));
    method = getClass().getDeclaredMethod("createDummyRootElement");
    assertFalse("Jaxb2Marshaller supports JAXBElement not in context path", marshaller.supports(method.getGenericReturnType()));
    assertFalse("Jaxb2Marshaller supports class not in context path", marshaller.supports(DummyType.class));
    assertFalse("Jaxb2Marshaller supports type not in context path", marshaller.supports((Type) DummyType.class));
    method = getClass().getDeclaredMethod("createDummyType");
    assertFalse("Jaxb2Marshaller supports JAXBElement not in context path", marshaller.supports(method.getGenericReturnType()));
    testSupportsPrimitives();
    testSupportsStandardClasses();
}
Also used : Type(java.lang.reflect.Type) FlightType(org.springframework.oxm.jaxb.test.FlightType) XmlType(javax.xml.bind.annotation.XmlType) Flights(org.springframework.oxm.jaxb.test.Flights) QName(javax.xml.namespace.QName) FlightType(org.springframework.oxm.jaxb.test.FlightType) Method(java.lang.reflect.Method) JAXBElement(javax.xml.bind.JAXBElement)

Example 13 with JAXBElement

use of javax.xml.bind.JAXBElement in project camel by apache.

the class Soap11DataFormatAdapter method createFaultFromException.

/**
     * Creates a SOAP fault from the exception and populates the message as well
     * as the detail. The detail object is read from the method getFaultInfo of
     * the throwable if present
     * 
     * @param exception the cause exception
     * @return SOAP fault from given Throwable
     */
@SuppressWarnings("unchecked")
private JAXBElement<Fault> createFaultFromException(final Throwable exception) {
    WebFault webFault = exception.getClass().getAnnotation(WebFault.class);
    if (webFault == null || webFault.targetNamespace() == null) {
        throw new RuntimeException("The exception " + exception.getClass().getName() + " needs to have an WebFault annotation with name and targetNamespace", exception);
    }
    QName name = new QName(webFault.targetNamespace(), webFault.name());
    Object faultObject;
    try {
        Method method = exception.getClass().getMethod("getFaultInfo");
        faultObject = method.invoke(exception);
    } catch (Exception e) {
        throw new RuntimeCamelException("Exception while trying to get fault details", e);
    }
    Fault fault = new Fault();
    fault.setFaultcode(FAULT_CODE_SERVER);
    fault.setFaultstring(exception.getMessage());
    Detail detailEl = new ObjectFactory().createDetail();
    @SuppressWarnings("rawtypes") JAXBElement<?> faultDetailContent = new JAXBElement(name, faultObject.getClass(), faultObject);
    detailEl.getAny().add(faultDetailContent);
    fault.setDetail(detailEl);
    return new ObjectFactory().createFault(fault);
}
Also used : QName(javax.xml.namespace.QName) WebFault(javax.xml.ws.WebFault) Fault(org.xmlsoap.schemas.soap.envelope.Fault) Method(java.lang.reflect.Method) JAXBElement(javax.xml.bind.JAXBElement) RuntimeCamelException(org.apache.camel.RuntimeCamelException) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) WebFault(javax.xml.ws.WebFault) ObjectFactory(org.xmlsoap.schemas.soap.envelope.ObjectFactory) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Detail(org.xmlsoap.schemas.soap.envelope.Detail)

Example 14 with JAXBElement

use of javax.xml.bind.JAXBElement in project camel by apache.

the class Soap11DataFormatAdapter method createExceptionFromFault.

/**
     * Creates an exception and eventually an embedded bean that contains the
     * fault detail. The exception class is determined by using the
     * elementNameStrategy. The qName of the fault detail should match the
     * WebFault annotation of the Exception class. If no fault detail is set a
     * SOAPFaultException is created.
     * 
     * @param fault Soap fault
     * @return created Exception
     */
private Exception createExceptionFromFault(Fault fault) {
    String message = fault.getFaultstring();
    Detail faultDetail = fault.getDetail();
    if (faultDetail == null || faultDetail.getAny().size() == 0) {
        try {
            return new SOAPFaultException(SOAPFactory.newInstance().createFault(message, fault.getFaultcode()));
        } catch (SOAPException e) {
            throw new RuntimeCamelException(e);
        }
    }
    JAXBElement<?> detailEl = (JAXBElement<?>) faultDetail.getAny().get(0);
    Class<? extends Exception> exceptionClass = getDataFormat().getElementNameStrategy().findExceptionForFaultName(detailEl.getName());
    Constructor<? extends Exception> messageConstructor;
    Constructor<? extends Exception> constructor;
    try {
        messageConstructor = exceptionClass.getConstructor(String.class);
        Object detail = JAXBIntrospector.getValue(detailEl);
        try {
            constructor = exceptionClass.getConstructor(String.class, detail.getClass());
            return constructor.newInstance(message, detail);
        } catch (NoSuchMethodException e) {
            return messageConstructor.newInstance(message);
        }
    } catch (Exception e) {
        throw new RuntimeCamelException(e);
    }
}
Also used : SOAPException(javax.xml.soap.SOAPException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) JAXBElement(javax.xml.bind.JAXBElement) Detail(org.xmlsoap.schemas.soap.envelope.Detail) RuntimeCamelException(org.apache.camel.RuntimeCamelException) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException)

Example 15 with JAXBElement

use of javax.xml.bind.JAXBElement in project camel by apache.

the class Soap12DataFormatAdapter method createExceptionFromFault.

/**
     * Creates an exception and eventually an embedded bean that contains the
     * fault detail. The exception class is determined by using the
     * elementNameStrategy. The qName of the fault detail should match the
     * WebFault annotation of the Exception class. If no fault detail is set
     * a {@link javax.xml.ws.soap.SOAPFaultException} is created.
     * 
     * @param fault Soap fault
     * @return created Exception
     */
private Exception createExceptionFromFault(Fault fault) {
    StringBuilder sb = new StringBuilder();
    for (Reasontext text : fault.getReason().getText()) {
        sb.append(text.getValue());
    }
    String message = sb.toString();
    Detail faultDetail = fault.getDetail();
    if (faultDetail == null || faultDetail.getAny().size() == 0) {
        try {
            return new SOAPFaultException(SOAPFactory.newInstance().createFault(message, fault.getCode().getValue()));
        } catch (SOAPException e) {
            throw new RuntimeCamelException(e);
        }
    }
    JAXBElement<?> detailEl = (JAXBElement<?>) faultDetail.getAny().get(0);
    Class<? extends Exception> exceptionClass = getDataFormat().getElementNameStrategy().findExceptionForFaultName(detailEl.getName());
    Constructor<? extends Exception> messageConstructor;
    Constructor<? extends Exception> constructor;
    try {
        messageConstructor = exceptionClass.getConstructor(String.class);
        Object detail = JAXBIntrospector.getValue(detailEl);
        try {
            constructor = exceptionClass.getConstructor(String.class, detail.getClass());
            return constructor.newInstance(message, detail);
        } catch (NoSuchMethodException e) {
            return messageConstructor.newInstance(message);
        }
    } catch (Exception e) {
        throw new RuntimeCamelException(e);
    }
}
Also used : Reasontext(org.w3._2003._05.soap_envelope.Reasontext) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) JAXBElement(javax.xml.bind.JAXBElement) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Detail(org.w3._2003._05.soap_envelope.Detail)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)210 QName (javax.xml.namespace.QName)71 ArrayList (java.util.ArrayList)43 Test (org.junit.Test)42 JAXBException (javax.xml.bind.JAXBException)28 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)28 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)24 JAXBContext (javax.xml.bind.JAXBContext)19 Unmarshaller (javax.xml.bind.Unmarshaller)18 Marshaller (javax.xml.bind.Marshaller)16 LineString (com.vividsolutions.jts.geom.LineString)15 StringWriter (java.io.StringWriter)14 List (java.util.List)14 Element (org.w3c.dom.Element)13 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)12 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)10 PrismObject (com.evolveum.midpoint.prism.PrismObject)9 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)9