Search in sources :

Example 6 with Header

use of org.apache.cxf.headers.Header 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 7 with Header

use of org.apache.cxf.headers.Header in project OpenAM by OpenRock.

the class OpenAMSessionTokenClientInterceptor method addToken.

/**
     * This method is called on the outbound client side, secure-request in JASPI terms. This method will add the
     * OpenAMSessionAssertion state to the message.
     * @param message the encapsulation of the soap request.
     */
@Override
protected void addToken(SoapMessage message) {
    OpenAMSessionAssertion openAMSessionAssertion = (OpenAMSessionAssertion) assertTokens(message);
    Header header = findSecurityHeader(message, true);
    final Element element = (Element) header.getObject();
    final Element openAMSessionAssertionElement = openAMSessionAssertion.getTokenElement();
    element.appendChild(element.getOwnerDocument().importNode(openAMSessionAssertionElement, true));
}
Also used : Header(org.apache.cxf.headers.Header) Element(org.w3c.dom.Element)

Example 8 with Header

use of org.apache.cxf.headers.Header 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 9 with Header

use of org.apache.cxf.headers.Header in project camel by apache.

the class CxfMessageHeadersRelayTest method validateReturnedOutOfBandHeader.

protected static void validateReturnedOutOfBandHeader(Map<String, Object> responseContext, boolean expect) {
    OutofBandHeader hdrToTest = null;
    List<Header> oobHdr = CastUtils.cast((List<?>) responseContext.get(Header.HEADER_LIST));
    if (!expect) {
        if (oobHdr == null || (oobHdr != null && oobHdr.size() == 0)) {
            return;
        }
        fail("Should have got *no* out-of-band headers, but some were found");
    }
    if (oobHdr == null) {
        fail("Should have got List of out-of-band headers");
    }
    assertTrue("HeaderHolder list expected to conain 1 object received " + oobHdr.size(), oobHdr.size() == 1);
    for (Header hdr1 : oobHdr) {
        if (hdr1.getObject() instanceof Node) {
            try {
                JAXBElement<?> job = (JAXBElement<?>) JAXBContext.newInstance(org.apache.cxf.outofband.header.ObjectFactory.class).createUnmarshaller().unmarshal((Node) hdr1.getObject());
                hdrToTest = (OutofBandHeader) job.getValue();
            } catch (JAXBException ex) {
                ex.printStackTrace();
            }
        }
    }
    assertNotNull("out-of-band header should not be null", hdrToTest);
    assertTrue("Expected out-of-band Header name testOobReturnHeaderName recevied :" + hdrToTest.getName(), "testOobReturnHeaderName".equals(hdrToTest.getName()));
    assertTrue("Expected out-of-band Header value testOobReturnHeaderValue recevied :" + hdrToTest.getValue(), "testOobReturnHeaderValue".equals(hdrToTest.getValue()));
    assertTrue("Expected out-of-band Header attribute testReturnHdrAttribute recevied :" + hdrToTest.getHdrAttribute(), "testReturnHdrAttribute".equals(hdrToTest.getHdrAttribute()));
}
Also used : SoapHeader(org.apache.cxf.binding.soap.SoapHeader) OutofBandHeader(org.apache.cxf.outofband.header.OutofBandHeader) Header(org.apache.cxf.headers.Header) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) OutofBandHeader(org.apache.cxf.outofband.header.OutofBandHeader) JAXBElement(javax.xml.bind.JAXBElement)

Example 10 with Header

use of org.apache.cxf.headers.Header in project camel by apache.

the class CxfMessageHeadersRelayTest method doTestInOutOfBandHeaderCamelTemplate.

protected void doTestInOutOfBandHeaderCamelTemplate(String producerUri) throws Exception {
    // START SNIPPET: sending
    Exchange senderExchange = new DefaultExchange(context, ExchangePattern.InOut);
    final List<Object> params = new ArrayList<Object>();
    Me me = new Me();
    me.setFirstName("john");
    me.setLastName("Doh");
    params.add(me);
    senderExchange.getIn().setBody(params);
    senderExchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "inOutOfBandHeader");
    List<Header> headers = buildOutOfBandHeaderList(false);
    Map<String, Object> requestContext = new HashMap<String, Object>();
    requestContext.put(Header.HEADER_LIST, headers);
    senderExchange.getIn().setHeader(Client.REQUEST_CONTEXT, requestContext);
    Exchange exchange = template.send(producerUri, senderExchange);
    org.apache.camel.Message out = exchange.getOut();
    MessageContentsList result = (MessageContentsList) out.getBody();
    Map<String, Object> responseContext = CastUtils.cast((Map<?, ?>) out.getHeader(Client.RESPONSE_CONTEXT));
    assertNotNull(responseContext);
    assertTrue("Expected the out of band header to propagate but it didn't", result.get(0) != null && ((Me) result.get(0)).getFirstName().equals("pass"));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) MessageContentsList(org.apache.cxf.message.MessageContentsList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) OutofBandHeader(org.apache.cxf.outofband.header.OutofBandHeader) Header(org.apache.cxf.headers.Header)

Aggregations

Header (org.apache.cxf.headers.Header)15 OutofBandHeader (org.apache.cxf.outofband.header.OutofBandHeader)9 JAXBException (javax.xml.bind.JAXBException)7 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)7 ArrayList (java.util.ArrayList)6 JAXBElement (javax.xml.bind.JAXBElement)5 Node (org.w3c.dom.Node)5 List (java.util.List)4 MessageContext (javax.xml.ws.handler.MessageContext)4 JAXBDataBinding (org.apache.cxf.jaxb.JAXBDataBinding)4 HashMap (java.util.HashMap)3 QName (javax.xml.namespace.QName)3 MessageContentsList (org.apache.cxf.message.MessageContentsList)3 Exchange (org.apache.camel.Exchange)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 Element (org.w3c.dom.Element)2 NetSuitePortType (com.netsuite.webservices.platform.NetSuitePortType)1 NetSuiteService (com.netsuite.webservices.platform.NetSuiteService)1 DataCenterUrls (com.netsuite.webservices.platform.core.DataCenterUrls)1 ListOrRecordRef (com.netsuite.webservices.platform.core.ListOrRecordRef)1