KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > components > quoteviewer > QuoteViewerApp


1
2 package components.quoteviewer;
3
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.PrintWriter JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.Hashtable JavaDoc;
9
10 import javax.servlet.http.HttpServlet JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 import org.jahia.params.ParamBean;
15 import org.jahia.utils.FileUtils;
16
17
18 public class QuoteViewerApp extends HttpServlet JavaDoc {
19
20
21     private String JavaDoc version = "1.2";
22
23     private static final String JavaDoc TEMPLATE_PATH = "\\quoteviewer" + File.separator + "templates" + File.separator;
24     private static final String JavaDoc TEMPLATE_FILE = "std.html";
25
26     /***
27         * doGet
28         * EV 10.12.2000
29         *
30         */

31     public void doGet( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response ) throws IOException JavaDoc
32     {
33         PrintWriter JavaDoc out = response.getWriter();
34         //System.out.println( "Quote View App ver" + version + " responding to " + request.getRemoteAddr() + " GET METHOD " );
35
//System.out.println( "Current application user : " + request.getRemoteUser());
36
out.println( drawForm( request, response, "" ) );
37     } // end doGet
38

39
40
41     /***
42         * doPost
43         * EV 10.12.2000
44         *
45         */

46     public void doPost( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response ) throws IOException JavaDoc
47     {
48         //System.out.println( "Quote View App ver" + version + " responding to " + request.getRemoteAddr() + " POST METHOD " );
49
PrintWriter JavaDoc out = response.getWriter();
50         String JavaDoc theQuote = request.getParameter( "quote" );
51         String JavaDoc windowParams = "width=800,height=600,scrollbars=1,status=1,resizable=1";
52         String JavaDoc 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     } // end doPost
58

59
60
61     /***
62         * drawForm
63         * EV 10.12.2000
64         *
65         */

66     public String JavaDoc drawForm( HttpServletRequest JavaDoc request,
67                     HttpServletResponse JavaDoc response,
68                     String JavaDoc defaultQuote )
69     {
70             String JavaDoc 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 JavaDoc quoteField = "<input type=\"text\" name=\"quote\" maxlength=\"5\" size=\"5\" value=\"" + defaultQuote + "\"><br>\n";
76             String JavaDoc submitField = "<input type=\"submit\" value=\"View Quote\"><br>\n";
77             String JavaDoc filePath = jParams.settings().getComponentsDiskPath() + TEMPLATE_PATH + TEMPLATE_FILE;
78
79             Hashtable JavaDoc tokens = new Hashtable JavaDoc();
80             tokens.put( "quoteField", quoteField );
81             tokens.put( "submitField", submitField );
82
83             String JavaDoc 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     } // end drawForm
95

96
97
98     /***
99         * composeSource
100         * EV 10.12.2000
101         *
102         */

103     public String JavaDoc composeSource( String JavaDoc filePath, Hashtable JavaDoc tokens )
104     {
105         String JavaDoc contents = "";
106         try {
107             contents = FileUtils.getInstance().readFile( filePath );
108         } catch (Exception JavaDoc e) {
109             //System.out.println( "QuoteViewerApp > Error occured : " + e );
110
contents = "- Error in reading file - ";
111             return contents;
112         }
113
114         int pos = 0;
115         String JavaDoc theKey = "";
116         Enumeration JavaDoc keys = tokens.keys();
117         while (keys.hasMoreElements()) {
118             theKey = (String JavaDoc) 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     } // end composeSource
129

130
131
132 } // end QuoteViewerApp
133
Popular Tags