Akamai doc is really hard to understand.
- AstinCHOI/AkamaiPurgeExample.java https://gist.github.com/AstinCHOI/417b520ed7bf8bc7f5b5eb61fdc8eb42
- Akamai EdgeGrid Client for Java for looking testcase https://github.com/akamai/AkamaiOPEN-edgegrid-java
- Fast Purge Api for looking JSON Data format https://developer.akamai.com/api/core_features/fast_purge/v3.html#postinvalidateurl
Using https://start.spring.io/ to start project and add depenedency Library
build.gradle
...
dependencies {
implementation 'com.google.guava:guava:28.1-jre'
implementation 'com.akamai.edgegrid:edgegrid-signer-rest-assured:4.0.1'
implementation 'com.akamai.edgegrid:edgegrid-signer-google-http-client:4.0.1'
implementation 'com.alibaba:fastjson:1.2.73'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Using RestAssured
@Test
public void purgeCacheByUrlAndRestAssured() {
final String hostname = "";
final String token = "";
final String secret = "";
final String clientToken = "";
final ClientCredential clientCreditial = ClientCredential.builder()
.host(hostname)
.accessToken(token)
.clientToken(clientToken)
.clientSecret(secret)
.build();
final List<String> urlList = ImmutableList.of(
"https://{{ TARGET_PURGE_IMAGE_URL }}"
);
final String jsonBody = new JSONObject()
.fluentPut("objects", urlList).toJSONString();
RestAssured
.given()
.relaxedHTTPSValidation()
.urlEncodingEnabled(false)
.config(RestAssured.config()
.encoderConfig(RestAssured.config().getEncoderConfig()
.appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.contentType("application/json")
.filter(new RestAssuredEdgeGridFilter(clientCreditial))
.body(jsonBody)
.when()
.post("/ccu/v3/invalidate/url/production")
.then()
.statusCode(201);
}
Using Google Http Api
@Test
public void purgeCacheByImageUrlAndGoogleApi() throws IOException, RequestSigningException {
final String hostname = "";
final String token = "";
final String secret = "";
final String clientToken = "";
final ClientCredential clientCreditial = ClientCredential.builder()
.host(hostname)
.accessToken(token)
.clientToken(clientToken)
.clientSecret(secret)
.build();
final List<String> urlList = ImmutableList.of(
"https://{{ TARGET_PURGE_IMAGE_URL }}"
);
final String jsonBody = new JSONObject()
.fluentPut("objects", urlList).toJSONString();
final String url = String.format("https://%s/ccu/v3/invalidate/url/production", hostname);
HttpTransport httpTransport = new ApacheHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
URI uri = URI.create(url);
HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", jsonBody));
GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCreditial);
requestSigner.sign(request);
HttpResponse response = request.execute();
System.out.println(response.getStatusCode()); // response status code
String text = null;
try (Scanner scanner = new Scanner(response.getContent(), StandardCharsets.UTF_8.name())) {
text = scanner.useDelimiter("\\A").next();
}
System.out.println(text); // response body
}
Using Google Http Api and purge all site by CpCode
@Test
public void cleanCacheByCpCodeAndGoogleApi() throws IOException, RequestSigningException {
final String hostname = "";
final String token = "";
final String secret = "";
final String clientToken = "";
final String cpcode = "{{ YOUR_CPCODE }}";
final ClientCredential clientCreditial = ClientCredential.builder()
.host(hostname)
.accessToken(token)
.clientToken(clientToken)
.clientSecret(secret)
.build();
final List<String> urlList = ImmutableList.of(cpcode);
final String jsonBody = new JSONObject()
.fluentPut("objects", urlList).toJSONString();
final String url = String.format("https://%s/ccu/v3/invalidate/cpcode/production", hostname);
HttpTransport httpTransport = new ApacheHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
URI uri = URI.create(url);
HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", jsonBody));
GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCreditial);
requestSigner.sign(request);
HttpResponse response = request.execute();
System.out.println(response.getStatusCode()); // response status code
String text = null;
try (Scanner scanner = new Scanner(response.getContent(), StandardCharsets.UTF_8.name())) {
text = scanner.useDelimiter("\\A").next();
}
System.out.println(text); // response body
}