Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.geronimo.samples.daytrader.web;
 
 import  javax.servlet.*;
 import  javax.servlet.http.*;
 
 
TradeServletAction provides servlet specific client side access to each of the Trade brokerage user operations. These include login, logout, buy, sell, getQuote, etc. TradeServletAction manages a web interface to Trade handling HttpRequests/HttpResponse objects and forwarding results to the appropriate JSP page for the web interface. TradeServletAction invokes TradeAction methods to actually perform each trading operation.
 
 public class TradeServletAction {
 
     private TradeServices tAction = null;
 
     TradeServletAction() {
         if (TradeConfig.getAccessMode() == .)
              = new TradeAction();
         else if (TradeConfig.getAccessMode() == .)
              = new TradeWebSoapProxy();
     }

    
Display User Profile information such as address, email, etc. for the given Trader Dispatch to the Trade Account JSP for display

Parameters:
userID The User to display profile info
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
results A short description of the results/success of this web request provided on the web page
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
 
     void doAccount(ServletContext ctx, HttpServletRequest req,
             HttpServletResponse respString userIDString results)
             throws javax.servlet.ServletException, java.io.IOException {
         try {
 
             AccountDataBean accountData = .getAccountData(userID);
             AccountProfileDataBean accountProfileData = 
                     .getAccountProfileData(userID);
             ArrayList orderDataBeans = (TradeConfig.getLongRun() ? new ArrayList() : (ArrayList.getOrders(userID));
             
             req.setAttribute("accountData"accountData);
             req.setAttribute("accountProfileData"accountProfileData);
             req.setAttribute("orderDataBeans"orderDataBeans);
             req.setAttribute("results"results);
             requestDispatch(ctxreqrespuserID, TradeConfig
                     .getPage(.));
         } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                             // error so I will
             // forward them to another page rather than throw a 500
             req.setAttribute("results"results
                     + "could not find account for userID = " + userID);
             requestDispatch(ctxreqrespuserID, TradeConfig
                     .getPage(.));
             // log the exception with an error level of 3 which means, handled
             // exception but would invalidate a automation run
             Log
                    .error(
                            "TradeServletAction.doAccount(...)",
                            "illegal argument, information should be in exception string",
                            e);
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException("TradeServletAction.doAccount(...)"
                    + " exception user =" + userIDe);
        }
    }

    
Update User Profile information such as address, email, etc. for the given Trader Dispatch to the Trade Account JSP for display If any in put is incorrect revert back to the account page w/ an appropriate message

Parameters:
userID The User to upddate profile info
password The new User password
cpassword Confirm password
fullname The new User fullname info
address The new User address info
cc The new User credit card info
email The new User email info
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doAccountUpdate(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString password,
            String cpasswordString fullNameString address,
            String creditcardString email)
            throws javax.servlet.ServletException, java.io.IOException {
        String results = "";
        // First verify input data
        boolean doUpdate = true;
        if (password.equals(cpassword) == false) {
            results = "Update profile error: passwords do not match";
            doUpdate = false;
        } else if (password.length() <= 0 || fullName.length() <= 0
                || address.length() <= 0 || creditcard.length() <= 0
                || email.length() <= 0) {
            results = "Update profile error: please fill in all profile information fields";
            doUpdate = false;
        }
        AccountProfileDataBean accountProfileData = new AccountProfileDataBean(
                userIDpasswordfullNameaddressemailcreditcard);
        try {
            if (doUpdate) {
                accountProfileData = 
                        .updateAccountProfile(accountProfileData);
                results = "Account profile update successful";
            }
        } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                            // error so I will
            // forward them to another page rather than throw a 500
            req
                    .setAttribute(
                            "results",
                            results
                                    + "invalid argument, check userID is correct, and the database is populated"
                                    + userID);
            Log
                    .error(
                            e,
                            "TradeServletAction.doAccount(...)",
                            "illegal argument, information should be in exception string",
                            "treating this as a user error and forwarding on to a new page");
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException(
                    "TradeServletAction.doAccountUpdate(...)"
                            + " exception user =" + userIDe);
        }
        doAccount(ctxreqrespuserIDresults);
    }

    
Buy a new holding of shares for the given trader Dispatch to the Trade Portfolio JSP for display

