Skip to content

Latest commit

 

History

History

edgegrid-signer-google-http-client

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Google HTTP Client Library - EdgeGrid Client for Java

Maven Central Javadoc

This library implements Akamai EdgeGrid Authentication for Java. This particular module is a binding for Google HTTP Client Library for Java. This project contains installation and usage instructions in the README.md.

Use Google HTTP Client Library for Java

Include the following Maven dependency in your project POM:

<dependency>
    <groupId>com.akamai.edgegrid</groupId>
    <artifactId>edgegrid-signer-google-http-client</artifactId>
    <version>5.0.0</version>
</dependency>

Sign your HTTP request with a defined client credential:

HttpClient client = HttpClients.custom()
        .setSSLSocketFactory(SSLSocketFactory.getSystemSocketFactory())
        .build();
HttpRequestFactory requestFactory = new ApacheHttpTransport(client).createRequestFactory();
URI uri = URI.create("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/billing-usage/v1/reportSources");
try {
    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(uri));
    GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(credential);

    requestSigner.sign(request);
    request.execute();
} catch (IOException | RequestSigningException e) {
    throw new RuntimeException("Error during HTTP request execution", e);
}

This, however, requires remembering to sign every request explicitly.

Alternately, you may create an HttpRequestFactory that will automatically sign requests via an Interceptor:

private HttpRequestFactory createSigningRequestFactory() {
    HttpTransport httpTransport = new ApacheHttpTransport();
    return httpTransport.createRequestFactory(new HttpRequestInitializer() {
        public void initialize(HttpRequest request) throws IOException {
            request.setInterceptor(new GoogleHttpClientEdgeGridInterceptor(clientCredentialProvider));
        }
    });
}

And then:

HttpRequestFactory requestFactory = createSigningRequestFactory();
URI uri = URI.create("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/billing-usage/v1/reportSources");
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(uri));

request.execute();

Note: In this example we used a clientCredentialProvider rather than a simpler ClientCredential. clientCredentialProvider provides a mechanism to construct a ClientCredential at the time of the request based on any logic you define. For example, your implementation could read credentials from a database or another secret store.