Results 1 to 3 of 3

Thread: How to set auth token in header of soap message from wsdl generated java classes

  1. #1
    Join Date
    Jun 2014
    Posts
    2
    Rep Power
    1

    Question How to set auth token in header of soap message from wsdl generated java classes

    Am authenticating and getting an auth response and token ok.
    But how do I set the auth token value into the soap envelope
    of my next request using the java classes created for me using wsimport?

    The various Zimbra request and response classes dont seem to provide a way to
    access the containing envelope.

    I can see there is a class called HeaderContext but I cant see where I can use this.


    HTML Code:
    [CODE]
    		ZcsService svc = new ZcsService();
    		ZcsPortType portType = svc.getZcsServicePort();
    		
    		
    		AccountSelector accountSelector = new AccountSelector();
    		accountSelector.setBy(AccountBy.NAME);
    		accountSelector.setValue("gary.rich@cisx.com");
    		
    				
    		AuthRequest authRequest = new AuthRequest();
    		authRequest.setAccount(accountSelector);
    		authRequest.setPersistAuthTokenCookie(true);
    		//authRequest.setPrefs();
    		authRequest.setPassword("mypwd");
    		AuthResponse authResponse = portType.authRequest(authRequest);
    		
    		String authToken = authResponse.getAuthToken();
    		JOptionPane.showMessageDialog(null, "Token - " + authToken);
    		
    			
    		
    		// Setup context with auth token ????
    	//	zimbra.ObjectFactory factory = new zimbra.ObjectFactory();
    	//	HeaderContext ch = factory.createHeaderContext();
    	//	ch.setAuthToken(authToken);	
    	//	JAXBElement<HeaderContext> context = factory.createContext(ch);
    		
    		
    		// Try and get message
    		MsgSpec msgSpec = new MsgSpec();
    		msgSpec.setId("374");
    		GetMsgRequest getMsgRqst = new GetMsgRequest();
    		getMsgRqst.setM(msgSpec);
    		GetMsgResponse getMsgResp = portType.getMsgRequest(getMsgRqst);
    				
    		JOptionPane.showMessageDialog(null, "GetMsgResponse - " + getMsgResp.toString());
    [/CODE]

  2. #2
    Join Date
    Aug 2010
    Location
    England
    Posts
    39
    Rep Power
    5

    Default

    Here are some snippets from our ZimbraSoap Utility.java code which may help. Note that we specify a prefix of "test" for classes when we generate the code from WSDL.

    Code:
    import com.sun.xml.bind.api.JAXBRIContext;
    import com.sun.xml.ws.api.message.Header;
    import com.sun.xml.ws.api.message.Headers;
    import com.sun.xml.ws.developer.SchemaValidationFeature;
    import com.sun.xml.ws.developer.WSBindingProvider;
    ...
    public class Utility {
    ...
        private static ZcsPortType zcsSvcEIF = null;
        private static ZcsPortType nvZcsSvcEIF = null;
        private static ZcsAdminPortType adminSvcEIF = null;
        private static ZcsAdminPortType nvAdminSvcEIF = null;
        private static String adminAuthToken = null;
        private static Map<String,String> acctAuthToks = Maps.newHashMap();
    
        public static void addSoapAuthHeader(WSBindingProvider bp, String authToken)
        throws JAXBException, ParserConfigurationException {
            JAXBRIContext.newInstance(testHeaderContext.class);
            testHeaderContext zimbraSoapHdrContext = new testHeaderContext();
            zimbraSoapHdrContext.setAuthToken(authToken);
            testAuthTokenControl tokenControl = new testAuthTokenControl();
            tokenControl.setVoidOnExpired(true);
            zimbraSoapHdrContext.setAuthTokenControl(tokenControl);
            Header soapHdr = Headers.create(makeZimbraSoapHeaderContext(zimbraSoapHdrContext));
            bp.setOutboundHeaders(soapHdr);
        }
        public static void addSoapAcctAuthHeader(WSBindingProvider bp, String authToken)
        throws JAXBException, ParserConfigurationException {
            addSoapAuthHeader(bp, authToken);
        }
    ...
        public static Element makeZimbraSoapHeaderContext(testHeaderContext contextJaxb)
        throws JAXBException, ParserConfigurationException {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            Document doc = dbf.newDocumentBuilder().newDocument();
    
            JAXBContext jaxb = JAXBContext.newInstance(testHeaderContext.class);
            Marshaller marshaller = jaxb.createMarshaller();
            // testHeaderContext doesn't have an @XmlRootElement annotation for some reason, however the JAXBElement
            // created using fact.createContext knows what the element name should be called, so use that.
            ObjectFactory fact = new ObjectFactory();
            JAXBElement<testHeaderContext> zimbraSoapHdrCtxt = fact.createContext(contextJaxb);
            marshaller.marshal(zimbraSoapHdrCtxt, doc);
            return doc.getDocumentElement();
        }
    ...
        public static ZcsPortType getZcsSvcEIF() {
            if (zcsSvcEIF == null) {
                // The ZcsService class is the Java type bound to the service section of the WSDL document.
                ZcsService zcsSvc = new ZcsService();
                SchemaValidationFeature feature = new SchemaValidationFeature();
                setZcsSvcEIF(zcsSvc.getZcsServicePort(feature));
            }
            return zcsSvcEIF;
        }
    
        public static ZcsPortType getNonValidatingZcsSvcEIF() throws Exception {
            if (nvZcsSvcEIF == null) {
                ZcsService zcsSvc = new ZcsService();
                setNvZcsSvcEIF(zcsSvc.getZcsServicePort());
            }
            return nvZcsSvcEIF;
        }
    Example calling code:
    Code:
        private static ZcsPortType acctSvcEIF;
    
        @BeforeClass
        public static void init() throws Exception {
            // if use validation - get issue processing handlerObject which should be caught by
            // an XmlAnyElement in AccountZimletDesc
            acctSvcEIF = Utility.getNonValidatingZcsSvcEIF();
            // acctSvcEIF = Utility.getZcsSvcEIF();
        }
    
        @Test
        public void simple() throws Exception {
           testGetInfoRequest req = new testGetInfoRequest();
           Utility.addSoapAcctAuthHeader((WSBindingProvider)acctSvcEIF);
    Gren Elliot
    Lead Engineer - Server
    Zimbra | Community & Collaboration

  3. #3
    Join Date
    Jun 2014
    Posts
    2
    Rep Power
    1

    Default

    Thanks very much for your help Gren,
    That did the trick.

Similar Threads

  1. java soap message for zimbra
    By devark in forum Developers
    Replies: 2
    Last Post: 02-19-2013, 10:44 PM
  2. XSD/WSDL Files for the SOAP Server?
    By photoadrian in forum Developers
    Replies: 5
    Last Post: 12-23-2012, 01:32 AM
  3. Replies: 1
    Last Post: 05-05-2012, 06:34 PM
  4. WSDL support soap
    By NordicEdge in forum Developers
    Replies: 0
    Last Post: 09-19-2011, 03:37 AM
  5. Support for SOAP, where is the WSDL?
    By timothyp in forum Developers
    Replies: 1
    Last Post: 03-19-2006, 12:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •