KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > plugin > presence > ImagePresenceProvider


1 /**
2  * $RCSfile: ImagePresenceProvider.java,v $
3  * $Revision: 1.6 $
4  * $Date: 2005/07/22 21:13:38 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.plugin.presence;
13
14 import org.jivesoftware.util.Log;
15 import org.xmpp.packet.Presence;
16
17 import javax.servlet.ServletOutputStream JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * The ImagePresenceProvider provides information about the users presence by returning
30  * images. There are many ways to specify the images to use.
31  *
32  * <ul>
33  * <li>Use a single parameter in the URL - Use the <b>images</b> parameter that will include a
34  * --IMAGE-- token. The --IMAGE-- token would be filtered and would be replaced with
35  * codes like "dnd", "offline", "away", etc.</li>
36  * <li>Use a parameter for each possible presence type - Possible parameters are: <b>offline</b>,
37  * <b>available</b>, <b>away</b>, <b>chat</b>, <b>dnd</b>, <b>xa</b> and <b>forbidden</b>. If
38  * the parameter was not passed then the default image will be used.</li>
39  * <li>Do not pass any parameter - When no parameter was passed the default images will be
40  * used.</li>
41  * </ul>
42  *
43  * If the required user was not found or the user making the request is not allowed to see the
44  * user presence then the image specified in the <b>forbidden</b> parameter will be used.
45  * However, if the request does not include the <b>forbidden</b> parameter then the default
46  * image for user offline will be used.
47  *
48  * @author Gaston Dombiak
49  *
50  */

51 class ImagePresenceProvider extends PresenceInfoProvider {
52
53     private PresenceStatusServlet servlet;
54     private Map JavaDoc<String JavaDoc, byte[]> imageCache = new HashMap JavaDoc<String JavaDoc, byte[]>();
55     private Map JavaDoc<String JavaDoc, String JavaDoc> imageTypeCache = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
56
57     public ImagePresenceProvider(PresenceStatusServlet servlet) {
58         this.servlet = servlet;
59     }
60
61     public void sendInfo(HttpServletRequest JavaDoc request,
62             HttpServletResponse JavaDoc response, Presence presence) throws IOException JavaDoc {
63         if (presence == null) {
64             writeImageContent(request, response, "offline", servlet.offline);
65         }
66         else if (presence.getShow() == null) {
67             writeImageContent(request, response, "available", servlet.available);
68         }
69         else if (presence.getShow().equals(org.xmpp.packet.Presence.Show.away)) {
70             writeImageContent(request, response, "away", servlet.away);
71         }
72         else if (presence.getShow().equals(org.xmpp.packet.Presence.Show.chat)) {
73             writeImageContent(request, response, "chat", servlet.chat);
74         }
75         else if (presence.getShow().equals(org.xmpp.packet.Presence.Show.dnd)) {
76             writeImageContent(request, response, "dnd", servlet.dnd);
77         }
78         else if (presence.getShow().equals(org.xmpp.packet.Presence.Show.xa)) {
79             writeImageContent(request, response, "xa", servlet.xa);
80         }
81     }
82
83     public void sendUserNotFound(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
84             throws IOException JavaDoc {
85         writeImageContent(request, response, "forbidden", servlet.offline);
86     }
87
88     private void writeImageContent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
89             String JavaDoc presenceType, byte[] defaultImage) throws IOException JavaDoc {
90         String JavaDoc images = request.getParameter("images");
91         if (request.getParameter(presenceType) != null) {
92             writeImageContent(request.getParameter(presenceType), defaultImage, response);
93         }
94         else if (images != null) {
95             writeImageContent(images.replace("--IMAGE--", presenceType), defaultImage, response);
96         }
97         else {
98             writeImageContent(null, defaultImage, response);
99         }
100     }
101
102     private void writeImageContent(String JavaDoc url, byte[] defaultContent, HttpServletResponse JavaDoc response)
103             throws IOException JavaDoc {
104         ServletOutputStream JavaDoc os = response.getOutputStream();
105         byte[] imageContent = defaultContent;
106         String JavaDoc contentType = "image/gif";
107         if (url != null) {
108             try {
109                 byte[] cachedContent = imageCache.get(url);
110                 if (cachedContent == null) {
111                     URLConnection JavaDoc connection = new URL JavaDoc(url).openConnection();
112                     InputStream JavaDoc in = connection.getInputStream();
113                     ByteArrayOutputStream JavaDoc bytes = new ByteArrayOutputStream JavaDoc();
114                     byte buffer[] = new byte[1024 * 4];
115                     int last_read_bytes = 0;
116                     while ((last_read_bytes = in.read(buffer)) != -1) {
117                         bytes.write(buffer, 0, last_read_bytes);
118                     }
119                     if (bytes.size() > 0) {
120                         imageCache.put(url, bytes.toByteArray());
121                         imageTypeCache.put(url, connection.getContentType());
122                     }
123                 }
124                 if (imageTypeCache.get(url) != null) {
125                     contentType = imageTypeCache.get(url);
126                     imageContent = imageCache.get(url);
127                 }
128             }
129             catch (IOException JavaDoc e) {
130                 Log.error(e);
131             }
132         }
133         response.setContentType(contentType);
134         os.write(imageContent);
135         os.flush();
136         os.close();
137     }
138
139 }
140
Popular Tags