📄 smartticketservlet.java.attempt
字号:
/* @@copyright@@ $Id: SmartTicketServlet.java.attempt,v 1.1 2003/05/14 22:23:57 zull Exp $ */package com.sun.j2me.blueprints.smartticket.server.web.midp;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import com.sun.j2me.blueprints.smartticket.shared.midp.*;import com.sun.j2me.blueprints.smartticket.shared.midp.model.*;/** This class processes an HTTP request and starts decoding the application request it contains. */public class SmartTicketServlet extends HttpServlet { public static final String SESSION_ATTRIBUTE_SMART_TICKET_BD = "com.sun.j2me.blueprints.smartticket.server.web.midp.SmartTicketBD"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); SmartTicketBD smartTicketBD = (SmartTicketBD)session.getAttribute(SESSION_ATTRIBUTE_SMART_TICKET_BD); response.setContentType("application/octet-stream"); try { ByteArrayOutputStream bufferedResponseContent = new ByteArrayOutputStream(); int contentLength = handleCall(smartTicketBD, request.getInputStream(), bufferedResponseContent); response.setContentLength(contentLength); if (session.isNew()) { response.setStatus(HttpServletResponse.SC_CREATED); response.setHeader("Location", response.encodeURL(request.getRequestURL().toString())); } response.getOutputStream().write(bufferedResponseContent.toByteArray(), 0, contentLength); } catch (ApplicationException ae) { ae.printStackTrace(); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { // FIXME } return; } public int handleCall(SmartTicketBD smartTicketBD, InputStream in, ByteArrayOutputStream out) throws IOException, ApplicationException { DataInputStream call = new DataInputStream(in); DataOutputStream result = new DataOutputStream(out); try { result.writeInt(MessageConstants.ERROR_NONE); byte method = call.readByte(); switch (method) { case MessageConstants.OPERATION_LOGIN_USER: login(smartTicketBD, call, result); break; case MessageConstants.OPERATION_CREATE_ACCOUNT: createAccount(smartTicketBD, call, result); break; case MessageConstants.OPERATION_UPDATE_ACCOUNT: updateAccount(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_THEATERS: getTheaters(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_THEATER_SCHEDULE: getTheaterSchedule(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_MOVIE: getMovie(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_MOVIE_POSTER: getMoviePoster(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_MOVIE_SHOWTIMES: getMovieShowtimes(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_SEATING_PLAN: getSeatingPlan(smartTicketBD, call, result); break; case MessageConstants.OPERATION_RESERVE_SEATS: reserveSeats(smartTicketBD, call, result); break; case MessageConstants.OPERATION_PURCHASE_TICKETS: purchaseTickets(smartTicketBD, call, result); break; case MessageConstants.OPERATION_CANCEL_SEAT_RESERVATION: cancelSeatReservation(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_LOCALES: getLocales(smartTicketBD, call, result); break; case MessageConstants.OPERATION_GET_RESOURCE_BUNDLE: getResourceBundle(smartTicketBD, call, result); break; case MessageConstants.OPERATION_INITIATE_SYNCHRONIZATION: initiateSynchronization(smartTicketBD, call, result); break; case MessageConstants.OPERATION_SYNCHRONIZE_MOVIE_RATINGS: synchronizeMovieRatings(smartTicketBD, call, result); break; case MessageConstants.OPERATION_COMMIT_MOVIE_RATINGS: commitMovieRatings(smartTicketBD, call, result); break; default: out.reset(); result.writeInt(MessageConstants.ERROR_UNKNOWN_OPERATION); } result.flush(); } catch (ModelException me) { out.reset(); result.writeInt(MessageConstants.ERROR_MODEL_EXCEPTION); me.serialize(result); } return result.size(); } private void login(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String username = call.readUTF(); String password = call.readUTF(); smartTicketBD.login(username, password); return; } private void createAccount(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { AccountInfo accountInfo = AccountInfo.deserialize(call); smartTicketBD.createAccount(accountInfo); return; } private void updateAccount(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { AccountInfo accountInfo = AccountInfo.deserialize(call); smartTicketBD.updateAccount(accountInfo); return; } private void getTheaters(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String zipCode = call.readUTF(); Theater[] theaters = smartTicketBD.getTheaters(zipCode); result.writeInt(theaters.length); for (int i = 0; i < theaters.length; i++) { theaters[i].serialize(result); } return; } private void getTheaterSchedule(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String theaterKey = call.readUTF(); TheaterSchedule theaterSchedule = smartTicketBD.getTheaterSchedule(theaterKey); theaterSchedule.serialize(result, true); return; } private void getMovie(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String movieKey = call.readUTF(); Movie movie = smartTicketBD.getMovie(movieKey); movie.serialize(result); return; } private void getMoviePoster(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String movieKey = call.readUTF(); byte[] poster = smartTicketBD.getMoviePoster(movieKey); result.writeInt(poster.length); result.write(poster, 0, poster.length); return; } private void getMovieShowtimes(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String theaterKey = call.readUTF(); String movieKey = call.readUTF(); int[] [] showtimes = smartTicketBD.getMovieShowTimes(theaterKey, movieKey); TheaterSchedule.serializeShowTimes(showtimes, result); return; } private void getSeatingPlan(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String theaterKey = call.readUTF(); String movieKey = call.readUTF(); int[] showtime = new int[3]; showtime[0] = call.readInt(); showtime[1] = call.readInt(); showtime[2] = call.readInt(); SeatingPlan seatingPlan = smartTicketBD.getSeatingPlan(theaterKey, movieKey, showtime); seatingPlan.serialize(result); return; } private void reserveSeats(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String theaterKey = call.readUTF(); String movieKey = call.readUTF(); int[] showtime = new int[3]; showtime[0] = call.readInt(); showtime[1] = call.readInt(); showtime[2] = call.readInt(); SeatingPlan.Seat[] seats = new SeatingPlan.Seat[call.readInt()]; for (int i = 0; i < seats.length; i++) { seats[i] = SeatingPlan.Seat.deserialize(call); } Reservation reservation = smartTicketBD.reserveSeats(theaterKey, movieKey, showtime, seats); reservation.serialize(result); return; } private void purchaseTickets(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String reservationId = call.readUTF(); smartTicketBD.purchaseTickets(reservationId); return; } private void cancelSeatReservation(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String reservationId = call.readUTF(); smartTicketBD.cancelSeatReservation(reservationId); return; } private void getLocales(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String[] locales = smartTicketBD.getLocales(); result.writeInt(locales.length); for (int i = 0; i < locales.length; i++) { result.writeUTF(locales[i]); } return; } private void getResourceBundle(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { String baseName = call.readUTF(); String locale = call.readUTF(); IndexedResourceBundle bundle = smartTicketBD.getResourceBundle(baseName, locale); bundle.serialize(result); return; } public void initiateSynchronization(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { SyncAnchor syncAnchor = SyncAnchor.deserialize(call); long clientTime = call.readLong(); syncAnchor = smartTicketBD.initiateSynchronization(syncAnchor, clientTime); syncAnchor.serialize(result); return; } public void synchronizeMovieRatings(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { int conflictResolutionStrategyId = call.readInt(); MovieRating[] changedMovieRatings = new MovieRating[call.readInt()]; for (int i = 0; i < changedMovieRatings.length; i++) { changedMovieRatings[i] = MovieRating.deserialize(call); } MovieRating[] updatedMovieRatings = smartTicketBD.synchronizeMovieRatings(changedMovieRatings, conflictResolutionStrategyId); if (updatedMovieRatings != null) { result.writeInt(updatedMovieRatings.length); for (int i = 0; i < updatedMovieRatings.length; i++) { updatedMovieRatings[i].serialize(result); } } else { result.writeInt(0); } return; } public void commitMovieRatings(SmartTicketBD smartTicketBD, DataInputStream call, DataOutputStream result) throws IOException, ModelException, ApplicationException { MovieRating[] resolvedMovieRatings = new MovieRating[call.readInt()]; for (int i = 0; i < resolvedMovieRatings.length; i++) { resolvedMovieRatings[i] = MovieRating.deserialize(call); } MovieRating[] updatedMovieRatings = smartTicketBD.commitMovieRatings(resolvedMovieRatings); if (updatedMovieRatings != null) { result.writeInt(updatedMovieRatings.length); for (int i = 0; i < updatedMovieRatings.length; i++) { updatedMovieRatings[i].serialize(result); } } else { result.writeInt(0); } return; } /** @link dependency */ /*# SmartTicketBD lnkSmartTicketBD; */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -