/* * Copyright (C) 2005-2009 Alfresco Software Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General protected 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 protected License for more details. * You should have received a copy of the GNU General protected 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.wcm.util; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.alfresco.config.JNDIConstants; import org.alfresco.mbeans.VirtServerRegistry; import org.alfresco.model.WCMAppModel; import org.alfresco.repo.avm.actions.AVMDeployWebsiteAction; import org.alfresco.repo.avm.util.AVMUtil; import org.alfresco.repo.domain.PropertyValue; import org.alfresco.service.cmr.avm.AVMNotFoundException; import org.alfresco.service.cmr.avm.AVMService; import org.alfresco.service.cmr.dictionary.DataTypeDefinition; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.security.PermissionService; import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.RegexQNamePattern; import org.alfresco.util.FileNameValidator; import org.alfresco.util.ParameterCheck; import org.alfresco.util.VirtServerUtils; import org.alfresco.wcm.sandbox.SandboxConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Helper methods and constants related to WCM directories, paths and store name manipulation. * * @author Ariel Backenroth, Kevin Roast, janv */ public class WCMUtil extends AVMUtil { private static Log logger = LogFactory.getLog(WCMUtil.class); /** * Extracts the sandbox store id from the avm path * * @param avmPath an absolute avm path * * @return the sandbox store id */ public static String getSandboxStoreId(final String avmPath) { return getStoreName(avmPath); } /** * Extracts the web project store id from the (sandbox) store name *
* For example, if the (sandbox) store name is: teststore--admin then the web project store id is: teststore *
* Note: Although the staging sandbox store name is currently equivalent to the web project store id, it should
* be derived using 'buildStagingStoreName'.
*
* @param storeName the sandbox store id
*
* @return the web project store id
*/
public static String getWebProjectStoreId(final String storeName)
{
final int index = storeName.indexOf(WCMUtil.STORE_SEPARATOR);
return (index == -1
? storeName
: storeName.substring(0, index));
}
/**
* Extracts the web project store id from the avm path
*
* For example, if the avm path is: teststore--admin:/www/ROOT then the web project id is: teststore
*
* @param avmPath an absolute avm path
*
* @return the web project store id.
*/
public static String getWebProjectStoreIdFromPath(final String avmPath)
{
return getWebProjectStoreId(getStoreName(avmPath));
}
/**
* Indicates whether the store name describes a preview store.
*
* @param storeName the store name
*
* @return true if the store is a preview store, false otherwise.
*/
protected static boolean isPreviewStore(final String storeName)
{
return ((storeName != null) && (storeName.endsWith(WCMUtil.STORE_SEPARATOR + WCMUtil.STORE_PREVIEW)));
}
/**
* http://wiki.alfresco.com/wiki/WCM_Deployment_Features#Debugging_.26_Testing
*
* Examples of locally deployed store names for web project "MyWebProj" are:
*
* MyWebProjlive
* MyWebProj--adminlive
*
* Note: if web project "MyWebProjlive" is pre-created then should be possible to browse staging:
*
* http://wiki.alfresco.com/wiki/WCM_Deployment_Features#Deployed_Runtime
*
* @param storeName
* @return
*/
protected static boolean isLocalhostDeployedStore(String wpStoreId, String storeName)
{
return ((storeName.startsWith(wpStoreId)) &&
(storeName.endsWith(AVMDeployWebsiteAction.LIVE_SUFFIX)) &&
(! wpStoreId.endsWith(AVMDeployWebsiteAction.LIVE_SUFFIX)));
}
/**
* Indicates whether the store name describes a workflow store.
*
* @param storeName the store name
*
* @return true if the store is a workflow store, false otherwise.
*/
protected static boolean isWorkflowStore(String storeName)
{
if (WCMUtil.isPreviewStore(storeName))
{
storeName = WCMUtil.getCorrespondingMainStoreName(storeName);
}
return ((storeName != null) && (storeName.indexOf(STORE_SEPARATOR + STORE_WORKFLOW) != -1));
}
/**
* Indicates whether the store name describes a user store.
*
* @param storeName the store name
*
* @return true if the store is a user store, false otherwise.
*/
public static boolean isUserStore(String storeName)
{
if (WCMUtil.isPreviewStore(storeName))
{
storeName = WCMUtil.getCorrespondingMainStoreName(storeName);
}
return ((storeName != null) && (storeName.indexOf(WCMUtil.STORE_SEPARATOR) != -1));
}
/**
* Indicates whether the store name describes a staging store.
*
* @param storeName the store name
*
* @return true if the store is a main store, false otherwise.
*/
public static boolean isStagingStore(String storeName)
{
return ((storeName != null) && (storeName.indexOf(WCMUtil.STORE_SEPARATOR) == -1));
}
/**
* Extracts the username from the store name.
*
* @param storeName the store name
*
* @return the username associated or null if this is a staging store.
*/
public static String getUserName(String storeName)
{
if (WCMUtil.isPreviewStore(storeName))
{
storeName = WCMUtil.getCorrespondingMainStoreName(storeName);
}
final int index = storeName.indexOf(WCMUtil.STORE_SEPARATOR);
return (index == -1
? null
: storeName.substring(index + WCMUtil.STORE_SEPARATOR.length()));
}
/**
* Extracts the workflow id
*
* @param storeName
* @return
*/
public static String getWorkflowId(String storeName)
{
if (WCMUtil.isPreviewStore(storeName))
{
storeName = WCMUtil.getCorrespondingMainStoreName(storeName);
}
final int index = storeName.indexOf(STORE_SEPARATOR + STORE_WORKFLOW);
return (index == -1
? null
: storeName.substring(index + WCMUtil.STORE_SEPARATOR.length()));
}
/**
* Returns the corresponding main store name if this is a preview store name.
*
* @param storeName the preview store name.
*
* @return the corresponding main store name.
*
* @exception IllegalArgumentException if this is not a preview store name.
*/
protected static String getCorrespondingMainStoreName(final String storeName)
{
if (!WCMUtil.isPreviewStore(storeName))
{
throw new IllegalArgumentException("store " + storeName + " is not a preview store");
}
return storeName.substring(0,
(storeName.length() -
(WCMUtil.STORE_SEPARATOR + WCMUtil.STORE_PREVIEW).length()));
}
/**
* Returns the corresponding preview store name if this is a main store name.
*
* @param storeName the main store name.
*
* @return the corresponding preview store name.
*
* @exception IllegalArgumentException if this is not a main store name.
*/
protected static String getCorrespondingPreviewStoreName(final String storeName)
{
if (WCMUtil.isPreviewStore(storeName))
{
throw new IllegalArgumentException("store " + storeName + " is already a preview store");
}
return storeName + WCMUtil.STORE_SEPARATOR + WCMUtil.STORE_PREVIEW;
}
/**
* Returns the corresponding path in the main store name if this is a path in
* a preview store.
*
* @param avmPath an avm path within the main store.
*
* @return the corresponding path within the preview store.
*
* @exception IllegalArgumentException if this is not a path within the preview store.
*/
protected static String getCorrespondingPathInMainStore(final String avmPath)
{
String storeName = getStoreName(avmPath);
storeName = WCMUtil.getCorrespondingMainStoreName(storeName);
return WCMUtil.getCorrespondingPath(avmPath, storeName);
}
/**
* Returns the corresponding path in the preview store name if this is a path in
* a main store.
*
* @param avmPath an avm path within the main store.
*
* @return the corresponding path within the preview store.
*
* @exception IllegalArgumentException if this is not a path within the preview store.
*/
protected static String getCorrespondingPathInPreviewStore(final String avmPath)
{
String storeName = getStoreName(avmPath);
storeName = WCMUtil.getCorrespondingPreviewStoreName(storeName);
return WCMUtil.getCorrespondingPath(avmPath, storeName);
}
/**
* Returns the corresponding path in the store provided.
*
* @param avmPath an avm path
* @param otherStore the other store name to return the corresponding path for
*
* @return the corresponding path within the supplied store
*/
public static String getCorrespondingPath(final String avmPath, final String otherStoreName)
{
return (buildAVMPath(otherStoreName, WCMUtil.getStoreRelativePath(avmPath)));
}
/**
* Returns the main staging store name for the specified web project
*
* @param wpStoreId web project store id to build staging store name for
* @return String main staging store name for the specified web project store id
*/
public static String buildStagingStoreName(final String wpStoreId)
{
ParameterCheck.mandatoryString("wpStoreId", wpStoreId);
return wpStoreId;
}
/**
* Returns the preview store name for the specified store id.
*
* @param storeId store id to build preview store name for
*
* @return preview store name for the specified store id
*/
protected static String buildStagingPreviewStoreName(final String storeId)
{
return (WCMUtil.buildStagingStoreName(storeId) + WCMUtil.STORE_SEPARATOR +
WCMUtil.STORE_PREVIEW);
}
/**
* Returns the user's main store name for a specific username
*
* @param storeId store id to build user store name for
* @param username of the user to build store name for
*
* @return the main store for the specified user and store id
*/
public static String buildUserMainStoreName(final String storeId,
final String userName)
{
ParameterCheck.mandatoryString("userName", userName);
String fixedUserName = FileNameValidator.getValidFileName(userName);
return (WCMUtil.buildStagingStoreName(storeId) + WCMUtil.STORE_SEPARATOR +
fixedUserName);
}
/**
* Returns the preview store name for a specific username.
*
* @param storeId store id to build user preview store name for
* @param username of the user to build preview store name for
*
* @return the preview store for the specified user and store id
*/
protected static String buildUserPreviewStoreName(final String storeId,
final String username)
{
return (WCMUtil.buildUserMainStoreName(storeId, username) + WCMUtil.STORE_SEPARATOR +
WCMUtil.STORE_PREVIEW);
}
/**
* Returns the store name for a specific workflow Id.
*
* @param storeId store id to build workflow store name for
* @param workflowId of the user to build workflow store name for
*
* @return the store for the specified workflow and store ids
*/
protected static String buildWorkflowMainStoreName(final String storeId,
final String workflowId)
{
ParameterCheck.mandatoryString("workflowId", workflowId);
return (WCMUtil.buildStagingStoreName(storeId) + WCMUtil.STORE_SEPARATOR +
workflowId);
}
/**
* Returns the preview store name for a specific workflow Id.
*
* @param storeId store id to build preview workflow store name for
* @param workflowId of the user to build preview workflow store name for
*
* @return the store for the specified preview workflow and store ids
*/
protected static String buildWorkflowPreviewStoreName(final String storeId,
final String workflowId)
{
return (WCMUtil.buildWorkflowMainStoreName(storeId, workflowId) +
WCMUtil.STORE_SEPARATOR + WCMUtil.STORE_PREVIEW);
}
/**
* Returns the root path for the specified store name
*
* eg. mystore -> mystore:/www
*
* @param storeName store to build root path for
*
* @return root path for the specified store name
*/
public static String buildStoreRootPath(final String storeName)
{
ParameterCheck.mandatoryString("storeName", storeName);
return buildAVMPath(storeName, AVM_PATH_SEPARATOR_CHAR + JNDIConstants.DIR_DEFAULT_WWW);
}
/**
* Returns the root path for the specified sandbox name
*
* * eg. mystore -> mystore:/www/avm_webapps
*
* @param storeName store to build root sandbox path for
*
* @return root sandbox path for the specified store name
*/
public static String buildSandboxRootPath(final String storeName)
{
ParameterCheck.mandatoryString("storeName", storeName);
return buildAVMPath(storeName, JNDIConstants.DIR_DEFAULT_WWW_APPBASE);
}
/**
* Returns the root webapp path for the specified store and webapp name
*
* @param storeName store to build root webapp path for
* @param webapp webapp folder name
*
* @return the root webapp path for the specified store and webapp name
*/
public static String buildStoreWebappPath(final String storeName, String webApp)
{
ParameterCheck.mandatoryString("webApp", webApp);
return WCMUtil.buildSandboxRootPath(storeName) + AVM_PATH_SEPARATOR_CHAR + webApp;
}
public static String lookupStoreDNS(AVMService avmService, String store)
{
ParameterCheck.mandatoryString("store", store);
final Map