KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > servlet > AddressbookCVF


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Export the address book as a number of CVF files in an archive.
28  */

29 package net.killingar.forum.servlet;
30
31 import net.killingar.forum.internal.User;
32 import net.killingar.forum.internal.Utils;
33 import net.killingar.forum.internal.managers.ForumManager;
34
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServlet JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40 import java.io.IOException JavaDoc;
41
42 public class AddressbookCVF extends HttpServlet JavaDoc
43 {
44   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
45   {
46         HttpSession JavaDoc session = request.getSession(true);
47
48         ForumManager manager;
49         try
50         {
51             synchronized (session)
52             {
53                 manager = (ForumManager)session.getAttribute("manager");
54                 if (manager == null)
55                 {
56                     manager = new ForumManager();
57                     session.setAttribute("manager", manager);
58                 }
59             }
60
61             // get manager
62
// get list of users
63

64             response.setContentType("application/x-zip-compressed");
65             response.setHeader("Content-Disposition", "attachment; filename=addressbook.zip");
66
67             User users[] = manager.getCommonGroupUsers(manager.getUserID());
68
69             java.util.zip.ZipOutputStream JavaDoc out = new java.util.zip.ZipOutputStream JavaDoc(response.getOutputStream());
70
71             for (int i = 0; i < users.length; i++)
72             {
73                 out.putNextEntry(new java.util.zip.ZipEntry JavaDoc(users[i].name+".vcf"));
74
75                 out.write("BEGIN:VCARD\r\n".getBytes());
76                 out.write("VERSION:2.1\r\n".getBytes());
77
78                 if (users[i].realName != null && !users[i].realName.equals(""))
79                 {
80                     users[i].realName = removeUmlaut(users[i].realName);
81
82                     String JavaDoc
83                         first = "",
84                         middle = "",
85                         last = "";
86
87                     if (users[i].realName.indexOf(" ") != -1)
88                     {
89                         first = users[i].realName.substring(0, users[i].realName.indexOf(" "));
90
91                         middle = users[i].realName.substring(users[i].realName.indexOf(" ")); // remove first name
92
if (middle.lastIndexOf(" ") != -1)
93                             middle = middle.substring(0, middle.lastIndexOf(" ")); // remove last name
94
else
95                             middle = "";
96                     }
97                     if (users[i].realName.lastIndexOf(" ") != -1)
98                         last = users[i].realName.substring(users[i].realName.lastIndexOf(" "));
99
100                     out.write(("N:"+last+";"+first+";"+middle+";\r\n").getBytes());
101
102                     out.write(("FN:"+users[i].realName+"\r\n").getBytes());
103                 }
104                 else
105                 {
106                     out.write(("FN:"+users[i].name+"\r\n").getBytes());
107                 }
108                 if (users[i].name != null && !users[i].name.equals("")) out.write(("NICKNAME:"+ removeUmlaut(users[i].name) +"\r\n").getBytes());
109                 if ((users[i].other != null && !users[i].other.equals("")) || (users[i].icq != null && !users[i].icq.equals("")))
110                 {
111                     if (users[i].other != null && !users[i].other.equals(""))
112                     {
113                         if (users[i].icq != null && !users[i].icq.equals(""))
114                             out.write(("NOTE:"+users[i].name+", "+ removeUmlaut(users[i].other)+" (icq "+removeUmlaut(users[i].icq)+")"+"\r\n").getBytes()); // FIXME
115
else
116                             out.write(("NOTE:"+users[i].name+", "+ removeUmlaut(users[i].other)+"\r\n").getBytes()); // FIXME
117
}
118                     else if (users[i].icq != null && !users[i].icq.equals(""))
119                         out.write(("NOTE:"+users[i].name+" (icq "+removeUmlaut(users[i].icq)+")"+"\r\n").getBytes()); // FIXME
120
}
121                 if (users[i].telephone != null && !users[i].telephone.equals("")) out.write(("TEL;HOME;VOICE:"+ removeUmlaut(users[i].telephone) +"\r\n").getBytes());
122                 if (users[i].mobilephone != null && !users[i].mobilephone.equals("")) out.write(("TEL;CELL;VOICE:"+ removeUmlaut(users[i].mobilephone)+"\r\n").getBytes());
123                 if (users[i].address != null && !users[i].address.equals("")) out.write(("ADR;HOME:;;"+removeUmlaut(users[i].address) +";;;;\r\n").getBytes()); // FIXME format of newline is "=0D=0A"
124
if (users[i].birthdate != null && !users[i].birthdate.equals("")) out.write(("BDAY:"+ new java.text.SimpleDateFormat JavaDoc("yyyyMMdd").format(users[i].birthdate)+"\r\n").getBytes());
125                 if (users[i].email != null && !users[i].email.equals("")) out.write(("EMAIL;PREF;INTERNET:"+ removeUmlaut(users[i].email) +"\r\n").getBytes());
126
127                 out.write("END:VCARD\r\n".getBytes());
128             }
129
130             out.finish();
131         }
132         catch (Exception JavaDoc e)
133         {
134             response.setContentType("text/plain");
135             e.printStackTrace(response.getWriter());
136         }
137   }
138
139     public static String JavaDoc removeUmlaut(String JavaDoc s)
140     {
141         if (s == null)
142             return null;
143
144         s = Utils.unHTML(s);
145         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s.length());
146         for (int i = 0; i < s.length(); i++)
147         {
148             switch(s.charAt(i))
149             {
150             case 'ë':
151             case 'é':
152             case 'è':
153                 sb.append("e");
154                 break;
155             case 'Ë':
156             case 'É':
157             case 'È':
158                 sb.append("E");
159                 break;
160             case 'å':
161             case 'ä':
162             case 'á':
163             case 'à':
164                 sb.append("a");
165                 break;
166             case 'Å':
167             case 'Ä':
168             case 'Á':
169             case 'À':
170                 sb.append("A");
171                 break;
172             case 'ö':
173             case 'ó':
174             case 'ò':
175                 sb.append("o");
176                 break;
177             case 'Ö':
178             case 'Ó':
179             case 'Ò':
180                 sb.append("O");
181                 break;
182             default:
183                 sb.append(s.charAt(i));
184                 break;
185             }
186         }
187         return sb.toString();
188     }
189 }
Popular Tags