Java Client Example: Authentication Using HTTP CookiesThe following code snippets show how to use CXF generated Java client stubs to obtain and use a cookie to manage your web services session: Step one: Create the Authentication stub For example: URL wsdlURL = new URL("http://serverName:portNumber/p6ws/services/AuthenticationService?wsdl"); AuthenticationService service = new AuthenticationService(wsdlURL); AuthenticationServicePortType servicePort = service.getAuthenticationServiceSOAP12PortHttp(); BindingProvider bp = (BindingProvider)servicePort; Step two: Invoke the Login operation For example: Boolean success = servicePort.login(userName, password, 1, true); If the Login operation is successful, it sends an XML message similar to the following: HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=6FBA83AE67D2E057CEC45B05A0414DB2; Path=/p6ws Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 Content-Type: text/xml;charset=utf-8 Content-Length: 254 Date: Thu, 03 Apr 2008 16:04:25 GMT <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><LoginReturn xmlns="http://xmlns.oracle.com/Primavera/P6/WS/Authentication/V1"><return>true</return></LoginReturn></SOAP-ENV:Body></SOAP-ENV:Envelope> Step three: Retrieve the cookie from the response message For example: private static List<String> cookieHeaders = null; Map<String, List<String>> responseHeaders = (Map<String, List<String>>)responseContext.get("javax.xml.ws.http.response.headers"); cookieHeaders = responseHeaders.get("Set-Cookie"); Step four: Use the cookie in all subsequent calls to P6 Web Services in current session For example: Map<String, List<String>> headers = (Map<String, List<String>>)bp.getRequestContext().get("javax.xml.ws.http.request.headers"); if (headers == null) { headers = new HashMap<String, List<String>>(); bp.getRequestContext().put("javax.xml.ws.http.request.headers", headers); } headers.put("cookie", cookieHeaders); |