
org.craftercms.cstudio.publishing.servlet.ReprocessServiceServlet Maven / Gradle / Ivy
The newest version!
package org.craftercms.cstudio.publishing.servlet;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.craftercms.cstudio.publishing.PublishedChangeSet;
import org.craftercms.cstudio.publishing.exception.PublishingException;
import org.craftercms.cstudio.publishing.processor.PublishingProcessor;
import org.craftercms.cstudio.publishing.target.PublishingTarget;
import org.craftercms.cstudio.publishing.target.TargetManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
/**
* Reindex the target site assuming that target index is clean
*/
public class ReprocessServiceServlet extends HttpServlet {
private static final Log LOGGER = LogFactory.getLog(ReprocessServiceServlet.class);
public static final String PARAM_PASSWORD = "password";
public static final String PARAM_PROCESSOR = "processor";
public static final String PARAM_TARGET = "target";
/**
* Deployer target manager
*/
protected TargetManager targetManager;
/**
* Deployer password
*/
private String password;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map parameters = this.getParameters(request);
String password = parameters.get(PARAM_PASSWORD);
// authenticate the request
if (password != null && password.equalsIgnoreCase(this.password)) {
// find the target
String paramTarget = parameters.get(PARAM_TARGET);
PublishingTarget target = this.targetManager.getTarget(paramTarget);
if (target != null) {
// find the processor
PublishingProcessor targetProcessor = null;
String processorName = parameters.get(PARAM_PROCESSOR);
if (!StringUtils.isEmpty(processorName)) {
List processors = target.getPostProcessors();
for (PublishingProcessor processor : processors) {
if (processorName.equalsIgnoreCase(processor.getName())) {
targetProcessor = processor;
break;
}
}
}
if (targetProcessor != null) {
// if found, reprocess the target
int status = reprocess(target, parameters, targetProcessor);
response.setStatus(status);
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("No processor found by name: " + processorName);
}
}
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("No configuration exists for " + paramTarget);
}
}
} else {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Illegal publish request received.");
}
}
}
/**
* Reprocess the site content by providing the list as created files
* @param target
* the target site
* @param parameters
* a list of parameters
* @param processor
* a processor to reprocess the site content
* @return response status
*/
private int reprocess(PublishingTarget target, Map parameters, PublishingProcessor processor) {
PublishedChangeSet changeSet = new PublishedChangeSet();
StringBuilder sbFullPath = new StringBuilder(target.getParameter(FileUploadServlet.CONFIG_ROOT));
sbFullPath.append(File.separator);
sbFullPath.append(target.getParameter(FileUploadServlet.CONFIG_CONTENT_FOLDER));
String fileRoot = sbFullPath.toString();
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Root location: " + sbFullPath.toString());
}
List createdFiles = new ArrayList();
this.addToList(fileRoot, "", createdFiles);
changeSet.setCreatedFiles(createdFiles);
try {
processor.doProcess(changeSet, parameters, target);
return HttpServletResponse.SC_OK;
} catch (PublishingException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Failed to reprocess target: " + target.getName(), e);
}
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
}
/**
* get a parameter map from the request
*
* @param request
* the request to get parameters from
* @return a map of parameters
*/
private Map getParameters(HttpServletRequest request) {
Enumeration paramNames = request.getParameterNames();
Map parameters = new HashMap();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String paramValue = request.getParameter(paramName);
parameters.put(paramName, paramValue);
}
return parameters;
}
/**
* add the current path to the change list
*
* @param fullPath
* the current path
* @param createdFiles
* a list to add the current path
*/
private void addToList(String fullPath, String sitePath, List createdFiles) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Processing " + fullPath);
}
URL resourceUrl = getResourceUrl(fullPath);
if (resourceUrl != null) {
String resourcePath = resourceUrl.getFile();
File file = new File(resourcePath);
if (file.isDirectory()) {
String[] children = file.list();
if (children != null && children.length > 0) {
for (String childName : children) {
addToList(fullPath + "/" + childName, sitePath + "/" + childName, createdFiles);
}
}
} else {
createdFiles.add(sitePath);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Added " + sitePath);
}
}
}
}
/**
* get the resource url for import
*
* @param filePath
* a resource path
* @return URL
*/
private URL getResourceUrl(String filePath) {
try {
return new File(filePath).toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException("Not able to find " + filePath);
}
}
/**
* Set the deployer password
* @param password
* a password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Set publishing target manager
* @param targetManager
* a target manager to set
*/
public void setTargetManager(TargetManager targetManager) { this.targetManager = targetManager; }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy