cancel
Showing results for 
Search instead for 
Did you mean: 

Web Services in an applet

agey
Champ in-the-making
Champ in-the-making
Hi all,

I want to integrate an applet into Alfresco. This applet scan a file from device, save it in the local machine and then upload it into Alfresco. To upload the file the applet use Alfresco webservices. This is the code:


public void saveAlfresco(File file){
    
     String userName = "admin";
     String password = "admin";
    
      try
      {    
         String serverhost = "http://localhost:8080/alfresco/api";
       // Start the session
         System.out.println("Start the session");
         AuthenticationUtils.startSession(userName, password);
                  
          // Get the content service
          System.out.println("Get the content service");
          ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService(serverhost);       
         
          Store store = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
         String ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains";
         String uuid = properties.getProperty("container").toString();
         String filename = properties.getProperty("scanFile").toString();
        
         System.out.println("Create a parent reference");
         // 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, uuid, null, ASSOC_CONTAINS,
                 "{" + Constants.NAMESPACE_CONTENT_MODEL + "}" + filename);
        
         System.out.println("Define the content format new ContentFormat('image/tiff', 'UTF-8')");
         // Define the content format for the content we are adding
         ContentFormat contentFormat = new ContentFormat("image/tiff", "UTF-8");
        
         NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, filename)};
         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();
        
         FileInputStream fileInputStream = new FileInputStream(file);
         byte[] bytes = ContentUtils.convertToByteArray(fileInputStream);
       
         System.out.println("Add content");
         Content content = contentService.write(newContentNode, Constants.PROP_CONTENT, bytes, contentFormat);
     
         System.out.println("Saved!!!! " + content.getUrl());

      }catch (RemoteException e) {
      e.printStackTrace();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (Exception e) {
      e.printStackTrace();
   }
      finally
      {
          // End the session
          AuthenticationUtils.endSession();
      }
  }

This code return an error when execute this line:

AuthenticationUtils.startSession(userName, password);
and then when it run this line:

AuthenticationUtils.endSession();
the following error appears in Java console:
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: org/alfresco/webservice/util/AuthenticationUtils
   at uk.co.mmscomputing.application.imageviewer.ImageTab.saveAlfresco(ImageTab.java:413)
   at uk.co.mmscomputing.application.imageviewer.ImageTab.save(ImageTab.java:347)
   at uk.co.mmscomputing.application.imageviewer.ImageTab$3.actionPerformed(ImageTab.java:147)
   at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
   at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
   at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
   at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
   at java.awt.Component.processMouseEvent(Unknown Source)
   at javax.swing.JComponent.processMouseEvent(Unknown Source)
   at java.awt.Component.processEvent(Unknown Source)
   at java.awt.Container.processEvent(Unknown Source)
   at java.awt.Component.dispatchEventImpl(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)

The userName and password are correct. What´s the problem? Does It need more configuration? I never use web services before.

Thanks a lot in advance,
8 REPLIES 8

openpj
Elite Collaborator
Elite Collaborator
Before starting the session you have to set the endpoint address in this way:

WebServiceFactory.setEndpointAddress(serverhost);
AuthenticationUtils.startSession(userName, password);

Hope this helps.

agey
Champ in-the-making
Champ in-the-making
thank you very much for your quick reply.

I tried it and now it stops in line:

String serverhost = "http://localhost:8080/alfresco/api";
ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService(serverhost); 
And the folowing error appears in Java console:
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: org/alfresco/webservice/util/AuthenticationUtils
   at uk.co.mmscomputing.application.imageviewer.ImageTab.saveAlfresco(ImageTab.java:416)
   at uk.co.mmscomputing.application.imageviewer.ImageTab.save(ImageTab.java:347)
   at uk.co.mmscomputing.application.imageviewer.ImageTab$3.actionPerformed(ImageTab.java:147)
   at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
   at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
   at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
   at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
   at java.awt.Component.processMouseEvent(Unknown Source)
   at javax.swing.JComponent.processMouseEvent(Unknown Source)
   at java.awt.Component.processEvent(Unknown Source)
   at java.awt.Container.processEvent(Unknown Source)
   at java.awt.Component.dispatchEventImpl(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.alfresco.webservice.util.AuthenticationUtils
   at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClassInternal(Unknown Source)
   … 27 more
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/alfresco/applets/org/alfresco/webservice/util/AuthenticationUtils.class
   at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
   at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
   at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   … 31 more

What library is needed?

openpj
Elite Collaborator
Elite Collaborator
After you set the endpoint you need only to retrieve the service in this way:

ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
Hope this helps.

agey
Champ in-the-making
Champ in-the-making
Ok, I set the endpoint this way:


public void saveAlfresco(File file){
   
     String userName = "admin";
     String password = "admin";
   
      try
      {    
         String serverhost = "http://localhost:8080/alfresco/api";
         WebServiceFactory.setEndpointAddress(serverhost);
     // Get the content service
        System.out.println("Get the content service");
        ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();

       // Start the session
         System.out.println("Start the session");
         AuthenticationUtils.startSession(userName, password);
         
          Store store = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
         String ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains";
         String uuid = properties.getProperty("container").toString();
         String filename = properties.getProperty("scanFile").toString();
       
         System.out.println("Create a parent reference");
         // 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, uuid, null, ASSOC_CONTAINS,
                 "{" + Constants.NAMESPACE_CONTENT_MODEL + "}" + filename);
       
         System.out.println("Define the content format new ContentFormat('image/tiff', 'UTF-8')");
         // Define the content format for the content we are adding
         ContentFormat contentFormat = new ContentFormat("image/tiff", "UTF-8");
       
         NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, filename)};
         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();
       
         FileInputStream fileInputStream = new FileInputStream(file);
         byte[] bytes = ContentUtils.convertToByteArray(fileInputStream);
      
         System.out.println("Add content");
         Content content = contentService.write(newContentNode, Constants.PROP_CONTENT, bytes, contentFormat);
    
         System.out.println("Saved!!!! " + content.getUrl());

      }catch (RemoteException e) {
      e.printStackTrace();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (Exception e) {
      e.printStackTrace();
   }
      finally
      {
          // End the session
          AuthenticationUtils.endSession();
      }
  }

