Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
70   243   25   11.67
30   137   0.36   6
6     4.17  
1    
 
 
  AttachmentUtilities       Line # 36 70 25 0% 0.0
 
No Tests
 
1    /* This software was designed and created by Jason Carroll.
2    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
3    * The author can be reached at jcarroll@cowsultants.com
4    * ITracker website: http://www.cowsultants.com
5    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it only under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10    * (at your option) any later version.
11    *
12    * This program is distributed in the hope that it will be useful,
13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    * GNU General Public License for more details.
16    */
17    package org.itracker.web.util;
18   
19    import java.io.IOException;
20   
21    import org.apache.log4j.Logger;
22    import org.apache.struts.action.ActionMessage;
23    import org.apache.struts.action.ActionMessages;
24    import org.apache.struts.upload.FormFile;
25    import org.itracker.core.resources.ITrackerResources;
26    import org.itracker.model.Issue;
27    import org.itracker.model.IssueAttachment;
28    import org.itracker.model.Project;
29    import org.itracker.model.User;
30    import org.itracker.services.ConfigurationService;
31    import org.itracker.services.ITrackerServices;
32    import org.itracker.services.IssueService;
33    import org.itracker.services.util.ProjectUtilities;
34    import org.itracker.web.forms.IssueForm;
35   
 
36    public class AttachmentUtilities {
37   
38    private static final Logger logger = Logger.getLogger(AttachmentUtilities.class);
39    private static boolean initialized = false;
40    private static final String CONTENT_TYPE = "multipart/form-data";
41    private static final String CHAR_ENCODING = "ISO-8859-1";
42    private static final long MAX_FILE_SIZE_KB = 256L;
43    private static final long MAX_TOTAL_FILE_SIZE_KB = 1000000L;
44   
45    private static long maxFileSize = MAX_FILE_SIZE_KB * 1024L;
46    private static long maxTotalFileSize = MAX_TOTAL_FILE_SIZE_KB * 1024L;
47    private static long spaceLeft = 0;
48   
49   
50    /**
51    *
52    * @deprecated use {@link AttachmentUtilities#validate(FormFile, ITrackerServices)} instead for more detailed messages
53    * @param file
54    * @param services
55    * @return
56    */
 
57  0 toggle public static boolean checkFile(FormFile file, ITrackerServices services) {
58   
59  0 return validate(file, services).isEmpty();
60    //
61    //
62    // if(! initialized) {
63    // if(! init(services)) {
64    // return false;
65    // }
66    // }
67    //
68    // if(file == null) {
69    // return false;
70    // }
71    //
72    // long origFileSize = file.getFileSize();
73    // if(origFileSize > maxFileSize) {
74    // logger.info("Cannot save attachment. File is " + (origFileSize / 1024L) + " kB and max file size is set to " + (maxFileSize / 1024L) + "kB.");
75    // return false;
76    // }
77    //
78    // if((spaceLeft - origFileSize) < 0) {
79    // logger.info("Cannot save attachment. Total allocated disk space already used.");
80    // return false;
81    // }
82    // spaceLeft = spaceLeft - origFileSize;
83    // // @TODO: please check this code and clean up
84    //// filename = attachmentDirName + File.separator + filename;
85    //
86    //// if(logger.isDebugEnabled()) {
87    //// logger.debug("Attempting to save attachment: " + filename);
88    //// }
89    //
90    //// FileOutputStream out = new FileOutputStream(filename);
91    //// out.write(file.getFileData());
92    //// out.close();
93    // return true;
94    }
95    /**
96    * Adds an attachment to issue.
97    * @param issue
98    * @param project
99    * @param user
100    * @param form
101    * @param services
102    * @param messages
103    * @return updated issue
104    * @throws Exception
105    */
 
106  0 toggle public static Issue addAttachment(Issue issue, Project project, User user,
107    IssueForm form, ITrackerServices services, ActionMessages messages) {
108   
109  0 if (ProjectUtilities.hasOption(ProjectUtilities.OPTION_NO_ATTACHMENTS,
110    project.getOptions())) {
111  0 messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.validate.attachment.disabled", project.getName()));
112  0 return issue;
113    }
114   
115  0 FormFile file = form.getAttachment();
116   
117  0 if (file == null || file.getFileName().trim().length() < 1) {
118  0 logger.info("addAttachment: skipping file " + file);
119  0 return issue;
120    }
121   
122  0 String origFileName = file.getFileName();
123  0 String contentType = file.getContentType();
124  0 int fileSize = file.getFileSize();
125   
126  0 String attachmentDescription = form.getAttachmentDescription();
127   
128  0 if (null == contentType || 0 >= contentType.length()) {
129  0 logger.info("addAttachment: got no mime-type, using default plain-text");
130  0 contentType = "text/plain";
131    }
132   
133  0 if (logger.isDebugEnabled()) {
134  0 logger.debug("addAttachment: adding file, name: " + origFileName
135    + " of type " + file.getContentType() + ", description: "
136    + form.getAttachmentDescription() + ". filesize: " + fileSize);
137    }
138  0 ActionMessages validation = AttachmentUtilities.validate(file, services);
139  0 if (validation.isEmpty()) {
140   
141    // if (AttachmentUtilities.checkFile(file, getITrackerServices())) {
142  0 int lastSlash = Math.max(origFileName.lastIndexOf('/'),
143    origFileName.lastIndexOf('\\'));
144  0 if (lastSlash > -1) {
145  0 origFileName = origFileName.substring(lastSlash + 1);
146    }
147   
148  0 IssueAttachment attachmentModel = new IssueAttachment(issue,
149    origFileName, contentType, attachmentDescription, fileSize,
150    user);
151   
152  0 attachmentModel.setIssue(issue);
153    // issue.getAttachments().add(attachmentModel);
154  0 byte[] fileData;
155  0 try {
156  0 fileData = file.getFileData();
157    } catch (IOException e) {
158  0 logger.error("addAttachment: failed to get file data", e);
159  0 messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
160  0 return issue;
161    }
162  0 if(services.getIssueService()
163    .addIssueAttachment(attachmentModel, fileData)) {
164  0 return services.getIssueService().getIssue(issue.getId());
165    }
166   
167   
168    } else {
169  0 if (logger.isDebugEnabled()) {
170  0 logger.debug("addAttachment: failed to validate: " + origFileName + ", " + validation);
171    }
172  0 messages.add(validation);
173    }
174  0 return issue;
175    }
176   
 
177  0 toggle public static ActionMessages validate(FormFile file, ITrackerServices services) {
178  0 ActionMessages msg = new ActionMessages();
179  0 if(! initialized) {
180  0 if(! init(services)) {
181  0 msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
182  0 return msg;
183    }
184    }
185   
186  0 if(file == null) {
187  0 return msg;
188    }
189   
190  0 long origFileSize = file.getFileSize();
191  0 if(origFileSize > maxFileSize) {
192  0 logger.info("Cannot save attachment. File is " + (origFileSize / 1024L) + " kB and max file size is set to " + (maxFileSize / 1024L) + "kB.");
193   
194  0 msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.validate.attachment.size", (Math.round((maxFileSize / 1024L) * 100) / 100) + " " + ITrackerResources.getString("itracker.web.generic.kilobyte")));
195   
196  0 return msg;
197    }
198   
199  0 if((spaceLeft - origFileSize) < 0) {
200  0 logger.info("Cannot save attachment. Total allocated disk space already used.");
201  0 msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.validate.attachment.quota", (Math.round((maxTotalFileSize / 1024L) * 100) / 100) + " " + ITrackerResources.getString("itracker.web.generic.kilobyte")));
202   
203    }
204  0 spaceLeft = spaceLeft - origFileSize;
205   
206  0 return msg;
207    }
208   
209   
210   
 
211  0 toggle private static boolean init(ITrackerServices services) {
212  0 if(! initialized) {
213  0 try {
214  0 ConfigurationService configurationService = services.getConfigurationService();
215  0 IssueService issueService = services.getIssueService();
216   
217  0 maxFileSize = configurationService.getLongProperty("max_attachment_size", MAX_FILE_SIZE_KB) * 1024L;
218  0 maxTotalFileSize = configurationService.getLongProperty("max_total_attachment_size", MAX_TOTAL_FILE_SIZE_KB) * 1024L;
219  0 spaceLeft = maxTotalFileSize - issueService.getAllIssueAttachmentSize();
220   
221  0 if(logger.isDebugEnabled()) {
222  0 logger.debug("Attachment Properties: MaxAttachmentSize set to " + (maxFileSize / 1024L) + " kB");
223  0 logger.debug("Attachment Properties: MaxTotalAttachmentsSize set to " + (maxTotalFileSize / 1024L) + " kB");
224  0 logger.debug("Attachment Properties: Current space left is " + (spaceLeft / 1024L) + " kB");
225    }
226  0 initialized = true;
227    } catch(Exception e) {
228  0 logger.error("Exception initializing AttachmentUtilities.", e);
229  0 throw new Error("Failed to initialize AttachmentUtilities.", e);
230    }
231    }
232  0 return initialized;
233    }
234   
 
235  0 toggle public static String getCONTENT_TYPE() {
236  0 return CONTENT_TYPE;
237    }
238   
 
239  0 toggle public static String getCHAR_ENCODING() {
240  0 return CHAR_ENCODING;
241    }
242   
243    }