Source Code Cross Referenced for ImagePresenceProvider.java in  » Net » openfire » org » jivesoftware » openfire » plugin » presence » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » openfire » org.jivesoftware.openfire.plugin.presence 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.