Now, I get this error in Java console:

Exception in thread "AWT-EventQueue-5" java.lang.NoClassDefFoundError: org/alfresco/webservice/types/Store
   at uk.co.mmscomputing.application.imageviewer.MainApp.getCenterPanel(MainApp.java:30)
   at uk.co.mmscomputing.util.UtilMainApp.createGUI(UtilMainApp.java:97)
   at uk.co.mmscomputing.util.UtilMainApp$2.run(UtilMainApp.java:135)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.alfresco.webservice.types.Store
   at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClassInternal(Unknown Source)
   … 11 more
Caused by: java.io.IOException: open HTTP connection failed:http://192.168.2.24:8080/alfresco/applets/org/alfresco/webservice/types/Store.class
   at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
   at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
   at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   … 15 more

"http://192.168.2.24:8080/alfresco/applets" is the url where applet is. Why search the webservices classes there?  The webservices classes are in alfresco-remote-api.jar, isn´t it? I tried to put alfresco-remote-api.jar library in folder applet but it returns the same error.

Thank you very much for your help. I have never used webservices and I don´t know how it works.

openpj
Elite Collaborator
Elite Collaborator
You have to download the Alfresco SDK for your version and then you need to use the same dependencies in the project SDK AlfrescoRemote. In this way you will have all the needed dependencies for your Java application.

Hope this helps.

agey
Champ in-the-making
Champ in-the-making
Hi OpenPj,

Thank you very much for your help. I tried your advice and it doesn´t work. It returns the same error.

I create an applet to scan some documents from scan device and I want to save them in Alfresco. The applet has a method 'saveAlfresco(File file)' to use Alfresco webservice. This applet is packed into a jar file and put in applet folder within alfresco deployed folder. A jsp page in Alfresco calls this applet the following way but it doesn´t work, it returns the error descrited in previous post:


<applet code="uk.co.mmscomputing.application.imageviewer.MainApp.class"
   codebase="http://192.168.2.24:8080/alfresco/applets"
   archive="scan-plugin-mmsc.jar"
   width="100%" height="600">
   <PARAM name="scanFile" value="<%= scan.getFileName() %>">
   <PARAM name="container" value="<%= scan.getSpaceNodeRef().getId() %>">
   Your browser does not support the applet tag.
</applet>

What am I doing wrong? Why look for the dependencies in the applet folder instead of using Alfresco's lib folder? Should I include any library into the applet?

Thanks a lot for any help,

agey
Champ in-the-making
Champ in-the-making
At the end I solved it copying all dependencies in folder where the applet is. Every jar file must be signed to access it from client and must be set in jsp as follows:


<applet code="uk.co.mmscomputing.application.imageviewer.MainApp.class"
   codebase="http://192.168.2.24:8080/alfresco/applets"
   archive="scan-plugin-mmsc.jar, alfresco-web-service-client-3.2.jar, activation.jar, alfresco-core.jar, axis-1.4.jar, bcprov-jdk15-137.jar, commons-discovery-0.2.jar, commons-logging-1.1.jar, jaxrpc.jar, mail.jar, opensaml-1.0.1.jar, saaj.jar, wsdl4j-1.5.1.jar, wsdl4j-1.6.2.jar, wss4j.jar, xalan.jar, xercesImpl-2.8.0.jar, xmlsec-1.4.1.jar"
   width="100%" height="600">
   <PARAM name="scanFile" value="<%= scan.getFileName() %>">
   <PARAM name="container" value="<%= scan.getSpaceNodeRef().getId() %>">
   Your browser does not support the applet tag.
</applet>

Now this error doesn't appear.  Smiley Very Happy

kunkunkunkun
Champ in-the-making
Champ in-the-making
What library are you using?
I`m doing a similar applet, but I can´t get mms computing library…
You are using these library or similar…?
If you use mms computing, can you send me the library??
Thanks!!