Parameters:
userID The User buying shares
symbol The stock to purchase
amount The quantity of shares to purchase
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doBuy(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString symbol,
            String quantitythrows ServletException, IOException {
        String results = "";
        try {
            OrderDataBean orderData = .buy(userIDsymbolnew Double(
                    quantity).doubleValue(), .);
            req.setAttribute("orderData"orderData);
            req.setAttribute("results"results);
        } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                            // error so I will
            // forward them to another page rather than throw a 500
            req.setAttribute("results"results + "illegal argument:");
            requestDispatch(ctxreqrespuserID, TradeConfig
                    .getPage(.));
            // log the exception with an error level of 3 which means, handled
            // exception but would invalidate a automation run
            Log.error(e"TradeServletAction.doBuy(...)",
                    "illegal argument. userID = " + userID"symbol = "
                            + symbol);
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException("TradeServletAction.buy(...)"
                    + " exception buying stock " + symbol + " for user "
                    + userIDe);
        }
        requestDispatch(ctxreqrespuserID, TradeConfig
                .getPage(.));
    }

    
Create the Trade Home page with personalized information such as the traders account balance Dispatch to the Trade Home JSP for display

Parameters:
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
results A short description of the results/success of this web request provided on the web page
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doHome(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString results)
            throws javax.servlet.ServletException, java.io.IOException {
        BigDecimal balance;
        String result = "";
        try {
            AccountDataBean accountData = .getAccountData(userID);
            Collection holdingDataBeans = .getHoldings(userID);
            // Edge Caching:
            // Getting the MarketSummary has been moved to the JSP
            // MarketSummary.jsp. This makes the MarketSummary a
            // standalone "fragment", and thus is a candidate for
            // Edge caching.
            // marketSummaryData = tAction.getMarketSummary();
            req.setAttribute("accountData"accountData);
            req.setAttribute("holdingDataBeans"holdingDataBeans);
            // See Edge Caching above
            // req.setAttribute("marketSummaryData", marketSummaryData);
            req.setAttribute("results"results);
        } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                            // error so I will
            // forward them to another page rather than throw a 500
            req.setAttribute("results"results + "check userID = " + userID
                    + " and that the database is populated");
            requestDispatch(ctxreqrespuserID, TradeConfig
                    .getPage(.));
            // log the exception with an error level of 3 which means, handled
            // exception but would invalidate a automation run
            Log
                    .error(
                            "TradeServletAction.doHome(...)"
                                    + "illegal argument, information should be in exception string"
                                    + "treating this as a user error and forwarding on to a new page",
                            e);
        } catch (javax.ejb.FinderException e) {
            // this is a user error so I will
            // forward them to another page rather than throw a 500
            req.setAttribute("results"results
                    + "\nCould not find account for + " + userID);
            // requestDispatch(ctx, req, resp,
            // TradeConfig.getPage(TradeConfig.HOME_PAGE));
            // log the exception with an error level of 3 which means, handled
            // exception but would invalidate a automation run
            Log
                    .error(
                            "TradeServletAction.doHome(...)"
                                    + "Error finding account for user "
                                    + userID
                                    + "treating this as a user error and forwarding on to a new page",
                            e);
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException("TradeServletAction.doHome(...)"
                    + " exception user =" + userIDe);
        }
        requestDispatch(ctxreqrespuserID, TradeConfig
                .getPage(.));
    }

    
Login a Trade User. Dispatch to the Trade Home JSP for display

