KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > claros > chat > ajax > ShowAvatar


1 package org.claros.chat.ajax;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.http.HttpServlet JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.claros.chat.controllers.AvatarController;
17 import org.claros.chat.models.Avatar;
18 import org.claros.commons.configuration.Paths;
19 import org.jivesoftware.smack.XMPPConnection;
20 import org.jivesoftware.smackx.packet.VCard;
21
22 public class ShowAvatar extends HttpServlet JavaDoc {
23     private static final long serialVersionUID = 5777405777244865645L;
24     private static Log log = LogFactory.getLog(ShowAvatar.class);
25     
26     /**
27      * Constructor of the object.
28      */

29     public ShowAvatar() {
30         super();
31     }
32     
33     /**
34      * The doGet method of the servlet. <br>
35      *
36      * This method is called when a form has its tag value method equals to get.
37      *
38      * @param request the request send by the client to the server
39      * @param response the response send by the server to the client
40      * @throws ServletException if an error occurred
41      * @throws IOException if an error occurred
42      */

43     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
44             throws ServletException JavaDoc, IOException JavaDoc {
45         response.setHeader("Expires","-1");
46         response.setHeader("Pragma","no-cache");
47         response.setHeader("Cache-control","no-cache");
48         response.setContentType("image/png");
49         OutputStream JavaDoc s = response.getOutputStream();
50
51         try {
52             XMPPConnection conn = (XMPPConnection)request.getSession().getAttribute("conn");
53             String JavaDoc user = request.getParameter("u");
54
55             try {
56                 if (conn != null && conn.isConnected()) {
57                     VCard vCard = new VCard();
58                     // picture is me myself
59
if (user == null) {
60                         vCard.load(conn, user);
61                         byte[] data = vCard.getAvatar();
62                         if (data != null && data.length > 0) {
63                             s.write(data);
64                             s.close();
65                             return;
66                         }
67                     } else {
68                         // somebody other's picture
69
vCard.load(conn, user);
70                         byte[] data = vCard.getAvatar();
71                         String JavaDoc hash = vCard.getAvatarHash();
72                         
73                         AvatarController.addAvatar(user, data, hash);
74                     }
75                 }
76             } catch (Exception JavaDoc e) {
77                 log.debug("unable to fetch vcard for user: " + user, e);
78             }
79             
80             Avatar avatar = (Avatar)AvatarController.getAvatar(user);
81             if (avatar != null) {
82                 byte[] b = avatar.getData();
83                 if (b != null && b.length > 0) {
84                     s.write(b);
85                 } else {
86                     showDefaultAvatar(s);
87                 }
88             } else {
89                 showDefaultAvatar(s);
90             }
91         } catch (RuntimeException JavaDoc e) {
92             // do nothing sier
93
}
94         s.flush();
95         s.close();
96     }
97     
98     /**
99      *
100      * @param s
101      */

102     private void showDefaultAvatar(OutputStream JavaDoc s) {
103         try {
104             BufferedInputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(Paths.getResFolder() + "/avatar.png"));
105             int byte_;
106             while ((byte_ = is.read ()) != -1) {
107                 s.write (byte_);
108             }
109         } catch (FileNotFoundException JavaDoc e) {
110             e.printStackTrace();
111         } catch (IOException JavaDoc e) {
112             e.printStackTrace();
113         }
114     }
115 }
116
Popular Tags