Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Akamai CCU V3使用JAVA项目或AEM快速清除实施_Java_Aem_Akamai - Fatal编程技术网

Akamai CCU V3使用JAVA项目或AEM快速清除实施

Akamai CCU V3使用JAVA项目或AEM快速清除实施,java,aem,akamai,Java,Aem,Akamai,有没有人尝试过将CCU V2到CCU V3的java代码与fast purge集成在一起。我阅读了文档,但无法理解在基于java的项目中需要做什么。在Akamai console中配置客户端后,如何编写代码以访问和清除。 非常感谢您的帮助 谢谢, Tushar一般的方法是编写一些代码,向快速清除端点发出HTTP请求 以下是一些示例: import com.akamai.edgegrid.auth.*; //other imports public void callAkamaiFastPurg

有没有人尝试过将CCU V2到CCU V3的java代码与fast purge集成在一起。我阅读了文档,但无法理解在基于java的项目中需要做什么。在Akamai console中配置客户端后,如何编写代码以访问和清除。 非常感谢您的帮助

谢谢,
Tushar

一般的方法是编写一些代码,向快速清除端点发出HTTP请求

以下是一些示例:

import com.akamai.edgegrid.auth.*;
//other imports

public void callAkamaiFastPurgeForUrls(Set<String> urlsToPurge) throws URISyntaxException, IOException, RequestSigningException {
    if(!urlsToPurge.isEmpty()) {
        int status;
        String json = getPurgeJson(urlsToPurge);
        HttpRequest signedRequest = getHttpRequest(json, compProperty);
        HttpResponse response = signedRequest.execute();

        status = response.getStatusCode();

        if (status == 201) {
            //handle success responses as you see fit
        } else {
            //handle non-success responses as you see fit
        }
    }
}

private static String getPurgeJson(Set<String> pathsToPurge) {
    //your code to turn the list of urls into JSON in this format:
    //{"objects":["https://www.yourdomain.com/page1.html","https://www.yourdomain.com/page2.html"]}

    return //JSON string such as the example above
}


private HttpRequest getHttpRequest(String json, Dictionary<String, Object> dispatcherAndAkamaiServletProperties) throws URISyntaxException, IOException, RequestSigningException {
    String hostName = yourCodeToFetchConfigValues("hostname");
    String accessToken = yourCodeToFetchConfigValues("accesstoken");
    String clientToken = yourCodeToFetchConfigValues("clienttoken");
    String clientSecret = yourCodeToFetchConfigValues("clientsecret");
    String apiUrl = yourCodeToFetchConfigValues("apiurl");
    String proxyServer = yourCodeToFetchConfigValues("proxyhostakamai");
    int proxyPort = yourCodeToFetchConfigValues("proxyport");

    HttpTransport httpTransport = new NetHttpTransport.Builder()
            .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyServer, proxyPort))).build();

    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();

    URI uri = new URI(HTTPS, hostName, apiUrl, null, null);
    HttpContent body = new ByteArrayContent("application/json", json.getBytes());
    HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(uri), body);
    HttpHeaders headers = request.getHeaders();
    headers.set("Host", hostName);
    ClientCredential credential = new DefaultCredential(clientToken, accessToken, clientSecret);
    RequestSigner signer = new EdgeGridV1Signer(Collections.emptyList(), 1024 * 2);

    return signer.sign(request, credential);
}
import com.akamai.edgegrid.auth.*;
//其他进口
public void callAkamaiFastPurgeForUrls(设置URLStopPurge)引发URISyntaxException、IOException、RequestSigningException{
如果(!urlsToPurge.isEmpty()){
智力状态;
字符串json=getPurgeJson(urlsToPurge);
HttpRequest signedRequest=getHttpRequest(json,compProperty);
HttpResponse response=signedRequest.execute();
status=response.getStatusCode();
如果(状态==201){
//根据您认为合适的方式处理成功响应
}否则{
//按照您认为合适的方式处理非成功响应
}
}
}
私有静态字符串getPurgeJson(设置PathStopPurge){
//将URL列表转换为JSON格式的代码:
//{“对象”:[“https://www.yourdomain.com/page1.html","https://www.yourdomain.com/page2.html"]}
返回//JSON字符串,如上面的示例
}
私有HttpRequest getHttpRequest(字符串json,字典分派器DAKAMAIServletProperties)抛出URI语法异常、IOException、RequestSigningException{
字符串hostName=yourCodeToFetchConfigValues(“主机名”);
字符串accessToken=yourCodeToFetchConfigValues(“accessToken”);
String clientToken=yourCodeToFetchConfigValues(“clientToken”);
String clientSecret=yourCodeToFetchConfigValues(“clientSecret”);
字符串APIRL=yourCodeToFetchConfigValues(“APIRL”);
字符串proxyServer=yourCodeToFetchConfigValues(“proxyhostakamai”);
int proxyPort=yourCodeToFetchConfigValues(“proxyPort”);
HttpTransport HttpTransport=新的NetHttpTransport.Builder()
.setProxy(新代理(Proxy.Type.HTTP,新InetSocketAddress(proxyServer,proxyPort))).build();
HttpRequestFactory requestFactory=httpTransport.createRequestFactory();
URI=新URI(HTTPS、主机名、apiUrl、null、null);
HttpContent body=newbytearraycontent(“application/json”,json.getBytes());
HttpRequest请求=requestFactory.buildPostRequest(新的GenericUrl(uri),主体);
HttpHeaders=request.getHeaders();
headers.set(“主机”,主机名);
ClientCredential=new DefaultCredential(clientToken、accessToken、clientSecret);
RequestSigner signer=new EdgeGridV1Signer(Collections.emptyList(),1024*2);
返回签名者。签名(请求、凭证);
}