Parameters:
userID The User to login
passwd The password supplied by the trader used to authenticate
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
results A short description of the results/success of this web request provided on the web page
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doLogin(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString passwd)
            throws javax.servlet.ServletException, java.io.IOException {
        String results = "";
        try {
            // Got a valid userID and passwd, attempt login
            AccountDataBean accountData = .login(userIDpasswd);
            if (accountData != null) {
                HttpSession session = req.getSession(true);
                session.setAttribute("uidBean"userID);
                session.setAttribute("sessionCreationDate",
                        new java.util.Date());
                results = "Ready to Trade";
                doHome(ctxreqrespuserIDresults);
                return;
            } else {
                req.setAttribute("results"results
                        + "\nCould not find account for user=" + userID);
                // log the exception with an error level of 3 which means,
                // handled exception but would invalidate a automation run
                Log.log(
                                "TradeServletAction.doLogin(...)",
                                "Error finding account for user " + userID + "",
                                "user entered a bad username or the database is not populated");
                throw new NullPointerException("User does not exist or password is incorrect!");
            }
        } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                            // error so I will
            // forward them to another page rather than throw a 500
            req.setAttribute("results"results + "illegal argument:"
                    + e.getMessage());
            // log the exception with an error level of 3 which means, handled
            // exception but would invalidate a automation run
            Log
                    .error(
                            e,
                            "TradeServletAction.doLogin(...)",
                            "illegal argument, information should be in exception string",
                            "treating this as a user error and forwarding on to a new page");
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException("TradeServletAction.doLogin(...)"
                    + "Exception logging in user=" + userID + " with password="
                    + passwd + "; " + e.getMessage(), e);
        }
        requestDispatch(ctxreqrespuserID, TradeConfig
                .getPage(.));
    }

    
Logout a Trade User Dispatch to the Trade Welcome JSP for display

