cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing the node ref in custom java class

srik
Champ in-the-making
Champ in-the-making
Hi Team

I have the requirement like to download the file from alfresco repository to custom java class, so far I am able to get the node ref in my java class .By using the node ref I am not able to get the file downloaded in my java class .Can you please provide me the sample code to get the file in my custom java class.

so far this the code implemented .

I get the following error like :   error while getting the repository service. and also error staring the session.



public void convertPDF2SVG(String nodeRef){

                System.out.println("Node Reference: " + nodeRef + "\n\n");

                String fileName = "PDFTron PDF2SVG User Manual.pdf";
                WebServiceFactory.setEndpointAddress("http://localhost:8080/alfresco/api");
                System.out.println("After Endpoint address");

                /*Start the session/
                try {
                        System.out.println("Inside Try to start session");
                        AuthenticationUtils.startSession("admin", "admin");
                        System.out.println("Session started successfully");
                } catch (AuthenticationFault e1) {
                        System.out.println("Unable to start Session: "+e1.getMessage());
                } catch (Exception e){
                        System.out.println("Inside Catch: " + e.getMessage());
                }

                /** Make sure Service Folder has been created,or download files will be failure */
                boolean Success = true;
                Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
                String parentSpace = "/app:company_home/cm:bneps";
                Reference SAMPLE_FOLDER = new Reference(storeRef, null, parentSpace);

                try
                {
                        WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[]{SAMPLE_FOLDER}, storeRef, null));
                        System.out.println("after getting repository service");
                }
                catch (Exception exception)
                {
                        System.out.println("Error while getting repository service");
                        Success=false;
                }

                /**Download files,if exception then "Success = False" */
                try
                {
                        if(Success)
                        {
                                System.out.println("success in try block");
                                ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
                                Reference contentReference = new Reference(storeRef,null,parentSpace+"/cm:"+fileName);
                                Content[] readResult = contentService.read(new Predicate(new Reference[]{contentReference}, storeRef, null), Constants.PROP_CONTENT);
                                for(Content content: readResult)
                                {
                                        String[] splitedUrl = content.getUrl().split("/");
                                        if(splitedUrl[splitedUrl.length-1].equals(fileName))
                                        {

                                                InputStream in=ContentUtils.getContentAsInputStream(content);


thanks
Srikanth S
3 REPLIES 3

romschn
Star Collaborator
Star Collaborator
One alternate option is - you can simply use the out-of-the-box api/node webscript to stream the content. You can use HttpClient to invoke the webscripts.
Get the authentication ticket using out-of-the-box login webscript -
http://localhost:8080/alfresco/service/api/login?u=admin&pw=admin
and then use the ticket to invoke
http://localhost:8080/alfresco/service/api/node/content/workspace/SpacesStore/noderef?alf_ticket=alf...


Hope this helps.

kaynezhang
World-Class Innovator
World-Class Innovator
You should replace whitespace in your path( filename) with _x0020_
try with

PDFTron_x0020_PDF2SVG_x0020_User_x0020_Manual.pdf

By the way why not use opencmis api?

gonial
Champ in-the-making
Champ in-the-making
I want use Opencmis for get a element in alfresco.
How i can use Cmis in this example?