此外,您可能需要更新您的truststore,以包含您正在呼叫的Akamai端点的证书,以便进行SSL通信。

@Shawn的回答基本正确。尽管您希望在板中有更多的东西,如自定义复制代理、自定义构件编辑器和传输处理程序(如果您要将其与AEM集成)。此外,也可能存在一些依赖性问题。 如果您需要所有这些方面的帮助,请参阅以下文章:

对于传输处理程序,您可以使用以下代码段:

    package com.myproject.bundle.core.services.impl;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.entity.ContentType;
import org.apache.jackrabbit.util.Base64;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.akamai.edgegrid.signer.ClientCredential;
import com.akamai.edgegrid.signer.exceptions.RequestSigningException;
import com.akamai.edgegrid.signer.googlehttpclient.GoogleHttpClientEdgeGridRequestSigner;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationResult;
import com.day.cq.replication.ReplicationTransaction;
import com.day.cq.replication.TransportContext;
import com.day.cq.replication.TransportHandler;
import com.myproject.bundle.core.configuration.BaseConfigurationService;
import com.myproject.bundle.core.constants.MyConstants;
import com.myproject.bundle.core.search.services.MyProjectConfigurationService;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;

/**
 * Transport handler to send test and purge requests to Akamai and handle
 * responses. The handler sets up basic authentication with the user/pass from
 * the replication agent's transport config and sends a GET request as a test
 * and POST as purge request. A valid test response is 200 while a valid purge
 * response is 201.
 * 
 * The transport handler is triggered by setting your replication agent's
 * transport URL's protocol to "akamai://".
 *
 * The transport handler builds the POST request body in accordance with
 * Akamai's Fast Purge REST API {@link https://developer.akamai.com/api/core_features/fast_purge/v3.html}
 * using the replication agent properties. 
 */
@Component(service = TransportHandler.class, immediate = true)
public class AkamaiTransportHandler implements TransportHandler {

    /**The Solr Server Configuration Service.*/
    @Reference
    MyProjectConfigurationService myProjectConfigurationService;

