1 2 package components.quoteviewer; 3 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.PrintWriter ; 7 import java.util.Enumeration ; 8 import java.util.Hashtable ; 9 10 import javax.servlet.http.HttpServlet ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 import org.jahia.params.ParamBean; 15 import org.jahia.utils.FileUtils; 16 17 18 public class QuoteViewerApp extends HttpServlet { 19 20 21 private String version = "1.2"; 22 23 private static final String TEMPLATE_PATH = "\\quoteviewer" + File.separator + "templates" + File.separator; 24 private static final String TEMPLATE_FILE = "std.html"; 25 26 31 public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException 32 { 33 PrintWriter out = response.getWriter(); 34 out.println( drawForm( request, response, "" ) ); 37 } 39 40 41 46 public void doPost( HttpServletRequest request, HttpServletResponse response ) throws IOException 47 { 48 PrintWriter out = response.getWriter(); 50 String theQuote = request.getParameter( "quote" ); 51 String windowParams = "width=800,height=600,scrollbars=1,status=1,resizable=1"; 52 String html = "<script language=\"javascript\">\n"; 53 html += "window.open( 'http://finance.yahoo.com/q?s=" + theQuote + "&d=v1', 'quote', '" + windowParams + "' );"; 54 html += "</script>"; 55 html += drawForm( request, response, theQuote ); 56 out.println(html); 57 } 59 60 61 66 public String drawForm( HttpServletRequest request, 67 HttpServletResponse response, 68 String defaultQuote ) 69 { 70 String html = ""; 71 ParamBean jParams = (ParamBean) request.getAttribute( "org.jahia.params" ); 72 if (jParams == null) 73 return "This application is designed to be run with Jahia"; 74 75 String quoteField = "<input type=\"text\" name=\"quote\" maxlength=\"5\" size=\"5\" value=\"" + defaultQuote + "\"><br>\n"; 76 String submitField = "<input type=\"submit\" value=\"View Quote\"><br>\n"; 77 String filePath = jParams.settings().getComponentsDiskPath() + TEMPLATE_PATH + TEMPLATE_FILE; 78 79 Hashtable tokens = new Hashtable (); 80 tokens.put( "quoteField", quoteField ); 81 tokens.put( "submitField", submitField ); 82 83 String actionURL; 84 if (request.getContextPath().equals("/")) { 85 actionURL = request.getServletPath(); 86 } else { 87 actionURL = request.getContextPath() + request.getServletPath(); 88 } 89 html = "<form method=\"POST\" action=\"" + response.encodeURL(actionURL) + "\">\n"; 90 html += composeSource( filePath, tokens ); 91 html += "</form>\n"; 92 93 return html; 94 } 96 97 98 103 public String composeSource( String filePath, Hashtable tokens ) 104 { 105 String contents = ""; 106 try { 107 contents = FileUtils.getInstance().readFile( filePath ); 108 } catch (Exception e) { 109 contents = "- Error in reading file - "; 111 return contents; 112 } 113 114 int pos = 0; 115 String theKey = ""; 116 Enumeration keys = tokens.keys(); 117 while (keys.hasMoreElements()) { 118 theKey = (String ) keys.nextElement(); 119 pos = contents.indexOf( "<!--" + theKey + "-->" ); 120 if (pos > -1) { 121 contents = contents.substring( 0, pos ) 122 + tokens.get(theKey) 123 + contents.substring( pos, contents.length() ); 124 } 125 } 126 127 return contents; 128 } 130 131 132 } | Popular Tags |