Help needed to launch a script in the client workstation


I would like to be able to run a client script (Bourne shell for Linux, batch file for Windows) in the client workstation. The contents of the script needs to be dynamically built. The contents of the script will depend on a server side calculation that is done after a list box selection is made on a dialog. The script should be launched when a (Button or Link) on the dialog is clicked.

  1. How can I tell if the client / browser is running on Windows or Linux ?

  2. I thought I could dynamically create the script contents with the CustomResource I wrote (see below) and use “application/x-sh” or “application/:x-msdos-program” or similar for the mimeType, and the script contents for the contents variable:

 * Map custom text to a Resource.
public class CustomResource extends StreamResource
	private final String mimeType;

	public CustomResource(final String contents,
			final String mimeType,
			final Application application) throws IOException
		// Use hashCode as a the cache key
		super(make(contents, application), "CustomResource-" + contents.hashCode(), application);
		this.mimeType = mimeType;

	public String getMIMEType()
		return mimeType;

	private static TextStream make(String textContents, Application application) throws IOException
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		final ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
		final TextStream stream = new TextStream(inputStream);
		return stream;

	 * Helper class to map an InputStream to a StreamResource.StreamSource.
	private static class TextStream implements StreamResource.StreamSource
		private InputStream inputStream;

		public TextStream(InputStream inputStream)
			this.inputStream = inputStream;

		public InputStream getStream()
			return inputStream;

I would update the (Link or Button) Resource every time the list box selection is changed and the script contents is recalculated, ready for the user to then click it.

Is there a way to bind this Resource to a (Button or Link), so that the user will be prompted to run the script by the browser when they click on the (Button or Link) ?

  1. Is it possible to make the browser always prompt the user to run the script (rather than the “Save As” or “Run”) prompt that is usually triggered when a link pointing to an executable resource is clicked ?

Is there a better or simpler approach to solve this problem ?

Thanks for any ideas you might have,

You can dig it from servletrequest. Its got “user-agent” in its header. You can listen requests using TransactionListener with ApplicationContext.
2. Yes, try Window#open(Resource) or open link with some uri and handle download stream directly. Manual has great examples of dynamic content served with URIHander.
3. At least with firefox there is in the settings, actions for each mime-type. There you can add application/x-sh to autosave. Or AUTORUN with bash. Just tested and I was shocked…it works.

getMainWindow().addURIHandler(new URIHandler() {

			public DownloadStream handleURI(URL context, String relativeUri) {
				String s= "!/bin/sh\n"+ "kedit &\n ";
				InputStream test = new ByteArrayInputStream(s.getBytes());
				if (relativeUri.contains("test")) {
					return new DownloadStream(test,"application/x-sh","");
				return null;

This is scary. There probably is no domain restrictions in the browser for this? Any site could then (automatically) open a suitable file and execute it.

I don’t really recommend to enable the autorun… just prompt the user every time.

Really scary indeed. I was so certain that it would NOT work… There ain’t domain restrictions either: you cannot set any and it works from any domain. Don’t blame me if you browse the “internets” with it :twisted:

Sami, Mauno,

Many thanks for your feedback - I think I understand how to code this now.

I also found another good example for the DownloadStream here:

Just one other thing I noticed while digging for information:

FileTypeResolver.initialExtToMimeMap has the following mapping:

text/sh    sh

instead of:

application/x-sh      sh

Is that intended, or a mistake ?

(There are also similar mappings for csh, etc)



Another (perhaps easier way) to determine the Operating System of the client browser, is to get the user-agent info from:

Application app = ...;

String userAgent = ((WebApplicationContext) app.getContext()).getBrowser().getBrowserApplication();

if (userAgent.indexOf("Linux") >= 0)


I think this is intended exactly for the reason that we don’t want to allow execution of shellscripts by default. Correct me if I’m wrong.

But you can skip this by providing your own MIME type for DownloadStreams.