EDIT: Apparently I had to make an AMP and map it to Alfresco.war. But now I can't access the code I wrote, so I guess I'll have to use Webscripts and the like. Can someone provide an example of how to add a document to the Alfresco Repository with a Java backed webscript?
ORIGINAL QUESTION:
I've searched google-wide for a way to add a document to the Alfresco Repository with Java code. But I was not able to find a way that would work. I know how I can add a document to the Repository: use the NodeService. But the problem is that I cannot get an instance of the NodeService. I've tried to inject it with @Autowired, I've tried using a bean and I've tried using an ApplicationContext. None of the ways worked...
Way #1:
Injection in a class:
@Autowired
NodeService nodeService
Way #2:
In service-context.xml:
<bean id="somerandombeanname" class="management.FileManager" >
<property name="moduleId" value="${project.artifactId}" />
<property name="serviceRegistry" ref="ServiceRegistry" />
<property name="nodeService" ref="NodeService" />
<property name="transactionService" ref="TransactionService" />
<property name="contentService" ref="ContentService" />
</bean>
In the class I added a getter and setter for all the services and the serviceRegistry:
private NodeService nodeService;
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
Way #3:
appContext = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
serviceRegistry = (ServiceRegistry) appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
nodeService = serviceRegistry.getNodeService();
Ways #1 and #2 gave me a NullPointerException simply stating the NodeService is null. Way #3 gave a mile long StackTrace because of an AlfrescoRuntimeException because it failed to initialize a keystore:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssl.keyStore' defined in class path resource [alfresco/encryption-context.xml]: Invocation of init method failed; nested exception is org.alfresco.error.AlfrescoRuntimeException: 04180000 Failed to initialize keystore:
Location: E:/Alfresco/alf_data/keystore/ssl.keystore
Provider: null
Type: JCEKS
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at management.FileManager.<init>(FileManager.java:36)
at simple.start.main(start.java:25)
... 5 more
Caused by: org.alfresco.error.AlfrescoRuntimeException: 04180000 Failed to initialize keystore:
Location: E:/Alfresco/alf_data/keystore/ssl.keystore
Provider: null
Type: JCEKS
at org.alfresco.encryption.AlfrescoKeyStoreImpl.loadKeyStore(AlfrescoKeyStoreImpl.java:566)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.safeInit(AlfrescoKeyStoreImpl.java:537)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.init(AlfrescoKeyStoreImpl.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1639)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1580)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1510)
... 18 more
Caused by: java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(URLClassPath.java:494)
at sun.misc.URLClassPath.findResource(URLClassPath.java:176)
at java.net.URLClassLoader$2.run(URLClassLoader.java:551)
at java.net.URLClassLoader$2.run(URLClassLoader.java:549)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(URLClassLoader.java:548)
at java.lang.ClassLoader.getResource(ClassLoader.java:1147)
at org.springframework.core.io.ClassPathResource.resolveURL(ClassPathResource.java:147)
at org.springframework.core.io.ClassPathResource.exists(ClassPathResource.java:135)
at org.alfresco.encryption.SpringKeyResourceLoader.getSafeInputStream(SpringKeyResourceLoader.java:67)
at org.alfresco.encryption.SpringKeyResourceLoader.loadKeyMetaData(SpringKeyResourceLoader.java:133)
at org.alfresco.encryption.AlfrescoKeyStoreImpl$KeyInfoManager.loadKeyMetaData(AlfrescoKeyStoreImpl.java:1016)
at org.alfresco.encryption.AlfrescoKeyStoreImpl$KeyInfoManager.<init>(AlfrescoKeyStoreImpl.java:998)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.getKeyInfoManager(AlfrescoKeyStoreImpl.java:395)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.loadKeyStore(AlfrescoKeyStoreImpl.java:560)
... 27 more
Yes, the keystore exists and yes I have regenerated a new keystore.
I'm using Alfresco 5.0.1 and I'm working on the Repository side (not Share).
Luckily I have code for file upload thorugh JAVA backed webscript. Hope this help you too.To create java backed webscript see this
Create one class named
CustomFileUpload.java
and put following contentNOTE: In destination you can put
nodeRef
of folder in which you want to upload.Than create bean in context file name it whatever you want say
mycustom-context.xml
and put it inALFRESCO_HOME\tomcat\shared\classes\alfresco\extension
and contentFinally register this web script in alfresco by creating
customupload.post.desc.xml
. and putAnd last create view as we have declared JSON is default format so we need to create
customupload.post.json.ftl
And put these both files in
ALFRESCO_HOME\tomcat\shared\classes\alfresco\extension\templates\webscripts\com\upload
Now restart server and hit http://localhost:8080/alfresco/service/upload/fileupload and you will see file uploaded in folder(Whatever you have given). For Reference
Will not work in alfresco
You need to inject it with proper setter method. Your bean should be like below.
Your java class should contain following for injecting nodeService.
Finally! This is the solution for adding a file to the Repository:
CustomFileUpload.java:
This java class is placed in the following folder:
Where org\example is the same as the package org.example. Now we have a class, now we need the configuration files as I call them:
Which is located here
You will also need these:
Which are located here
Noticed the folder? It's the same like the package mentioned earlier.
Contents of customfileupload-context.xml:
Contents of customfileupload.post.desc.xml:
Contents of customfileupload.post.json.ftl:
This is it. With this you'll be able to upload a file to the Repository of Alfresco with Alfresco 5.