    @Reference
    BaseConfigurationService baseConfigurationService;

    /**Logger Instantiation for Akamai Transport Handler*/
    private static final Logger LOGGER = LoggerFactory.getLogger(AkamaiTransportHandler.class);

    /** Protocol for replication agent transport URI that triggers this transport handler. */
    private static final String AKAMAI_PROTOCOL = "akamai://";

    /**Config Pid for Akamai Flush*/
    private static final String AKAMAI_FLUSH_CONFIG_PID = "com.myproject.bundle.core.configuration.AkamaiFlushConfiguration";

    /** Replication agent type property name. Valid values are "arl" and "cpcode". */
    private static final String PROPERTY_AKAMAI_TYPE = "type";

    /** Replication agent multifield CP Code property name.*/
    private static final String PROPERTY_AKAMAI_CP_CODES = "4321xxx";

    /** Replication agent domain property name. Valid values are "staging" and "production". */
    private static final String PROPERTY_AKAMAI_DOMAIN = "domain";

    /** Replication agent action property name. Valid values are "remove" and "invalidate". */
    private static final String PROPERTY_AKAMAI_ACTION = "action";

    /** Replication agent default type value */
    private static final String PROPERTY_AKAMAI_TYPE_DEFAULT = "url";

    /** Replication agent default domain value */
    private static final String PROPERTY_AKAMAI_DOMAIN_DEFAULT = "production";

    /** Replication agent default action value */
    private static final String PROPERTY_AKAMAI_ACTION_DEFAULT = "invalidate";

