//JAVA JAX-RPC SOAP client call to ProcessGeocode using HandlerChain to create //SoapHeader //RPC can be acquired via NetBeans pulgin on JAVA SE 1.6 using Wscompile- //Command line script to create stubs: //C:\glassfish3\glassfish\bin\src>wscompile -gen:client -f:searchschema -keep -verbose -Xprintstacktrace -Xserializable config1.xml //Config.1 contents: //<?xml version="1.0" encoding="UTF-8"?> //<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> //<wsdl packageName="src" location="file:C:\Users\ksouders\Downloads\wcfWSDL.wsdl"/> //</configuration> //Location is downloaded WCF WSDL public static StringBuilder JavaPCMilerWCFClientCall(String authToken) throws UnsupportedEncodingException { try { Date date = new Date(); rfc1123 = rfc1123Format.format(date); try { // System.setProperty("http.proxyHost", "localhost"); //System.setProperty("http.proxyPort", "8888"); Service_Impl svcImpl = new Service_Impl(); Iterator iter = svcImpl.getPorts(); HashMap map = new HashMap(); HandlerInfo hInfo = new HandlerInfo(AuthHeaderHandler.class, map, AuthHeaderHandler.HEADERS); List handlerChain = new ArrayList(); handlerChain.add(hInfo); HandlerRegistry registry = svcImpl.getHandlerRegistry(); while (iter.hasNext()) { registry.setHandlerChain((QName) iter.next(), handlerChain); } IService svc = svcImpl.getBasicHttpBinding_IService(); RequestHeader rqHdr = new RequestHeader("Current", "myRequest"); GeocodeRequestBody gcRqBody = new GeocodeRequestBody(); GeocodeLocation loc = new GeocodeLocation(); Address addr = new Address("457 North Harrison St", "Princeton", "NJ", null, null, null, null, PostCodeType.Both, CountryAbbreviationType.FIPS); loc.setRegion(DataRegion.NA); loc.setAddress(addr); GeocodeLocation[] locs = new GeocodeLocation[]{loc}; ArrayOfGeocodeLocation locsArray = new ArrayOfGeocodeLocation(locs); gcRqBody.setLocations(locsArray); GeocodeRequest gcRq = new GeocodeRequest(rqHdr, gcRqBody); map.put("Authorization", authToken.trim()); map.put("Date", rfc1123); GeocodeResponse gcResp = svc.processGeocode(gcRq); String str; if (gcResp.getHeader().getSuccess() == true) { sb = new StringBuilder(); System.out.println("success"); str = "success" + "\n"; //sb.append(str + "\n"); GeocodeOutputLocation[] outLocs = gcResp.getBody().getLocations().getGeocodeOutputLocation(); System.out.println(outLocs.length + " locations returned"); str = str + outLocs.length + " locations returned" + "\n"; for (GeocodeOutputLocation outLoc : outLocs) { Address locAddr = outLoc.getAddress(); Coordinates locCoords = outLoc.getCoords(); System.out.print(locAddr.getStreetAddress() + " " + locAddr.getCity() + " " + locAddr.getState() + " "); System.out.println(locCoords.getLat() + "," + locCoords.getLon()); str = str + (locAddr.getStreetAddress() + " " + locAddr.getCity() + " " + locAddr.getState() + " " + "\n"); str = str + (locCoords.getLat() + "," + locCoords.getLon() + "\n"); } sb.append(str); } } catch (Exception ex) { ex.printStackTrace(); } return sb; } catch (InvalidKeyException ex) { Logger.getLogger(JavaPCMilerWCFClient.class.getName()).log(Level.SEVERE, null, ex); } catch (GeneralSecurityException ex) { Logger.getLogger(JavaPCMilerWCFClient.class.getName()).log(Level.SEVERE, null, ex); } return null; } //Set SOAPHeader Here: public class AuthHeaderHandler implements Handler { /** * Header element namespace */ private static final String AUTH_NS_URI = "http://www.alk.com"; /** * Header element */ private static final String AUTH_ELEMENT = "AuthHeader"; /** * Header element qualifed name */ private static final QName AUTH_HEADER = new QName(AUTH_NS_URI, AUTH_ELEMENT); private HandlerInfo info; /** * Set of SOAP headers that this handler knows how to process. It is * expected that this is set on the HandlerInfo for instances of this * handler's subclasses. */ public static final QName[] HEADERS = new QName[]{AUTH_HEADER}; public void init(HandlerInfo config) { info = config; } public void destroy() { } public QName[] getHeaders() { return info.getHeaders(); } /** * Sign outgoing request message. */ public boolean handleRequest(MessageContext mc) { addAuthHeader((SOAPMessageContext) mc); return true; } /** * Check incoming response message. */ public boolean handleResponse(MessageContext mc) { return true; } /** * Check incoming response message. */ public boolean handleFault(MessageContext mc) { return true; } public void addAuthHeader(SOAPMessageContext mc) { try { /** * Dig down into message, locate or create header block. */ SOAPMessage msg = mc.getMessage(); SOAPPart part = msg.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPHeader header = envelope.getHeader(); if (header == null) { header = envelope.addHeader(); } /** * Create new header element. We don't specify a role on this header * element, meaning the target role is the "ultimate destination". */ String auth = this.info.getHandlerConfig().get("Authorization").toString(); String date = this.info.getHandlerConfig().get("Date").toString(); SOAPHeaderElement headerElement = (SOAPHeaderElement) header.addChildElement(AUTH_HEADER); SOAPElement authElement = headerElement.addChildElement("Authorization"); authElement.addTextNode(auth); SOAPElement dateElement = headerElement.addChildElement("Date"); dateElement.addTextNode(date); } catch (SOAPException e) { System.out.println("Unable to add auth header"); e.printStackTrace(); } } }
JAVA - JAX-RPC Client SOAP ProcessGeocode Snippet Print
Modified on: Wed, 21 Jun, 2017 at 3:33 PM
Did you find it helpful? Yes No
Send feedbackSorry we couldn't be helpful. Help us improve this article with your feedback.