1 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 ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 51 class ImagePresenceProvider extends PresenceInfoProvider { 52 53 private PresenceStatusServlet servlet; 54 private Map <String , byte[]> imageCache = new HashMap <String , byte[]>(); 55 private Map <String , String > imageTypeCache = new HashMap <String , String >(); 56 57 public ImagePresenceProvider(PresenceStatusServlet servlet) { 58 this.servlet = servlet; 59 } 60 61 public void sendInfo(HttpServletRequest request, 62 HttpServletResponse response, Presence presence) throws IOException { 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 request, HttpServletResponse response) 84 throws IOException { 85 writeImageContent(request, response, "forbidden", servlet.offline); 86 } 87 88 private void writeImageContent(HttpServletRequest request, HttpServletResponse response, 89 String presenceType, byte[] defaultImage) throws IOException { 90 String 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 url, byte[] defaultContent, HttpServletResponse response) 103 throws IOException { 104 ServletOutputStream os = response.getOutputStream(); 105 byte[] imageContent = defaultContent; 106 String contentType = "image/gif"; 107 if (url != null) { 108 try { 109 byte[] cachedContent = imageCache.get(url); 110 if (cachedContent == null) { 111 URLConnection connection = new URL (url).openConnection(); 112 InputStream in = connection.getInputStream(); 113 ByteArrayOutputStream bytes = new ByteArrayOutputStream (); 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 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 |