cancel
Showing results for 
Search instead for 
Did you mean: 

NullPointer@uploading a file from local system to alfresco

amit_wmw
Champ in-the-making
Champ in-the-making
Hello,

Below is the code I have written to upload a file from my local system to Alfresco repository..
####################################################

package org.alfresco.sample;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.BufferedInputStream;

import org.alfresco.jcr.api.JCRNodeRef;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.alfresco.repo.model.filefolder.FileFolderServiceImpl;
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.ContentWriter;
import org.alfresco.repo.content.RoutingContentService;



public class FirstJCRClient

{

public static void main(String[] args) throws Exception
{
// access the Alfresco JCR Repository (here it's via programmatic approach, but it could also be injected)
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
Repository repository = (Repository)context.getBean("JCR.Repository");

// login to workspace (here we rely on the default workspace defined by JCR.Repository bean)
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));

try
{
// first, access the company home
Node rootNode = session.getRootNode();
Node companyHome = rootNode.getNode("app:company_home");

FirstJCRClient t= new FirstJCRClient();
t.tCheckin();

}

catch (Exception e)
{
e.printStackTrace();
}
finally
{
session.logout();
System.exit(0);
}
}
public void tCheckin()
{
try{
NodeRef root= new NodeRef(".+://.+/." + "companyHome");

System.out.println("Inside tcheckin-1");
// NodeRef companyHome = getRepositoryContext().getCompanyHome();
File file=new File("C:/Test/Test.doc");
System.out.println("Inside tcheckin-2");
FileFolderService fileFolderService = new FileFolderServiceImpl();
System.out.println("Inside tcheckin-3");
FileInfo fileinfo = fileFolderService.create(root, file.getName(), ContentModel.TYPE_CONTENT);
System.out.println("Inside tcheckin-4");
NodeRef fileRef = fileinfo.getNodeRef();
System.out.println("Inside tcheckin-5");
InputStream contentStream=new BufferedInputStream(new FileInputStream(file), 1025);
ContentService contentService= new RoutingContentService();
ContentWriter writer = contentService.getWriter(fileRef, ContentModel.PROP_CONTENT, true);
writer.putContent(contentStream);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

####################################################################

And below is the error I am getting while executing..

##########################################################33
Error:
———-
10:54:01,245 WARN [remoting.rmi.RmiRegistryFactoryBean] Could not detect RMI registry - creating new one
10:54:04,945 WARN [alfresco.util.OpenOfficeConnectionTester] A connection to OpenOffice could not be established.
10:54:10,499 INFO [domain.schema.SchemaBootstrap] Schema managed by database dialect org.hibernate.dialect.MySQLInnoDBDialect.
10:54:11,157 INFO [domain.schema.SchemaBootstrap] No changes were made to the schema.
10:54:13,110 INFO [repo.admin.ConfigurationChecker] The Alfresco root data directory ('dir.root') is: C:\Alfresco\alf_data
10:54:13,182 INFO [admin.patch.PatchExecuter] Checking for patches to apply …
10:54:13,301 INFO [repo.module.ModuleServiceImpl] Found 0 module(s).
10:54:13,405 ERROR [alfresco.smb.protocol] Error accessing Win32 NetBIOS, check DLL is on the path
10:54:13,419 INFO [service.descriptor.DescriptorService] Alfresco JVM - v1.6.0_07-b06; maximum heap size 63.563MB
10:54:13,419 WARN [service.descriptor.DescriptorService] Alfresco JVM - WARNING - maximum heap size 63.563MB is less than recommended 512MB
10:54:13,419 INFO [service.descriptor.DescriptorService] Alfresco started (Community Network): Current version 2.1.0 (482) schema 64 - Installed version 2.1.0 (482) schema 64
Inside tcheckin-1
Inside tcheckin-2
Inside tcheckin-3
java.lang.NullPointerException
at org.alfresco.repo.model.filefolder.FileFolderServiceImpl.isFolder(FileFolderServiceImpl.java:255)
at org.alfresco.repo.model.filefolder.FileFolderServiceImpl.create(FileFolderServiceImpl.java:673)
at org.alfresco.sample.FirstJCRClient.tCheckin(FirstJCRClient.java:112)
at org.alfresco.sample.FirstJCRClient.main(FirstJCRClient.java:87)

##########################################################33
Its failing exactly at this line of my code "FileInfo fileinfo = fileFolderService.create(root, file.getName(), ContentModel.TYPE_CONTENT);".

Please if you can provide some suggestions here, will be of much appreciation!

Thanks
Amit.
22 REPLIES 22

sangireddi
Champ in-the-making
Champ in-the-making
Thank you Amit..

sangireddi
Champ in-the-making
Champ in-the-making
Hi Amit…
Thanks for your suggestions but Now i am not getting any error but i am unable to see that file in alfresco companyhome
i am getting fileRef.getId() but this id is not there alfresco alf_node table and i am getting contentUrl also…

Anything i missed ? ? ?


LOGIC :

   File file=new File("/home/chander/test.txt");
           System.out.println("Inside tcheckin-2 :");
           ServiceRegistry serviceRegistry = (ServiceRegistry) context.getBean("ServiceRegistry");
           NodeRef rootNodeRef = serviceRegistry.getNodeService().getRootNode(new StoreRef("workspace://SpacesStore"));

        // Find children of root node with association name "app:company_home" (must be one)
           System.out.println("rootNodeRef  :"+rootNodeRef.getId());
        List<NodeRef> nodes = (List<NodeRef>) serviceRegistry.getSearchService().selectNodes(rootNodeRef, "app:company_home", null,serviceRegistry.getNamespaceService(), false);

        if (nodes.size() == 0) {
        throw new AlfrescoRuntimeException("Unable to find store path: app:company_home");
        }
System.out.println("nodes size :"+nodes.size());
        NodeRef companyHome = nodes.get(0);
          
        System.out.println("companyHome :"+companyHome);
        FileFolderService fileFolderService = (FileFolderService) context.getBean("fileFolderService");
           System.out.println("file.getName() : "+file.getName());
           FileInfo fileinfo = fileFolderService.create(companyHome, file.getName(), ContentModel.TYPE_CONTENT);
           System.out.println("fileinfo.getName() : "+fileinfo.getName());
           NodeRef fileRef = fileinfo.getNodeRef();
           System.out.println(" fileRef.getId() :"+fileRef.getId());
           InputStream contentStream=new BufferedInputStream(new FileInputStream(file));         
           ContentService contentService = (ContentService) context.getBean("contentService");     
           System.out.println("Inside tcheckin-5 :"+contentService.toString());
           ContentWriter writer = contentService.getWriter(fileRef, ContentModel.PROP_CONTENT, true);        
           String contentUrl = writer.getContentUrl();           
            System.out.println("contentUrl :"+contentUrl);
          
           writer.putContent(contentStream);
           }          
           catch (Exception e)
           {
           e.printStackTrace();
           }


LOG:

Inside tcheckin-2 :
rootNodeRef  :46da2090-c366-4f0d-9db2-7d4eaf1781c2
nodes size :1
companyHome :workspace://SpacesStore/a433799e-aefc-4a9c-8667-da24f310b8b8
file.getName() : test.txt
fileinfo.getName() : test.txt
fileRef.getId() :3e72944d-8438-4cee-a762-d18300e9aa41
Inside tcheckin-5 Smiley Surprisedrg.alfresco.repo.content.RoutingContentService@2af4fb44
contentUrl :store://2008/10/29/10/53/b61702b4-b122-4e6a-a844-8fc6b5facc1d.bin

sangireddi
Champ in-the-making
Champ in-the-making
Hi All..
I am getting that file in alf_data/contentstore but i am unable to see in alfresco ui and alf_node table alfresco database alse
Thanks in advance.

amit_wmw
Champ in-the-making
Champ in-the-making
Hi Sangi

Save the session object at the end (after writer.putContent(contentStream);
###################
session.save();
###################

You will then be able to see your uploded file in alfresco ui(console)  :wink:

regards
Amit

sangireddi
Champ in-the-making
Champ in-the-making
Hi Amit..
Thank You Now i am able to see in alfresco also Smiley Very Happy

sangireddi
Champ in-the-making
Champ in-the-making
Hi Amit …
This is working fine but we need to restart out server and one more thing is it worked well for first file but if i try to upload again node created in repository but i cant view that one in alfresco web client eventhough i restarted my server.
Plz give me your suggestions again

Thank you in advance

sangireddi
Champ in-the-making
Champ in-the-making
This is how i am creating Session :

context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
       Repository repository = (Repository)context.getBean("JCR.Repository");
       // /home/chander/apps/eswar/alfrescoLabs3b/tomcat/webapps/alfresco/WEB-INF/classes/alfresco
       // login to workspace (here we rely on the default workspace defined by JCR.Repository bean)
        Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
      System.out.println("Session Id "+session.getUserID());

sangireddi
Champ in-the-making
Champ in-the-making
Hi All…
It is resolved

Thank you for suggestions

amit_wmw
Champ in-the-making
Champ in-the-making
Hi everybody,

Do you have any ideas on how we can "programatically" do versioning of these uploaded files? If so, please share…

Thanks in advance
Amit.

sangireddi
Champ in-the-making
Champ in-the-making
Hi All
i am unable to see uploaded files in alfresco even though i did session.save() after ContentWriter.putContent(InputStream).
Any suggestions ?

Thanks in advance