23
23
import java .io .File ;
24
24
import java .io .IOException ;
25
25
import java .io .OutputStream ;
26
- import java .io .PrintWriter ;
27
26
import java .io .StringReader ;
28
27
import java .rmi .RemoteException ;
29
28
44
43
import com .silverpeas .gallery .model .PhotoPK ;
45
44
import com .silverpeas .tags .authentication .AuthenticationManager ;
46
45
import com .silverpeas .tags .util .Admin ;
46
+ import com .silverpeas .util .FileUtil ;
47
47
import com .silverpeas .util .StringUtil ;
48
48
import com .silverpeas .util .web .servlet .RestRequest ;
49
49
50
50
import com .stratelia .silverpeas .silvertrace .SilverTrace ;
51
51
import com .stratelia .webactiv .util .EJBUtilitaire ;
52
- import com .stratelia .webactiv .util .FileRepositoryManager ;
53
52
import com .stratelia .webactiv .util .JNDINames ;
54
53
import com .stratelia .webactiv .util .ResourceLocator ;
55
54
import com .stratelia .webactiv .util .exception .SilverpeasRuntimeException ;
59
58
60
59
public class WebFileServer extends HttpServlet {
61
60
62
- PrintWriter out ;
61
+ private static final long serialVersionUID = 1L ;
63
62
private Admin admin = null ;
64
63
65
64
@ Override
@@ -72,8 +71,8 @@ public void init(ServletConfig config) {
72
71
}
73
72
74
73
@ 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 {
77
76
SilverTrace .info ("peasUtil" , "OnlineFileServer.doPost" , "root.MSG_GEN_ENTER_METHOD" );
78
77
RestRequest restRequest = new RestRequest (req , "" );
79
78
String userId = AuthenticationManager .getUserId (req );
@@ -105,14 +104,11 @@ public OnlineFile getFileFromOldURL(RestRequest restRequest) throws RemoteExcept
105
104
106
105
String attachmentId = restRequest .getWebRequest ().getParameter ("attachmentId" );
107
106
if (StringUtil .isDefined (attachmentId )) {
108
- SimpleDocument attachment = AttachmentServiceFactory .getAttachmentService ().searchDocumentById (
107
+ SimpleDocument attachment = AttachmentServiceFactory .getAttachmentService ().
108
+ searchDocumentById (
109
109
new SimpleDocumentPK (attachmentId ), null );
110
110
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 );
116
112
}
117
113
} else if (StringUtil .isDefined (imageId )) {
118
114
PhotoDetail image = getGalleryBm ().getPhoto (new PhotoPK (imageId , componentId ));
@@ -124,11 +120,9 @@ public OnlineFile getFileFromOldURL(RestRequest restRequest) throws RemoteExcept
124
120
sourceFile = image .getId () + "_preview.jpg" ;
125
121
}
126
122
directory = "image" + image .getId ();
127
- file = new OnlineFile (mimeType , sourceFile , directory );
128
- file .setComponentId (componentId );
123
+ file = new OnlineFile (mimeType , sourceFile , directory , componentId );
129
124
} else {
130
- file = new OnlineFile (mimeType , sourceFile , directory );
131
- file .setComponentId (componentId );
125
+ file = new OnlineFile (mimeType , sourceFile , directory , componentId );
132
126
}
133
127
return file ;
134
128
}
@@ -151,13 +145,16 @@ protected OnlineFile getWantedAttachment(RestRequest restRequest) {
151
145
String attachmentId = restRequest .getElementValue ("attachmentId" );
152
146
String language = restRequest .getElementValue ("lang" );
153
147
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 );
157
156
if (attachment != null ) {
158
- file = new OnlineFile (attachment .getContentType (), attachment .getFilename (), attachment .
159
- getAttachmentPath ());
160
- file .setComponentId (componentId );
157
+ file = new OnlineAttachment (attachment );
161
158
}
162
159
}
163
160
return file ;
@@ -171,27 +168,22 @@ protected OnlineFile getWantedAttachment(RestRequest restRequest) {
171
168
* this String is null that an exception had been catched the html document generated is empty !!
172
169
* also, we display a warning html page
173
170
*/
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 ();
179
173
if (!realFile .exists () && !realFile .isFile ()) {
180
- realFile = new File (file .getDirectory ());
181
- }
182
- if (!realFile .exists () && !realFile .isFile ()) {
183
- displayWarningHtmlCode (res );
174
+ displayWarningHtmlCode (response );
184
175
return ;
185
176
}
186
177
SilverTrace .info ("peasUtil" , "OnlineFileServer.display()" , "root.MSG_GEN_ENTER_METHOD" ,
187
- " htmlFilePath " + filePath );
178
+ " htmlFilePath " + realFile . getPath () );
188
179
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 ());
191
183
} catch (Exception e ) {
192
184
SilverTrace .warn ("peasUtil" , "OnlineFileServer.doPost" , "root.EX_CANT_READ_FILE" , "file name="
193
- + filePath );
194
- displayWarningHtmlCode (res );
185
+ + realFile . getPath () );
186
+ displayWarningHtmlCode (response );
195
187
}
196
188
}
197
189
0 commit comments