Search in sources :

Example 1 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding in project tdi-studio-se by Talend.

the class NetsuiteManagement_CXF method initializeStub.

public void initializeStub() throws Exception {
    URL wsdl_locationUrl = this.getClass().getResource("/wsdl/netsuite.wsdl");
    QName serviceQname = new QName("urn:platform_2014_2.webservices.netsuite.com", "NetSuiteService");
    NetSuiteService service = new NetSuiteService(wsdl_locationUrl, serviceQname);
    NetSuitePortType port = service.getNetSuitePort();
    BindingProvider provider = (BindingProvider) port;
    Map<String, Object> requestContext = provider.getRequestContext();
    requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, _url);
    Preferences preferences = new Preferences();
    preferences.setDisableMandatoryCustomFieldValidation(Boolean.FALSE);
    preferences.setWarningAsError(Boolean.FALSE);
    preferences.setIgnoreReadOnlyFields(Boolean.TRUE);
    preferences.setDisableMandatoryCustomFieldValidation(Boolean.TRUE);
    SearchPreferences searchPreferences = new SearchPreferences();
    searchPreferences.setPageSize(this._pageSize);
    searchPreferences.setBodyFieldsOnly(Boolean.valueOf(false));
    RecordRef role = new RecordRef();
    role.setInternalId(this._role);
    Passport passport = new Passport();
    passport.setEmail(this._email);
    passport.setPassword(this._pwd);
    passport.setRole(role);
    passport.setAccount(this._account);
    // Get the webservices domain for your account
    GetDataCenterUrlsRequest dataCenterRequest = new GetDataCenterUrlsRequest();
    dataCenterRequest.setAccount(this._account);
    DataCenterUrls urls = null;
    GetDataCenterUrlsResponse reponse = port.getDataCenterUrls(dataCenterRequest);
    if (reponse != null && reponse.getGetDataCenterUrlsResult() != null) {
        urls = reponse.getGetDataCenterUrlsResult().getDataCenterUrls();
    }
    if (urls == null) {
        throw new Exception("Can't get a correct webservice domain! Please check your configuration or try to run again.");
    }
    String wsDomain = urls.getWebservicesDomain();
    String endpoint = wsDomain.concat(new URL(this._url).getPath());
    requestContext.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
    requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);
    List<Header> list = (List<Header>) requestContext.get(Header.HEADER_LIST);
    if (list == null) {
        list = new ArrayList<Header>();
        requestContext.put(Header.HEADER_LIST, list);
    }
    Header searchPreferences_header = new Header(new QName("urn:messages_2014_2.platform.webservices.netsuite.com", "searchPreferences"), searchPreferences, new JAXBDataBinding(searchPreferences.getClass()));
    Header preferences_header = new Header(new QName("urn:messages_2014_2.platform.webservices.netsuite.com", "preferences"), preferences, new JAXBDataBinding(preferences.getClass()));
    list.add(searchPreferences_header);
    list.add(preferences_header);
    LoginRequest request = new LoginRequest();
    request.setPassport(passport);
    port.login(request);
    this._port = port;
    Arrays.asList(new String[] { "", "", "" });
}
Also used : GetDataCenterUrlsResponse(com.netsuite.webservices.platform.messages.GetDataCenterUrlsResponse) QName(javax.xml.namespace.QName) RecordRef(com.netsuite.webservices.platform.core.RecordRef) ListOrRecordRef(com.netsuite.webservices.platform.core.ListOrRecordRef) DataCenterUrls(com.netsuite.webservices.platform.core.DataCenterUrls) BindingProvider(javax.xml.ws.BindingProvider) LoginRequest(com.netsuite.webservices.platform.messages.LoginRequest) URL(java.net.URL) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) SOAPException(javax.xml.soap.SOAPException) ParseException(java.text.ParseException) JAXBException(javax.xml.bind.JAXBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException) NetSuitePortType(com.netsuite.webservices.platform.NetSuitePortType) SearchPreferences(com.netsuite.webservices.platform.messages.SearchPreferences) Passport(com.netsuite.webservices.platform.core.Passport) Header(org.apache.cxf.headers.Header) NetSuiteService(com.netsuite.webservices.platform.NetSuiteService) GetDataCenterUrlsRequest(com.netsuite.webservices.platform.messages.GetDataCenterUrlsRequest) List(java.util.List) SearchCustomFieldList(com.netsuite.webservices.platform.core.SearchCustomFieldList) ArrayList(java.util.ArrayList) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) SearchPreferences(com.netsuite.webservices.platform.messages.SearchPreferences) Preferences(com.netsuite.webservices.platform.messages.Preferences)

Example 2 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding 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 3 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding in project camel by apache.

the class HeaderTesterImpl method addReplyOutOfBandHeader.

private void addReplyOutOfBandHeader() {
    if (context != null) {
        MessageContext ctx = context.getMessageContext();
        if (ctx != null) {
            try {
                OutofBandHeader ob = new OutofBandHeader();
                ob.setName("testOobReturnHeaderName");
                ob.setValue("testOobReturnHeaderValue");
                ob.setHdrAttribute("testReturnHdrAttribute");
                JAXBElement<OutofBandHeader> job = new JAXBElement<OutofBandHeader>(new QName(Constants.TEST_HDR_NS, Constants.TEST_HDR_RESPONSE_ELEM), OutofBandHeader.class, null, ob);
                Header hdr = new Header(new QName(Constants.TEST_HDR_NS, Constants.TEST_HDR_RESPONSE_ELEM), job, new JAXBDataBinding(ob.getClass()));
                List<Header> hdrList = CastUtils.cast((List<?>) ctx.get(Header.HEADER_LIST));
                hdrList.add(hdr);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
Also used : OutofBandHeader(org.apache.cxf.outofband.header.OutofBandHeader) Header(org.apache.cxf.headers.Header) QName(javax.xml.namespace.QName) OutofBandHeader(org.apache.cxf.outofband.header.OutofBandHeader) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) MessageContext(javax.xml.ws.handler.MessageContext) JAXBElement(javax.xml.bind.JAXBElement) JAXBException(javax.xml.bind.JAXBException)

Example 4 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding in project BIMserver by opensourceBIM.

the class GenericWebServiceServlet method loadBus.

public void loadBus(ServletConfig servletConfig) {
    this.bus = BusFactory.newInstance().createBus();
    Bus bus = getBus();
    HeaderManager headerManager = bus.getExtension(HeaderManager.class);
    headerManager.registerHeaderProcessor(new HeaderProcessor() {

        @Override
        public String getNamespace() {
            return "uri:org.bimserver.shared";
        }

        @Override
        public InterceptorProvider getInterceptorProvider() {
            return null;
        }

        @Override
        public DataBinding getDataBinding() {
            try {
                return new JAXBDataBinding(Token.class);
            } catch (JAXBException e) {
                LOGGER.error("", e);
            }
            return null;
        }
    });
    BusFactory.setDefaultBus(bus);
    for (Class<? extends PublicInterface> clazz : getBimServer().getServicesMap().getInterfaceClasses()) {
        JaxWsServerFactoryBean serverFactoryBean = new JaxWsServerFactoryBean();
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("mtom-enabled", Boolean.TRUE);
        serverFactoryBean.setBindingId(bindingId);
        serverFactoryBean.setProperties(properties);
        serverFactoryBean.setServiceClass(clazz);
        serverFactoryBean.getOutFaultInterceptors().add(new StatusCodeModifyingFaultInterceptor());
        serverFactoryBean.setInvoker(new CustomInvoker(getBimServer().getServiceFactory(), clazz));
        if (getBimServer().getConfig().getEnvironment() != Environment.WAR) {
            // equals check allowed, just keep using the constant
            serverFactoryBean.setAddress((bindingId == WSDLConstants.NS_SOAP11 ? "/soap11/" : "/soap12/") + clazz.getSimpleName());
        } else {
            // equals check allowed, just keep using the constant
            serverFactoryBean.setAddress("/" + clazz.getSimpleName());
        }
        serverFactoryBean.setTransportId("http://schemas.xmlsoap.org/soap/http");
        serverFactoryBean.create();
    }
}
Also used : Bus(org.apache.cxf.Bus) HeaderProcessor(org.apache.cxf.headers.HeaderProcessor) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) InterceptorProvider(org.apache.cxf.interceptor.InterceptorProvider) Token(org.bimserver.shared.Token) HeaderManager(org.apache.cxf.headers.HeaderManager) CustomInvoker(org.bimserver.webservices.CustomInvoker) DataBinding(org.apache.cxf.databinding.DataBinding) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean)

Example 5 with JAXBDataBinding

use of org.apache.cxf.jaxb.JAXBDataBinding in project cxf by apache.

the class WrapperClassGeneratorTest method testForXmlList.

@org.junit.Test
public void testForXmlList() throws Exception {
    JaxWsImplementorInfo implInfo = new JaxWsImplementorInfo(AddNumbersImpl.class);
    JaxWsServiceFactoryBean jaxwsFac = new JaxWsServiceFactoryBean(implInfo);
    jaxwsFac.setBus(BusFactory.getDefaultBus());
    Service service = jaxwsFac.create();
    ServiceInfo serviceInfo = service.getServiceInfos().get(0);
    InterfaceInfo interfaceInfo = serviceInfo.getInterface();
    OperationInfo inf = interfaceInfo.getOperations().iterator().next();
    Class<?> requestClass = inf.getInput().getMessagePart(0).getTypeClass();
    Class<?> responseClass = inf.getOutput().getMessagePart(0).getTypeClass();
    // Create request wrapper Object
    List<String> partNames = Arrays.asList(new String[] { "arg0" });
    List<String> elTypeNames = Arrays.asList(new String[] { "list" });
    List<Class<?>> partClasses = Arrays.asList(new Class<?>[] { List.class });
    String className = requestClass.getName();
    className = className.substring(0, className.lastIndexOf(".") + 1);
    WrapperHelper wh = new JAXBDataBinding().createWrapperHelper(requestClass, null, partNames, elTypeNames, partClasses);
    List<Object> paraList = new ArrayList<>();
    List<String> valueList = new ArrayList<>();
    valueList.add("str1");
    valueList.add("str2");
    valueList.add("str3");
    paraList.add(valueList);
    Object requestObj = wh.createWrapperObject(paraList);
    // Create response wrapper Object
    partNames = Arrays.asList(new String[] { "return" });
    elTypeNames = Arrays.asList(new String[] { "list" });
    partClasses = Arrays.asList(new Class<?>[] { List.class });
    className = responseClass.getName();
    className = className.substring(0, className.lastIndexOf(".") + 1);
    wh = new JAXBDataBinding().createWrapperHelper(responseClass, null, partNames, elTypeNames, partClasses);
    List<Object> resPara = new ArrayList<>();
    List<Integer> intValueList = new ArrayList<>();
    intValueList.add(1);
    intValueList.add(2);
    intValueList.add(3);
    resPara.add(intValueList);
    Object responseObj = wh.createWrapperObject(resPara);
    JAXBContext jaxbContext = JAXBContext.newInstance(requestClass, responseClass);
    java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
    Marshaller marshaller = jaxbContext.createMarshaller();
    // check marshall wrapper
    marshaller.marshal(requestObj, bout);
    String expected = "<arg0>str1 str2 str3</arg0>";
    assertTrue("The generated request wrapper class does not contain the correct annotations", bout.toString().contains(expected));
    bout.reset();
    marshaller.marshal(responseObj, bout);
    expected = "<return>1</return><return>2</return><return>3</return>";
    assertTrue("The generated response wrapper class is not correct", bout.toString().contains(expected));
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) WrapperHelper(org.apache.cxf.databinding.WrapperHelper) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) ArrayList(java.util.ArrayList) List(java.util.List) Marshaller(javax.xml.bind.Marshaller) JaxWsServiceFactoryBean(org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean) Service(org.apache.cxf.service.Service) JaxWsImplementorInfo(org.apache.cxf.jaxws.support.JaxWsImplementorInfo) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo)

Aggregations

JAXBDataBinding (org.apache.cxf.jaxb.JAXBDataBinding)51 QName (javax.xml.namespace.QName)24 Test (org.junit.Test)18 JAXBException (javax.xml.bind.JAXBException)15 Header (org.apache.cxf.headers.Header)15 XMLStreamReader (javax.xml.stream.XMLStreamReader)12 ArrayList (java.util.ArrayList)10 DataBinding (org.apache.cxf.databinding.DataBinding)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)7 DepthXMLStreamReader (org.apache.cxf.staxutils.DepthXMLStreamReader)7 Service (org.apache.cxf.service.Service)6 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)6 JAXBContext (javax.xml.bind.JAXBContext)5 Bus (org.apache.cxf.Bus)5 URL (java.net.URL)4 OutofBandHeader (org.apache.cxf.outofband.header.OutofBandHeader)4 HashMap (java.util.HashMap)3 List (java.util.List)3 MessageContext (javax.xml.ws.handler.MessageContext)3