cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get document url?

alexyz78
Champ in-the-making
Champ in-the-making
Hi,
I'm trying to get the url of a document in this way:

   protected void executeImpl(Action action, NodeRef nodeRef) {
                ContentService contentService = serviceRegistry.getContentService();
                ContentReader contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); // line 26
      ContentData contentData = contentReader.getContentData();
      System.out.println("The document url is: " + contentData.getContentUrl());
   }

I always get the exception:

net.sf.acegisecurity.BadCredentialsException: Bad credentials presented
        at net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider.authenticate(DaoAuthenticationProvider.java:290)
        at net.sf.acegisecurity.providers.ProviderManager.doAuthentication(ProviderManager.java:159)
        at net.sf.acegisecurity.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:49)
        at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:372)
        at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:77)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
        at org.alfresco.repo.model.ml.MLContentInterceptor.invoke(MLContentInterceptor.java:91)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
        at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:49)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
        at org.alfresco.repo.audit.AuditComponentImpl.audit(AuditComponentImpl.java:275)
        at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:69)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at $Proxy28.getReader(Unknown Source)
        at org.alfresco.sample.PreviewAction.executeImpl(PreviewAction.java:26)
        at org.alfresco.repo.action.executer.ActionExecuterAbstractBase.execute(ActionExecuterAbstractBase.java:127)
        at org.alfresco.repo.action.ActionServiceImpl.directActionExecution(ActionServiceImpl.java:591)
        at org.alfresco.repo.action.ActionServiceImpl.executeActionImpl(ActionServiceImpl.java:528)

So I tried to add the following code before getting the ContentReader:

AuthenticationService authService = contentService.getAuthenticationService();
authService.authenticate("admin", "admin".toCharArray());

Now it's ok, but I'm still a little bit puzzled. Why do I need to be authenticated even if this code is fired from an action in an authenticated context?

Should I use authService.getCurrentUserName() and then check if authentication exists for the current user ?

Thanks a lot,
Alessandro
1 REPLY 1

kajan
Champ in-the-making
Champ in-the-making
You can get the URL as follows :

For that you need nodeService, siteInfo & sysAdminParams

final String siteName = siteInfo.getShortName();
final Map<QName, Serializable> contentProps = nodeService.getProperties(nodeRef);
final String shareUrl = UrlUtil.getShareUrl(sysAdminParams);
String workSpace = (String) contentProps.get(ContentModel.PROP_STORE_PROTOCOL);
String spacesStore = (String) contentProps.get(ContentModel.PROP_STORE_IDENTIFIER);
String uiid = (String) contentProps.get(ContentModel.PROP_NODE_UUID);
String documentUrl = shareUrl + "/page/site/" + siteName + "/document-details?nodeRef=" + workSpace + "://" + spacesStore + "/" + uiid;

Cheers