001: package org.claros.chat.ajax;
002:
003: import java.io.BufferedInputStream;
004: import java.io.FileInputStream;
005: import java.io.FileNotFoundException;
006: import java.io.IOException;
007: import java.io.OutputStream;
008:
009: import javax.servlet.ServletException;
010: import javax.servlet.http.HttpServlet;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.claros.chat.controllers.AvatarController;
017: import org.claros.chat.models.Avatar;
018: import org.claros.commons.configuration.Paths;
019: import org.jivesoftware.smack.XMPPConnection;
020: import org.jivesoftware.smackx.packet.VCard;
021:
022: public class ShowAvatar extends HttpServlet {
023: private static final long serialVersionUID = 5777405777244865645L;
024: private static Log log = LogFactory.getLog(ShowAvatar.class);
025:
026: /**
027: * Constructor of the object.
028: */
029: public ShowAvatar() {
030: super ();
031: }
032:
033: /**
034: * The doGet method of the servlet. <br>
035: *
036: * This method is called when a form has its tag value method equals to get.
037: *
038: * @param request the request send by the client to the server
039: * @param response the response send by the server to the client
040: * @throws ServletException if an error occurred
041: * @throws IOException if an error occurred
042: */
043: public void doGet(HttpServletRequest request,
044: HttpServletResponse response) throws ServletException,
045: IOException {
046: response.setHeader("Expires", "-1");
047: response.setHeader("Pragma", "no-cache");
048: response.setHeader("Cache-control", "no-cache");
049: response.setContentType("image/png");
050: OutputStream s = response.getOutputStream();
051:
052: try {
053: XMPPConnection conn = (XMPPConnection) request.getSession()
054: .getAttribute("conn");
055: String user = request.getParameter("u");
056:
057: try {
058: if (conn != null && conn.isConnected()) {
059: VCard vCard = new VCard();
060: // picture is me myself
061: if (user == null) {
062: vCard.load(conn, user);
063: byte[] data = vCard.getAvatar();
064: if (data != null && data.length > 0) {
065: s.write(data);
066: s.close();
067: return;
068: }
069: } else {
070: // somebody other's picture
071: vCard.load(conn, user);
072: byte[] data = vCard.getAvatar();
073: String hash = vCard.getAvatarHash();
074:
075: AvatarController.addAvatar(user, data, hash);
076: }
077: }
078: } catch (Exception e) {
079: log.debug("unable to fetch vcard for user: " + user, e);
080: }
081:
082: Avatar avatar = (Avatar) AvatarController.getAvatar(user);
083: if (avatar != null) {
084: byte[] b = avatar.getData();
085: if (b != null && b.length > 0) {
086: s.write(b);
087: } else {
088: showDefaultAvatar(s);
089: }
090: } else {
091: showDefaultAvatar(s);
092: }
093: } catch (RuntimeException e) {
094: // do nothing sier
095: }
096: s.flush();
097: s.close();
098: }
099:
100: /**
101: *
102: * @param s
103: */
104: private void showDefaultAvatar(OutputStream s) {
105: try {
106: BufferedInputStream is = new BufferedInputStream(
107: new FileInputStream(Paths.getResFolder()
108: + "/avatar.png"));
109: int byte_;
110: while ((byte_ = is.read()) != -1) {
111: s.write(byte_);
112: }
113: } catch (FileNotFoundException e) {
114: e.printStackTrace();
115: } catch (IOException e) {
116: e.printStackTrace();
117: }
118: }
119: }
|