KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > docoriented > client > RequestHandler


1 /* Copyright 2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2  http://developer.sun.com/berkeley_license.html
3  $Id: RequestHandler.java,v 1.17 2005/08/12 19:43:03 smitha Exp $ */

4
5 package com.sun.j2ee.blueprints.docoriented.client;
6
7 import java.util.*;
8 import java.text.*;
9
10 import javax.servlet.http.*;
11
12 import com.sun.j2ee.blueprints.docoriented.client.stringposervice.*;
13 import com.sun.j2ee.blueprints.docoriented.client.objectposervice.*;
14 import com.sun.j2ee.blueprints.docoriented.client.anytypeposervice.*;
15 import com.sun.j2ee.blueprints.docoriented.client.anyposervice.*;
16 import com.sun.j2ee.blueprints.docoriented.client.attachmentposervice.*;
17
18 /**
19  * Handles responsibilities related to getting HTTP request
20  * info and making the calls to the Web service endpoint
21  */

22 public class RequestHandler {
23     private SchemaPOServiceBD schemaPOService;
24     private StringPOServiceBD stringPOService;
25     private AnyTypePOServiceBD anyTypePOService;
26     private AnyPOServiceBD anyPOService;
27     private AttachmentPOServiceBD attachmentPOService;
28     
29     public RequestHandler(){
30         schemaPOService = new SchemaPOServiceBD();
31         stringPOService = new StringPOServiceBD();
32         anyTypePOService = new AnyTypePOServiceBD();
33         anyPOService = new AnyPOServiceBD();
34         attachmentPOService = new AttachmentPOServiceBD();
35     }
36     
37     /**
38      * Handles the http request, calls the appropriate Business Delegate
39      * to access the web service, and provides an appropriate response.
40      * Sets the response message by setting a request attribute for use
41      * in the client JSP pages.
42      * @throws RequestHandlerException with client message for
43      * bad date format, bad number format, servicelocator JNDI lookup
44      * problems, web service application defined exceptions,
45      * web service access problems so that user can know about
46      * a possible corrective action. Also, throws RequestHandlerException
47      * thrown from calling delegate to access service when got a
48      * service-defined application exception.
49      */

50     public void handle(HttpServletRequest request,
51             HttpServletResponse response) throws RequestHandlerException{
52         try {
53             String JavaDoc ret = null;
54             //extract request parameters
55
String JavaDoc poID = request.getParameter("po_id");
56             if((poID == null) || (poID.equals("")))
57                 throw new RequestHandlerException("Request Handler Exception: Please enter a valid PO ID.");
58             String JavaDoc orderDate = request.getParameter("po_date");
59             String JavaDoc itemId1 = request.getParameter("po_itemID1");
60             String JavaDoc quantity1 = request.getParameter("po_quantity1");
61             String JavaDoc unitPrice1 = request.getParameter("po_unitPrice1");
62             String JavaDoc itemId2 = request.getParameter("po_itemID2");
63             String JavaDoc quantity2 = request.getParameter("po_quantity2");
64             String JavaDoc unitPrice2 = request.getParameter("po_unitPrice2");
65             Address shipTo = new Address(request.getParameter("po_shipping_street"),
66                     request.getParameter("po_shipping_city"),
67                     request.getParameter("po_shipping_state"),
68                     request.getParameter("po_shipping_zipCode"));
69             Address billTo = new Address(request.getParameter("po_billing_street"),
70                     request.getParameter("po_billing_city"),
71                     request.getParameter("po_billing_state"),
72                     request.getParameter("po_billing_zipCode"));
73             LineItem item1 = new LineItem(itemId1,
74                     Integer.parseInt(quantity1), Float.valueOf(unitPrice1).floatValue());
75             LineItem item2 = new LineItem(itemId2,
76                     Integer.parseInt(quantity2), Float.valueOf(unitPrice2).floatValue());
77             LineItem[] items = new LineItem[2];
78             items[0] = item1 ;
79             items[1] = item2 ;
80             Date date = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
81             Calendar cal = Calendar.getInstance();
82             cal.setTime(date);
83             //create a Purchase Order object
84
PurchaseOrder po = new PurchaseOrder();
85             po.setPoId(poID);
86             po.setCreateDate(cal);
87             po.setShipTo(shipTo);
88             po.setBillTo(billTo);
89             po.setItems(items);
90             if(request.getParameter("type").equals("String")){
91                 ret = stringPOService.submitPO(po);
92             } else if(request.getParameter("type").equals("Object")){
93                 ret = schemaPOService.submitPO(po);
94             } else if(request.getParameter("type").equals("AnyType")){
95                 ret = anyTypePOService.submitPO(po);
96             } else if(request.getParameter("type").equals("Any")){
97                 ret = anyPOService.submitPO(po);
98             } else if(request.getParameter("type").equals("Attachment")){
99                 ret = attachmentPOService.submitPO(po);
100             }
101             request.setAttribute("result", ret);
102         } catch(java.text.ParseException JavaDoc pe){
103             //catch exception for date format, when creating XML doc of
104
//purchase order before sending xml document to webservice
105
pe.printStackTrace(System.err);
106             throw new RequestHandlerException("Request Handler Exception: Date format in PurchaseOrder input form needs to be like 2005-08-12. You entered an unexpected value for the date "+ pe.getMessage());
107         } catch(java.lang.NumberFormatException JavaDoc nfe){
108             //catch exception for number format, when creating XML doc of
109
//purchase order before sending xml document to webservice
110
nfe.printStackTrace(System.err);
111             throw new RequestHandlerException("Request Handler Exception: For PurchaseOrder user input form, you need to use proper number format. You entered an unexpected value "+ nfe.getMessage());
112         }
113     }
114 }
Popular Tags