cancel
Showing results for 
Search instead for 
Did you mean: 

Changing file name on download

mmrvelj
Confirmed Champ
Confirmed Champ
Hi all,

In Community Alfresco 4.x share I would like to have a button next to the document, which would enable download of the document. Difference from normal download button is that I would like to be able to change the file name of the file which is being downloaded. An example would be that versioned file called my-document.docx gets renamed to my-document-v1.2-DRAFT.docx.

I am new to Alfresco, so could you give me some pointers how to achieve this?
3 REPLIES 3

hunjet
Champ in-the-making
Champ in-the-making
Hi mmrvelj,

To add new button you'll need to customize one (or more) of the existing share components (depending on where you want to add the new button). Check David's blog for info about share customization (http://blogs.alfresco.com/wp/ddraper/).

For changing document name on download you could create custom web script where you can put "Content-disposition" header in response containing filename. As a filename you can put your custom string such as "my-document-v1.2-DRAFT.docx".

Hope this help,
Blaž

mmrvelj
Confirmed Champ
Confirmed Champ
Hi!

Thanks for hints. I tried to follow your suggestion, but I made mistake somewhere. I wrote test code:

package com.test.alfresco;

import java.io.IOException;

import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;

public class DownloadScript extends AbstractWebScript {

   @Override
   public void execute(WebScriptRequest req, WebScriptResponse res)
         throws IOException {
      byte[] content= "%PDF-1.2 ".getBytes();
      res.setContentType("application/pdf");
      res.setContentEncoding(null);
      res.addHeader("Content-Disposition", "inline;filename=F1F002340.PDF");
      res.addHeader("Content-Length", Integer.toString(content.length));
      res.addHeader("Pragma", "no-cache");
      res.getOutputStream().write(content); // Here I write document
   }

}

By following some examples, I packaged this to amp file, and put it to $ALFRESCO_HOME/amps.

Bean definition is:


   <bean id="webscript.com.test.alfresco.downloadscript.get"
      class="com.test.alfresco.DownloadScript" parent="webscript" />

But when I try to start alfresco, I get:


2012-06-05 17:45:54,505  ERROR [web.context.ContextLoader] [main] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webscript.com.test.alfresco.downloadscript.get' defined in class path resource [alfresco/web-scripts-application-context.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/alfresco/util/StringBuilderWriter
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
        at org.alfresco.web.app.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:63)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:563)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:498)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
        at org.apache.catalina.core.StandardService.start(StandardService.java:519)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
        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.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.NoClassDefFoundError: org/alfresco/util/StringBuilderWriter
        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
        at java.lang.Class.getConstructor0(Class.java:2699)
        at java.lang.Class.getDeclaredConstructor(Class.java:1985)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:65)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
        … 38 more
Caused by: java.lang.ClassNotFoundException: org.alfresco.util.StringBuilderWriter
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
        … 44 more
Jun 5, 2012 5:45:59 PM org.apache.catalina.core.StandardContext start

Do you know what did I do wrong?

mmrvelj
Confirmed Champ
Confirmed Champ
Small update.

Probably something was messed up in alfresco, since after I reverted virtual machine that I am using for testing and reinstalled my code, it worked Smiley Happy.

Now I would like to be able to link my web script to button in share.  I know how to create new button on a document by modifying share-config-custom.xml file, but I am not sure how to "tell the button to execute web script" and how to pass node reference to my web script?

Do you have any suggestions how to do this?