cancel
Showing results for 
Search instead for 
Did you mean: 

Communication between applet and bean

p3d3r0s0
Champ in-the-making
Champ in-the-making
Hey,
I'm trying to change a file's content through an applet (so i can do it on the client's side). My idea is to have a jsp where you press a button and the backing bean sends the file's noderef to an applet and it changes the file and sends back the new file's content to the backing bean.

1- Is this possible?
2- How can I access the file's content in the applet (send it's inputStream has a parameter?)
3- How can I send the file's content from the applet to the backing bean?

I believe I solve the second problem with information from this post http://forums.alfresco.com/en/viewtopic.php?f=48&t=18828&hilit=applet, but how can I solve the third problem?

Best reguards,
Pedro
11 REPLIES 11

savic_prvoslav
Champ on-the-rise
Champ on-the-rise
How can I send the file's content from the applet to the backing bean?

you can create servlet and you get servlet from your applet and send the content.
in servlet you save content to node. that is how I integrated zoho editor, works great. and it does not take much time.

gyro_gearless
Champ in-the-making
Champ in-the-making
Hey,
I'm trying to change a file's content through an applet (so i can do it on the client's side). My idea is to have a jsp where you press a button and the backing bean sends the file's noderef to an applet and it changes the file and sends back the new file's content to the backing bean.

1- Is this possible?
2- How can I access the file's content in the applet (send it's inputStream has a parameter?)
3- How can I send the file's content from the applet to the backing bean?

I believe I solve the second problem with information from this post http://forums.alfresco.com/en/viewtopic.php?f=48&t=18828&hilit=applet, but how can I solve the third problem?

Best reguards,
Pedro

Yes, i suppose this can be done - and when i lately realized that integration between Java applets and browsers has improved a lot over the past years, i am thinking again about using applets in favor of all that sh***** Javascript stuff 🙂

Anyway, you may have to tie up some loose ends, so here are a few pointers:

http://download-llnw.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html
http://java.sun.com/products/plugin/1.3/docs/jsobject.html
http://www.jguru.com/faq/view.jsp?EID=16833

This should get you started on interaction between applets and HTML/DOM.

For interaction with the Alfresco repository, you may use HTTP GET/POST requests to predefined or custom webscripts - should be easy with say Commons::HttpClient in your applet.

Cheers & HTH
Gyro

p3d3r0s0
Champ in-the-making
Champ in-the-making
Hey, first of all, thank you for both your suggestions savic and gyro.
I've been looking into webscripts and servlets but i dont really understand how they work.
I found a lot of usefull information about webscripts(http://wiki.alfresco.com/wiki/Web_Scripts) but it seems like a complicated solution.
I was thinking about using Servlets i have seen a few examples (like the UploadContentServlet) and i noticed that servlets are configured in the web.xml file, but i dont understand how i can access a servlets methods or how to get information between a servlet and an applet (i suppose you use HttpServletRequest and HttpServletResponse for that, but how?)

1- Are there any examples on how to send information between an applet and a servlet?
2- Can i then access the servlets information in a bean?

Best reguards,
Pedro

p3d3r0s0
Champ in-the-making
Champ in-the-making
Im doing a very simple attempt at communicating between an applet and a servlet, but its not working.

in my applet i have:


package aTest;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class appletTest extends Applet{

   private static final long serialVersionUID = 1L;

   private String webServerStr = null;
    private String hostName = "localhost";
   private int port = 8080;
    private String servletPath = "/alfresco/test";
   
   private TextArea statusTextArea;
   private Panel statusBarPanel;
   private Label statusLabel;

    public void init() {
      this.setLayout(new BorderLayout());

      
      statusBarPanel = new Panel();
        statusTextArea = new TextArea(3, 50);
      statusBarPanel.setLayout(new FlowLayout());
      this.add(BorderLayout.SOUTH, statusBarPanel);

      statusLabel = new Label("Status Messages");
      statusBarPanel.add(statusLabel);

      statusTextArea = new TextArea(3, 50);
        statusBarPanel.add(statusTextArea);

        // get the host name and port of the applet's web server       
       URL hostURL = getCodeBase();
       hostName = hostURL.getHost();
       port = hostURL.getPort();
       
       if (port == -1){
          port = 80;
       }
       
        log("Web Server host name: " + hostName);
       
       webServerStr = "http://" + hostName + ":" + port + servletPath;
        log("Web String full = " + webServerStr);
       
       post();
    }

    protected void log(String msg)
   {
       statusTextArea.append(msg + "\n");   
   }

    public void post(){
//       String webServerStr = "http://10.50.104.67:8080/test";
      URL studentDBservlet;
      try {
         studentDBservlet = new URL( webServerStr );
         URLConnection servletConnection = studentDBservlet.openConnection(); 
         servletConnection.setDoInput(true);         
         servletConnection.setDoOutput(true);
         
         // Don't use a cached version of URL connection.
         servletConnection.setUseCaches (false);
         servletConnection.setDefaultUseCaches (false);
         
         // Specify the content type that we will send binary data
         servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");
         
         // send the student object to the servlet using serialization
         ObjectOutputStream outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
         
         String output = "Communication Achieved.";
         // serialize the object
         outputToServlet.writeObject(output);
         
         outputToServlet.flush();          
         outputToServlet.close();
      } catch (Exception e) {
         e.printStackTrace();
      }

    }
}

in my servlet i have:


package aTest;

import java.io.IOException;
import java.io.ObjectInputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.alfresco.web.app.servlet.BaseServlet;

public class servTest extends BaseServlet{

   private static final long serialVersionUID = 1L;

    public void init(ServletConfig config) throws ServletException{
       System.out.println("customServletInitiated");
        super.init(config);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      
      ObjectInputStream inputFromApplet = null;
      String input = null;
      
      try {
         inputFromApplet = new ObjectInputStream(request.getInputStream());
         input = (String) inputFromApplet.readObject();
         inputFromApplet.close();
         System.out.println(input);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   
}

and i have configured web.xml like this:


   <servlet>
      <servlet-name>testServlet</servlet-name>
      <servlet-class>aTest.servTest</servlet-class>
   </servlet>
.
.
   <servlet-mapping>
      <servlet-name>testServlet</servlet-name>
      <url-pattern>/test</url-pattern>
   </servlet-mapping>

But i cant get the "Communication Achieved." string in my console.
What am i doing wrong?

Best reguards

savic_prvoslav
Champ on-the-rise
Champ on-the-rise
you can not just hit servlet like you do it,
you must be Authenticated user Smiley Happy, imagine if anyone could do it like you do.
so solution is to on submit to send ticket value like so:http://wiki.alfresco.com/wiki/URL_Addressability#Ticket_URL_Argument

or to be exact: /test?ticket=lfdslkfsadlj34324

you can obtain ticket of current user with
Application.getCurrentUser(FacesContext.getCurrentInstance()).getTicket()

so now with this knowledge you will be able to submit data to test servlet, also submit id of node you are about to save,  and then create your node with
 
NodeRef nodeRef = new NodeRef(Repository.getStoreRef(), id);
          Node node = new Node(nodeRef);
and with content writer you can set new content to the node…
Hope this was helpfull for you.

p3d3r0s0
Champ in-the-making
Champ in-the-making
I cant seem to be able to get FacesContext.getCurrentInstance(), it seems that its always null.
I was making a GET to the servlet to get the ticket using:

String ticket = Application.getCurrentUser(FacesContext.getCurrentInstance()).getTicket();
and sending "ticket" back to the applet so i could then do POSTs.

so why is facescontext null?
did i get the wrong idea?

Best reguards,
Pedro

savic_prvoslav
Champ on-the-rise
Champ on-the-rise
you should pass ticket value to applet form alfresco.

if applet is in alfresco.

if not then you should use alfresco api to obtain ticket.

p3d3r0s0
Champ in-the-making
Champ in-the-making
So what i was doing is right, right?
I do a GET from the applet asking the servlet for a ticket. The servlet should be able to access the Alfresco API and get the ticket and then send it to the applet.
But i cant seem to get the ticket inside the servlet. Using FacesContext.getCurrentInstance() gets me a null.
Is there another way to get the ticket?

Best regards,
Pedro

EDIT:
nevermind, i got facesContext working now, had to use this to get the ticket:

Application.getCurrentUser(request.getSession()).getTicket();
thanks savic Smiley Wink

p3d3r0s0
Champ in-the-making
Champ in-the-making
I still dont seem to be able to send information to the servlet.

im using this method:

    public void sendInfoToServer(){
      URL servlet;
      try {
         String output = "Info from applet.";
// gets ticket
         String ticket = getTicket();

         servlet = new URL( webServerStr + "?" + "alf_ticket" + "=" + ticket);
         URLConnection servletConnection = servlet.openConnection();
         log("Connected at: " + servletConnection.getURL());
         
// inform the connection that we will send output and accept input
         servletConnection.setDoInput(true);
         servletConnection.setDoOutput(true);
         
// Don't use a cached version of URL connection.
         servletConnection.setUseCaches (false);
         
         servletConnection.setRequestProperty ("Content=length", String.valueOf(output.getBytes().length));
         
         ObjectOutputStream outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
// "sends" output to servlet
         outputToServlet.writeObject(output);
         outputToServlet.flush();          
         outputToServlet.close();

         log("Sent information to server");
      } catch (Exception e) {
         e.printStackTrace();
      }

in this method i connect using the ticket and i should be able to send hte string "Info from applet." to servlet, but it doesnt seem to ge through.
Am i missing something?