mnu_startinapplet.jsp 10.1 KB
Newer Older
prova's avatar
prova committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
<?xml version = '1.0' encoding = 'utf-8'?>
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.net.URL" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.xml.parsers.DocumentBuilder" %>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory" %>
<%@ page import="javax.xml.parsers.ParserConfigurationException" %>

<%! private static String RELATIVE_PATH = "../"; %>
<%
//  String hrefz = "\"/client/sicraweb.jsp?";
//  Enumeration enum = request.getParameterNames();
//  while(enum.hasMoreElements())
//  {
//    String name = (String)enum.nextElement();
//    href += name+"="+request.getParameter(name);
//    if (enum.hasMoreElements()) href += "&";
//  }
//  href+="\"";
  
  // VERIFICA DEI PARAMETRI
  // in questa parte faccio la verifica dei parametri e nel caso cartauz tiro un cartauz
  String appType=request.getParameter("appServerType");
  String port=request.getParameter("port");
  String j2eeUser=request.getParameter("j2eeUser");
  String j2eePassword=request.getParameter("j2eePassword");
  String user=request.getParameter("user");
  String password=request.getParameter("password");
  String specJnlp = null;
  String serverType = null;
  String rmiPort = null;
  
  if (appType == null) appType = "jboss";
  if (port == null) port = String.valueOf(request.getServerPort());
  if (j2eeUser == null) 
  {
    String db = request.getParameter("db");
    if (db != null)
    {
      String[] tt = db.split(":");
      if (tt.length > 0) j2eeUser = tt[0];
      if (tt.length > 1) rmiPort  = tt[1];
    }
  }

  String appUrl = appType+":"+request.getServerName();
  if (port != null) appUrl += ":"+port;
  if (rmiPort != null) appUrl += ":"+rmiPort;
  
  String args = request.getParameter("args");
  
  String title = "Sicr@Web inside a web browser";
  String menuParam = "false";
  
  // JNLP 
  String jnlpFileName = request.getParameter("jnlp");
  if (jnlpFileName != null)
  {
    System.out.println("jnlp="+jnlpFileName);
    if (!jnlpFileName.toLowerCase().endsWith(".jnlp")) jnlpFileName += ".jnlp"; 
    
    String filename = config.getServletContext().getRealPath(jnlpFileName);
    File jnlpFile = new File(filename);
    if (!jnlpFile.exists()) response.sendError(500, "Il file '"+jnlpFileName+"' non  stato trovato!");
    
    System.out.println("jnlp file path="+filename);
    String[] result = caricaParametriConnessioneDaJNLP(new FileInputStream(jnlpFile));
    args = result[0];
    if (result[1] != null) title = result[1];
    
    menuParam = caricaNuovoMenuDaJNLP(new FileInputStream(jnlpFile));
  }  
   
  if (args != null)
  {
    String[] aa = args.split(";");
    if (aa.length > 5)
    {
      response.sendError(500, "Numero di argomenti nel JNLP non supportato: "+aa.length+". La vecchia sintassi non  supportata, convertire il JNLP alla nuova sintassi [max 5 parametri].");
    }
    
    appUrl = aa[0];
    j2eeUser = aa[1];
    j2eePassword = aa[2];
    if (aa.length>3) user = aa[3];
    if (aa.length>4) password = aa[4];
  
    String[] token = appUrl.split(":");
    appType = token[0];
    //port = token[2];
    port = token[2];
  }
         
  if (appType==null || appType.equalsIgnoreCase("jboss"))
  {
    serverType="jboss";
    specJnlp="jboss-client.jnlp";
  }
  else if ("OC4J".equalsIgnoreCase(appType))
  {
    serverType="oc4j";
    specJnlp="orion-client.jnlp";
  }
  else response.sendError(500,"il parametro appServerType deve valere \"JBOSS\" o \"OC4J\" (default = JBOSS)");
  
  if (j2eeUser == null) response.sendError(500,"il parametro j2eeUser deve essere valorizzato");
  
  // se non scrivo la password la imposto allo user
  if (j2eePassword == null) j2eePassword = j2eeUser;

  // dalle ultime versioni per ovviare al bug jnlp della 1.6
  // il setup cambai il nome al sicraweb-resources
  // questo algoritmo trova il nome giusto
  URL url=null;
  InputStream is = null;
  int i=0;
  String resourcePath=null;
  String res = null;
  //while(url==null)
  while(is == null)
  {
    res = "/sicraweb-resources"+(i==0 ? "" : "-ver"+i)+".jnlp";
    url = config.getServletContext().getResource(res);
    is = config.getServletContext().getResourceAsStream(res);
    i++;
    //if (url != null)
//    if (is != null)
//    {
//      System.out.println("resourcePath="+resourcePath);
//      resourcePath = resourcePath.substring(resourcePath.indexOf("/localhost/client"));
//    }
  }
  System.out.println("real path="+config.getServletContext().getRealPath(res));
//  System.out.println("url="+url);
//  System.out.println("url path="+url.getPath());
//  System.out.println("url file="+url.getFile());
//  System.out.println("url host="+url.getHost());

  System.out.println("filename="+res);
  
  // Calcolo il classpath estraendolo dai file JNLP
  String archive = RELATIVE_PATH+"signed-jars/it.saga.pubblici.menu.client.jar";
  String sicrawebResources = caricaJarDaJNLP(is);
  String jbossClient = caricaJarDaJNLP(new FileInputStream(config.getServletContext().getRealPath(specJnlp)));
  String actalisSiaCapi = caricaJarDaJNLP(new FileInputStream(config.getServletContext().getRealPath("actalis-siacapi.jnlp")));
  
  archive = archive+","+jbossClient+","+sicrawebResources+","+actalisSiaCapi;
  
 
  %>

