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>

===

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s