Parameters:
userID The User to logout
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
results A short description of the results/success of this web request provided on the web page
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doLogout(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDthrows ServletException,
            IOException {
        String results = "";
        try {
            .logout(userID);
        } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                            // error so I will
            // forward them to another page, at the end of the page.
            req.setAttribute("results"results + "illegal argument:"
                    + e.getMessage());
            // log the exception with an error level of 3 which means, handled
            // exception but would invalidate a automation run
            Log
                    .error(
                            e,
                            "TradeServletAction.doLogout(...)",
                            "illegal argument, information should be in exception string",
                            "treating this as a user error and forwarding on to a new page");
        } catch (Exception e) {
            // log the exception and foward to a error page
            Log.error(e"TradeServletAction.doLogout(...):",
                    "Error logging out" + userID"fowarding to an error page");
            // set the status_code to 500
            throw new ServletException("TradeServletAction.doLogout(...)"
                    + "exception logging out user " + userIDe);
        }
        HttpSession session = req.getSession();
        if (session != null) {
            session.invalidate();
        }
        Object o = req.getAttribute("TSS-RecreateSessionInLogout");
        if (o != null && ((Booleano).equals(.)) {
            // Recreate Session object before writing output to the response
            // Once the response headers are written back to the client the
            // opportunity
            // to create a new session in this request may be lost
            // This is to handle only the TradeScenarioServlet case
            session = req.getSession(true);
        }
        requestDispatch(ctxreqrespuserID, TradeConfig
                .getPage(.));
    }

    
Retrieve the current portfolio of stock holdings for the given trader Dispatch to the Trade Portfolio JSP for display

Parameters:
userID The User requesting to view their portfolio
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
results A short description of the results/success of this web request provided on the web page
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doPortfolio(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString results)
            throws ServletException, IOException {
        try {
            // Get the holdiings for this user
            Collection quoteDataBeans = new ArrayList();
            Collection holdingDataBeans = .getHoldings(userID);
            // Walk through the collection of user
            // holdings and creating a list of quotes
            if (holdingDataBeans.size() > 0) {
                Iterator it = holdingDataBeans.iterator();
                while (it.hasNext()) {
                    HoldingDataBean holdingData = (HoldingDataBeanit.next();
                    QuoteDataBean quoteData = .getQuote(holdingData
                            .getQuoteID());
                    quoteDataBeans.add(quoteData);
                }
            } else {
                results = results + ".  Your portfolio is empty.";
            }
            req.setAttribute("results"results);
            req.setAttribute("holdingDataBeans"holdingDataBeans);
            req.setAttribute("quoteDataBeans"quoteDataBeans);
            requestDispatch(ctxreqrespuserID, TradeConfig
                    .getPage(.));
        } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                            // error so I will
            // forward them to another page rather than throw a 500
            req.setAttribute("results"results + "illegal argument:"
                    + e.getMessage());
            requestDispatch(ctxreqrespuserID, TradeConfig
                    .getPage(.));
            // log the exception with an error level of 3 which means, handled
            // exception but would invalidate a automation run
            Log
                    .error(
                            e,
                            "TradeServletAction.doPortfolio(...)",
                            "illegal argument, information should be in exception string",
                            "user error");
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException("TradeServletAction.doPortfolio(...)"
                    + " exception user =" + userIDe);
        }
    }

    
Retrieve the current Quote for the given stock symbol Dispatch to the Trade Quote JSP for display

Parameters:
userID The stock symbol used to get the current quote
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doQuotes(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString symbols)
            throws ServletException, IOException {
        String results = "";
        // Edge Caching:
        // Getting Quotes has been moved to the JSP
        // Quote.jsp. This makes each Quote a
        // standalone "fragment", and thus is a candidate for
        // Edge caching.
        //			
        requestDispatch(ctxreqrespuserID, TradeConfig
                .getPage(.));
    }

    
Register a new trader given the provided user Profile information such as address, email, etc. Dispatch to the Trade Home JSP for display

Parameters:
userID The User to create
passwd The User password
fullname The new User fullname info
ccn The new User credit card info
money The new User opening account balance
address The new User address info
email The new User email info
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
Returns:
The userID of the new trader
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doRegister(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString passwd,
            String cpasswdString fullnameString ccn,
            String openBalanceStringString emailString address)
            throws ServletException, IOException {
        String results = "";
        try {
            // Validate user passwords match and are atleast 1 char in length
            if ((passwd.equals(cpasswd)) && (passwd.length() >= 1)) {
                AccountDataBean accountData = .register(userIDpasswd,
                        fullnameaddressemailccnnew BigDecimal(
                                openBalanceString));
                if (accountData == null) {
                    results = "Registration operation failed;";
                    ..println(results);
                    req.setAttribute("results"results);
                    requestDispatch(ctxreqrespuserID, TradeConfig
                            .getPage(.));
                } else {
                    doLogin(ctxreqrespuserIDpasswd);
                    results = "Registration operation succeeded;  Account "
                            + accountData.getAccountID() + " has been created.";
                    req.setAttribute("results"results);
                }
            } else {
                // Password validation failed
                results = "Registration operation failed, your passwords did not match";
                ..println(results);
                req.setAttribute("results"results);
                requestDispatch(ctxreqrespuserID, TradeConfig
                        .getPage(.));
            }
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException("TradeServletAction.doRegister(...)"
                    + " exception user =" + userIDe);
        }
    }

    
Sell a current holding of stock shares for the given trader. Dispatch to the Trade Portfolio JSP for display

Parameters:
userID The User buying shares
symbol The stock to sell
indx The unique index identifying the users holding to sell
ctx the servlet context
req the HttpRequest object
resp the HttpResponse object
Throws:
javax.servlet.ServletException If a servlet specific exception is encountered
javax.io.IOException If an exception occurs while writing results back to the user
    void doSell(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDInteger holdingID)
            throws ServletException, IOException {
        String results = "";
        try {
            OrderDataBean orderData = .sell(userIDholdingID,
                    .);
            req.setAttribute("orderData"orderData);
            req.setAttribute("results"results);
        } catch (java.lang.IllegalArgumentException e) { // this is a user
                                                            // error so I will
            // just log the exception and then later on I will redisplay the
            // portfolio page
            // because this is just a user exception
            Log
                    .error(
                            e,
                            "TradeServletAction.doSell(...)",
                            "illegal argument, information should be in exception string",
                            "user error");
        } catch (Exception e) {
            // log the exception with error page
            throw new ServletException("TradeServletAction.doSell(...)"
                    + " exception selling holding " + holdingID + " for user ="
                    + userIDe);
        }
        requestDispatch(ctxreqrespuserID, TradeConfig
                .getPage(.));
    }
    void doWelcome(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString statusthrows ServletException,
            IOException {
        req.setAttribute("results"status);
        requestDispatch(ctxreqrespnull, TradeConfig
                .getPage(.));
    }
    private void requestDispatch(ServletContext ctx, HttpServletRequest req,
            HttpServletResponse respString userIDString page)
            throws ServletException, IOException {
        ctx.getRequestDispatcher(page).include(reqresp);
    }
    private void sendRedirect(HttpServletResponse respString page)
            throws ServletException, IOException {
        resp.sendRedirect(resp.encodeRedirectURL(page));
    }
New to GrepCode? Check out our FAQ X