[Opensource] Swing client

Turgay Zengin turgay_zengin at hotmail.com
Sun Jul 7 02:33:24 PDT 2002


Hi,
I have something working about the standalone client-Expresso integration 
and I want to share that with you. Although this is working, it cannot be 
said that it is perfectly working. Something more should be done to tell 
Expresso that this is a standalone client, not a browser, and behave 
differently. I don't know what is to be done for this functionality, any 
help will be appreciated.

Please note that I didn't try sending ControllerRequest/Response yet, things 
may get easier if that's done.

Okay, what I did is:

1) Extend com.jcorporate.expresso.services.controller.LoginController, to 
send the session id back to the client, this is important to be able to use 
the same session all the time. Don't forget to change the action-mapping for 
/Login in struts-config.xml.

public class CustomLoginController extends LoginController {
  public void postLoginProcessing(ControllerRequest 
request,ControllerResponse response) {
    try {
      ServletControllerRequest scr = (ServletControllerRequest) request;
      HttpServletResponse hres = (HttpServletResponse) 
scr.getServletResponse();
      ObjectOutputStream out = new 
ObjectOutputStream(hres.getOutputStream());
      HttpServletRequest hreq = (HttpServletRequest) 
scr.getServletRequest();

      Properties props = new Properties();
      props.put("expstatus","connected...ok");
      props.put("jsessionid",hreq.getSession().getId());

      out.writeObject(props);
      out.close();
    } catch (Exception e) {System.out.println(e.toString()); }
  }
}

Although it works, closing the OutputStream here is not a good idea, but I 
don't know what to do at this time.

2) Here is the client code for login:
public class ExpressoTest extends javax.swing.JFrame {

  private String jSessionId="";
  private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
    // Add your handling code here:
    URL servlet = null;
    try{
      servlet = new 
URL("http://localhost:8080/Login.do?state=processLogin");
      com.oreilly.servlet.HttpMessage msg = new 
com.oreilly.servlet.HttpMessage(servlet);
      Properties props = new Properties();
      props.put("dbContext","default");
      props.put("LoginName",txtUserName.getText());
      props.put("Password",txtPassword.getText());

      InputStream in = msg.sendPostMessage(props);
      ObjectInputStream oin = new ObjectInputStream(in);
      Object o = oin.readObject();
      oin.close();

      props = (Properties) o;

      txtStatus.setText((String) props.getProperty("expstatus"));
      jSessionId = (String) props.getProperty("jsessionid");

    } catch(Exception e) {
        System.out.println("Error:\n"+e.toString());
    }
  }
}

When we get the response, open up the Properties object, we now know what 
our session id is. The HttpMessage object is a helper object, which can be 
found in the com.oreilly.servlet package from www.servlets.net). It helps to 
reduce coding.

3) Now let's make a test running a controller and getting the results. As I 
said, I didn't try sending ControllerRequest/Response back and forth yet, 
this should be the way to go for full Expresso integration. Here is the 
client code:

private void btnGetDataActionPerformed(java.awt.event.ActionEvent evt) {
  // Add your handling code here:
  URL servlet = null;
  try{
    servlet = new 
URL("http://localhost:8080/helpdesk/EquipmentController.do;jsessionid=" + 
jSessionId);

    com.oreilly.servlet.HttpMessage msg = new 
com.oreilly.servlet.HttpMessage(servlet);

    Properties props = new Properties();
    props.put("eqid",txtEquipmentId.getText());
    props.put("state","showEq");

    InputStream in = msg.sendPostMessage(props);
    ObjectInputStream oin = new ObjectInputStream(in);
    Object o = oin.readObject();
    oin.close();

    props = (Properties) o;
    txtStatus.setText((String) props.getProperty("expstatus"));
    txtResults.setText((String) props.getProperty("eqserialno"));
  } catch(Exception e) {
      System.out.println("Error:\n"+e.toString());
  }
}

Here is the most tricky part (cost me about 5-6 hours): The 
";jsessionid=..." which is basically manually doing the url-rewriting, must 
come as the first parameter. So, 
http://....SomeController.do;jsessionid=xxxx is correct, but if you add 
anything in between, like the state parameter, the session id is not 
considered (At first, I constructed the URL like 
http://localhost:8080/...Controller.do?state=showEq;jsessionid=....") This 
caused the jsessionid to be rejected. A post in the Tomcat Users list showed 
me the solution.

4)The EquipmentController.showEq state shows the client the details of an 
Equipment, here we'll get only the serialno:

public class ShowEquipment extends State {
  public void run(ControllerRequest request, ControllerResponse response) {
    ServletControllerRequest scr = (ServletControllerRequest)request;
    HttpServletResponse hres = 
(HttpServletResponse)scr.getServletResponse();
    HttpServletRequest hreq = (HttpServletRequest)scr.getServletRequest();

    String eqid = request.getParameter("eqid");
    try {
      Controller myController = getController();
      String myName = new String(thisClass + "run()");
      ErrorCollection errs = new ErrorCollection();

      Equipment eqInit = new Equipment(request.getUid());
      eqInit.setDBName( request.getDBName() );
      eqInit.setField("Id",eqid);
      eqInit.retrieve();

      Properties props = new Properties();
      props.put("expstatus","got data ok.");
      props.put("eqserialno",eqInit.getField("SerialNo"));

      try{
        ObjectOutputStream out = new 
ObjectOutputStream(hres.getOutputStream());
        out.writeObject(props);
        out.close();
      }catch (Exception e) {System.out.println(e.toString()); }

    } catch(DBException dbe) {
        throw new ControllerException(thisClass + ":Unable to run:" + 
dbe.getMessage());
    }
  }
}

This is it. As I said, I am not finished yet, because Struts and Expresso 
try to do a lot more work although I am finished at my controller (I close 
the output stream) - this causes many exceptions for Expresso and Tomcat, 
but the data transfer and Expresso security is all working.

I need help to get rid of those exceptions, and maybe with the addition of a 
few classes, we can offer "accessing Expresso from standalone clients".

It's already been a long message on Sunday, wish you all a good day!

Turgay Zengin.

_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com




More information about the Opensource mailing list