Logo Search packages:      
Sourcecode: jaminid version File versions  Download package

String com::prolixtech::jaminid_examples::ajax::SimpleAjaxServer::demultiplex ( Request  connRequest,
Response  connResponse 
) [inline]

the main function of the oracle, is to demux the request and produce an output that is relative to the request.

in this example, it is a static HTML, that is all we care about for now.

Parameters:
connRequest 
connResponse 
Returns:
the output

Reimplemented from com::prolixtech::jaminid::ContentOracle.

Definition at line 55 of file SimpleAjaxServer.java.

References com::prolixtech::jaminid::Request::getLocation(), com::prolixtech::jaminid::Request::getParam(), com::prolixtech::jaminid::Request::getParamOrNull(), com::prolixtech::jaminid::Response::sendFile(), and com::prolixtech::jaminid::Response::setStatus().

                                                                          {
      HashMap<String,String> outputHash=new HashMap<String,String>();
      System.out.println(connRequest.getLocation());
      
      
      if(connRequest.getLocation().equals("/runsim.html") ){
            String strat = connRequest.getParamOrNull("strategy");
            double d = Double.parseDouble(connRequest.getParam("start"));
            long startDate = new java.util.Date(connRequest.getParam("sday")+"-" + connRequest.getParam("smon") + "-" + connRequest.getParam("syear")).getTime();
                  long endDate = new java.util.Date(connRequest.getParam("eday")+"-" + connRequest.getParam("emon") + "-" + connRequest.getParam("eyear")).getTime();
                  String ticker = connRequest.getParam("ticker");
            if(strat==null){
                  String msg = "<p>Running simulator on symbol <B>" + ticker + 
                        "</B> with an initial investment of <B>$" + dcf.format(d) + "</B>" +
                        " from <B>" + (new Date(startDate)).toString() + "</B> to <B>" + (new Date(endDate)).toString() + "</B></p>";  
                  
                  outputHash.put("RUNSIMDETS", msg);
            } else {
//                      strategy=InsiderTrading&start=10000&ticker=IBM&sday=1&smon=Jan&syear=1985&eday=1&emon=Jan&eyear=2005
                  
                        
                        
                              String mxa = "showtrans.html?strategy="+strat+"&start="+d+"&sdate="+startDate+"&edate="+endDate+"&ticker="+ticker;
                        try {
                                    Thread.sleep(1000 + rand.nextInt(5000));
                              } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                              }
                              
                              double v = rand.nextDouble() * 2 * d;
                        
                        String cl = (v-d>=0 ? "greentext" : "redtext");
                        
                        StringBuffer a = new                      StringBuffer();
                        a.append(strat);
                        a.append("|");
                        a.append("<h1> RandomStrategy </h1>");
                        a.append("<p>Balance: ");
                        a.append(dcf.format(v));
                        a.append("<br/>");
                        a.append("% Change: ");
                        a.append("<span class='"+cl+"'>");
                        a.append(dcf.format((v-d)/d * 100));
                        a.append("</span>");
                        a.append("</p>");
                        
                        a.append("<p><a href="+mxa+">Number of transactions: ");
                        a.append(rand.nextInt(100));
                        a.append("</a></p>");
                        System.out.println(a.toString());
                        return a.toString();
                              
                        
                  
                  
//                      long endDate = new java.util.Date("15-Oct-2005").getTime();
                  
                  

                  
            }
            
      }
      
      
      
      String loc = connRequest.getLocation();
        if(loc.equals("/")) loc = DEFAULT;
        File x = new File(ROOT_PATH + loc);
        
        
        try {
            x = x.getCanonicalFile();
            if(! (x.getAbsolutePath().startsWith(root_canonical))) throw new IOException("Not within filesystem limits");
            if(x.getName().endsWith("html") || x.getName().endsWith("htm")) {
                  StringBuffer a = new StringBuffer();
                  BufferedReader br = new BufferedReader(new FileReader (x));
                  while(true){
                        String line = br.readLine();
                        if(line==null)
                              break;
                        a.append(line);
                        a.append("\n");
                        
                  }
                  br.close();
                  fillInTemplate(a,connRequest,connResponse, outputHash);
                  return a.toString();
                  
            } else {
                  connResponse.sendFile(x);
            }
        } catch (IOException e) {
            connResponse.setStatus(Protocol.NOT_FOUND);
        } catch (Exception e){
            connResponse.setStatus(Protocol.INTERNAL_SERVER_ERROR);
        }
        return "";
        
                
    }


Generated by  Doxygen 1.6.0   Back to index