    /**Transport URI*/
    private static final String TRANSPORT_URI = "transportUri";

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean canHandle(AgentConfig config) {
        final String transportURI = config.getTransportURI();

        return (transportURI != null) && (transportURI.toLowerCase().startsWith(AKAMAI_PROTOCOL));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ReplicationResult deliver(TransportContext ctx, ReplicationTransaction tx)
            throws ReplicationException {

        final ReplicationActionType replicationType = tx.getAction().getType();

        if (replicationType == ReplicationActionType.TEST) {
            return ReplicationResult.OK;
        } else if (replicationType == ReplicationActionType.ACTIVATE ||
                replicationType == ReplicationActionType.DEACTIVATE ||
                replicationType == ReplicationActionType.DELETE) {
            LOGGER.info("Replication  Type in Akamai Handler: {}", replicationType);
            String resourcePath = tx.getAction().getPath();
            if (StringUtils.startsWith(resourcePath, myProjectConfigurationService.getContentpath()) 
                || StringUtils.startsWith(resourcePath, myProjectConfigurationService.getAssetpath())) {
// checking for my project specific root page and root dam path.
                LOGGER.info("Calling activate in Akamai for path: {}", resourcePath);
                try {
                    return doActivate(ctx, tx);
                } catch (RequestSigningException e) {
                    LOGGER.error("Signing ceremony unsuccessful....");
                    throw new ReplicationException("Signing ceremony unsuccessful: {}", e);
                } catch (IOException e) {
                    LOGGER.error("IO Exception in deliver \n");
                    throw new ReplicationException("IO Exception in deliver: {}", e);
                }
            }
            return ReplicationResult.OK;
        } else {
            throw new ReplicationException("Replication action type " + replicationType + " not supported.");
        }
    }

    private String getTransportURI(TransportContext ctx) throws IOException {
        LOGGER.info("Entering getTransportURI method.");
        final ValueMap properties = ctx.getConfig().getProperties();
        final String AKAMAI_HOST = baseConfigurationService.getPropValueFromConfiguration(AKAMAI_FLUSH_CONFIG_PID, "akamaiHost");
        final String domain = PropertiesUtil.toString(properties.get(PROPERTY_AKAMAI_DOMAIN), PROPERTY_AKAMAI_DOMAIN_DEFAULT);
        final String action = PropertiesUtil.toString(properties.get(PROPERTY_AKAMAI_ACTION), PROPERTY_AKAMAI_ACTION_DEFAULT);
        final String type = PropertiesUtil.toString(properties.get(PROPERTY_AKAMAI_TYPE), PROPERTY_AKAMAI_TYPE_DEFAULT);
        String defaultTransportUri = MyConstants.HTTPS + AKAMAI_HOST + "/ccu/v3/"
                                    + action + MyConstants.BACK_SLASH + type + MyConstants.BACK_SLASH + domain;
        String transporturi = PropertiesUtil.toString(properties.get(TRANSPORT_URI), defaultTransportUri);

        if(StringUtils.isEmpty(transporturi)) {
            return defaultTransportUri;
        }
        if (transporturi.startsWith(AKAMAI_PROTOCOL)) {
            transporturi = transporturi.replace(AKAMAI_PROTOCOL, MyConstants.HTTPS);
        }
        transporturi =  transporturi + "/ccu/v3/"
                + action + MyConstants.BACK_SLASH + type + MyConstants.BACK_SLASH + domain;
        LOGGER.info("Exiting getTransportURI method of Akamai Transport Handler : {}", transporturi);
        return transporturi;
    }

    /**
     * Send purge request to Akamai via a POST request
     *
     * Akamai will respond with a 201 HTTP status code if the purge request was
     * successfully submitted.
     *
     * @param ctx Transport Context
     * @param tx Replication Transaction
     * @return ReplicationResult OK if 201 response from Akamai
     * @throws ReplicationException
     * @throws RequestSigningException 
     * @throws IOException 
     * @throws JSONException 
     */
    private ReplicationResult doActivate(TransportContext ctx, ReplicationTransaction tx)
            throws ReplicationException, RequestSigningException, IOException {
        LOGGER.info("Inside doActivate of Akamai");
        final String AKAMAI_ACCESS_TOKEN = baseConfigurationService.getPropValueFromConfiguration(AKAMAI_FLUSH_CONFIG_PID, "akamaiAccessToken");
        final String AKAMAI_CLIENT_TOKEN = baseConfigurationService.getPropValueFromConfiguration(AKAMAI_FLUSH_CONFIG_PID, "akamaiClientToken");
        final String AKAMAI_CLIENT_SECRET = baseConfigurationService.getPropValueFromConfiguration(AKAMAI_FLUSH_CONFIG_PID, "akamaiClientSecret");
        final String AKAMAI_HOST = baseConfigurationService.getPropValueFromConfiguration(AKAMAI_FLUSH_CONFIG_PID, "akamaiHost");

        ClientCredential clientCredential = ClientCredential.builder().accessToken(AKAMAI_ACCESS_TOKEN).
                clientToken(AKAMAI_CLIENT_TOKEN).clientSecret(AKAMAI_CLIENT_SECRET).host(AKAMAI_HOST).build();


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

        JSONObject jsonObject = createPostBody(ctx, tx);


        URI uri = URI.create(getTransportURI(ctx));

        HttpRequest request = httpRequestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", jsonObject.toString()));
        final HttpResponse response = sendRequest(request, ctx, clientCredential);


        if (response != null) {
            final int statusCode = response.getStatusCode();
            LOGGER.info("Response code recieved: {}", statusCode);
            if (statusCode == HttpStatus.SC_CREATED) {
                return ReplicationResult.OK;
            }
        }
        return new ReplicationResult(false, 0, "Replication failed");
    }

    /**
     * Build preemptive basic authentication headers and send request.
     *
     * @param request The request to send to Akamai
     * @param ctx The TransportContext containing the username and password
     * @return JSONObject The HTTP response from Akamai
     * @throws ReplicationException if a request could not be sent
     * @throws RequestSigningException 
     */
    private HttpResponse sendRequest(final HttpRequest request, final TransportContext ctx,
            ClientCredential clientCredential)
            throws ReplicationException, RequestSigningException {

        LOGGER.info("Inside Send Request method of Akamai");
        final String auth = ctx.getConfig().getTransportUser() + ":" + ctx.getConfig().getTransportPassword();
        final String encodedAuth = Base64.encode(auth);

         HttpHeaders httpHeaders = new HttpHeaders();
         httpHeaders.setAuthorization("Basic " + encodedAuth);
         httpHeaders.setContentType(ContentType.APPLICATION_JSON.getMimeType());

         request.setHeaders(httpHeaders);

         GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCredential);
         requestSigner.sign(request);

         HttpResponse response;

         try {
             response = request.execute();
         } catch (IOException e) {
             LOGGER.error("IO Exception in sendRequest");
             throw new ReplicationException("Could not send replication request.", e);
         }
         LOGGER.info("Sucessfully executed Send Request for Akamai");
         return response;
    }