<HTML>
  <HEAD>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"/>
    <TITLE>Saga S.p.A.  -  SicraWeb  -  <%=title%></TITLE>
    <script language="javascript">
      function init() {        
        window.onbeforeunload = confirmUnload;
        window.onunload = onUnload;
      }
      
      function confirmUnload() {
        if (document.sicraweb != null && !document.sicraweb.hasBeenClosed()) return "LA CHIUSURA DELLA PAGINA COMPORTA LA CHIUSURA DELL'APPLICAZIONE.";
      }
      
      function onUnload() {
        if (document.sicraweb != null & !document.sicraweb.hasBeenClosed()) document.sicraweb.close();
      }
      
    </script>
  </HEAD>
  <BODY onload="init();">
    <!--h2><%=title%></h2-->
    <applet width="1024" height="670" name="sicraweb" archive="<%=archive%>" code="it.saga.pubblici.menu.MnuMainApplet">
      <param name="argomenti" value="<%=appUrl%>;<%=j2eeUser%>;<%=j2eePassword%>"/>
      <param name="menuMaggioli" value="<%=menuParam%>"/>
      Your browser does not support the applet tag.
    </applet>
  </BODY>
</HTML>

<%! 
  private Document loadXmlDocument(InputStream input) throws Exception
  {
    // Step 1: create a DocumentBuilderFactory and configure it
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    //factory.setValidating(validate);
    
    // Step 2: create a DocumentBuilder that satisfies the constraints
    DocumentBuilder builder = factory.newDocumentBuilder();
    //log.debug("builder validate="+builder.isValidating());
   
//      // imposto l'entity resolver
//      if (entityResolver != null)
//      {
//        //if (log.isDebugEnabled()) log.debug("setting entity resolver: "+entityResolver);
//        builder.setEntityResolver(entityResolver);
//      }
      
    // Step 3: parse the input file
    Document doc = builder.parse( input );  
    return doc;
  }
  
  /**
   * Estrai i parametri da connessione dal JNLP 
   */
  private String[] caricaParametriConnessioneDaJNLP(InputStream jnlpInputStream) throws Exception
  {
    Document doc = loadXmlDocument(jnlpInputStream);
    Element jnlp = doc.getDocumentElement();
    NodeList tt = jnlp.getElementsByTagName("title");
    String title = null;
    Element elTitle = (Element)tt.item(0);
    if (elTitle != null)
    {
      title = elTitle.getFirstChild().getNodeValue();
      System.out.println("title="+title);
    }    
        
    NodeList nl = jnlp.getElementsByTagName("application-desc");
    Element applicationDesc = (Element)nl.item(0);
    NodeList arguments = applicationDesc.getElementsByTagName("argument");
    String args = null;
    for (int i=0; i<arguments.getLength(); i++) 
    {
      Element el = (Element)arguments.item(i);
      String arg = el.getFirstChild().getNodeValue();
      System.out.println("arg #"+i+": "+arg);
      if (args == null) args = arg;
      else              args = args+";"+arg;

    }
    return new String[] {args, title};    
  }  
  
  private String caricaJarDaJNLP(InputStream jnlpInputStream) throws Exception
  {
    Document doc = loadXmlDocument(jnlpInputStream);
    Element jnlp = doc.getDocumentElement();
    NodeList nl = jnlp.getElementsByTagName("jar");
    
    String archive = null; 
    for (int i=0; i<nl.getLength(); i++) 
    {
      Element jar = (Element)nl.item(i);
      String href = jar.getAttribute("href");
      if (href.startsWith("/client")) href = href.substring(8);
      //System.out.println(href);
      href = RELATIVE_PATH+href; 
      if (archive == null) archive = href;
      else archive = archive+","+href;
    }
    //System.out.println("archive="+archive);
    return archive;
  }
  
  private String caricaNativeLibsDaJNLP(InputStream jnlpInputStream) throws Exception
  {
    Document doc = loadXmlDocument(jnlpInputStream);
    Element jnlp = doc.getDocumentElement();
    NodeList nl = jnlp.getElementsByTagName("nativelib");
    
    String archive = null; 
    for (int i=0; i<nl.getLength(); i++) 
    {
      Element jar = (Element)nl.item(i);
      String href = jar.getAttribute("href");
      if (href.startsWith("/client")) href = href.substring(8);
      //System.out.println(href);
      href = RELATIVE_PATH+href;
      
      if (archive == null) archive = href;
      else archive = archive+","+href;
    }
    //System.out.println("archive="+archive);
    return archive;
  }
  
  private String caricaNuovoMenuDaJNLP(FileInputStream jnlpInputStream) throws Exception
  {
    String result = "false";
    Document doc = loadXmlDocument(jnlpInputStream);
    Element jnlp = doc.getDocumentElement();
    NodeList nl = jnlp.getElementsByTagName("resources");
    Element resources = (Element)nl.item(0);
    NodeList properties = resources.getElementsByTagName("property");
    for (int i=0; i<properties.getLength(); i++) 
    {
      Element el = (Element)properties.item(i);
      if(el.getAttribute("name") != null && el.getAttribute("name").equals("menu.maggioli"))
      {
        result = el.getAttribute("value");
      }
    }
    
    return result;
  }
  
  
  %>