Lire un contenu en utilisant les services web

cancel
Showing results for 
Search instead for 
Did you mean: 
debutant_alfres
Member II

Lire un contenu en utilisant les services web

bonjour tlm,

-j'ai ajouter un nouveau contenu (fichier xml) dans le "Company_home" et
je veux écrire un programme simple en java  qui utilise les services web qui permet de lire le contenu de ce fichier
j'espere que j'ai bien expliqué ma problematique
est il possible que quelqun puisse m'aider a realiser ca, et si c'est possible de m'ecrire le code

si vous m'aidez, je serais reconaissant

Cordialement
8 Replies
cleseach
Active Member II

Re: Lire un contenu en utilisant les services web

Bonjour,

De mémoire, vous trouverez tout le code nécessaire dans les exemples du SDK.

Cordialement,
Charles Le Seac'h
debutant_alfres
Member II

Re: Lire un contenu en utilisant les services web

merci pour la reponse,

Entendu,
j'ai lu les exemples dans le sdk, il y'a une classe qui s'appele ContentReadAndWrite.java

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.sample.webservice;

import java.io.InputStream;

import org.alfresco.webservice.content.Content;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.ContentFormat;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.ContentUtils;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;

/**
* Web service sample 3
* <p>
* This web service sample shows how new content can be added to the repository and how content
* can be read and updated via the web service API.
*
* @author Roy Wetherall
*/
public class ContentReadAndWrite extends SamplesBase
{
    /** Content strings used in the sample */
    private static final String INITIAL_CONTENT = "This is some new content that I am adding to the repository";
    private static final String UPDATED_CONTENT = "This is the updated content";

    /** The type of the association we are creating to the new content */
    private static final String ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains";
   
    /**
     * Main function
     */
    public static void main(String[] args) throws Exception
    {
        // Start the session
        AuthenticationUtils.startSession(USERNAME, PASSWORD);
       
        try
        {        
            // Make sure smaple data has been created
            createSampleData();
           
            // Get the content service
            ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();       
           
            // Create new content in the respository
            Reference newContentReference = createNewContent(contentService, "sampleThreeFileOne.txt", INITIAL_CONTENT);
           
            // Read the newly added content from the respository
            Content[] readResult = contentService.read(
                                                new Predicate(new Reference[]{newContentReference}, STORE, null),
                                                Constants.PROP_CONTENT);
            Content content = readResult[0];
           
            // Get the content from the download servlet using the URL and display it
            System.out.println("The newly added content is:");
            System.out.println(ContentUtils.getContentAsString(content));
           
            // Update the content with something new
            contentService.write(newContentReference, Constants.PROP_CONTENT, UPDATED_CONTENT.getBytes(), null);
           
            // Now output the updated content
            Content[] readResult2 = contentService.read(
                                                new Predicate(new Reference[]{newContentReference}, STORE, null),
                                                Constants.PROP_CONTENT);
            Content content2 = readResult2[0];
            System.out.println("The updated content is:");
            System.out.println(ContentUtils.getContentAsString(content2));
           
            // Upload binary content into the repository
            Reference reference = Query1.executeSearch();
            ParentReference parentReference = new ParentReference(reference.getStore(), reference.getUuid(), null, ASSOC_CONTAINS, "{" + Constants.NAMESPACE_CONTENT_MODEL + "}test.jpg");
           
            // Create the content
            NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, "test.jpg")};
            CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_CONTENT, properties);
            CML cml = new CML();
            cml.setCreate(new CMLCreate[]{create});
            UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);    
           
            // Get the created node and create the format
            Reference newContentNode = result[0].getDestination();             
            ContentFormat format = new ContentFormat("image/jpeg", "UTF-8"); 
           
            // Open the file and convert to byte array
            InputStream viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg");
            byte[] bytes = ContentUtils.convertToByteArray(viewStream);
           
            // Write the content
            WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, bytes, format);
       
        }
        finally
        {
            // End the session
            AuthenticationUtils.endSession();
        }
    }
   
    /**
     * Helper method to create new content.
     * 
     * @param contentService    the content web service
     * @param content           the content itself
     * @return                  a reference to the created content node
     * @throws Exception       
     */
    public static Reference createNewContent(ContentServiceSoapBindingStub contentService, String name, String contentString)
        throws Exception
    {
        // Update name
        name = System.currentTimeMillis() + "_" + name;
       
        // Create a parent reference, this contains information about the association we are createing to the new content and the
        // parent of the new content (the space retrived from the search)
        ParentReference parentReference = new ParentReference(STORE, null, "/app:company_home/cm:sample_folder", ASSOC_CONTAINS,
                "{" + Constants.NAMESPACE_CONTENT_MODEL + "}" + name);
       
        // Define the content format for the content we are adding
        ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");
       
        NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, name)};
        CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_CONTENT, properties);
        CML cml = new CML();
        cml.setCreate(new CMLCreate[]{create});
        UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);    
       
        Reference newContentNode = result[0].getDestination();
        Content content = contentService.write(newContentNode, Constants.PROP_CONTENT, contentString.getBytes(), contentFormat);
       
        // Get a reference to the newly created content
        return content.getNode();
    }    
}

dans cette exemple il y a une creation d'un noeud avant sa lecture
 Content[] readResult = contentService.read(
                                                new Predicate(new Reference[]{newContentReference}, STORE, null),
                                                Constants.PROP_CONTENT);

moi ce que je veux c'est recuperer un noeud qui existe deja dans company_home

cordialement
cleseach
Active Member II

Re: Lire un contenu en utilisant les services web

Bonjour,

