Poorman jnlp dynamic servlet

I was tired from changing jnlp codebase address between test and production environment. So I wrote this servlet that serves a jnlp file, but with the right codebase address, in accord with the server address.

Dont forget to set the jnlp mime-type (application/x-java-jnlp-file) in your jee server. I hope you like it.

=== JnlpServlet.java
package org.veiogaga;

import java.io.IOException;
import java.io.PrintWriter;

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

public class JnlpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String httpScheme = request.getScheme();
		String serverAdress = request.getServerName();
		int serverPort = request.getServerPort();
		String contextRoot = request.getContextPath();		

		response.setContentType("application/x-java-jnlp-file");
        PrintWriter out = response.getWriter();
        out.println(getJnlp(httpScheme,serverAdress,serverPort,contextRoot));
        out.close();

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	//adjust accordingly with your requirements
	private String getJnlp(String httpScheme, String  serverAdress, int serverPort, String contextRoot){

		//String codebase = "http://localhost:8080/MyApp/";
		String codebase = httpScheme+"://"+serverAdress+":"+serverPort+"/"+contextRoot+"/";
		String href =  "myapp.jnlp";

		        String jnlp = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"

                + "<jnlp codebase=\"%s\" href=\"%s\" spec=\"1.0+\">\n" //codebase

                + "<information>\n"
                + "<title>MyApp title </title>\n"
                + "<vendor>veioGaga Inc.</vendor>\n"
                + "<homepage href=\"http://www.example.org\" />\n"
                + "<description>This app will ghange the world</description>\n"
                + "<description kind=\"short\">just a little bit</description>\n"
                + "<offline-allowed />\n"
                + "</information>\n"
                + "<security>\n"
                + "<all-permissions />\n"
                + "</security>\n"
                + "<resources>\n"
                + "<j2se version=\"1.5+\" />\n"
                + "<jar eager=\"true\" href=\"jnlp/myapp.jar\" main=\"true\" />\n" 

                + "<jar href=\"jnlp/myapp.jar\" />\n"
                + "</resources>\n"
                + "    <application-desc main-class=\"org.veiogaga.app.WorldGui\" />\n"
                + "</jnlp>";

		return String.format(jnlp, codebase,href,httpScheme,serverAdress,serverPort,contextRoot);
	}
}
=== servlet

=== web.xml:
<servlet>
    <description> Generate JNLP dinamicaly</description>
    <display-name>JnlpServlet</display-name>
    <servlet-name>JnlpServlet</servlet-name>
    <servlet-class>org.veiogaga.JnlpServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>JnlpServlet</servlet-name>
    <url-pattern>/myapp.jnlp</url-pattern>
</servlet-mapping>

===

java plugin in ubuntu 12.04

IcedTea java plugin  is broken right now in ubuntu 12.04.
So I’ve downloaded java 7 from oracle and did:

sudo rm  /etc/alternatives/mozilla-javaplugin.so

sudo ln  -s /home/user/opt/jdk1.7.0_05/jre/lib/i386/libnpjp2.so /etc/alternatives/mozilla-javaplugin.so

And it works!
Test it with about:plugins in firefox.Hope it helps.

source: http://www.oracle.com/technetwork/java/javase/manual-plugin-install-linux-136395.html