Recommendation.java
资源名称:WAPpush.zip [点击查看]
上传用户:weisa_1
上传日期:2007-10-14
资源大小:287k
文件大小:8k
源码类别:
手机WAP编程
开发平台:
Java
- package push;
- import push.Winner;
- import members.Members;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.util.*;
- /**
- * A class that demonstrates a push service for recommending a site to a friend (by a push message).
- *
- * @version 1.0
- * @since 1.0
- * @see HttpServlet
- */
- public final class Recommendation extends HttpServlet {
- private static final String XML_VERSION = "<?xml version="1.0"?>";
- private static final String CONTENT_TYPE = "text/vnd.wap.wml";
- private static final String DOC_TYPE = "<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"n" +
- " "http://www.wapforum.org/DTD/wml_1.1.xml">";
- private static int numberOfQuestions=0;
- private static String grades;
- private static final Vector questions = new Vector();
- private static final String HOME_PAGE = "/index.jsp";
- private static String feedbackFile = null;
- private static Winner winner = null;
- private static String pushAddressType = null;
- /**
- * <code>doGet</code>
- *
- * @param request a <code>HttpServletRequest</code> value
- * @param response a <code>HttpServletResponse</code> value
- * @exception ServletException if an error occurs
- * @exception IOException if an error occurs
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- /**
- * <code>doPost</code>
- *
- * @param request a <code>HttpServletRequest</code> value
- * @param response a <code>HttpServletResponse</code> value
- * @exception ServletException if an error occurs
- * @exception IOException if an error occurs
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- /* Session tracking. See the description in index.jsp */
- HttpSession session = request.getSession();
- boolean cookiesCheck = request.getHeader("cookie") != null && request.isRequestedSessionIdValid();
- final String jsessionID = cookiesCheck ? "" : ";jsessionid=" + session.getId();
- String action = request.getParameter("action");
- if(action == null) action ="";
- pushAddressType = Members.getPushAddressType();
- /* start generating the response ... */
- response.setContentType(CONTENT_TYPE);
- /* prevent caching of the response */
- response.setHeader("cache-control", "no-cache");
- PrintWriter out = response.getWriter();
- out.println(XML_VERSION);
- out.println(DOC_TYPE);
- out.println("<wml>");
- out.println("<!-- provides a way back using the prev element -->n" +
- "<template>" +
- " <do type="prev">" +
- " <prev/>" +
- " </do>" +
- "</template>");
- if(session.getAttribute("name") == null) {
- out.println("<!-- This service can be only accessed by members -->n" +
- "<card title="Members only" id="thanks">" +
- " <do type="accept" label="Ok">" +
- " <go href="" + request.getContextPath() + HOME_PAGE + jsessionID + ""/>" +
- " </do>" +
- " <p>This service is provided only for members.<br/>" +
- " <anchor title="Ok">Ok" +
- " <go href="" + request.getContextPath() + HOME_PAGE + jsessionID + ""/>" +
- " </anchor>" +
- " </p>" +
- "</card>");
- }
- else if(!action.equals("push")) {
- out.println("<!-- Push message form -->n" +
- "<card title="Tell a friend" id="friend" newcontext="true">" +
- "<p>Tell a friend:<br/>" +
- "Name: <input name="name"/>" +
- (pushAddressType.equals("PLMN") ?
- "Phone: <input name="phone" format="*N"/>" :
- "IP address: <input name="ip"/>") +
- "Message: <input name="message" value="Your friend recommends that you visit WML Zoo. See you there!"/>" +
- " <anchor title="Send push message">Send push message" +
- " <go method="post" href="" + request.getRequestURI() + jsessionID + "">" +
- " <postfield name="name" value="$(name)"/>" +
- (pushAddressType.equals("PLMN") ?
- " <postfield name="phone" value="$(phone)"/>" :
- " <postfield name="ip" value="$(ip)"/>") +
- " <postfield name="message" value="$(message)"/>" +
- " <postfield name="action" value="push"/>" +
- " </go></anchor>" +
- " </p></card>");
- }
- else {
- PushInitiator pusher = Members.getInitiator();
- /* next, send the push message */
- String recommendedUrl = "http://" + request.getServerName() + ":" +
- request.getServerPort() + request.getContextPath();
- String pushMessage = request.getParameter("message");
- String friendName = request.getParameter("name");
- System.out.println("friend = " + friendName);
- System.out.println("addresstype = " + pushAddressType);
- boolean sended = false;
- String message = PushInitiator.createSiMessage(recommendedUrl, null, null, null,
- null, pushMessage);
- String errorReason = "";
- try {
- /* Check the default address type. If it is IPv4 then send the message by using the
- IP address of the friend. If PLMN then send the message to the given phone number.
- If the address (IP or phone number) is not given, the friend should has
- currently active session */
- if(pushAddressType.equals("IPv4")) {
- String ip = request.getParameter("ip");
- System.out.println("ip= " + ip);
- if(ip != null && !ip.trim().equals("")) {
- pusher.sendPushMessage(ip, pushAddressType, message,
- PushService.SI_CONTENT_TYPE);
- System.out.println("Push message sent to address: " + ip);
- sended = true;
- }
- else {
- User friendUser = User.findUser(friendName);
- System.out.println("friendUser = " + friendUser);
- if(friendUser != null) {
- pusher.sendPushMessage(friendUser.getAddress(), pushAddressType, message,
- PushService.SI_CONTENT_TYPE);
- System.out.println("Push message sent to address: " + friendUser.getAddress());
- sended = true;
- }
- else errorReason = "Your friend " + friendName + " is not currently on line!";
- }
- }
- else if(pushAddressType.equals("PLMN")) {
- String phone = request.getParameter("phone");
- System.out.println("phone number= " + phone);
- if(phone == null || (phone = phone.trim()).equals("")) {
- /* If the phone number is not given, try to find from the database */
- User friendUser = User.findUser(friendName);
- if(friendUser != null) phone = friendUser.getAddress(pushAddressType);
- }
- if(phone != null && !(phone = phone.trim()).equals("")) {
- pusher.sendPushMessage(phone, pushAddressType, message, PushService.SI_CONTENT_TYPE);
- System.out.println("Push message sent to number: " + phone);
- sended = true;
- }
- else errorReason = "phone number is not given?";
- }
- else throw new Exception("wrong push-addresstype: " + pushAddressType);
- } catch(Exception e) {
- System.err.println(e);
- errorReason = e.toString();
- }
- if(sended) {
- out.println("<!-- Thanks for the recommendation -->n" +
- "<card title="Thank you" id="thanks">" +
- " <do type="accept" label="Ok">" +
- " <go href="" + request.getContextPath() + HOME_PAGE + jsessionID + ""/>" +
- " </do>" +
- " <p>Your message has been delivered.<br/>Thank you for recommending our zoo to your friend!<br/>" +
- " <anchor title="Ok">Ok" +
- " <go href="" + request.getContextPath() + HOME_PAGE + jsessionID + ""/>" +
- " </anchor>" +
- " </p>" +
- "</card>");
- }
- else {
- out.println("<card title="" + errorReason + "" id="error">" +
- " <do type="accept" label="Ok">" +
- " <go href="" + request.getContextPath() + HOME_PAGE + jsessionID + ""/>" +
- " </do>" +
- " <p>Your message has not been delivered.<br/>The possible reason is: " + errorReason +
- " <br/><anchor title="Ok">Ok" +
- " <go href="" + request.getContextPath() + HOME_PAGE + jsessionID + ""/>" +
- " </anchor>" +
- " </p>" +
- "</card>");
- }
- }
- out.println("</wml>");
- }
- }