Skip to content

Commit bec24ba

Browse files
author
Emmanuel Hugonnet
committed
Preparing the release of the 5.12
Adding humanReadable function to format sizes Using the same code as in silverpeas to manage attachedFiles thus avoiding NPE
1 parent e6497a9 commit bec24ba

File tree

5 files changed

+175
-203
lines changed

5 files changed

+175
-203
lines changed

src/main/java/com/silverpeas/tags/almanach/AlmanachTagUtil.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121
package com.silverpeas.tags.almanach;
2222

23-
import java.util.Calendar;
2423
import java.util.Collection;
2524

2625
import com.silverpeas.tags.ComponentTagUtil;
@@ -45,11 +44,6 @@ public AlmanachTagUtil(String componentId, String userId) {
4544
this.componentId = componentId;
4645
}
4746

48-
public Collection<EventDetail> getMonthEvents() {
49-
return getAlmanachBm().getMonthEvents(new EventPK("", spaceId, componentId),
50-
Calendar.getInstance().getTime());
51-
}
52-
5347
public Collection<EventDetail> getAllEvents() {
5448
return getAlmanachBm().getAllEvents(new EventPK("", spaceId, componentId));
5549
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2000 - 2012 Silverpeas
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* As a special exception to the terms and conditions of version 3.0 of
10+
* the GPL, you may redistribute this Program in connection withWriter Free/Libre
11+
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12+
* FLOSS exception. You should have recieved a copy of the text describing
13+
* the FLOSS exception, and it is also available here:
14+
* "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
package com.silverpeas.tags.servlets;
25+
26+
import java.io.File;
27+
import java.io.IOException;
28+
29+
import org.silverpeas.attachment.model.SimpleDocument;
30+
31+
/**
32+
* @author ehugonnet
33+
*/
34+
public class OnlineAttachment extends OnlineFile {
35+
36+
private SimpleDocument document;
37+
38+
public OnlineAttachment(SimpleDocument document) {
39+
super(document.getContentType(), document.getFilename(), document.getAttachmentPath(), document.
40+
getInstanceId());
41+
this.document = document;
42+
}
43+
44+
@Override
45+
public File getContentFile() throws IOException {
46+
return new File(document.getAttachmentPath());
47+
}
48+
}
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
/**
22
* Copyright (C) 2000 - 2012 Silverpeas
33
*
4-
* This program is free software: you can redistribute it and/or modify
5-
* it under the terms of the GNU Affero General Public License as
6-
* published by the Free Software Foundation, either version 3 of the
7-
* License, or (at your option) any later version.
4+
* This program is free software: you can redistribute it and/or modify it under the terms of the
5+
* GNU Affero General Public License as published by the Free Software Foundation, either version 3
6+
* of the License, or (at your option) any later version.
87
*
9-
* As a special exception to the terms and conditions of version 3.0 of
10-
* the GPL, you may redistribute this Program in connection with Free/Libre
11-
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12-
* FLOSS exception. You should have received a copy of the text describing
13-
* the FLOSS exception, and it is also available here:
14-
* "http://www.silverpeas.org/legal/licensing"
8+
* As a special exception to the terms and conditions of version 3.0 of the GPL, you may
9+
* redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
10+
* applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
11+
* text describing the FLOSS exception, and it is also available here:
12+
* "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
1513
*
16-
* This program is distributed in the hope that it will be useful,
17-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19-
* GNU Affero General Public License for more details.
14+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Affero General Public License for more details.
2017
*
21-
* You should have received a copy of the GNU Affero General Public License
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
* You should have received a copy of the GNU Affero General Public License along with this program.
19+
* If not, see <http://www.gnu.org/licenses/>.
2320
*/
24-
2521
package com.silverpeas.tags.servlets;
2622

23+
import com.stratelia.silverpeas.silvertrace.SilverTrace;
24+
import com.stratelia.webactiv.util.FileRepositoryManager;
25+
26+
import java.io.File;
27+
import java.io.FileNotFoundException;
28+
import java.io.IOException;
29+
2730
public class OnlineFile {
31+
2832
private String mimeType;
2933
private String sourceFile;
3034
private String directory;
3135
private String componentId;
3236

33-
public OnlineFile(String mimeType, String sourceFile, String directory) {
37+
public OnlineFile(String mimeType, String sourceFile, String directory, String componentId) {
3438
this.mimeType = mimeType;
3539
this.sourceFile = sourceFile;
3640
this.directory = directory;
41+
this.componentId = componentId;
3742
}
3843

3944
public String getMimeType() {
@@ -44,16 +49,18 @@ public String getSourceFile() {
4449
return sourceFile;
4550
}
4651

47-
public String getDirectory() {
48-
return directory;
49-
}
50-
5152
public String getComponentId() {
5253
return componentId;
5354
}
54-
55-
public void setComponentId(String componentId) {
56-
this.componentId = componentId;
55+
public File getContentFile() throws IOException {
56+
String filePath = FileRepositoryManager.getAbsolutePath(componentId) + directory
57+
+ File.separator + sourceFile;
58+
File realFile = new File(filePath);
59+
if (!realFile.exists() && !realFile.isFile()) {
60+
throw new FileNotFoundException("File " + filePath + "not found!");
61+
}
62+
SilverTrace.info("peasUtil", "OnlineFile.write()", "root.MSG_GEN_ENTER_METHOD", " file "
63+
+ filePath);
64+
return realFile;
5765
}
58-
5966
}

src/main/java/com/silverpeas/tags/servlets/WebFileServer.java

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.File;
2424
import java.io.IOException;
2525
import java.io.OutputStream;
26-
import java.io.PrintWriter;
2726
import java.io.StringReader;
2827
import java.rmi.RemoteException;
2928

@@ -44,12 +43,12 @@
4443
import com.silverpeas.gallery.model.PhotoPK;
4544
import com.silverpeas.tags.authentication.AuthenticationManager;
4645
import com.silverpeas.tags.util.Admin;
46+
import com.silverpeas.util.FileUtil;
4747
import com.silverpeas.util.StringUtil;
4848
import com.silverpeas.util.web.servlet.RestRequest;
4949

5050
import com.stratelia.silverpeas.silvertrace.SilverTrace;
5151
import com.stratelia.webactiv.util.EJBUtilitaire;
52-
import com.stratelia.webactiv.util.FileRepositoryManager;
5352
import com.stratelia.webactiv.util.JNDINames;
5453
import com.stratelia.webactiv.util.ResourceLocator;
5554
import com.stratelia.webactiv.util.exception.SilverpeasRuntimeException;
@@ -59,7 +58,7 @@
5958

6059
public class WebFileServer extends HttpServlet {
6160

62-
PrintWriter out;
61+
private static final long serialVersionUID = 1L;
6362
private Admin admin = null;
6463

6564
@Override
@@ -72,8 +71,8 @@ public void init(ServletConfig config) {
7271
}
7372

7473
@Override
75-
public void service(HttpServletRequest req, HttpServletResponse res)
76-
throws ServletException, IOException {
74+
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException,
75+
IOException {
7776
SilverTrace.info("peasUtil", "OnlineFileServer.doPost", "root.MSG_GEN_ENTER_METHOD");
7877
RestRequest restRequest = new RestRequest(req, "");
7978
String userId = AuthenticationManager.getUserId(req);
@@ -105,14 +104,11 @@ public OnlineFile getFileFromOldURL(RestRequest restRequest) throws RemoteExcept
105104

106105
String attachmentId = restRequest.getWebRequest().getParameter("attachmentId");
107106
if (StringUtil.isDefined(attachmentId)) {
108-
SimpleDocument attachment = AttachmentServiceFactory.getAttachmentService().searchDocumentById(
107+
SimpleDocument attachment = AttachmentServiceFactory.getAttachmentService().
108+
searchDocumentById(
109109
new SimpleDocumentPK(attachmentId), null);
110110
if (attachment != null) {
111-
mimeType = attachment.getContentType();
112-
sourceFile = attachment.getFilename();
113-
directory = attachment.getAttachmentPath();
114-
file = new OnlineFile(mimeType, sourceFile, directory);
115-
file.setComponentId(componentId);
111+
file = new OnlineAttachment(attachment);
116112
}
117113
} else if (StringUtil.isDefined(imageId)) {
118114
PhotoDetail image = getGalleryBm().getPhoto(new PhotoPK(imageId, componentId));
@@ -124,11 +120,9 @@ public OnlineFile getFileFromOldURL(RestRequest restRequest) throws RemoteExcept
124120
sourceFile = image.getId() + "_preview.jpg";
125121
}
126122
directory = "image" + image.getId();
127-
file = new OnlineFile(mimeType, sourceFile, directory);
128-
file.setComponentId(componentId);
123+
file = new OnlineFile(mimeType, sourceFile, directory, componentId);
129124
} else {
130-
file = new OnlineFile(mimeType, sourceFile, directory);
131-
file.setComponentId(componentId);
125+
file = new OnlineFile(mimeType, sourceFile, directory, componentId);
132126
}
133127
return file;
134128
}
@@ -151,13 +145,16 @@ protected OnlineFile getWantedAttachment(RestRequest restRequest) {
151145
String attachmentId = restRequest.getElementValue("attachmentId");
152146
String language = restRequest.getElementValue("lang");
153147
if (StringUtil.isDefined(attachmentId)) {
154-
SimpleDocument attachment =
155-
AttachmentServiceFactory.getAttachmentService().searchDocumentById(new SimpleDocumentPK(
156-
attachmentId), language);
148+
SimpleDocumentPK pk;
149+
if (StringUtil.isDefined(componentId)) {
150+
pk = new SimpleDocumentPK(attachmentId, componentId);
151+
} else {
152+
pk = new SimpleDocumentPK(attachmentId);
153+
}
154+
SimpleDocument attachment = AttachmentServiceFactory.getAttachmentService().
155+
searchDocumentById(pk, language);
157156
if (attachment != null) {
158-
file = new OnlineFile(attachment.getContentType(), attachment.getFilename(), attachment.
159-
getAttachmentPath());
160-
file.setComponentId(componentId);
157+
file = new OnlineAttachment(attachment);
161158
}
162159
}
163160
return file;
@@ -171,27 +168,22 @@ protected OnlineFile getWantedAttachment(RestRequest restRequest) {
171168
* this String is null that an exception had been catched the html document generated is empty !!
172169
* also, we display a warning html page
173170
*/
174-
private void display(HttpServletResponse res, OnlineFile file) throws IOException {
175-
String filePath = FileRepositoryManager.getAbsolutePath(file.getComponentId())
176-
+ file.getDirectory() + File.separator + file.getSourceFile();
177-
178-
File realFile = new File(filePath);
171+
private void display(HttpServletResponse response, OnlineFile file) throws IOException {
172+
File realFile = file.getContentFile();
179173
if (!realFile.exists() && !realFile.isFile()) {
180-
realFile = new File(file.getDirectory());
181-
}
182-
if (!realFile.exists() && !realFile.isFile()) {
183-
displayWarningHtmlCode(res);
174+
displayWarningHtmlCode(response);
184175
return;
185176
}
186177
SilverTrace.info("peasUtil", "OnlineFileServer.display()", "root.MSG_GEN_ENTER_METHOD",
187-
" htmlFilePath " + filePath);
178+
" htmlFilePath " + realFile.getPath());
188179
try {
189-
res.setContentType(file.getMimeType());
190-
FileUtils.copyFile(realFile, res.getOutputStream());
180+
response.setContentType(FileUtil.getMimeType(realFile.getName()));
181+
response.setHeader("Content-Length", String.valueOf(realFile.length()));
182+
FileUtils.copyFile(realFile, response.getOutputStream());
191183
} catch (Exception e) {
192184
SilverTrace.warn("peasUtil", "OnlineFileServer.doPost", "root.EX_CANT_READ_FILE", "file name="
193-
+ filePath);
194-
displayWarningHtmlCode(res);
185+
+ realFile.getPath());
186+
displayWarningHtmlCode(response);
195187
}
196188
}
197189

0 commit comments

Comments
 (0)