/**
* $Id:WebStart.java $
*
* Copyright 2004~2005 JingFei International Cooperation LTD. All rights reserved. *
*/

package com.jfimagine.jfdraw.webstart;


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

import javax.servlet.*;
import javax.servlet.http.*;


/**
* WebStart class is used to create a dynamic jnlp http response for web start JFDraw.
*
* @author CookieMaker
*
* @version $Revision: 1.2.0 $
*/

public class WebStart extends HttpServlet {


private ServletConfig config;
public void init (ServletConfig config) throws ServletException
{
     super.init(config);
     this.config = config;
}

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

     //Get arguments from http request
     String fileName =request.getParameter("fileName");
     String fileName2 =request.getParameter("fileName2");
     String fileName3 =request.getParameter("fileName3");
     String fileName4 =request.getParameter("fileName4");

     int httpHeadLen ="http://".length();

     //prepare the content of JNLP file.
     StringBuffer buf =new StringBuffer();
     buf.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
     buf.append("\n");

     buf.append("<jnlp spec=\"1.0+\" codebase=\"http://www.jfimagine.com/webstart\" >");
     buf.append("\n");

     buf.append("<information>");
     buf.append("\n");

     buf.append("<title>JFDraw Web start Solution</title>");
     buf.append("\n");

     buf.append("<vendor>JingFei International</vendor>");
     buf.append("\n");

     buf.append("<homepage href=\"http://www.jfimagine.com\"/>");
     buf.append("\n");

     buf.append("<description>JFDraw Web Start Solution</description>");
     buf.append("\n");

     buf.append("<description kind=\"short\">A vector graphs application and library package</description>");
     buf.append("\n");

     buf.append("<icon href=\"jfimagine.small.jpg\"/>"); buf.append("\n");

     buf.append("<offline-allowed/>"); buf.append("\n");

     buf.append("</information>"); buf.append("\n");

     buf.append("<resources>"); buf.append("\n");

     buf.append("<j2se version=\"1.4+\" />"); buf.append("\n");

     buf.append("<jar href=\"jfdraw_webstart.jar\"/>"); buf.append("\n");

     buf.append("</resources>"); buf.append("\n");

     buf.append("<application-desc main-class=\"JFDraw\">"); buf.append("\n");


     //package the parameters to <argument> pairs.
     if (fileName!=null && fileName.length()>httpHeadLen){
          buf.append("<argument>"+fileName+"</argument>");
          buf.append("\n");
     }
     if (fileName2!=null && fileName2.length()>httpHeadLen){
          buf.append("<argument>"+fileName2+"</argument>");
          buf.append("\n");
     }
     if (fileName3!=null && fileName3.length()>httpHeadLen){
          buf.append("<argument>"+fileName3+"</argument>");
          buf.append("\n");
     }
     if (fileName4!=null && fileName4.length()>httpHeadLen){
          buf.append("<argument>"+fileName4+"</argument>");
          buf.append("\n");
     }

     buf.append("</application-desc>");

     buf.append("<security>"); buf.append("\n");
     buf.append("<all-permissions/>"); buf.append("\n");
     buf.append("</security>"); buf.append("\n");
     buf.append("</jnlp>"); buf.append("\n");


     //output the JNLP file.
     PrintWriter out = response.getWriter();
     //jnlp content type
     response.setContentType("application/x-java-jnlp-file");
     //set immediate expire time to force browser to fetch jnlp content each time.
     response.setHeader("Expires", "0");
     response.addDateHeader("Date", java.util.Calendar.getInstance().getTime().getTime());
     response.addDateHeader("Last-Modified",java.util.Calendar.getInstance().getTime().getTime());
     out.println(buf.toString());
     out.close();
}


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

}