Merged DEV to HEAD

51986: Propagate originating stack trace when InvalidNodeRefException passes through MT interceptor
          - CLOUD-1685, CLOUD-1827, CLOUD-1828


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@52000 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2013-07-02 16:00:07 +00:00
parent 81ba1715c4
commit d54e65080a

View File

@@ -1,429 +1,432 @@
/* /*
* Copyright (C) 2005-2013 Alfresco Software Limited. * Copyright (C) 2005-2013 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.alfresco.repo.tenant; package org.alfresco.repo.tenant;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.repository.AssociationRef; import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.InvalidNodeRefException; import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.Path; import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.util.EqualsHelper; import org.alfresco.util.EqualsHelper;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.aop.support.DelegatingIntroductionInterceptor; import org.springframework.aop.support.DelegatingIntroductionInterceptor;
/** /**
* Interceptor to translate Node * Interceptor to translate Node
* *
* @since 3.0 * @since 3.0
* @author Derek Hulley, janv * @author Derek Hulley, janv
*/ */
public class MultiTNodeServiceInterceptor extends DelegatingIntroductionInterceptor //implements NodeService public class MultiTNodeServiceInterceptor extends DelegatingIntroductionInterceptor //implements NodeService
{ {
private static final long serialVersionUID = -5462852271914961462L; private static final long serialVersionUID = -5462852271914961462L;
private static Log logger = LogFactory.getLog(MultiTNodeServiceInterceptor.class); private static Log logger = LogFactory.getLog(MultiTNodeServiceInterceptor.class);
private static ThreadLocal<Boolean> ignoreMT = new ThreadLocal<Boolean>(); private static ThreadLocal<Boolean> ignoreMT = new ThreadLocal<Boolean>();
private TenantService tenantService; private TenantService tenantService;
/** /**
* Change the filtering behaviour of this interceptor on the current thread. * Change the filtering behaviour of this interceptor on the current thread.
* Use this to switch off the filtering and pass references in and out as they * Use this to switch off the filtering and pass references in and out as they
* are found. * are found.
* *
* @param ignoreMT <tt>true</tt> if the current thread is able to handle * @param ignoreMT <tt>true</tt> if the current thread is able to handle
* MultiTenant-enabled references. * MultiTenant-enabled references.
* @return <tt>true</tt> if the current transaction is MT aware * @return <tt>true</tt> if the current transaction is MT aware
*/ */
public static boolean setIgnoreMT(boolean ignoreMT) public static boolean setIgnoreMT(boolean ignoreMT)
{ {
boolean wasIgnoreMT = isIgnoreMT(); boolean wasIgnoreMT = isIgnoreMT();
MultiTNodeServiceInterceptor.ignoreMT.set(Boolean.valueOf(ignoreMT)); MultiTNodeServiceInterceptor.ignoreMT.set(Boolean.valueOf(ignoreMT));
return wasIgnoreMT; return wasIgnoreMT;
} }
/** /**
* @return Returns <tt>true</tt> if the current thread has marked itself * @return Returns <tt>true</tt> if the current thread has marked itself
* as being able to handle MultiTenant references. * as being able to handle MultiTenant references.
*/ */
static public boolean isIgnoreMT() static public boolean isIgnoreMT()
{ {
if (ignoreMT.get() == null) if (ignoreMT.get() == null)
{ {
return false; return false;
} }
else else
{ {
return ignoreMT.get(); return ignoreMT.get();
} }
} }
public void setTenantService(TenantService tenantService) public void setTenantService(TenantService tenantService)
{ {
this.tenantService = tenantService; this.tenantService = tenantService;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable public Object invoke(MethodInvocation invocation) throws Throwable
{ {
// See if we can shortcut // See if we can shortcut
if (!AuthenticationUtil.isMtEnabled()) if (!AuthenticationUtil.isMtEnabled())
{ {
return invocation.proceed(); return invocation.proceed();
} }
String methodName = invocation.getMethod().getName(); String methodName = invocation.getMethod().getName();
if (logger.isTraceEnabled()) if (logger.isTraceEnabled())
{ {
logger.trace("Intercepting method " + methodName); logger.trace("Intercepting method " + methodName);
} }
Object[] args = invocation.getArguments(); Object[] args = invocation.getArguments();
// Convert each of the arguments to the underlying (full) reference. // Convert each of the arguments to the underlying (full) reference.
for (int i = 0; i < args.length; i++) for (int i = 0; i < args.length; i++)
{ {
Object arg = args[i]; Object arg = args[i];
Object newArg = convertInboundValue(arg); Object newArg = convertInboundValue(arg);
if (logger.isTraceEnabled()) if (logger.isTraceEnabled())
{ {
if (!EqualsHelper.nullSafeEquals(newArg, arg)) if (!EqualsHelper.nullSafeEquals(newArg, arg))
{ {
logger.trace( logger.trace(
"Argument converted: \n" + "Argument converted: \n" +
" Before: " + arg + "\n" + " Before: " + arg + "\n" +
" After: " + newArg); " After: " + newArg);
} }
} }
// Substitute the new value // Substitute the new value
args[i] = newArg; args[i] = newArg;
} }
// Make the call // Make the call
Object ret = null; Object ret = null;
try try
{ {
ret = invocation.proceed(); ret = invocation.proceed();
} }
catch (InvalidNodeRefException inre) catch (InvalidNodeRefException inre)
{ {
throw new InvalidNodeRefException(inre.getMessage(), tenantService.getBaseName(inre.getNodeRef())); throw new InvalidNodeRefException(
} "Failed invocation: " + invocation,
tenantService.getBaseName(inre.getNodeRef()),
if (methodName.equals("getProperty")) inre);
{ }
if (ret != null)
{ if (methodName.equals("getProperty"))
// Convert the outbound value {
QName qname = (QName)args[1]; if (ret != null)
if (qname.equals(ContentModel.PROP_STORE_IDENTIFIER)) {
{ // Convert the outbound value
String rawStoreId = (String)ret; QName qname = (QName)args[1];
ret = tenantService.getBaseName(rawStoreId); if (qname.equals(ContentModel.PROP_STORE_IDENTIFIER))
} {
else if (qname.equals(ContentModel.PROP_CREATOR) || qname.equals(ContentModel.PROP_MODIFIER) || qname.equals(ContentModel.PROP_OWNER)) String rawStoreId = (String)ret;
{ ret = tenantService.getBaseName(rawStoreId);
// ALF-6029 (eg. upgrade from V3.0.x - else need to patch all affected nodes) }
String rawUserId = (String)ret; else if (qname.equals(ContentModel.PROP_CREATOR) || qname.equals(ContentModel.PROP_MODIFIER) || qname.equals(ContentModel.PROP_OWNER))
if ("admin".equals(rawUserId)) {
{ // ALF-6029 (eg. upgrade from V3.0.x - else need to patch all affected nodes)
ret = tenantService.getDomainUser(rawUserId, tenantService.getCurrentUserDomain()); String rawUserId = (String)ret;
} if ("admin".equals(rawUserId))
} {
else ret = tenantService.getDomainUser(rawUserId, tenantService.getCurrentUserDomain());
{ }
ret = convertOutboundValue(ret); }
} else
} {
ret = convertOutboundValue(ret);
return ret; }
} }
else if (methodName.equals("getProperties"))
{ return ret;
if (ret != null) }
{ else if (methodName.equals("getProperties"))
// Convert the outbound values {
Map<QName, Serializable> rawValues = (Map<QName, Serializable>)ret; if (ret != null)
for (Map.Entry<QName, Serializable> rawValue : rawValues.entrySet()) {
{ // Convert the outbound values
QName qname = rawValue.getKey(); Map<QName, Serializable> rawValues = (Map<QName, Serializable>)ret;
Serializable value = rawValue.getValue(); for (Map.Entry<QName, Serializable> rawValue : rawValues.entrySet())
{
if (qname.equals(ContentModel.PROP_STORE_IDENTIFIER) && (value != null)) QName qname = rawValue.getKey();
{ Serializable value = rawValue.getValue();
value = tenantService.getBaseName((String)value);
} if (qname.equals(ContentModel.PROP_STORE_IDENTIFIER) && (value != null))
else if (qname.equals(ContentModel.PROP_CREATOR) || qname.equals(ContentModel.PROP_MODIFIER) || qname.equals(ContentModel.PROP_OWNER)) {
{ value = tenantService.getBaseName((String)value);
// ALF-6029 (eg. upgrade from V3.0.x - else need to patch all affected nodes) }
String rawUserId = (String)value; else if (qname.equals(ContentModel.PROP_CREATOR) || qname.equals(ContentModel.PROP_MODIFIER) || qname.equals(ContentModel.PROP_OWNER))
if ("admin".equals(rawUserId)) {
{ // ALF-6029 (eg. upgrade from V3.0.x - else need to patch all affected nodes)
value = tenantService.getDomainUser(rawUserId, tenantService.getCurrentUserDomain()); String rawUserId = (String)value;
} if ("admin".equals(rawUserId))
} {
value = tenantService.getDomainUser(rawUserId, tenantService.getCurrentUserDomain());
rawValues.put(qname, (Serializable)convertOutboundValue(value)); }
} }
ret = rawValues; rawValues.put(qname, (Serializable)convertOutboundValue(value));
} }
return ret; ret = rawValues;
} }
else if (methodName.equals("getStores"))
{ return ret;
if ((ret == null) || (! (ret instanceof List))) }
{ else if (methodName.equals("getStores"))
return null; {
} if ((ret == null) || (! (ret instanceof List)))
{
List<StoreRef> rawValues = (List<StoreRef>)ret; return null;
final List<StoreRef> convertedValues = new ArrayList<StoreRef>(rawValues.size()); }
for (StoreRef ref : rawValues) List<StoreRef> rawValues = (List<StoreRef>)ret;
{ final List<StoreRef> convertedValues = new ArrayList<StoreRef>(rawValues.size());
StoreRef storeRef = ref;
try for (StoreRef ref : rawValues)
{ {
if (tenantService.isEnabled() && (! storeRef.getProtocol().equals(StoreRef.PROTOCOL_AVM))) StoreRef storeRef = ref;
{ try
// MT: return tenant stores only (although for super System return all stores - as used by {
// ConfigurationChecker, IndexRecovery, IndexBackup etc) if (tenantService.isEnabled() && (! storeRef.getProtocol().equals(StoreRef.PROTOCOL_AVM)))
if (!AuthenticationUtil.isRunAsUserTheSystemUser()) {
{ // MT: return tenant stores only (although for super System return all stores - as used by
tenantService.checkDomain(storeRef.getIdentifier()); // ConfigurationChecker, IndexRecovery, IndexBackup etc)
storeRef = tenantService.getBaseName(storeRef); if (!AuthenticationUtil.isRunAsUserTheSystemUser())
} {
} tenantService.checkDomain(storeRef.getIdentifier());
storeRef = tenantService.getBaseName(storeRef);
convertedValues.add(storeRef); }
} }
catch (RuntimeException re)
{ convertedValues.add(storeRef);
// deliberately ignore - stores in different domain will not be listed }
} catch (RuntimeException re)
} {
// deliberately ignore - stores in different domain will not be listed
return convertedValues; }
} }
else
{ return convertedValues;
// Convert the outbound value }
ret = convertOutboundValue(ret); else
} {
// Convert the outbound value
// done ret = convertOutboundValue(ret);
return ret; }
}
// done
/** return ret;
* Convert outbound collection to spoofed (ie. without tenant prefix) values. }
*/
private Collection<Object> convertOutboundValues(Collection<Object> rawValues) /**
{ * Convert outbound collection to spoofed (ie. without tenant prefix) values.
/* */
* Return types can be Lists or Sets, so cater for both. private Collection<Object> convertOutboundValues(Collection<Object> rawValues)
*/ {
final Collection<Object> convertedValues; /*
if (rawValues instanceof List) * Return types can be Lists or Sets, so cater for both.
{ */
convertedValues = new ArrayList<Object>(rawValues.size()); final Collection<Object> convertedValues;
} if (rawValues instanceof List)
else if (rawValues instanceof Set) {
{ convertedValues = new ArrayList<Object>(rawValues.size());
convertedValues = new HashSet<Object>(rawValues.size(), 1.0F); }
} else if (rawValues instanceof Set)
else {
{ convertedValues = new HashSet<Object>(rawValues.size(), 1.0F);
throw new IllegalArgumentException("Interceptor can only handle List and Set return types."); }
} else
{
for (Object rawValue : rawValues) throw new IllegalArgumentException("Interceptor can only handle List and Set return types.");
{ }
Object convertedValue = convertOutboundValue(rawValue);
convertedValues.add(convertedValue); for (Object rawValue : rawValues)
} {
// Done Object convertedValue = convertOutboundValue(rawValue);
return convertedValues; convertedValues.add(convertedValue);
} }
// Done
/** return convertedValues;
* Convert outbound single value to spoofed (ie. without tenant prefix) value. }
*/
@SuppressWarnings("unchecked") /**
private Object convertOutboundValue(Object rawValue) * Convert outbound single value to spoofed (ie. without tenant prefix) value.
{ */
if (rawValue == null) @SuppressWarnings("unchecked")
{ private Object convertOutboundValue(Object rawValue)
return null; {
} if (rawValue == null)
{
Object value = rawValue; return null;
if (rawValue instanceof Collection) }
{
// Deal with collections Object value = rawValue;
value = convertOutboundValues((Collection<Object>)rawValue); if (rawValue instanceof Collection)
} {
else if (rawValue instanceof StoreRef) // Deal with collections
{ value = convertOutboundValues((Collection<Object>)rawValue);
StoreRef ref = (StoreRef) rawValue; }
value = tenantService.getBaseName(ref); else if (rawValue instanceof StoreRef)
} {
else if (rawValue instanceof NodeRef) StoreRef ref = (StoreRef) rawValue;
{ value = tenantService.getBaseName(ref);
NodeRef ref = (NodeRef) rawValue; }
value = tenantService.getBaseName(ref); else if (rawValue instanceof NodeRef)
} {
else if (rawValue instanceof ChildAssociationRef) NodeRef ref = (NodeRef) rawValue;
{ value = tenantService.getBaseName(ref);
ChildAssociationRef ref = (ChildAssociationRef) rawValue; }
value = tenantService.getBaseName(ref); else if (rawValue instanceof ChildAssociationRef)
} {
else if (rawValue instanceof AssociationRef) ChildAssociationRef ref = (ChildAssociationRef) rawValue;
{ value = tenantService.getBaseName(ref);
AssociationRef ref = (AssociationRef) rawValue; }
value = tenantService.getBaseName(ref); else if (rawValue instanceof AssociationRef)
} {
else if (rawValue instanceof Path) AssociationRef ref = (AssociationRef) rawValue;
{ value = tenantService.getBaseName(ref);
Path ref = (Path)rawValue; }
Path outboundPath = new Path(); else if (rawValue instanceof Path)
Iterator<Path.Element> itr = ref.iterator(); {
while (itr.hasNext()) Path ref = (Path)rawValue;
{ Path outboundPath = new Path();
Path.Element pathElement = itr.next(); Iterator<Path.Element> itr = ref.iterator();
if (pathElement instanceof Path.ChildAssocElement) while (itr.hasNext())
{ {
pathElement = new Path.ChildAssocElement(tenantService.getBaseName(((Path.ChildAssocElement)pathElement).getRef())); Path.Element pathElement = itr.next();
} if (pathElement instanceof Path.ChildAssocElement)
outboundPath.append(pathElement); {
} pathElement = new Path.ChildAssocElement(tenantService.getBaseName(((Path.ChildAssocElement)pathElement).getRef()));
value = outboundPath; }
} outboundPath.append(pathElement);
// Done }
return value; value = outboundPath;
} }
// Done
/** return value;
* Convert inbound collection to non-spoofed (ie. with tenant prefix) values. }
*/
private Collection<Object> convertInboundValues(Collection<Object> rawValues) /**
{ * Convert inbound collection to non-spoofed (ie. with tenant prefix) values.
/* */
* Return types can be Lists or Sets, so cater for both. private Collection<Object> convertInboundValues(Collection<Object> rawValues)
*/ {
final Collection<Object> convertedValues; /*
if (rawValues instanceof List) * Return types can be Lists or Sets, so cater for both.
{ */
convertedValues = new ArrayList<Object>(rawValues.size()); final Collection<Object> convertedValues;
} if (rawValues instanceof List)
else if (rawValues instanceof Set) {
{ convertedValues = new ArrayList<Object>(rawValues.size());
convertedValues = new HashSet<Object>(rawValues.size(), 1.0F); }
} else if (rawValues instanceof Set)
else {
{ convertedValues = new HashSet<Object>(rawValues.size(), 1.0F);
throw new IllegalArgumentException("Interceptor can only handle List and Set return types."); }
} else
{
for (Object rawValue : rawValues) throw new IllegalArgumentException("Interceptor can only handle List and Set return types.");
{ }
Object convertedValue = convertInboundValue(rawValue);
convertedValues.add(convertedValue); for (Object rawValue : rawValues)
} {
// Done Object convertedValue = convertInboundValue(rawValue);
return convertedValues; convertedValues.add(convertedValue);
} }
// Done
/** return convertedValues;
* Convert outbound single value to non-spoofed (ie. with tenant prefix) value. }
*/
@SuppressWarnings("unchecked") /**
private Object convertInboundValue(Object rawValue) * Convert outbound single value to non-spoofed (ie. with tenant prefix) value.
{ */
if (rawValue == null) @SuppressWarnings("unchecked")
{ private Object convertInboundValue(Object rawValue)
return null; {
} if (rawValue == null)
{
Object value = rawValue; return null;
if (rawValue instanceof StoreRef) }
{
StoreRef ref = (StoreRef) rawValue; Object value = rawValue;
value = tenantService.getName(ref); if (rawValue instanceof StoreRef)
} {
else if (rawValue instanceof NodeRef) StoreRef ref = (StoreRef) rawValue;
{ value = tenantService.getName(ref);
NodeRef ref = (NodeRef) rawValue; }
value = tenantService.getName(ref); else if (rawValue instanceof NodeRef)
} {
else if (rawValue instanceof ChildAssociationRef) NodeRef ref = (NodeRef) rawValue;
{ value = tenantService.getName(ref);
ChildAssociationRef ref = (ChildAssociationRef) rawValue; }
value = tenantService.getName(ref); else if (rawValue instanceof ChildAssociationRef)
} {
else if (rawValue instanceof AssociationRef) ChildAssociationRef ref = (ChildAssociationRef) rawValue;
{ value = tenantService.getName(ref);
AssociationRef ref = (AssociationRef) rawValue; }
value = tenantService.getName(ref); else if (rawValue instanceof AssociationRef)
} {
else if (rawValue instanceof Collection) AssociationRef ref = (AssociationRef) rawValue;
{ value = tenantService.getName(ref);
// Deal with collections }
value = convertInboundValues((Collection<Object>)rawValue); else if (rawValue instanceof Collection)
} {
else if (rawValue instanceof Path) // Deal with collections
{ value = convertInboundValues((Collection<Object>)rawValue);
Path ref = (Path)rawValue; }
Path inboundPath = new Path(); else if (rawValue instanceof Path)
Iterator<Path.Element> itr = ref.iterator(); {
while (itr.hasNext()) Path ref = (Path)rawValue;
{ Path inboundPath = new Path();
Path.Element pathElement = itr.next(); Iterator<Path.Element> itr = ref.iterator();
if (pathElement instanceof Path.ChildAssocElement) while (itr.hasNext())
{ {
pathElement = new Path.ChildAssocElement(tenantService.getName(((Path.ChildAssocElement)pathElement).getRef())); Path.Element pathElement = itr.next();
} if (pathElement instanceof Path.ChildAssocElement)
inboundPath.append(pathElement); {
} pathElement = new Path.ChildAssocElement(tenantService.getName(((Path.ChildAssocElement)pathElement).getRef()));
value = inboundPath; }
} inboundPath.append(pathElement);
// Done }
return value; value = inboundPath;
} }
} // Done
return value;
}
}