    /**
     * Build the Akamai purge request body based on the replication agent
     * settings and append it to the POST request.
     *
     * @param request The HTTP POST request to append the request body
     * @param ctx TransportContext
     * @param tx ReplicationTransaction
     * @throws ReplicationException if errors building the request body 
     */
    private JSONObject createPostBody(final TransportContext ctx,
            final ReplicationTransaction tx) throws ReplicationException {
        final ValueMap properties = ctx.getConfig().getProperties();
        final String type = PropertiesUtil.toString(properties.get(PROPERTY_AKAMAI_TYPE), PROPERTY_AKAMAI_TYPE_DEFAULT);
        JSONObject json = new JSONObject();
        JSONArray purgeObjects = null;

        if (type.equals(PROPERTY_AKAMAI_TYPE_DEFAULT)) {
            try {
                String content = IOUtils.toString(tx.getContent().getInputStream(), Charset.defaultCharset());

                if (StringUtils.isNotBlank(content)) {
                    LOGGER.info("Content of Akamai is:\n {}", content);
                    purgeObjects = new JSONArray(content);
                }
            } catch (JSONException | IOException e) {
                throw new ReplicationException("Could not retrieve content from content builder", e);
            }
        }
        if (null != purgeObjects && purgeObjects.length() > 0) {
            try {
                json.put("objects", purgeObjects);
            } catch (JSONException e) {
                throw new ReplicationException("Could not build purge request content", e);
            }
        } else {
            throw new ReplicationException("No CP codes or pages to purge");
        }
        return json;
    }
}
此外,您还需要以下依赖项:

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

com.akamai.edgegrid
edgegrid签名者google http客户端
4.0.0
com.akamai.edgegrid
边缘签名者核心
4.0.0
com.google.http-client
谷歌http客户端
1.22.0
对于我的情况,捆绑包没有解决,我必须添加以下依赖项来解决它。但在您的情况下可能会有所不同:

    <dependency>
        <groupId>io.opencensus</groupId>
        <artifactId>opencensus-api</artifactId>
        <version>0.24.0</version>
    </dependency>
    <dependency>
        <groupId>io.opencensus</groupId>
        <artifactId>opencensus-contrib-http-util</artifactId>
        <version>0.24.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-context</artifactId>
        <version>1.24.0</version>
    </dependency>
 <Import-Package>
        javax.annotation;version=0.0.0,
 </Import-Package>

开放式普查
OpenCensusAPI
0.24.0
开放式普查
opencensus contrib http util
0.24.0
io.grpc
grpc上下文
1.24.0
javax.annotation;版本=0.0.0,
对于“IOUtils.toString(tx.getContent().getInputStream(),Charset.defaultCharset());”行,它在内部调用您的自定义构件编辑器(您可以参考我前面提供的文章链接)。但是,您可以直接在传输处理程序本身中创建内容对象,因为传输处理程序正在以任何方式创建自己的请求。但是,如果需要会话,则需要实现ContentBuilder,因为TransportHandler实现不提供这种利用

谢谢, 舒巴姆