001: /**
002: * $RCSfile$
003: * $Revision: 1682 $
004: * $Date: 2005-07-22 18:13:38 -0300 (Fri, 22 Jul 2005) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.plugin.presence;
011:
012: import org.jivesoftware.util.Log;
013: import org.xmpp.packet.Presence;
014:
015: import javax.servlet.ServletOutputStream;
016: import javax.servlet.http.HttpServletRequest;
017: import javax.servlet.http.HttpServletResponse;
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.URL;
022: import java.net.URLConnection;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: /**
027: * The ImagePresenceProvider provides information about the users presence by returning
028: * images. There are many ways to specify the images to use.
029: *
030: * <ul>
031: * <li>Use a single parameter in the URL - Use the <b>images</b> parameter that will include a
032: * --IMAGE-- token. The --IMAGE-- token would be filtered and would be replaced with
033: * codes like "dnd", "offline", "away", etc.</li>
034: * <li>Use a parameter for each possible presence type - Possible parameters are: <b>offline</b>,
035: * <b>available</b>, <b>away</b>, <b>chat</b>, <b>dnd</b>, <b>xa</b> and <b>forbidden</b>. If
036: * the parameter was not passed then the default image will be used.</li>
037: * <li>Do not pass any parameter - When no parameter was passed the default images will be
038: * used.</li>
039: * </ul>
040: *
041: * If the required user was not found or the user making the request is not allowed to see the
042: * user presence then the image specified in the <b>forbidden</b> parameter will be used.
043: * However, if the request does not include the <b>forbidden</b> parameter then the default
044: * image for user offline will be used.
045: *
046: * @author Gaston Dombiak
047: *
048: */
049: class ImagePresenceProvider extends PresenceInfoProvider {
050:
051: private PresenceStatusServlet servlet;
052: private Map<String, byte[]> imageCache = new HashMap<String, byte[]>();
053: private Map<String, String> imageTypeCache = new HashMap<String, String>();
054:
055: public ImagePresenceProvider(PresenceStatusServlet servlet) {
056: this .servlet = servlet;
057: }
058:
059: public void sendInfo(HttpServletRequest request,
060: HttpServletResponse response, Presence presence)
061: throws IOException {
062: if (presence == null) {
063: writeImageContent(request, response, "offline",
064: servlet.offline);
065: } else if (presence.getShow() == null) {
066: writeImageContent(request, response, "available",
067: servlet.available);
068: } else if (presence.getShow().equals(
069: org.xmpp.packet.Presence.Show.away)) {
070: writeImageContent(request, response, "away", servlet.away);
071: } else if (presence.getShow().equals(
072: org.xmpp.packet.Presence.Show.chat)) {
073: writeImageContent(request, response, "chat", servlet.chat);
074: } else if (presence.getShow().equals(
075: org.xmpp.packet.Presence.Show.dnd)) {
076: writeImageContent(request, response, "dnd", servlet.dnd);
077: } else if (presence.getShow().equals(
078: org.xmpp.packet.Presence.Show.xa)) {
079: writeImageContent(request, response, "xa", servlet.xa);
080: }
081: }
082:
083: public void sendUserNotFound(HttpServletRequest request,
084: HttpServletResponse response) throws IOException {
085: writeImageContent(request, response, "forbidden",
086: servlet.offline);
087: }
088:
089: private void writeImageContent(HttpServletRequest request,
090: HttpServletResponse response, String presenceType,
091: byte[] defaultImage) throws IOException {
092: String images = request.getParameter("images");
093: if (request.getParameter(presenceType) != null) {
094: writeImageContent(request.getParameter(presenceType),
095: defaultImage, response);
096: } else if (images != null) {
097: response.sendRedirect(images.replace("--IMAGE--",
098: presenceType));
099: } else {
100: writeImageContent(null, defaultImage, response);
101: }
102: }
103:
104: private void writeImageContent(String url, byte[] defaultContent,
105: HttpServletResponse response) throws IOException {
106: ServletOutputStream os = response.getOutputStream();
107: byte[] imageContent = defaultContent;
108: String contentType = "image/gif";
109: if (url != null) {
110: try {
111: byte[] cachedContent = imageCache.get(url);
112: if (cachedContent == null) {
113: URLConnection connection = new URL(url)
114: .openConnection();
115: InputStream in = connection.getInputStream();
116: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
117: byte buffer[] = new byte[1024 * 4];
118: int last_read_bytes = 0;
119: while ((last_read_bytes = in.read(buffer)) != -1) {
120: bytes.write(buffer, 0, last_read_bytes);
121: }
122: if (bytes.size() > 0) {
123: imageCache.put(url, bytes.toByteArray());
124: imageTypeCache.put(url, connection
125: .getContentType());
126: }
127: }
128: if (imageTypeCache.get(url) != null) {
129: contentType = imageTypeCache.get(url);
130: imageContent = imageCache.get(url);
131: }
132: } catch (IOException e) {
133: Log.error(e);
134: }
135: }
136: response.setContentType(contentType);
137: os.write(imageContent);
138: os.flush();
139: os.close();
140: }
141:
142: }
|