send multiple paths in one dispatcher cache invalidate API
How to send page multiple paths to invalidate dispatcher cache in one request instead of loop?
import com.amazonaws.http.apache.request.impl.HttpGetWithBody;
import com.esa.service.CacheInvalidationConfig;
import com.esa.service.CacheInvalidationService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;
@8220494(service = CacheInvalidationService.class, immediate=true)
public class CacheInvalidationServiceImpl implements CacheInvalidationService {
@3214626
private CacheInvalidationConfig cacheInvalidationConfig;
private static final String DISPATCHER_URL_PATH_API = "/dispatcher/invalidate.cache";
private static final Logger LOG = LoggerFactory.getLogger(CacheInvalidationServiceImpl.class);
public void invalidateCache(Set<String> pagePaths) {
String [] dispatcherHost = cacheInvalidationConfig.getDispatcherURL();
for (String path : pagePaths) {
for (String dispatcherURL : dispatcherHost) {
String apiURL = dispatcherURL + DISPATCHER_URL_PATH_API;
Request request = Request.Get(apiURL);
request.setHeader("Host", "flush");
request.setHeader("CQ-Action", "Activate");
request.setHeader("CQ-Handle", path);
HttpResponse httpResponse = null;
try {
httpResponse = request.execute().returnResponse();
LOG.info("Prepared URL : {} ,CQ Handle : {}", apiURL, path);
if (null != httpResponse && null != httpResponse.getStatusLine()) {
final int statusCode = httpResponse.getStatusLine().getStatusCode();
LOG.info("Response Code for Cache Clear Request: {}", statusCode);
}
}
catch (Exception e) {
LOG.error("Dispatcher API Error : {}",e.getMessage(),e);
}
}
}
}
}