1 25 26 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 ; 36 import javax.servlet.http.HttpServlet ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.servlet.http.HttpSession ; 40 import java.io.IOException ; 41 42 public class AddressbookCVF extends HttpServlet 43 { 44 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 45 { 46 HttpSession 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 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 out = new java.util.zip.ZipOutputStream (response.getOutputStream()); 70 71 for (int i = 0; i < users.length; i++) 72 { 73 out.putNextEntry(new java.util.zip.ZipEntry (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 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(" ")); if (middle.lastIndexOf(" ") != -1) 93 middle = middle.substring(0, middle.lastIndexOf(" ")); 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()); else 116 out.write(("NOTE:"+users[i].name+", "+ removeUmlaut(users[i].other)+"\r\n").getBytes()); } 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()); } 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()); if (users[i].birthdate != null && !users[i].birthdate.equals("")) out.write(("BDAY:"+ new java.text.SimpleDateFormat ("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 e) 133 { 134 response.setContentType("text/plain"); 135 e.printStackTrace(response.getWriter()); 136 } 137 } 138 139 public static String removeUmlaut(String s) 140 { 141 if (s == null) 142 return null; 143 144 s = Utils.unHTML(s); 145 StringBuffer sb = new StringBuffer (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 |