cancel
Showing results for 
Search instead for 
Did you mean: 

Read content from repository from Spring bean

jerrycooper
Champ in-the-making
Champ in-the-making
Hello,
I have problem with  access to repository from Spring bean.
I will have one xml file in known space in Alfresco repository and I want to read it.

My code of bean:
i have setters for services. in …context.xml I'm putting into bean filefolderservice, nodeservice, authentication service and contentservice


private FileFolderService fileFolderService;
private NodeService nodeService;
public static StoreRef SPACES_STORE = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");

@Override
    public void afterPropertiesSet() throws Exception {
   
try {
  AuthenticationUtil.runAs(new RunAsWork<String>() {
      
      @SuppressWarnings("synthetic-access")
      public String doWork() throws Exception {
                  List<StoreRef> stores = nodeService.getStores(); //can't get nothing
                  return "";
      }
  }, "admin");
  } catch (Exception e) {…}

and result in log:

14:57:10,762 ERROR [cz.xxx.web.bean.DeadlineBean] java.lang.UnsupportedOperationException: Can not find {http://www.alfresco.org/model/system/1.0}base.ReadProperties
   at org.alfresco.repo.security.permissions.impl.model.PermissionModel.getPermissionReference(PermissionModel.java:1250)
   at org.alfresco.repo.security.permissions.impl.PermissionServiceImpl.getPermissionReference(PermissionServiceImpl.java:926)
   at org.alfresco.repo.security.permissions.impl.PermissionServiceImpl.hasPermission(PermissionServiceImpl.java:946)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy22.hasPermission(Unknown Source)
   at org.alfresco.repo.security.permissions.impl.acegi.ACLEntryVoter.vote(ACLEntryVoter.java:431)
   at net.sf.acegisecurity.vote.AffirmativeBased.decide(AffirmativeBased.java:69)
   at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:394)
   at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:77)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:43)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:135)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy10.getRootNode(Unknown Source)
   at cz.xxx.web.bean.DeadlineBean.afterPropertiesSet(DeadlineBean.java:99)

I tried method authenticationService.authenticate("admin", "passwordalfresco".toCharArray()); but error in the log was the same.

Know everybody how to easy access the file? If possible in Foundation API.
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
Please post more information.   What you have at the moment seems O.K.

In particular your spring context defining your bean and some description of how your bean is being called.    It looks like some sort of policy handler but I'd rather not have to guess what you are trying to do.

jerrycooper
Champ in-the-making
Champ in-the-making
For developing I'm still using 3.3.

Spring context:
faktury-model-context.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
    <!– Registration of new models –>   
    <bean id="extension.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap">
        <property name="models">
            <list>
                <value>alfresco/extension/fakturyModel.xml</value>
       </list>
        </property>
    </bean>
    
    <bean id="DeadlineBean" class="cz.xxx.web.bean.DeadlineBean">   
      <property name="contentService">
                    <ref bean="ContentService" />
                </property>
      <property name="authenticationService">
         <ref bean="AuthenticationService" />
      </property>
   
      <property name="nodeService">
         <ref bean="NodeService" />
      </property>
               <property name="fileFolderService">
         <ref bean="FileFolderService" />
      </property>
</bean>    
</beans>

Bean implements InitializingBean so afterPropertiesSet() method is called after setters.
package cz.xxx.web.bean;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.web.bean.BrowseBean;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;

public class DeadlineBean implements InitializingBean{
    private Map<String, String> deadline_firmy=new HashMap<String,String>();
    private FileFolderService fileFolderService;
    private NodeService nodeService;
    private AuthenticationService authenticationService;
    private ContentService contentService;
    private static Logger logger = Logger.getLogger(DeadlineBean.class);
    public static StoreRef SPACES_STORE = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
  
    public DeadlineBean() {
   
   deadline_firmy.put("TITLE1", "3"); //test code, in future
   deadline_firmy.put("TITLE2", "1");//the map will be filled from xml
   
   if (logger.isDebugEnabled()) {
     logger.debug("TEST start");
   }
   }
 
   
    public void setFileFolderService(FileFolderService fileFolderService) {
   this.fileFolderService = fileFolderService;
    }

    public void setNodeService(NodeService nodeService) {
   this.nodeService = nodeService;
    }
   
    public void setDeadline_firmy(Map<String,String> deadline_firmy) {
   this.deadline_firmy = deadline_firmy;
    }


    public Map<String,String> getDeadline_firmy() {
   return deadline_firmy;
    }
   
    public void setAuthenticationService(AuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }

    public void setContentService(ContentService contentService) {
        this.contentService = contentService;
    }

   
   @Override
    public void afterPropertiesSet() throws Exception {
   
   try {
       if (logger.isDebugEnabled()) {
          logger.debug(SPACES_STORE.toString());
      }
      
       AuthenticationUtil.runAs(new RunAsWork<String>() {      
      @SuppressWarnings("synthetic-access")
      public String doWork() throws Exception {

          List<StoreRef> stores = nodeService.getStores();
          return "";
      }
       }, "admin");
   
   } catch (Exception e) {
       if (logger.isDebugEnabled()) {
      logger.error("error" + e.toString());
      StringWriter sWriter = new StringWriter();
      e.printStackTrace(new PrintWriter(sWriter));
      
      
      logger.error(sWriter.getBuffer().toString());
       }

   }
   if (logger.isDebugEnabled()) {
       logger.debug("TEST after");
   }
   
    }
}

This bean is only test code, but after finishing it, it will be established for reading xml file from repository, parsing it and filling map deadline_firmy. The map is read by jsp, then image is displayed in some rows.
<m:deadlineImg id="col10-deadline" value="/alfresco/images/icons/button_red.png" deadlineTime='#{DeadlineBean.deadline_firmy[r.title]}' startDeadline="#{r.properties['sg_fa:deadline']}" style="padding-left: 3px"/>

Due to the calling of DeadlineBean during start of Alfresco, I think calling method in browse.jsp isn't problem now (the image(s) is displayed correctly).

jerrycooper
Champ in-the-making
Champ in-the-making
Anybody tried direct access to repository using only Spring bean?

srviktor
Champ in-the-making
Champ in-the-making
I'm trying to do the same, reading the content of an xml from a bean.
Have you found a solution?