不错不错~~~
001 package com.jivesoftware.forum.action;
002
003 import com.jivesoftware.base.UnauthorizedException;
004 import com.jivesoftware.base.JiveGlobals;
005 import com.jivesoftware.forum.*;
006 import com.jivesoftware.util.ByteFormat;
007 import com.jivesoftware.util.MultipartParamUtils;
008 import com.jivesoftware.webwork.action.CommandDriven;
009
010 import java.io.IOException;
011 import java.io.InputStream;
012 import java.util.*;
013 import javax.servlet.http.HttpSession;
014
015 public class AttachAction extends PostAction
016 implements CommandDriven {
017
018 public AttachAction() {
019 maxRequestSize = 0;
020 attachID = -1L;
021 }
022
023 public String getDoAttachFiles() {
024 return doAttachFiles;
025 }
026
027 public void setDoAttachFiles(String doAttachFiles) {
028 if (doAttachFiles != null && !"".equals(doAttachFiles.trim()))
029 this.doAttachFiles = "true";
030 }
031
032 public void addErrorMessage(String message) {
033 super.addErrorMessage(message);
034 }
035
036 public int[] getDeleteAttachIDs() {
037 return deleteAttachIDs;
038 }
039
040 public void setDeleteAttachIDs(int deleteAttachIDs[]) {
041 this.deleteAttachIDs = deleteAttachIDs;
042 }
043
044 public long getAttachID() {
045 return attachID;
046 }
047
048 public void setAttachID(long attachID) {
049 this.attachID = attachID;
050 }
051
052 public String getEncSubject() {
053 return encSubject;
054 }
055
056 public void setEncSubject(String encSubject) {
057 if (encSubject != null && !"".equals(encSubject))
058 this.encSubject = encSubject;
059 }
060
061 public String getEncBody() {
062 return encBody;
063 }
064
065 public void setEncBody(String encBody) {
066 if (encBody != null && !"".equals(encBody))
067 this.encBody = encBody;
068 }
069
070 public AttachmentManager getAttachmentManager() {
071 return getForumFactory().getAttachmentManager();
072 }
073
074 public String getName() {
075 return (String) getServletRequest().getSession().getAttribute("jive.post.name." + getSessionSuffix());
076 }
077
078 public String getEmail() {
079 return (String) getServletRequest().getSession().getAttribute("jive.post.email." + getSessionSuffix());
080 }
081
082 public String getSubject() {
083 return (String) getServletRequest().getSession().getAttribute("jive.post.subject." + getSessionSuffix());
084 }
085
086 public String getBody() {
087 return (String) getServletRequest().getSession().getAttribute("jive.post.body." + getSessionSuffix());
088 }
089
090 public boolean isReply() {
091 Boolean replyVal = (Boolean) getServletRequest().getSession().getAttribute("jive.post.reply." + getSessionSuffix());
092 return replyVal != null && replyVal.booleanValue();
093 }
094
095 public String getDoCancel() {
096 String doCancel = params.getParameter("doCancel");
097 if (doCancel != null && !"".equals(doCancel.trim()))
098 return "true";
099 else
100 return null;
101 }
102
103 /* public boolean isEdit()
104 {
105 return false;
106 }
107 */
108
109 protected void doValidation() {
110 AttachmentManager manager = getAttachmentManager();
111 int requestSize = getServletRequest().getContentLength();
112 if (requestSize > maxRequestSize) {
113 ByteFormat byteFormat = new ByteFormat();
114 List args = new ArrayList();
115 args.add(byteFormat.format(new Long(manager.getMaxAttachmentSize() * 1024)));
116 addErrorMessage(getText("attach.error_generic", args));
117 return;
118 }
119 boolean hasValid = false;
120 int i = 0;
121 do {
122 if (i >= manager.getMaxAttachmentsPerMessage())
123 break;
124 String contentType = params.getParameterContentType("attachFile" + (i + 1));
125 int attachSize = params.getParameterSize("attachFile" + (i + 1));
126 if (manager.isValidType(contentType)) {
127 hasValid = true;
128 break;
129 }
130 if (attachSize < manager.getMaxAttachmentSize() * 1024) {
131 hasValid = true;
132 break;
133 }
134 i++;
135 } while (true);
136 if (!hasValid)
137 addErrorMessage(getText("attach.error_content_type"));
138 }
139
140 public String doDefault() {
141 try {
142 if (!loadJiveObjects()) {
143 return ERROR;
144 }
145 } catch (UnauthorizedException e) {
146 setLoginAttributes();
147 addErrorMessage(getText("attach.error_unauth"));
148 return LOGIN;
149 }
150 if (!getCanAttach(getForum())) {
151 setLoginAttributes();
152 addErrorMessage(getText("attach.error_unauth"));
153 return LOGIN;
154 }
155 return INPUT;
156 }
157
158 public String doDelete() {
159 try {
160 if (!loadJiveObjects()) {
161 return ERROR;
162 }
163 } catch (UnauthorizedException e) {
164 setLoginAttributes();
165 addErrorMessage(getText("attach.error_unauth"));
166 return LOGIN;
167 }
168
169 if (this.attachID == -1L) {
170 addErrorMessage(getText("attach.error_unable_delete"));
171 return "error";
172 }
173 ForumMessage msg = getMessage();
174 Attachment attachment = null;
175 Iterator attachments = msg.getAttachments();
176 long attachID;
177
178 do {
179 if (!attachments.hasNext())
180 break;
181 attachment = (Attachment) attachments.next();
182 attachID = attachment.getID();
183 } while (this.attachID != attachID);
184 try {
185 msg.deleteAttachment(attachment);
186 } catch (AttachmentException ae) {
187 addErrorMessage(ae.getMessage());
188 return ERROR;
189 } catch (UnauthorizedException ue) {
190 addErrorMessage(ue.getMessage());
191 return ERROR;
192 }
193 getServletRequest().setAttribute("partialURL", getPartialURL());
194 return "success-delete";
195 }
196
197 public String doExecute() {
198 try {
199 if (loadJiveObjects() == false) {
200 return "error";
201 }
202 } catch (UnauthorizedException e) {
203 setLoginAttributes();
204 addErrorMessage(getText("attach.error_unauth"));
205 return "login";
206 }
207 if ("true".equals(getDoCancel())) {
208 request.setAttribute("tid", getTid());
209 if (isReply()) {
210 return "cancel-reply";
211 }
212 return "cancel-topic";
213 }
214 if (getCanAttach(getForum()) == false) {
215 setLoginAttributes();
216 addErrorMessage(getText("attach.error_unauth"));
217 return "login";
218 }
219 if (isNothingSelected()) {
220 addErrorMessage(getText("attach.error_at_least_one"));
221 request.setAttribute("tid", getTid());
222 return "input";
223 }
224 if (!isEdit()) {
225 /*try{
226 addAttachments();
227 }catch(UnauthorizedException ue){
228 ue.printStackTrace();
229 addErrorMessage(getText("attach.error_unauth"));
230 return "error";
231 }*/
232 addAttachments();
233 if (getHasErrors()) {
234 return ERROR;
235 }
236 request.setAttribute("tid", getTid());
237 if (isReply())
238 return "success-reply";
239 request.setAttribute("tid", getTid());
240 if (isReply())
241 return "success-reply";
242 else
243 return "success-topic";
244
245 } else {
246 try {
247 addAttachments(getMessage());
248 if (getHasErrors())
249 return "error";
250 else
251 return "success-edit";
252 } catch (UnauthorizedException ue) {
253 ue.printStackTrace();
254 addErrorMessage(getText("attach.error_unauth"));
255 return ERROR;
256 }
257 }
258 }
259
260 protected int getExtraParamCount() {
261 return 0;
262 }
263
264 protected boolean loadJiveObjects()
265 throws UnauthorizedException {
266 boolean success = true;
267 AttachmentManager manager = getAttachmentManager();
268 maxRequestSize = (manager.getMaxAttachmentsPerMessage() * manager.getMaxAttachmentSize() + manager.getMaxAttachmentsPerMessage() * 16 + getExtraParamCount() * 16) * 1024;
269 try {
270 params = new MultipartParamUtils(getServletRequest(), maxRequestSize);
271 } catch (IOException ioe) {
272 addErrorMessage(ioe.getMessage());
273 success = false;
274 }
275 if (success)
276 return super.loadJiveObjects();
277 else
278 return success;
279 }
280
281 protected void addAttachments() {
282 AttachmentManager manager;
283 HttpSession session;
284 String key;
285 List attachIDs;
286 manager = getAttachmentManager();
287 session = request.getSession();
288 String suffix = getSessionSuffix();
289 key = "jive.post.tempAttachIDs" + suffix;
290 attachIDs = (List) session.getAttribute(key);
291 if (attachIDs == null) attachIDs = new ArrayList(5);
292 for (int i = 0; i < manager.getMaxAttachmentsPerMessage(); i++) {
293
294 String attachmentName;
295 String contentType;
296 InputStream in;
297 attachmentName = params.getParameter("attachFile" + (i + 1));
298 contentType = params.getParameterContentType("attachFile" + (i + 1));
299 in = null;
300 in = params.getParameterData("attachFile" + (i + 1));
301 try {
302 try {
303 if (attachmentName != null && contentType != null && in != null) {
304 attachmentName = getCanonicalName(attachmentName);
305 Attachment attach = manager.createTempAttachment(attachmentName, contentType, in);
306 attachIDs.add(new Long(attach.getID()));
307 }
308 in.close();
309 } catch (AttachmentException ae) {
310 if (ae.getErrorType() == 0)
311 addError("attachFile" + (i + 1), getText("attach.error_too_large_field"));
312 else if (ae.getErrorType() == 2)
313 addError("attachFile" + (i + 1), getText("attach.error_content_type"));
314 else if (ae.getErrorType() == 1)
315 addError("attachFile" + (i + 1), getText("attach.error_too_many"));
316 else
317 addError("attachFile" + (i + 1), getText("attach.error_no_read_perm"));
318 }
319 } catch (Exception e) {
320 e.printStackTrace();
321 }
322 if (attachIDs.size() > 0) session.setAttribute(key, attachIDs);
323 }
324
325 }
326
327 protected void addAttachments(ForumMessage forumMessage) throws UnauthorizedException {
328 AttachmentManager manager = getAttachmentManager();
329 for (int i = 0; i < manager.getMaxAttachmentsPerMessage(); i++) {
330 String attachmentName = params.getParameter("attachFile" + (i + 1));
331 String contentType = params.getParameterContentType("attachFile" + (i + 1));
332 InputStream in = null;
333 try {
334 // Try adding the attachment to the message
335 in = params.getParameterData("attachFile" + (i + 1));
336 if (attachmentName != null && contentType != null && in != null) {
337 // Adjust the name (we only want the canonical name of the file)
338 attachmentName = getCanonicalName(attachmentName);
339 forumMessage.createAttachment(attachmentName, contentType, in);
340 }
341 }
342 /*catch(UnauthorizedException ue)*/ catch (AttachmentException ae) {
343 if (ae.getErrorType() == AttachmentException.TOO_LARGE) {
344 addError("attachFile" + (i + 1), getText("attach.error_too_large_field"));
345 } else if (ae.getErrorType() == AttachmentException.BAD_CONTENT_TYPE) {
346 addError("attachFile" + (i + 1), getText("attach.error_content_type"));
347 } else if (ae.getErrorType() == AttachmentException.TOO_MANY_ATTACHMENTS) {
348 addError("attachFile" + (i + 1), getText("attach.error_too_many"));
349 } else {
350 addError("attachFile" + (i + 1), getText("attach.error_no_read_perm"));
351 }
352 } finally {
353 try {
354 in.close();
355 } catch (Exception e) {
356 }
357 }
358 }
359 }
360
361 private boolean isNothingSelected() {
362 boolean nothing = true;
363 AttachmentManager manager = getAttachmentManager();
364 for (int i = 0; i < manager.getMaxAttachmentsPerMessage(); i++) {
365 String attachmentName = params.getParameter("attachFile" + (i + 1));
366 if (attachmentName != null)
367 nothing = false;
368 }
369
370 return nothing;
371 }
372
373 /* private String getCanonicalName(String filename) {
374 int forwardSlash = filename.lastIndexOf("/");
375 int backwardSlash = filename.lastIndexOf("\\");
376 if (forwardSlash != -1 && forwardSlash > backwardSlash)
377 filename = filename.substring(forwardSlash + 1, filename.length());
378 else if (backwardSlash != -1 && backwardSlash >= forwardSlash)
379 filename = filename.substring(backwardSlash + 1, filename.length());
380 return filename;
381 }
382 */
383 private String getCanonicalName(String filename)
384 {
385 int forwardSlash = filename.lastIndexOf("/");
386 int backwardSlash = filename.lastIndexOf("\\");
387 if(forwardSlash != -1 && forwardSlash > backwardSlash)
388 filename = filename.substring(forwardSlash + 1, filename.length());
389 else
390 if(backwardSlash != -1 && backwardSlash >= forwardSlash)
391 filename = filename.substring(backwardSlash + 1, filename.length());
392 if(JiveGlobals.getCharacterEncoding().equalsIgnoreCase("UTF-8") && JiveGlobals.getLocale().getCountry().equalsIgnoreCase("CN"))
393 try
394 {
395 filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
396 }
397 catch(Exception e) { }
398 return filename;
399 }
400
401 public static String SUCCESS_TOPIC = "success-topic";
402 public static String SUCCESS_MESSAGE = "success-message";
403 protected MultipartParamUtils params;
404 private int maxRequestSize;
405 private int deleteAttachIDs[];
406 private long attachID;
407 private String doAttachFiles;
408 private String encSubject;
409 private String encBody;
410 }