1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
76
77
78
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 }