View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   * $Id: PictureServlet.java,v 1.3 2005/12/02 14:10:53 nilsandreas Exp $
19   */
20  package no.feide.moria.servlet;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.http.HttpServlet;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletOutputStream;
27  
28  import no.feide.moria.log.MessageLogger;
29  
30  import org.apache.commons.codec.binary.Base64;
31  
32  import java.io.IOException;
33  
34  /***
35   * This Servlet is only used to show pictures.
36   * @author Eva Indal
37   * @version $Revision: 1.3 $
38   */
39  public class PictureServlet
40  extends HttpServlet {
41  
42      /***
43       * Used for logging.
44       */
45      MessageLogger log = new MessageLogger(PictureServlet.class);
46      
47      /***
48       * Name of the request attribute containing the actual picture(s).
49       */
50      public static final String PICTURE_ATTRIBUTE = "picture";
51  
52  
53      /***
54       * Implements the HttpServlet.doGet method.
55       * @param request
56       *            The HTTP request.
57       * @param response
58       *            The HTTP response.
59       * @throws IOException
60       * @throws ServletException
61       * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
62       *      javax.servlet.http.HttpServletResponse)
63       */
64      public void doGet(HttpServletRequest request, HttpServletResponse response)
65      throws IOException, ServletException {
66                  
67          try {
68  
69              int idx = 0;
70              String index = request.getParameter("index");
71              if (index != null) {
72                  idx = Integer.parseInt(index);
73              }
74  
75              // login.feide.no/Picture can be entered as an URL without any further arguments.
76  	    // That will lead to a null pointer exception, caught by the catch-clause below, but 
77              // we really should avoid creating such errors. For one, they end up in the log file looking
78  	    // horribly serious, when there is little reason to worry.
79  
80  
81              response.setContentType("image/jpeg");
82              String[] picture = (String[]) request.getSession().getAttribute(PICTURE_ATTRIBUTE);
83  	    
84  	    if (picture == null) {
85  		log.logWarn("PictureServlet called without picture context. Nothing to display");
86  	    }
87  	    else {
88  		    byte[] decoded = Base64.decodeBase64(picture[idx].getBytes("ISO-8859-1"));
89  
90  		    ServletOutputStream writer = response.getOutputStream();
91  		    writer.write(decoded);
92  		    writer.close();
93  	    }
94  
95          } catch (Throwable t) {
96              log.logWarn("Picture display failed", t);
97          }
98  
99      }
100 }