Created
July 21, 2020 07:06
-
-
Save sreeni-b/1dfbea5c867f98d0a6d65ae1601cb06d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aemks.core.services.impl; | |
import com.aemks.core.services.MSMService; | |
import com.day.cq.contentsync.handler.util.RequestResponseFactory; | |
import com.day.cq.wcm.api.commands.WCMCommand; | |
import com.day.cq.wcm.msm.api.MSMNameConstants; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.engine.SlingRequestProcessor; | |
import org.osgi.service.component.annotations.Component; | |
import org.osgi.service.component.annotations.Reference; | |
import org.osgi.service.component.propertytypes.ServiceDescription; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* This service uses WCM Command Servlet to create live copy. | |
*/ | |
@Component(service = MSMService.class) | |
@ServiceDescription("MSM Service") | |
public class MSMServiceImpl implements MSMService { | |
private static final String CMD_LIVE_COPY = "createLiveCopy" ; | |
private static final String CMD = "cmd" ; | |
private static final String WCM_COMMAND_ENDPOINT = "/bin/wcmcommand"; | |
private static final String CHARSET = "_charset_"; | |
@Reference | |
private RequestResponseFactory requestResponseFactory; | |
@Reference | |
private SlingRequestProcessor requestProcessor; | |
private static final Logger LOGGER = LoggerFactory.getLogger(MSMServiceImpl.class); | |
/** | |
* This method accepts the required parameters to create a live copy | |
* and calls the WCM Command Servlet to create live copy. | |
* @param resourceResolver the resource resolver | |
* @param srcPath the source path (path from which live copy has to be created) | |
* @param destPath the destination path (path under which live copy has to be created) | |
* @param title live copy root page title | |
* @param label live copy root page name | |
* @param rolloutConfigs Rollout Configurations to be included | |
*/ | |
@Override | |
public void createLiveCopy(ResourceResolver resourceResolver, String srcPath, String destPath, String title, String label, String[] rolloutConfigs) { | |
try { | |
Map<String, Object> params = new HashMap<>(); | |
params.put(CHARSET, StandardCharsets.UTF_8); | |
params.put(CMD, CMD_LIVE_COPY); | |
params.put(WCMCommand.SRC_PATH_PARAM, srcPath); | |
params.put(WCMCommand.DEST_PATH_PARAM, destPath); | |
params.put(WCMCommand.PAGE_TITLE_PARAM, title); | |
params.put(WCMCommand.PAGE_LABEL_PARAM, label); | |
params.put(MSMNameConstants.PN_ROLLOUT_CONFIGS, rolloutConfigs); | |
HttpServletRequest req = requestResponseFactory.createRequest("POST", WCM_COMMAND_ENDPOINT, | |
params); | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
HttpServletResponse response = requestResponseFactory.createResponse(out); | |
requestProcessor.processRequest(req, response, resourceResolver); | |
} catch (ServletException | IOException e) { | |
LOGGER.error("An error occurred while creating live copy", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment