import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.springframework.stereotype.Service; import com.test.gcrmws.core.context.GCRMWSContext; @Service public class GoogleUrlSigner { public String getLicnsedGeoCoderURL(String address,String inputUrl, String inputKey, String clientId) throws InvalidKeyException, NoSuchAlgorithmException, URISyntaxException { String request = null; URL url = null; try { inputUrl=inputUrl+"?sensor=false"+"&address="+address+"&client="+clientId; byte[] key = convertBase64toBinary(inputKey); url = new URL(inputUrl); request = signRequest(url.getPath(), url.getQuery(),key); } catch (MalformedURLException e) { GCRMWSContext.getLogger().logMessage(e.getMessage()); } catch (UnsupportedEncodingException e) { GCRMWSContext.getLogger().logMessage(e.getMessage()); } return request; } private byte[] convertBase64toBinary(String keyString) { // Convert the key from 'web safe' base 64 to binary keyString = keyString.replace('-', '+'); keyString = keyString.replace('_', '/'); byte[] key = Base64.decode(keyString); return key; } private String signRequest(String path, String query,byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, URISyntaxException { // Retrieve the proper URL components to sign String resource = path + '?' + query; // Get an HMAC-SHA1 signing key from the raw key bytes SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1"); // Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 // key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(sha1Key); // compute the binary signature for the request byte[] sigBytes = mac.doFinal(resource.getBytes()); // base 64 encode the binary signature // String signature = Base64.encodeBytes(sigBytes); String signature = Base64.encodeToString(sigBytes, true); // convert the signature to 'web safe' base 64 signature = signature.replace('+', '-'); signature = signature.replace('/', '_'); return signature; } }
Main class – How to call this above method:
public it.units.GoogleGeocoding.GeocodeResponse getLocation( StringBuilder geocodingIP, GoogleUrlSigner googleUrlSigner, @GWSSuppressLogging Map<String, String> grlSyncLogMap, RestConnector googleMapRestConnector, String addr,String... addressElements) { Map<String, String> queryParams = new LinkedHashMap<String, String>(); GeocodeResponse geoResponse = null; String signature = null; StringBuilder address = new StringBuilder(); for (String addrelem : addressElements) { if (address.length() > 0) { address.append('+'); } try { address.append(URLEncoder.encode(addrelem, "UTF-8").replace("+", "%20")); } catch (UnsupportedEncodingException e) { GCRMWSContext.getLogger().logMessage(e.getMessage()); } } Map<Param, String> map = googleMapRestConnector.getConfig(); String endpoint = java.text.MessageFormat.format( map.get(Param.ENDPOINT_URL), geocodingIP); map.put(Param.ENDPOINT_URL, endpoint); try { signature = googleUrlSigner.getLicnsedGeoCoderURL( address.toString(), endpoint, grlSyncLogMap.get("google.geocoder.key"), grlSyncLogMap.get("google.geocoder.client")); } catch (InvalidKeyException e) { GCRMWSContext.getLogger().logMessage(e.getMessage()); } catch (NoSuchAlgorithmException e) { GCRMWSContext.getLogger().logMessage(e.getMessage()); } catch (URISyntaxException e) { GCRMWSContext.getLogger().logMessage(e.getMessage()); } queryParams.put("sensor", "false"); queryParams.put("address", addr); queryParams.put("client", grlSyncLogMap.get("google.geocoder.client")); queryParams.put("signature", signature); try { geoResponse = (it.units.GoogleGeocoding.GeocodeResponse) googleMapRestConnector.invoke(HttpMethod.GET, null, null, queryParams); /* * System.out.println("LocationType: "+ * geoResponse.getResult().getGeometry().getLocationType()); * System.out.println("FormattedAddress: "+ * geoResponse.getResult().getFormattedAddress()); * System.out.println("AddressTypes: "+ * geoResponse.getResult().getAddressTypes()); * System.out.println("Lat: "+ * geoResponse.getResult().getGeometry().getLocation().getLat()); * System.out.println("Lan: "+ * geoResponse.getResult().getGeometry().getLocation().getLng()); */ } catch (NullPointerException ne) { GCRMWSContext.getLogger().logMessage(ne.getMessage()); } catch (RestException re) { GCRMWSContext.getLogger().logMessage(re.getMessage()); } catch (Exception e) { GCRMWSContext.getLogger().logMessage(e.getMessage()); } return geoResponse; }