Skip to content

Commit

Permalink
CAMEL-12554 - camel-geocoder - Use new API
Browse files Browse the repository at this point in the history
using com.google.maps:google-maps-services instead of com.google.code.geocoder-java:geocoder-java
  • Loading branch information
jborza authored and onderson committed Jul 10, 2018
1 parent f303d87 commit e31c625
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 194 deletions.
17 changes: 8 additions & 9 deletions components/camel-geocoder/pom.xml
Expand Up @@ -42,17 +42,12 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<!-- Java client library for Google Maps API Web Services -->
<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>${geocoder-java-version}</version>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.2.7</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-version}</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
Expand All @@ -79,6 +74,10 @@
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -65,10 +65,11 @@ with the following path and query parameters:
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *clientId* (producer) | To use google premium with this client id | | String
| *clientKey* (producer) | To use google premium with this client key | | String
| *apiKey* (producer) | Google Maps API Key | | String
| *clientId* (producer) | Client Id for Google Maps API Premium Plan | | String
| *clientKey* (producer) | Private key for Google Maps API Premium Plan | | String
| *headersOnly* (producer) | Whether to only enrich the Exchange with headers, and leave the body as-is. | false | boolean
| *language* (producer) | The language to use. | en | String
| *language* (producer) | The language in which to return results. | en | String
| *httpClientConfigurer* (advanced) | Register a custom configuration strategy for new HttpClient instances created by producers or consumers such as to configure authentication mechanisms etc | | HttpClientConfigurer
| *httpConnectionManager* (advanced) | To use a custom HttpConnectionManager to manage connections | | HttpConnectionManager
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
Expand Down Expand Up @@ -121,6 +122,8 @@ Exchange.
|`CamelGeoCoderCountryLong` |The country long name.

|`CamelGeoCoderCountryShort` |The country short name.

|`CamelGeoCoderPostalCode` |The postal code.
|=======================================================================

Notice not all headers may be provided depending on available data and
Expand Down
Expand Up @@ -28,6 +28,7 @@ public final class GeoCoderConstants {
public static final String CITY = "CamelGeoCoderCity";
public static final String COUNTRY_LONG = "CamelGeoCoderCountryLong";
public static final String COUNTRY_SHORT = "CamelGeoCoderCountryShort";
public static final String POSTAL_CODE = "CamelGeoCoderPostalCode";

private GeoCoderConstants() {
}
Expand Down
Expand Up @@ -18,8 +18,7 @@

import java.security.InvalidKeyException;

import com.google.code.geocoder.AdvancedGeoCoder;
import com.google.code.geocoder.Geocoder;
import com.google.maps.GeoApiContext;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
Expand Down Expand Up @@ -48,10 +47,12 @@ public class GeoCoderEndpoint extends DefaultEndpoint {
private String latlng;
@UriParam(defaultValue = "en")
private String language = "en";
@UriParam
@UriParam(label = "security", secret = true)
private String clientId;
@UriParam
@UriParam(label = "security", secret = true)
private String clientKey;
@UriParam(label = "security", secret = true)
private String apiKey;
@UriParam
private boolean headersOnly;
@UriParam(label = "proxy")
Expand Down Expand Up @@ -158,6 +159,15 @@ public void setClientKey(String clientKey) {
this.clientKey = clientKey;
}


private String getApiKey() {
return apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

/**
* The proxy host name
*/
Expand Down Expand Up @@ -257,42 +267,54 @@ public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager
this.httpConnectionManager = httpConnectionManager;
}

Geocoder createGeocoder() throws InvalidKeyException {
HttpConnectionManager connectionManager = this.httpConnectionManager;
if (connectionManager == null) {
connectionManager = new MultiThreadedHttpConnectionManager();
}

HttpClient httpClient = new HttpClient(connectionManager);
if (proxyHost != null && proxyPort != null) {
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
}

// validate that if proxy auth username is given then the proxy auth method is also provided
if (proxyAuthUsername != null && proxyAuthMethod == null) {
throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
}

CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
if (proxyAuthMethod != null) {
configureProxyAuth(configurer, proxyAuthMethod, proxyAuthUsername, proxyAuthPassword, proxyAuthDomain, proxyAuthHost);
}
if (httpClientConfigurer != null) {
configurer.addConfigurer(httpClientConfigurer);
}

configurer.configureHttpClient(httpClient);

Geocoder geocoder;
GeoApiContext createGeoApiContext() {
if (clientId != null) {
geocoder = new AdvancedGeoCoder(httpClient, clientId, clientKey);
return new GeoApiContext.Builder()
.enterpriseCredentials(clientId, clientKey)
.build();
} else {
geocoder = new AdvancedGeoCoder(httpClient);
return new GeoApiContext.Builder()
.apiKey(getApiKey())
.build();
}

return geocoder;
}

// Geocoder createGeocoder() throws InvalidKeyException {
// HttpConnectionManager connectionManager = this.httpConnectionManager;
// if (connectionManager == null) {
// connectionManager = new MultiThreadedHttpConnectionManager();
// }
//
// HttpClient httpClient = new HttpClient(connectionManager);
// if (proxyHost != null && proxyPort != null) {
// httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
// }
//
// // validate that if proxy auth username is given then the proxy auth method is also provided
// if (proxyAuthUsername != null && proxyAuthMethod == null) {
// throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
// }
//
// CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
// if (proxyAuthMethod != null) {
// configureProxyAuth(configurer, proxyAuthMethod, proxyAuthUsername, proxyAuthPassword, proxyAuthDomain, proxyAuthHost);
// }
// if (httpClientConfigurer != null) {
// configurer.addConfigurer(httpClientConfigurer);
// }
//
// configurer.configureHttpClient(httpClient);
//
// Geocoder geocoder;
// if (clientId != null) {
// geocoder = new AdvancedGeoCoder(httpClient, clientId, clientKey);
// } else {
// geocoder = new AdvancedGeoCoder(httpClient);
// }
//
// return geocoder;
// }

/**
* Configures the proxy authentication method to be used
*
Expand Down

0 comments on commit e31c625

Please sign in to comment.