Return mac address from an applet command

I am trying to retrive to vaadin app the client Mac Address via Applet Integration.

I already call doExecute(String command, Object params) successfully…

Now i have to pass the applet function result - getMacAddresses() - to Vaadin Application.

Checking the source of Screenshot addon i see Httpclient/PostMethod usage to pass the image stream to server.

But, on my case, i just want to return a simple string. Should i use the httpclient/postmethod too?.. or there is a simple way?

thanks in advance.

Currently posting directly from the applet to server is the easiest way. Check the
Screenshot add-on
code for details.

doPostToServer() method in the
ScreenshotApplet.java
sending a (multipart image data in this case). Especially you need to take care of the HTTP session cookie and correct parameter name.

You should also override the
changeVariables
method on to receive any custom variables. Note that the Screenshot component mentioned above extend the default Upload implementation and therefore no server-side receiver code is needed in that case.

Hi Sami,

i’m triying to receive my custom variables sent from an Applet, as you describe by using a similar function to doPostToServer() method:

PostMethod post = new PostMethod(postUrl);
					post.setRequestHeader("Cookie", sessionCookie);

					int i = 0;
					for (RemoteDevice rm : devicesDiscovered) {
						debug("p"+String.valueOf(i) + "-" + rm.getBluetoothAddress() +"-"+ rm.getFriendlyName(false));
						
						post.addParameter("p"+String.valueOf(i++), rm.getBluetoothAddress());
					}

					HttpClient client = new HttpClient();
					client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
					
					debug(post.toString());
					
					int status = client.executeMethod(post);
					debug("status" + status);

Im getting this debug info from my applet:


debug: Post to server: http://localhost:8080//farmabide/
debug: p0-0023F1E6EBC7-W350i
debug: p1-001D25AC8226-newuni
debug: org.apache.commons.httpclient.methods.PostMethod@9945ce
debug: status 200

Im getting this parameters in my Application class by implementing HttpServletRequestListener:


[Start of request
 Query string: null
 Path: /
 Parameters: 
p1 = 001D25AC8226
p0 = 0023F1E6EBC7

Im inserting my applet with this code:



applet = new AppletIntegration() {

            private static final long serialVersionUID = 1L;

            @Override
            public void attach() {
                setAppletArchives(Arrays.asList(new String[] { "medisync-applet.jar" }));
                setAppletClass("es.medisync.applet.HeadlessApplet");
              
                setWidth("1px");
                setHeight("1px");

            }
            
            @Override
        	public void changeVariables(Object source, Map variables) {
        		System.out.println("SincronizacionTab4.SincronizacionTab4(...).new AppletIntegration() {...}.changeVariables()");
        		
        		// Let superclass read any common variables.
        		super.changeVariables(source, variables);
        		
        		// Sets the currently selected color
        	    if (variables.containsKey("p1")) {
        	        final String newValue = (String)variables.get("p1");
        	        // Changing the property of the component will
        	        // trigger a ValueChangeEvent
        	        bluetoothCombo.addItem(newValue);
        	    }
        	    
        	    if (variables.containsKey("p2")) {
        	        final String newValue = (String)variables.get("p2");
        	        // Changing the property of the component will
        	        // trigger a ValueChangeEvent
        	        bluetoothCombo.addItem(newValue);
        	    }
        	}
        };
		
		addComponent(applet);

The problem is that
changeVariables
is not executing.

Any idea?

Thanks in advance,

If I didn’t miss anything important: The parameter naming.

Vaadin uses it internal scheme for http post parameter names. Typically they are format:
Here the is something that is matched to the variable name in changeVariables and is internally managed (currently like ‘PID’ and a number). I did this in the
Screenshot
applet implementation and there the POST parameter is something like ‘PID4_file’.

In the
AppletIntegration add-on
this is automatically handled and if inherit the AbstractVaadinApplet there is function getPaintableId that you can use to obtain the first part of the post parameter. For the latter part you can use whatever name you like (the server-side code with ‘p1’ and ‘p2’ is quite ok).

I just added in my Applet class getPaintableId()+“p1” but changeVariables is still not executing. Now i’m getting
PID98p1
on server:


  post.addParameter(getPaintableId()+"p1","some_value");

I’ve also tried overriding paintContent method in my AppletIntegration class:


    @Override
    public void paintContent(PaintTarget target) throws PaintException {
		super.paintContent(target);
		target.addVariable(this, "p1", "---");
	}

Nothing.

I’ve opted for retrieving parameter value in
onRequestStart
method and manually updating server side component.


String p1= request.getParameter("p1");
if(p1!=null && !"".equals(p1)){
...
}

Anyway, thank you very much.

Hi,

You have a valid workaround there. But this still bothers me so I checked this further. It seems that in my case (the screenshot) the multipart post request is handled differently by the framework and that is why it does not work for you.

Admittedly it is not good to mess with the internal protocol like this, and I think that I’ll add the function to AppletIntegration add-on directly to update a variable to server-side. That would be the right way to solve this.

Hi! Why source of screenshot addon isn’t full?