Count Number of Document Downloads in Alfresco

Oh yes, i still hear some voices behind fences crying for stupid solution or even bad requirement that can not be handled exactly using Alfresco. And yes, i say its possible to count the number of times a document was downloaded without changing alfresco code itself:

Therefore, we using a ProxyFactoryBean advices the “small” contentService to redefine the “huge” ContentService. Within the around aspect, we looking for the the method “getReader” to create a proxy again to marshalling a ContentReader implemented class. Right there, we looking for appropriated methods being executed in time responsible to rertrieve content itself (e.g. InputStream). Within this block,  we keep almost sure that the caller class provides “download mechanism” for a client( for instance: BaseDownloadContentServlet.processDownloadRequest).

Beandefinition:

<bean id="contentServiceInterceptor" class="de.dmc.alfresco.configuration.ContentServiceInterceptor"/>
<bean id="readerInterceptor" class="de.dmc.alfresco.configuration.ContentReaderInterceptor"/>
<bean id="ContentService" class="org.springframework.aop.framework.ProxyFactoryBean">

 <property name="proxyInterfaces">

    <value>org.alfresco.service.cmr.repository.ContentService</value>

  </property>

<property name="target">

    <ref bean="contentService"/>

  </property>

<property name="interceptorNames">

    <list>

      <idref bean="ContentService_transaction"/>

      <idref bean="AuditMethodInterceptor"/>

      <idref bean="exceptionTranslator"/>

      <idref bean="mlContentInterceptor"/>

      <idref bean="ContentService_security"/>

      <idref bean="ContentService_security"/>

      <idref bean="contentServiceInterceptor"/>

    </list>

  </property>

</bean>

Around Aspect:

    package de.dmc.alfresco.configuration;

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;

    import org.alfresco.service.cmr.repository.ContentReader;
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;

    public class ContentServiceInterceptor implements MethodInterceptor{

       class ContentReaderHandler implements InvocationHandler {

          private ContentReader cdr;

          public ContentReaderHandler(ContentReader cdr){
             this.cdr = cdr;
          }

          public Object invoke(Object proxy, Method method, Object[] args) {
             try {
                if(method.getName().startsWith("getContent")){
                   //here we begin to count
                }
                System.out.println(method.getName());
                return method.invoke(cdr, args);
             } catch (Exception e) {
                throw new RuntimeException(e);
             }
          }
       }

       @SuppressWarnings("unchecked")
       @Override
       public Object invoke(MethodInvocation invocation) throws Throwable {
          if(invocation.getMethod().getName().equals("getReader")){
             Class cdrClass = Class.forName("org.alfresco.service.cmr.repository.ContentReader");
             return Proxy.newProxyInstance(cdrClass.getClassLoader(),
                   new Class[] { cdrClass },
                   new ContentReaderHandler((ContentReader)invocation.proceed()));

          }
          return invocation.proceed();
       }

    }

Comments

One Response to “Count Number of Document Downloads in Alfresco”

  1. liao guowei on June 30th, 2009 10:07 am

    Can you send me the full download count example code? thank you!

Leave a Reply




Security Code: