cancel
Showing results for 
Search instead for 
Did you mean: 

Display all stores with a servlet

z3r0
Champ in-the-making
Champ in-the-making
Hi, I want to read with a servlet, all stores. I have three classes
ServletStores:

import java.io.*;
import java.util.ArrayList;

import javax.servlet.*;
import javax.servlet.http.*;

public class ServletStores extends HttpServlet {
   
   public void doGet( HttpServletRequest requ, HttpServletResponse resp )
     throws ServletException, IOException
     {
       GetStores gs = new GetStores();
       ArrayList al = new ArrayList();
       try {
         al = gs.getStores();
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
       resp.setContentType( "text/html" );
       PrintWriter out = resp.getWriter();
       out.println( "<html>" );
       out.println( "Hello, i am a servlet!" );
       for(int i=0; i<al.size(); i++) {
          out.println(al.get(i)+""+"<br />");
       }
       out.println( "</html>" );
       out.close();
     }
}

GetStores

import java.util.ArrayList;

import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.WebServiceFactory;

public class GetStores extends SamplesBase
{
    public ArrayList getStores() throws Exception {
       
       ArrayList al = new ArrayList();
       
        // Start the session
        AuthenticationUtils.startSession(USERNAME, PASSWORD);
       
        try {  
            // Get the respoitory service
            RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory.getRepositoryService();
           
            // Get array of stores available in the repository
            Store[] stores = repositoryService.getStores();
            if (stores == null) {
                // NOTE: empty array are returned as a null object, this is a issue with the generated web service code.
                al.add("There are no stores avilable in the repository.");
            }
            else {
                // Output the names of all the stores available in the repository
                System.out.println("The following stores are available in the repository:");
                for (Store store : stores) {
                    al.add(store.getScheme() + "://" + store.getAddress());
                }
            }
        }
        finally {
            // End the session
            AuthenticationUtils.endSession();
        }
        return al;
    }      
}

and SampleBase

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.types.Store;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;

/**
* @author Roy Wetherall
*/
public class SamplesBase
{
    /** Admin user name and password used to connect to the repository */
    protected static final String USERNAME = "admin";
    protected static final String PASSWORD = "admin";
   
    /** The store used throughout the samples */
    protected static final Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
   
    protected static final Reference SAMPLE_FOLDER = new Reference(STORE, null, "/app:company_home/cm:sample_folder");
   
    protected static void createSampleData() throws Exception
    {
        try
        {
            // Check to see if the sample folder has already been created or not
            WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[]{SAMPLE_FOLDER}, STORE, null));
        }
        catch (Exception exception)
        {
            // Create parent reference to company home
            ParentReference parentReference = new ParentReference(
                    STORE,
                    null,
                    "/app:company_home",
                    Constants.ASSOC_CONTAINS,
                    Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "sample_folder"));

            // Create folder
            NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, "Web Service Sample Folder")};
            CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_FOLDER, properties);
            CML cml = new CML();
            cml.setCreate(new CMLCreate[]{create});
            UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);               
           
            // Create parent reference to sample folder
            Reference sampleFolder = results[0].getDestination();
            ParentReference parentReference2 = new ParentReference(
                    STORE,
                    sampleFolder.getUuid(),
                    null,
                    Constants.ASSOC_CONTAINS,
                    Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "sample_content"));
           
            // Create content
            NamedValue[] properties2 = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, "SampleContent.txt")};
            CMLCreate create2 = new CMLCreate("1", parentReference2, null, null, null, Constants.TYPE_CONTENT, properties2);
            CML cml2 = new CML();
            cml2.setCreate(new CMLCreate[]{create2});
            UpdateResult[] results2 = WebServiceFactory.getRepositoryService().update(cml2); 
           
            // Set content
            ContentFormat format = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8");
            byte[] content = "This is some test content provided by the Alfresco development team!".getBytes();
            WebServiceFactory.getContentService().write(results2[0].getDestination(), Constants.PROP_CONTENT, content, format);
           
        }
    }
}

I have included all the libraries, but I get this error:

type: Exception report
message:
description: The server encountered an internal error () that prevented it from fulfilling this request.
exception: javax.servlet.ServletException: Servlet execution threw an exception
root cause: java.lang.NoClassDefFoundError: Could not initialize class GetStores
   ServletStores.doGet(ServletStores.java:12)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note: The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.

My web.xml looks like this.

<!DOCTYPE web-app PUBLIC
  '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
  'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
  <display-name>Mein erstes Servlet</display-name>
  <servlet>
    <servlet-name>servletstores</servlet-name>
    <servlet-class>ServletStores</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>servletstores</servlet-name>
    <url-pattern>/servletstores</url-pattern>
  </servlet-mapping>
</web-app>

Can someone please help me?
2 REPLIES 2

mrogers
Star Contributor
Star Contributor
You seem to have a class loading error (java.lang.NoClassDefFoundError)   so the error is likely to be in your packaging of your code.

And what is the SamplesBase class doing? 

But I think you would be far, far better off using a web script for this.

z3r0
Champ in-the-making
Champ in-the-making
Hi, am new to Alfresco. The classes GetStores and SampleBase come from the SDK. I have simply taken the classes. I would like to know how to with a servlet logs into the system. Furthermore, I would like to know how I get all the files displayed.
Is that possible with a servlet?