diff --git a/config/alfresco/action-services-context.xml b/config/alfresco/action-services-context.xml index 9cbf25a7b3..9d2685ce3d 100644 --- a/config/alfresco/action-services-context.xml +++ b/config/alfresco/action-services-context.xml @@ -503,6 +503,18 @@ false + + + + + + + + + + + false + diff --git a/config/alfresco/messages/action-config.properties b/config/alfresco/messages/action-config.properties index 6eebeed7e2..24afb97244 100644 --- a/config/alfresco/messages/action-config.properties +++ b/config/alfresco/messages/action-config.properties @@ -132,3 +132,8 @@ start-avm-workflow.workflow-name.display-label=The name of the WCM workflow to i copy-to-web-project.title=Copy item to a folder in a web project copy-to-web-project.description=This will copy the matched item to a folder in a web project. + +avm-link-validation.title=Performs a link validation check. +avm-link-validation.description=Performs a link validation check. +avm-link-validation.monitor.display-label=The status object used to determine progress + diff --git a/source/java/org/alfresco/linkvalidation/HrefValidationProgress.java b/source/java/org/alfresco/linkvalidation/HrefValidationProgress.java index ae61fc99f9..9bcf472092 100644 --- a/source/java/org/alfresco/linkvalidation/HrefValidationProgress.java +++ b/source/java/org/alfresco/linkvalidation/HrefValidationProgress.java @@ -26,6 +26,7 @@ *----------------------------------------------------------------------------*/ package org.alfresco.linkvalidation; +import java.io.Serializable; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicBoolean; @@ -46,8 +47,10 @@ import java.util.concurrent.atomic.AtomicBoolean; * Note: It is safest to instantiate a fresh HrefValidationProgress * object for every invocation of updateHrefInfo(). */ -public class HrefValidationProgress +public class HrefValidationProgress implements Serializable { + private static final long serialVersionUID = 3031274879702889688L; + AtomicInteger webapp_update_count_; AtomicInteger dir_update_count_; AtomicInteger file_update_count_; diff --git a/source/java/org/alfresco/linkvalidation/LinkValidationAction.java b/source/java/org/alfresco/linkvalidation/LinkValidationAction.java new file mode 100644 index 0000000000..422e8af1ab --- /dev/null +++ b/source/java/org/alfresco/linkvalidation/LinkValidationAction.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing + */ + +package org.alfresco.linkvalidation; + +import java.util.List; + +import org.alfresco.repo.action.ParameterDefinitionImpl; +import org.alfresco.repo.action.executer.ActionExecuterAbstractBase; +import org.alfresco.repo.avm.AVMNodeConverter; +import org.alfresco.repo.domain.PropertyValue; +import org.alfresco.sandbox.SandboxConstants; +import org.alfresco.service.cmr.action.Action; +import org.alfresco.service.cmr.action.ParameterDefinition; +import org.alfresco.service.cmr.avm.AVMService; +import org.alfresco.service.cmr.avmsync.AVMSyncException; +import org.alfresco.service.cmr.dictionary.DataTypeDefinition; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.util.Pair; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Performs a link validation check. + * + * @author gavinc + */ +public class LinkValidationAction extends ActionExecuterAbstractBase +{ + public static final String NAME = "avm-link-validation"; + + public static final String PARAM_MONITOR = "monitor"; + + private LinkValidationService linkValidationService; + private AVMService avmService; + + private static Log logger = LogFactory.getLog(LinkValidationAction.class); + + /** + * Sets the LinkValidationService instance to use + * + * @param service The LinkValidationService instance + */ + public void setLinkValidationService(LinkValidationService service) + { + this.linkValidationService = service; + } + + /** + * Sets the AVMService instance to use + * + * @param service The AVMService instance + */ + public void setAvmService(AVMService service) + { + this.avmService = service; + } + + @Override + protected void addParameterDefinitions(List paramList) + { + paramList.add(new ParameterDefinitionImpl(PARAM_MONITOR, DataTypeDefinition.ANY, false, + getParamDisplayLabel(PARAM_MONITOR))); + } + + @Override + protected void executeImpl(Action action, NodeRef actionedUponNodeRef) + { + // get the store to check the links for (is represented by the actioned upon node) + Pair avmVersionPath = AVMNodeConverter.ToAVMVersionPath(actionedUponNodeRef); + String path = avmVersionPath.getSecond(); + + // get store name and path parts. + String [] storePath = path.split(":"); + if (storePath.length != 2) + { + throw new AVMSyncException("Malformed source path: " + path); + } + + // extract the store name + String store = storePath[0]; + + // get the monitor object + HrefValidationProgress monitor = (HrefValidationProgress)action.getParameterValue(PARAM_MONITOR); + + if (logger.isDebugEnabled()) + logger.debug("Performing link validation check for store '" + store + "'"); + + LinkValidationReport report = null; + try + { + // firstly call updateHrefInfo to scan the whole store for broken links + // NOTE: currently this is NOT done incrementally + this.linkValidationService.updateHrefInfo(store, false, 10000, 30000, 5, monitor); + + // retrieve the manifest of all the broken links and files + List manifests = this.linkValidationService.getBrokenHrefManifests(store); + + // create the report object using the link check results + report = new LinkValidationReport(monitor, manifests); + } + catch (Throwable err) + { + if (report != null) + { + report.setError(err); + } + else + { + report = new LinkValidationReport(err); + } + + logger.error("Link Validation Error: ", err); + } + + // store the report as a store property on the store we ran the link check on + this.avmService.deleteStoreProperty(store, SandboxConstants.PROP_LINK_VALIDATION_REPORT); + this.avmService.setStoreProperty(store, SandboxConstants.PROP_LINK_VALIDATION_REPORT, + new PropertyValue(DataTypeDefinition.ANY, report)); + } +} diff --git a/source/java/org/alfresco/linkvalidation/LinkValidationReport.java b/source/java/org/alfresco/linkvalidation/LinkValidationReport.java new file mode 100755 index 0000000000..830b69cd60 --- /dev/null +++ b/source/java/org/alfresco/linkvalidation/LinkValidationReport.java @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing + */ +package org.alfresco.linkvalidation; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Object representing the result of a link validation action being executed. + * This object combines the results of the multiple service calls required to + * detect broken links and retrieve them. + *

+ * This object is then typically added to the store being checked as a store + * property. + *

+ * + * @author gavinc + */ +public class LinkValidationReport implements Serializable +{ + private int numberFilesChecked = 0; + private int numberLinksChecked = 0; + private int numberBrokenLinks = 0; + private boolean successful = true; + + private Throwable error; + private List brokenFiles; + private Map brokenLinksByFile; + + private static final long serialVersionUID = 7562964706845609991L; + + /** + * Constructs a link validation report from the results of a check + * + * @param status The object containing status i.e. file, link counts and the list + * of files containing broken links + * @param manifests The manifest of broken links and files + */ + public LinkValidationReport(HrefValidationProgress status, List manifests) + { + this.numberFilesChecked = status.getFileUpdateCount(); + this.numberLinksChecked = status.getUrlUpdateCount(); + + // create a list of broken files + this.brokenFiles = new ArrayList(manifests.size()); + + // create a map of broken links by file. + this.brokenLinksByFile = new HashMap(manifests.size()); + + // build the required list and maps + for (HrefManifest manifest : manifests) + { + String fileName = manifest.getFileName(); + this.brokenFiles.add(fileName); + this.brokenLinksByFile.put(fileName, manifest); + this.numberBrokenLinks = this.numberBrokenLinks + manifest.getHrefs().size(); + } + } + + /** + * Constructs a link validation report from an error that occurred + * + * @param error The error that caused the link check to fail + */ + public LinkValidationReport(Throwable error) + { + this.setError(error); + + this.brokenFiles = Collections.emptyList(); + this.brokenLinksByFile = Collections.emptyMap(); + } + + public int getNumberFilesChecked() + { + return this.numberFilesChecked; + } + + public int getNumberLinksChecked() + { + return this.numberLinksChecked; + } + + public int getNumberBrokenFiles() + { + return this.brokenFiles.size(); + } + + public int getNumberBrokenLinks() + { + return this.numberBrokenLinks; + } + + public List getFilesWithBrokenLinks() + { + return this.brokenFiles; + } + + public List getBrokenLinksForFile(String file) + { + List links = null; + + HrefManifest manifest = this.brokenLinksByFile.get(file); + if (manifest != null) + { + links = manifest.getHrefs(); + } + + return links; + } + + public boolean wasSuccessful() + { + return this.successful; + } + + public void setError(Throwable error) + { + this.error = error; + this.successful = false; + } + + public Throwable getError() + { + return this.error; + } +} + + + + + + diff --git a/source/java/org/alfresco/sandbox/SandboxConstants.java b/source/java/org/alfresco/sandbox/SandboxConstants.java index dcf3edbb65..345a65ca18 100644 --- a/source/java/org/alfresco/sandbox/SandboxConstants.java +++ b/source/java/org/alfresco/sandbox/SandboxConstants.java @@ -50,4 +50,5 @@ public class SandboxConstants public final static QName PROP_WEBSITE_NAME = QName.createQName(null, ".website.name"); public final static QName PROP_AUTHOR_NAME = QName.createQName(null, ".author.name"); public final static QName PROP_WEB_PROJECT_NODE_REF = QName.createQName(null, ".web_project.noderef"); + public final static QName PROP_LINK_VALIDATION_REPORT = QName.createQName(null, ".link.validation.report"); }