Il faut souvent agréger différents exemples pour obtenir le résultat que l'on souhaite ;-)
Explorez donc les exemples du SDK, et vous trouverez comment faire une requête pour retrouver un fichier.

Cordialement,
Charles Le Seac'h
debutant_alfres
Member II

Re: Lire un contenu en utilisant les services web

merci Cleseach,

je vais fouiller dans les examples en esperant trouver la solution rapidement

cordialemnt
debutant_alfres
Member II

Re: Lire un contenu en utilisant les services web

bonjour,

j'ai ecrit une methode qui permet de lire un contenu d'un noeud en utilisant les web service proposés par alfresco
voici le code

public static void readContent(String id)
        throws Exception
    {
    AuthenticationUtils.startSession(USERNAME, PASSWORD);
   
    try
    {        
       
        // Get the content service
        ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();       
       
        // Create new content in the respository
        Reference newContentReference = new Reference(STORE, id, null);;
       
        // Read the newly added content from the respository
        Content[] readResult = contentService.read(
                                            new Predicate(new Reference[]{newContentReference}, STORE, null),
                                            Constants.PROP_CONTENT);
        Content content = readResult[0];
       
        // Get the content from the download servlet using the URL and display it
        System.out.println("The content is:");
        System.out.println(ContentUtils.getContentAsString(content));
    }
    finally
    {
        // End the session
        AuthenticationUtils.endSession();
    }
    }

j'appele cette methede dans un main() avec un UUID d'un noeud "b0239789-3769-4401-8da3-7b00265f3328" et voila l'erreur
Exception in thread "main" org.alfresco.webservice.util.WebServiceException: Unable to get content as string.
   at org.alfresco.webservice.util.ContentUtils.getContentAsString(ContentUtils.java:99)
   at org.alfresco.sample.webservice.ContentReadAndWrite.readContent(ContentReadAndWrite.java:195)
   at org.alfresco.sample.webservice.ContentReadAndWrite.main(ContentReadAndWrite.java:125)
Caused by: java.net.MalformedURLException: no protocol: null?ticket=TICKET_d2e8553c954399d4057499f0fd3cf44ace895460
   at java.net.URL.<init>(Unknown Source)
   at java.net.URL.<init>(Unknown Source)
   at java.net.URL.<init>(Unknown Source)
   at org.alfresco.webservice.util.ContentUtils.getContentAsString(ContentUtils.java:79)
   … 2 more
quelqun peut m'aider  Smiley Sad

Cordialement
cleseach
Active Member II

Re: Lire un contenu en utilisant les services web

Bonjour,

Que vaut content lorsque vous vous en servez ?

Cordialement,
Charles Le Seac'h
debutant_alfres
Member II

Re: Lire un contenu en utilisant les services web

bonjour,

d'apres ce que j'ai compris de l'exemple:

"content" contient le contenu resultant de lexecution de la methode "contentSrervice.read"

j'ai changé un autre IUUD et ca marche

mais j'ai un autre probleme , hier j'ai executer un programme qui permet de creer des noeud et remplire leur contenu par un contenu d'un fichier xml qui est stocké en locale, et ca marcheé, mais le probleme c'est quand je rentre dans le le NodeBrowser je trouve les noeud mais leur contenu n'existe pas
par contre contre je rentre dans explorer d'alfresco je trouve ces noeuds et leu contenu comme

la j'ai rien compris :roll:  :?:  :!:

cordialement
debutant_alfres
Member II

Re: Lire un contenu en utilisant les services web

j'ai ecrit un programme qui permet de lire le contenu d'un noeud et ca marche Smiley Very Happy
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.sample.webservice;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;

import org.alfresco.webservice.repository.RepositoryService;
import org.alfresco.webservice.content.Content;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.ContentFormat;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.Node;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationDetails;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.ContentUtils;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;



/**
* Web service sample 3
* <p>
* This web service sample shows how new content can be added to the repository and how content
* can be read and updated via the web service API.
*
* @author Roy Wetherall
*/
public class ReadIU
{
 
 

    /** The type of the association we are creating to the new content */
    private static final String ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains";
    protected static final Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
    /**
     * Main function
     */
    public static void main(String[] args) throws Exception
    {

       
       String chemin = readContent("312c2cd4-3d90-4f15-a152-b130aea72ac1");
       System.out.println(chemin);
    }
  
  
   
   
   
  
    public static String readContent(String UUID)
        throws Exception
    {
    AuthenticationUtils.startSession("admin", "alfrescoadmin");
   
    try
    {        
       AuthenticationDetails details =AuthenticationUtils.getAuthenticationDetails();
      System.out.println(details.getUserName());
      System.out.println(details.getSessionId());
        // Get the content service
        ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();       
       
        // Read  content node given by his uuid from the repository
        Reference newContentReference = new Reference(STORE, UUID, null);
        Content[] readResult = contentService.read(
                                            new Predicate(new Reference[]{newContentReference}, STORE, null),
                                            Constants.PROP_CONTENT);
        Content content = readResult[0];
       
        //  Get the uuid node
        String uuid= newContentReference.getUuid();
      
       
        
        // Output the content node and its uuid
        System.out.println("The content is:");
       // System.out.println(ContentUtils.getContentAsString(content));
        String contenu=ContentUtils.getContentAsString(content);
        System.out.println(contenu);
           
        System.out.println(uuid);
       
       
       // Put the content in a file
        String path="C://Alfresco//alf_data//x-alf-"+ uuid+"";
        BufferedWriter file = new BufferedWriter(new FileWriter(path));
        file.write(contenu);
       
      file.close();
      return path;
    }
    finally
    {
        // End the session
        AuthenticationUtils.endSession();
    }
    }

}