001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *       http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.order;
018    
019    import java.io.IOException;
020    
021    import javax.annotation.Resource;
022    import javax.jms.ConnectionFactory;
023    import javax.jms.Destination;
024    import javax.jms.Queue;
025    import javax.jms.Connection;
026    import javax.jms.Session;
027    import javax.jms.MessageProducer;
028    import javax.jms.TextMessage;
029    import javax.jms.JMSException;
030    import javax.jms.TextMessage;
031    import javax.naming.InitialContext;
032    import javax.naming.NamingException;
033    import javax.servlet.ServletException;
034    import javax.servlet.http.HttpServlet;
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    public class OrderSenderServlet extends HttpServlet {
039    
040        @Resource(name="CommonConnectionFactory")
041        private ConnectionFactory factory;
042    
043        @Resource(name="OrderQueue")
044        private Queue receivingQueue;
045    
046        public void init() throws ServletException {
047            super.init();       
048        }
049    
050        protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
051            manageOrders(req,res);
052        }
053    
054        protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
055            doGet(req,res);
056        }
057    
058        private void manageOrders(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
059            String path = "/error.jsp";
060            Connection connection = null;
061            MessageProducer messageProducer = null;
062            Session sess = null;
063            try {
064                String customerId = req.getParameter("customerId");
065                String orderId = req.getParameter("orderId");
066                String qty = req.getParameter("quantity");
067                String model = req.getParameter("model");
068    
069                if ( !customerId.equals("") && !orderId.equals("") && !qty.equals("") ) {
070                    System.out.println("Start Sending Order Request");
071                    // creating online order request
072                    String orderRequest = "<Order orderId=\""+orderId+"\" custId=\""+customerId+"\" qty=\""+qty+"\" model=\""+model+"\"/>" ;
073                    connection = factory.createConnection();
074                    sess = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);   
075                    path = "/order.jsp";
076                    TextMessage msg = sess.createTextMessage("<OrderId=" + orderId + " CustomerId=" + customerId
077                                                             + " Quantity=" + qty + " Model=" + model + ">" );
078                    messageProducer = sess.createProducer(receivingQueue);
079                    messageProducer.send(msg);
080                    System.out.println("Order Request Send");
081                }
082                else {
083                    String error = "";
084    
085                    if ( customerId.equals("") ) {
086                        error = "Customer Id Cannot be Empty";
087                    }
088                    else if ( orderId.equals("") ) {
089                        error = "Order Id Cannot be Empty";
090                    }
091                    else if ( qty.equals("") ) {
092                        error = "Quantity Cannot be Empty";
093                    }
094                    req.setAttribute("error",error);
095                }
096            }
097            catch ( Exception e ) {
098                System.out.println("Error "+e);
099                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    }