001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.samples.daytrader.web; 018 019 import javax.servlet.*; 020 import javax.servlet.http.*; 021 022 import org.apache.geronimo.samples.daytrader.direct.*; 023 import org.apache.geronimo.samples.daytrader.util.*; 024 025 import java.io.IOException; 026 import org.apache.geronimo.samples.daytrader.*; 027 028 /** 029 * 030 * TradeAppServlet provides the standard web interface to Trade and can be 031 * accessed with the Go Trade! link. Driving benchmark load using this interface 032 * requires a sophisticated web load generator that is capable of filling HTML 033 * forms and posting dynamic data. 034 */ 035 036 public class TradeAppServlet extends HttpServlet { 037 038 /** 039 * Servlet initialization method. 040 */ 041 public void init(ServletConfig config) throws ServletException { 042 super.init(config); 043 java.util.Enumeration en = config.getInitParameterNames(); 044 while (en.hasMoreElements()) { 045 String parm = (String) en.nextElement(); 046 String value = config.getInitParameter(parm); 047 TradeConfig.setConfigParam(parm, value); 048 } 049 try { 050 TradeDirect.init(); 051 } catch (Exception e) { 052 Log.error(e, 053 "TradeAppServlet:init -- Error initializing TradeDirect"); 054 } 055 } 056 057 /** 058 * Returns a string that contains information about TradeScenarioServlet 059 * 060 * @return The servlet information 061 */ 062 public java.lang.String getServletInfo() { 063 return "TradeAppServlet provides the standard web interface to Trade"; 064 } 065 066 /** 067 * Process incoming HTTP GET requests 068 * 069 * @param request 070 * Object that encapsulates the request to the servlet 071 * @param response 072 * Object that encapsulates the response from the servlet 073 */ 074 public void doGet(javax.servlet.http.HttpServletRequest request, 075 javax.servlet.http.HttpServletResponse response) 076 throws ServletException, IOException { 077 performTask(request, response); 078 } 079 080 /** 081 * Process incoming HTTP POST requests 082 * 083 * @param request 084 * Object that encapsulates the request to the servlet 085 * @param response 086 * Object that encapsulates the response from the servlet 087 */ 088 public void doPost(javax.servlet.http.HttpServletRequest request, 089 javax.servlet.http.HttpServletResponse response) 090 throws ServletException, IOException { 091 performTask(request, response); 092 } 093 094 /** 095 * Main service method for TradeAppServlet 096 * 097 * @param request 098 * Object that encapsulates the request to the servlet 099 * @param response 100 * Object that encapsulates the response from the servlet 101 */ 102 public void performTask(HttpServletRequest req, HttpServletResponse resp) 103 throws ServletException, IOException { 104 105 String action = null; 106 String userID = null; 107 // String to create full dispatch path to TradeAppServlet w/ request 108 // Parameters 109 String dispPath = null; // Dispatch Path to TradeAppServlet 110 111 resp.setContentType("text/html"); 112 TradeServletAction tsAction = new TradeServletAction(); 113 114 // Dyna - need status string - prepended to output 115 action = req.getParameter("action"); 116 117 ServletContext ctx = getServletConfig().getServletContext(); 118 119 if (action == null) { 120 tsAction.doWelcome(ctx, req, resp, ""); 121 return; 122 } else if (action.equals("login")) { 123 userID = req.getParameter("uid"); 124 String passwd = req.getParameter("passwd"); 125 String inScenario = req.getParameter("inScenario"); 126 tsAction.doLogin(ctx, req, resp, userID, passwd); 127 return; 128 } else if (action.equals("register")) { 129 userID = req.getParameter("user id"); 130 String passwd = req.getParameter("passwd"); 131 String cpasswd = req.getParameter("confirm passwd"); 132 String fullname = req.getParameter("Full Name"); 133 String ccn = req.getParameter("Credit Card Number"); 134 String money = req.getParameter("money"); 135 String email = req.getParameter("email"); 136 String smail = req.getParameter("snail mail"); 137 tsAction.doRegister(ctx, req, resp, userID, passwd, cpasswd, 138 fullname, ccn, money, email, smail); 139 return; 140 } 141 142 // The rest of the operations require the user to be logged in - 143 // Get the Session and validate the user. 144 HttpSession session = req.getSession(); 145 userID = (String) session.getAttribute("uidBean"); 146 147 if (userID == null) { 148 System.out 149 .println("TradeAppServlet service error: User Not Logged in"); 150 tsAction.doWelcome(ctx, req, resp, "User Not Logged in"); 151 return; 152 } 153 if (action.equals("quotes")) { 154 String symbols = req.getParameter("symbols"); 155 tsAction.doQuotes(ctx, req, resp, userID, symbols); 156 } else if (action.equals("buy")) { 157 String symbol = req.getParameter("symbol"); 158 String quantity = req.getParameter("quantity"); 159 tsAction.doBuy(ctx, req, resp, userID, symbol, quantity); 160 } else if (action.equals("sell")) { 161 int holdingID = Integer.parseInt(req.getParameter("holdingID")); 162 tsAction.doSell(ctx, req, resp, userID, new Integer(holdingID)); 163 } else if (action.equals("portfolio") 164 || action.equals("portfolioNoEdge")) { 165 tsAction.doPortfolio(ctx, req, resp, userID, "Portfolio as of " 166 + new java.util.Date()); 167 } else if (action.equals("logout")) { 168 tsAction.doLogout(ctx, req, resp, userID); 169 } else if (action.equals("home")) { 170 tsAction.doHome(ctx, req, resp, userID, "Ready to Trade"); 171 } else if (action.equals("account")) { 172 tsAction.doAccount(ctx, req, resp, userID, ""); 173 } else if (action.equals("update_profile")) { 174 String password = req.getParameter("password"); 175 String cpassword = req.getParameter("cpassword"); 176 String fullName = req.getParameter("fullname"); 177 String address = req.getParameter("address"); 178 String creditcard = req.getParameter("creditcard"); 179 String email = req.getParameter("email"); 180 tsAction.doAccountUpdate(ctx, req, resp, userID, 181 password == null ? "" : password.trim(), 182 cpassword == null ? "" : cpassword.trim(), 183 fullName == null ? "" : fullName.trim(), 184 address == null ? "" : address.trim(), 185 creditcard == null ? "" : creditcard.trim(), 186 email == null ? "" : email.trim()); 187 } else { 188 System.out.println("TradeAppServlet: Invalid Action=" + action); 189 tsAction.doWelcome(ctx, req, resp, 190 "TradeAppServlet: Invalid Action" + action); 191 } 192 } 193 194 private void sendRedirect(HttpServletResponse resp, String page) 195 throws ServletException, IOException { 196 resp.sendRedirect(resp.encodeRedirectURL(page)); 197 } 198 199 // URL Path Prefix for dispatching to TradeAppServlet 200 private final static String tasPathPrefix = "/app?action="; 201 202 }