Search in sources :

Example 16 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project tesb-rt-se by Talend.

the class CustomerServiceClient method useCustomerServiceRest.

public void useCustomerServiceRest(String[] args) throws Exception {
    JAXBElementProvider provider = new JAXBElementProvider();
    provider.setUnmarshallAsJaxbElement(true);
    provider.setMarshallAsJaxbElement(true);
    List<Object> providers = new ArrayList<Object>();
    providers.add(provider);
    providers.add(new ResponseExceptionMapper<NoSuchCustomerException>() {

        @Override
        public NoSuchCustomerException fromResponse(Response r) {
            return new NoSuchCustomerException();
        }
    });
    CustomerService customerService = JAXRSClientFactory.createFromModel("http://localhost:" + port + "/services/jaxrs", CustomerService.class, "classpath:/data/model/CustomerService-jaxrs.xml", providers, null);
    System.out.println("Using RESTful CustomerService");
    Customer customer = createCustomer("Smith");
    customerService.updateCustomer(customer);
    customer = customerService.getCustomerByName("Smith");
    printCustomerDetails(customer);
    customer = customerService.getCustomerByName("Barry");
    if (customer != null) {
        throw new RuntimeException("Barry should not be found");
    }
    System.out.println("Status : " + WebClient.client(customerService).getResponse().getStatus());
    try {
        customerService.getCustomerByName("John");
        throw new RuntimeException("Exception is expected");
    } catch (NoSuchCustomerException ex) {
        System.out.println("NoSuchCustomerException : John");
    }
}
Also used : Response(javax.ws.rs.core.Response) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) CustomerService(org.example.customers.CustomerService) Customer(org.example.customers.Customer) ArrayList(java.util.ArrayList) NoSuchCustomerException(org.example.customers.NoSuchCustomerException)

Example 17 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project jbpm by kiegroup.

the class RestWorkItemHandlerTest method initialize.

@SuppressWarnings({ "rawtypes" })
@BeforeClass
public static void initialize() throws Exception {
    SimpleRESTApplication application = new SimpleRESTApplication();
    RuntimeDelegate delegate = RuntimeDelegate.getInstance();
    JAXRSServerFactoryBean bean = delegate.createEndpoint(application, JAXRSServerFactoryBean.class);
    bean.setProvider(new JAXBElementProvider());
    bean.setAddress("http://localhost:9998" + bean.getAddress());
    server = bean.create();
    server.start();
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) BeforeClass(org.junit.BeforeClass)

Example 18 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project jbpm by kiegroup.

the class RestWorkitemHandlerClientCreationTest method initialize.

@SuppressWarnings({ "rawtypes" })
@BeforeClass
public static void initialize() throws Exception {
    SimpleRESTApplication application = new SimpleRESTApplication();
    RuntimeDelegate delegate = RuntimeDelegate.getInstance();
    JAXRSServerFactoryBean bean = delegate.createEndpoint(application, JAXRSServerFactoryBean.class);
    bean.setProvider(new JAXBElementProvider());
    bean.setAddress("http://localhost:9998" + bean.getAddress());
    server = bean.create();
    server.start();
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) BeforeClass(org.junit.BeforeClass)

Example 19 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project ddf by codice.

the class WfsResponseExceptionMapper method fromResponse.

public WfsException fromResponse(Response response) {
    WfsException wfsEx = null;
    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                is.reset();
                msg = IOUtils.toString(is);
            } catch (IOException e) {
                wfsEx = new WfsException("Error reading Response" + (msg != null ? ": " + msg : ""), e);
            }
            if (msg != null) {
                try {
                    JAXBElementProvider<ServiceExceptionReport> provider = new JAXBElementProvider<ServiceExceptionReport>();
                    Unmarshaller um = provider.getJAXBContext(ServiceExceptionReport.class, ServiceExceptionReport.class).createUnmarshaller();
                    XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
                    xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                    xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
                    xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
                    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(msg));
                    ServiceExceptionReport report = (ServiceExceptionReport) um.unmarshal(xmlStreamReader);
                    wfsEx = convertToWfsException(report);
                } catch (JAXBException | XMLStreamException e) {
                    wfsEx = new WfsException("Error parsing Response: " + msg, e);
                }
            }
        } else {
            wfsEx = new WfsException("Error reading response, entity type not understood: " + response.getEntity().getClass().getName());
        }
        if (wfsEx != null) {
            wfsEx.setHttpStatus(response.getStatus());
        }
    } else {
        wfsEx = new WfsException("Error handling response, response is null");
    }
    return wfsEx;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) ServiceExceptionReport(ogc.schema.opengis.wfs.exception.v_1_0_0.ServiceExceptionReport) IOException(java.io.IOException) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) XMLStreamException(javax.xml.stream.XMLStreamException) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 20 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project syncope by apache.

the class SyncopeClientFactoryBean method defaultJAXBProvider.

@SuppressWarnings({ "rawtypes" })
protected JAXBElementProvider<?> defaultJAXBProvider() {
    JAXBElementProvider<?> defaultJAXBProvider = new JAXBElementProvider();
    DocumentDepthProperties depthProperties = new DocumentDepthProperties();
    depthProperties.setInnerElementCountThreshold(500);
    defaultJAXBProvider.setDepthProperties(depthProperties);
    Map<String, Object> marshallerProperties = new HashMap<>();
    marshallerProperties.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    defaultJAXBProvider.setMarshallerProperties(marshallerProperties);
    Map<String, String> collectionWrapperMap = new HashMap<>();
    collectionWrapperMap.put(PolicyTO.class.getName(), "policies");
    defaultJAXBProvider.setCollectionWrapperMap(collectionWrapperMap);
    return defaultJAXBProvider;
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) DocumentDepthProperties(org.apache.cxf.staxutils.DocumentDepthProperties) HashMap(java.util.HashMap) PolicyTO(org.apache.syncope.common.lib.policy.PolicyTO)

Aggregations

JAXBElementProvider (org.apache.cxf.jaxrs.provider.JAXBElementProvider)30 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)6 WebClient (org.apache.cxf.jaxrs.client.WebClient)6 ArrayList (java.util.ArrayList)5 Unmarshaller (javax.xml.bind.Unmarshaller)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 InputStream (java.io.InputStream)4 StringReader (java.io.StringReader)4 JAXBException (javax.xml.bind.JAXBException)4 QName (javax.xml.namespace.QName)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 IOException (java.io.IOException)3 Response (javax.ws.rs.core.Response)3 RuntimeDelegate (javax.ws.rs.ext.RuntimeDelegate)3 LoggingInInterceptor (org.apache.cxf.ext.logging.LoggingInInterceptor)3 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2