1

In my project, whenever an API is called it gets cached in Akamai. But when a client changes something in the database through UI. We need to invalidate cached API's response in AKAMAI and fill it with the new fresh json data. I found some link on the internet : akamai-purging but I am not able to understand what is cp-code in this link they are talking about?

Here is my sample code which is giving: 405 Not Allowed

Code:

public static void main(String[] args) throws IOException, RequestSigningException {

    URL url = new URL("https://xxx-host-name-/scripts.4535eaf743502b25ba3a.js");

    HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport();
    HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
    AkamaiPostData postData = new AkamaiPostData();
    postData.setHostname(AkamaiConstants.SITE_HOST_NAME);

    Gson gson = new Gson();
    String postDataJSON = gson.toJson(postData);
    byte[] contentBytes = postDataJSON.getBytes();
    HttpContent content = new ByteArrayContent("application/json", contentBytes);
    HttpRequest request = requestFactory.buildDeleteRequest(new GenericUrl(url));
    HttpHeaders headers = request.getHeaders();
    headers.set("Host", "xxx-host-name-");

    ClientCredential credential = new DefaultCredential(AkamaiConstants.CLIENT_TOKEN, AkamaiConstants.ACCESS_TOKEN, AkamaiConstants.CLIENT_SECRET);
    RequestSigner signer = new EdgeGridV1Signer(Collections.EMPTY_LIST, 1024 * 2);
    HttpRequest signedRequest = signer.sign(request, credential);
    HttpResponse response = signedRequest.execute();
    String result = response.parseAsString();
    System.out.println("result::" + result);
}
Gurinder
  • 943
  • 2
  • 9
  • 19

2 Answers2

3

So finally I was able to do that. This approach I have used is fast purging approach provided by Akamai.

Code example:

public class AkamaiFastPurge {

    private static final String HTTPS = "https";

    public static void main(String... s) throws URISyntaxException, IOException, RequestSigningException {


        ClientCredential credential = ClientCredential.builder()
                .accessToken("Your-access-token")
                .clientToken("Your-client-token")
                .clientSecret("Your-client-secret")
                .host("Your-host")
                .build();


        ArrayList arrayList = new ArrayList<String>();

        // You can add multiple urls.
        arrayList.add("*****-Actual-url-you-want-to-purge*****");

        HttpResponse response = invalidateUrls(credential, arrayList, "production");

        System.out.println(response.getStatusCode());
        System.out.println(response.getStatusMessage());
    }

    public static HttpResponse invalidateUrls(ClientCredential clientCredential, List<String> urls, String network) {

        HttpTransport httpTransport = new ApacheHttpTransport();
        HttpRequestFactory requestFactory = httpTransport.createRequestFactory();

        HttpRequest request = null;

        try {

            // This is fast purge approach
            URI uri = new URI(HTTPS, "api.ccu.akamai.com", "/ccu/v3/invalidate/url/" + network, null, null);

            String requestBody = getStringRequestBody(urls);

            request = requestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", requestBody));
            GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCredential);
            requestSigner.sign(request);

            return request.execute();

        } catch (IOException e) {
//            log.error("IOException in Akamai Utility", e);
        } catch (RequestSigningException e) {
//            log.error("RequestSigningException in Akamai Utility", e);
        } catch (URISyntaxException e) {
//            log.error("UriSyntaxException in Akamai Utility", e);
        }

        return null;
    }

    public static String getStringRequestBody(List<String> urls) {

        Map<String, List<String>> akamaiRequestMap = Maps.newHashMap();
        akamaiRequestMap.put("objects", urls);

        return new Gson().toJson(akamaiRequestMap);
    }
}

Dependencies I have used for it:

 <dependency>
        <groupId>com.akamai.edgegrid</groupId>
        <artifactId>edgegrid-signer-google-http-client</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
</dependencies>
Gurinder
  • 943
  • 2
  • 9
  • 19
0

Purging content in Akamai requires API credentials. You can learn more about the Akamai Fast Purge API on the Akamai Developer site. You'll need to either contact your Akamai account team or configure the API access you need in the Luna Control Center. The API's documentation will give you the links to understanding the API and how to configure the appropriate credentials.

As a small bit of follow up, a CP Code is Akamai's unique identifier tying a particular domain or service to your contract for billing purposes. In addition to the credentials needed to access the API, you'll definitely need to know the CP Code that is being used by your Akamai powered domain to make successful calls to the Fast Purge API.

Josh Cheshire
  • 334
  • 1
  • 10
  • Thanks for your reply, I just want to know what are the steps to it. 1) First I have to enable my purging through Akamai team. 2) On the basis of tokens, I will purge my APIs. 3) Purging an API means Akamai will automatically invalidate it and hit the same API again itself for fresh data? Please correct me if I am wrong – Gurinder Sep 06 '18 at 11:12
  • That's mostly correct, except that you should remember Akamai is going to hit the API based on an end-user request. It's only once the request comes to Akamai that it will be relayed to your origin. – Josh Cheshire Sep 11 '18 at 16:13