View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *       http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.samples.order;
18  
19  import java.io.IOException;
20  
21  import javax.annotation.Resource;
22  import javax.jms.ConnectionFactory;
23  import javax.jms.Destination;
24  import javax.jms.Queue;
25  import javax.jms.Connection;
26  import javax.jms.Session;
27  import javax.jms.MessageProducer;
28  import javax.jms.TextMessage;
29  import javax.jms.JMSException;
30  import javax.jms.TextMessage;
31  import javax.naming.InitialContext;
32  import javax.naming.NamingException;
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServlet;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  public class OrderSenderServlet extends HttpServlet {
39  
40      @Resource(name="CommonConnectionFactory")
41      private ConnectionFactory factory;
42  
43      @Resource(name="OrderQueue")
44      private Queue receivingQueue;
45  
46      public void init() throws ServletException {
47          super.init();       
48      }
49  
50      protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
51          manageOrders(req,res);
52      }
53  
54      protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
55          doGet(req,res);
56      }
57  
58      private void manageOrders(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
59          String path = "/error.jsp";
60          Connection connection = null;
61          MessageProducer messageProducer = null;
62          Session sess = null;
63          try {
64              String customerId = req.getParameter("customerId");
65              String orderId = req.getParameter("orderId");
66              String qty = req.getParameter("quantity");
67              String model = req.getParameter("model");
68  
69              if ( !customerId.equals("") && !orderId.equals("") && !qty.equals("") ) {
70                  System.out.println("Start Sending Order Request");
71                  // creating online order request
72                  String orderRequest = "<Order orderId=\""+orderId+"\" custId=\""+customerId+"\" qty=\""+qty+"\" model=\""+model+"\"/>" ;
73                  connection = factory.createConnection();
74                  sess = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);   
75                  path = "/order.jsp";
76                  TextMessage msg = sess.createTextMessage("<OrderId=" + orderId + " CustomerId=" + customerId
77                                                           + " Quantity=" + qty + " Model=" + model + ">" );
78                  messageProducer = sess.createProducer(receivingQueue);
79                  messageProducer.send(msg);
80                  System.out.println("Order Request Send");
81              }
82              else {
83                  String error = "";
84  
85                  if ( customerId.equals("") ) {
86                      error = "Customer Id Cannot be Empty";
87                  }
88                  else if ( orderId.equals("") ) {
89                      error = "Order Id Cannot be Empty";
90                  }
91                  else if ( qty.equals("") ) {
92                      error = "Quantity Cannot be Empty";
93                  }
94                  req.setAttribute("error",error);
95              }
96          }
97          catch ( Exception e ) {
98              System.out.println("Error "+e);
99              e.printStackTrace();
100         }
101         finally {
102             try {
103                 if ( messageProducer != null ) messageProducer.close();
104                 if ( sess != null )sess.close();
105                 if ( connection!= null )connection.close();
106             }
107             catch ( JMSException e ) {
108                 e.printStackTrace();
109             }
110         }
111         getServletContext().getRequestDispatcher(path).forward(req,res);
112     }
113 }