1    package com.instantbank.lettertemplate.imagesRepository.web;
2    
3    import java.io.*;
4    import java.sql.*;
5    import javax.servlet.*;
6    import javax.servlet.http.*;
7    
8    // utilities:
9    
10   // for working with EJB
11   import java.rmi.RemoteException;
12   import javax.naming.Context;
13   import javax.naming.InitialContext;
14   
15   import com.instantbank.lettertemplate.control.util.*;
16   import com.instantbank.common.utilcomponents.Debug;
17   import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
18   import com.instantbank.common.utilcomponents.JNDINames;
19   import com.instantbank.common.utilcomponents.CommonUtil;
20   
21   import com.instantbank.component.lettertemplate.ejb.LetterTemplate;
22   import com.instantbank.component.lettertemplate.ejb.LetterTemplateHome;
23   
24   /**
25    *  Servlet acting as a controller in order to ask ejb services for image View
26    *  action in imagesRepository usecase.
27    *
28    * @author InstantBank (Consuelo Franky, Jorge Cardenas)
29    * @created August 2002
30    */
31   public class ImageShowServlet extends HttpServlet {
32     /**
33      *  Home of ejb that provides the services for this servlet.
34      */
35     private LetterTemplateHome lethome = null;
36   
37     /**
38      *  The ejb that provides the services for this servlet.
39      */
40     private LetterTemplate letejb = null;
41     private Debug debug = null;
42   
43   
44     /**
45      *  init of servlet: obtains home ejb reference
46      *
47      * @exception ServletException
48      */
49     public void init() throws ServletException {
50       debug = new Debug();
51       debug.setDebugginOn(true);
52       debug.setPreMessage("** ImageShowServlet: ");
53       debug.println("init()");
54   
55       //obtain LetterTemplateHome reference:
56       try {
57         // Contact the LetterTemplateHome through JNDI.
58         InitialContext ctx = new InitialContext();
59         lethome = (LetterTemplateHome)ctx.lookup
60           (JNDINames.LETTERTEMPLATE_EJBHOME);
61       }
62       catch(Exception e) {
63         e.printStackTrace();
64         debug.println("Problem instantiating LetterTemplate EJB:" + e.toString());
65         throw new ServletException
66           (LetterTemplateExceptionMessage.PROBLEM_INSTANTIATING_LETTERS_EJB + e.getMessage());
67       }
68     }
69   
70   
71     /**
72      *  Gets the bytes of an image from the database, through an ejb service and
73      *  sends the bytes to the browser
74      *
75      * @param request HTTP request
76      * @param response HTTP response
77      * @exception ServletException
78      * @exception IOException
79      */
80     public void doGet(HttpServletRequest request, HttpServletResponse response)
81        throws ServletException, IOException {
82       ServletOutputStream out = response.getOutputStream();
83   
84       HttpSession session = request.getSession();
85   
86       if(CommonUtil.controlTimeout(request, response,
87         getServletConfig().getServletContext())) {
88         debug.println("invalidate session and return by timeout");
89         session.invalidate();
90         return;
91         // here there is a forward to /control_web/TimeOutPageOff.jsp
92         // prepared by controlTimeout()
93       }
94   
95       byte[] image = null;
96       String imageName = request.getParameter("imageName");
97   
98       try {
99         String companyId
100           = (String)request.getSession().getAttribute(WebKeys.CompanyId);
101        Long userId
102           = (Long)request.getSession().getAttribute(WebKeys.UserId);
103        letejb = lethome.create(companyId, userId);
104  
105        image = letejb.loadImageRepository(imageName);
106  
107        debug.println("after getting the image ");
108  
109        response.setContentType("image/gif");
110  
111        out.write(image, 0, image.length);
112        out.flush();
113  
114      }
115      catch(Exception ex) {
116        debug.println("Exception:" + ex);
117        response.setContentType("text/html");
118        out.println("<H3>EXCEPTION: " + ex.getMessage() + "</H3>");
119      }
120    }
121  
122  
123    /**
124     *  doPost has the same effect than doGet
125     *
126     * @param request Description of the Parameter
127     * @param response Description of the Parameter
128     * @exception ServletException
129     * @exception IOException
130     */
131    public void doPost(HttpServletRequest request, HttpServletResponse response)
132       throws ServletException, IOException {
133      doGet(request, response);
134    }
135  
136  }
137