?? shopservlet.java
字號:
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* http://www.opensource.org/licenses/cddl1.php
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* http://www.opensource.org/licenses/cddl1.php. If
* applicable, add the following below this CDDL HEADER,
* with the fields enclosed by brackets "[]" replaced
* with your own identifying information:
* Portions Copyright [yyyy]
* [name of copyright owner]
*/
/*
* $(@)ShopServlet.java $Revision: 1.3 $ $Date: 2006/08/04 23:43:04 $
*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*/
/*
* @(#)ShopServlet.java 1.1 05/11/09
*
* Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
*/
package com.sun.dream.shop;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.Properties;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sun.sjc.idtv.vod.shared.data.*;
import com.sun.sjc.idtv.vod.server.conductor.*;
import com.sun.sjc.idtv.vod.server.subscriber.*;
import javax.naming.*;
import javax.rmi.*;
import java.sql.SQLException;
import com.sun.dream.AuthPlugin;
/**
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class ShopServlet extends HttpServlet {
private Vector shoppingCart = null;
private HashMap cartMap = null;
private static final SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy-kk:mm:ss");
public static Properties iProperties = new Properties();
private SessionCache sessioncache;
private String sessionId;
private conductor conductor1;
private SubscriberManagementSystem sms;
private AuthPlugin authPlugin;
private Class authPluginClass;
public void init() throws ServletException {
super.init();
sessioncache = (SessionCache)getServletContext().getAttribute("sessioncache");
if (sessioncache == null) {
try {
sessioncache = new SessionCache();
getServletContext().setAttribute("sessioncache",sessioncache);
} catch (Exception ex) {
System.out.println("Unable to create: SessionCache "+ex.getMessage());
}
}
authPluginClass = (Class) getServletContext().getAttribute("authPluginClass");
try {
if (authPluginClass == null) {
String authPluginClassName = getServletContext().getInitParameter("AuthPluginClassName");
if (authPluginClassName == null) {
System.err.println("authPluginClassName is NULL ");
} else {
System.out.println("authPluginClassName is "+authPluginClassName);
authPluginClass = Class.forName(authPluginClassName);
Class authPluginInterface = Class.forName("com.sun.dream.AuthPlugin");
if (authPluginInterface.isAssignableFrom(authPluginClass)) {
getServletContext().setAttribute("authPluginClass", authPluginClass);
} else {
authPluginClass = null;
System.err.println("authPluginClass is not valid instance of com.sun.dream.AuthPlugin");
}
}
}
if (authPluginClass != null)
authPlugin = (AuthPlugin) authPluginClass.newInstance();
} catch (Exception ex) {
authPluginClass = null;
System.err.println("authPluginClass could not be found");
ex.printStackTrace();
}
conductor1 = (conductor)getServletContext().getAttribute("conductor");
if (conductor1 == null) {
try {
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("java:comp/env/ejb/conductor");
conductor1 = (conductor) ((conductorHome)PortableRemoteObject.narrow(objRef, conductorHome.class)).create();
getServletContext().setAttribute("conductor", conductor1);
} catch (Exception ex) {
System.out.println("Unable to create: "+ "java:comp/env/ejb/conductor. "+ ex.getMessage());
}
}
sms = (SubscriberManagementSystem)getServletContext().getAttribute("sms");
if (sms == null) {
try {
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("java:comp/env/ejb/SubscriberManagementSystem");
sms = (SubscriberManagementSystem) ((SubscriberManagementSystemHome)PortableRemoteObject.narrow(objRef, SubscriberManagementSystemHome.class)).create();
getServletContext().setAttribute("sms", sms);
} catch (Exception ex) {
System.out.println("Unable to create: "+ "java:comp/env/ejb/SubscriberManagementSystem. "+ ex.getMessage());
}
}
ServletConfig config = this.getServletConfig();
String dbDriver = config.getInitParameter(Const.DATABASE_DRIVER);
String dbURL = config.getInitParameter(Const.DATABASE_URL);
String dbUsername = config.getInitParameter(Const.DATABASE_USERNAME);
String dbPasswd = config.getInitParameter(Const.DATABASE_PASSWORD);
if (dbDriver != null && !"".equals(dbDriver.trim())) {
iProperties.put(Const.DATABASE_DRIVER, dbDriver);
}
if (dbURL != null && !"".equals(dbURL.trim())) {
iProperties.put(Const.DATABASE_URL, dbURL);
}
if (dbUsername != null && !"".equals(dbUsername.trim())) {
iProperties.put(Const.DATABASE_USERNAME, dbUsername);
}
if (dbPasswd != null && !"".equals(dbPasswd.trim())) {
iProperties.put(Const.DATABASE_PASSWORD, dbPasswd);
}
}
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
String userID = null;
if (authPlugin.authenticateUser(request, response)) {
userID = authPlugin.getUserId();
sessionId = authPlugin.getSessionId();
sessioncache.setAttribute(sessionId, "userid", userID);
} else
System.err.println("ERROR: User has not authenticated");
String action = request.getParameter("shop_action");
if (action == null || "".equals(action))
throw new ShopException(ShopException.NULL_ARGUMENT,
"Missing required action parameter.");
if ("add_cart".equals(action)) {
doAddCart(request, response);
} else if ("validate_payment".equals(action)){
doValidatePayment(request,response);
} else if ("confirm_payment".equals(action)) {
doConfirmPayment(request, response);
} else if ("buy_rights".equals(action)) {
doBuyRights(request, response);
} else if ("transfer_rights".equals(action)) {
doTransferRights(request, response);
} else if ("assert_fairuse".equals(action)) {
doFairUse(request, response);
}
}catch (ShopException ex) {
request.setAttribute("opera.exception", ex.getMessage());
}catch (SQLException sqlEx) {
System.out.println("SQL Error in ShopServlet");
}
}
protected void doAddCart(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
String itemID = request.getParameter("item_id");
String qty = request.getParameter("quantity");
//should from db
String descr = request.getParameter("descr");
String listPrice = request.getParameter("list_price");
String price = request.getParameter("price");
String cat = request.getParameter("category");
if (itemID == null) {
throw new ShopException(ShopException.NULL_ARGUMENT,
"Unable to add to cart: missing item_id");
}
HttpSession session = request.getSession();
shoppingCart = (Vector) sessioncache.getAttribute(sessionId, "opera.cart");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -