KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > modules > filemgr > FileMgrViewAction


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 package com.raptus.owxv3.modules.filemgr;
7
8
9 import java.io.*;
10
11 import javax.servlet.ServletOutputStream JavaDoc;
12 import javax.servlet.http.*;
13
14 import org.apache.struts.action.*;
15
16 import com.raptus.owxv3.*;
17 import com.raptus.owxv3.api.*;
18
19
20 /**
21  *
22  * <hr>
23  * <table width="100%" border="0">
24  * <tr>
25  * <td width="24%"><b>Filename</b></td><td width="76%">EAFileMgrViewAction.java</td>
26  * </tr>
27  * <tr>
28  * <td width="24%"><b>Author</b></td><td width="76%">REEA</td>
29  * </tr>
30  * <tr>
31  * <td width="24%"><b>Date</b></td><td width="76%">25th of September 2001</td>
32  * </tr>
33  * </table>
34  * <hr>
35  * <table width="100%" border="0">
36  * <tr>
37  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
38  * </tr>
39  * </table>
40  * <hr>
41  * <table width="100%" border="0">
42  * <tr>
43  * <td>
44  * This class is used to view a picture or to open a file download box in the browser.<br>
45  * The class is extending the OmniaWebAction class because it is intended to be public<br>
46  * This action is called from the "All file list" and "Assigned file list" screen of the EADMIN part<br>
47  * of File Manager.<br>
48  * This class can be called also to display an image in any public page or to place a download link on a<br>
49  * page of a certain file.<br>
50  * Example of image display:<br>
51  * &lt;img SRC="../../../../../../owxv3/modules/filemgr/view.do?vmodule=filemgr&action=display&filename=logo.gif"&gt; <br>
52  ** Example of download link:<br>
53  * &lt;a HREF="../../../../../../owxv3/modules/filemgr/view.do?vmodule=filemgr&action=download&filename=pricelist.pdf"&gt;Dowload pricelist &lt;/a&gt;
54  *
55  * </td>
56  * </tr>
57  * </table>
58  * <hr>
59  */

60
61
62 //public class FileMgrViewAction extends OmniaWebAction
63
public class FileMgrViewAction extends VModuleRoleAction
64 {
65     /**
66      *The perform method.
67      *Two actions can be performed:
68      *display and download
69      */

70     public ActionForward perform(ActionMapping mapping,
71                              ActionForm form,
72                              javax.servlet.http.HttpServletRequest JavaDoc request,
73                              javax.servlet.http.HttpServletResponse JavaDoc response)
74                       throws java.io.IOException JavaDoc,
75                              javax.servlet.ServletException JavaDoc
76     {
77
78         try{
79
80             /**
81              *We need a VModule object to read the file repository
82              */

83             String JavaDoc vmid=getSelectedVModule(form,request);
84             VModuleManager vmmanager=VModuleManager.getInstance();
85             VModule vm=vmmanager.getVModule(vmid);
86             XMLConfigManager cm=XMLConfigManager.getInstance();
87
88             //String repository=vm.getStringProperty(FileMgrConstants.VMODULE_PROPERTY_REPOSITORY);
89
String JavaDoc repository=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=repository","value");
90             if( ! repository.endsWith(File.separator) ) repository=repository+File.separator;
91
92             /**
93              *Deciding what action we should perform
94              */

95             String JavaDoc action=request.getParameter(FileMgrConstants.HTTPGET_PARAM_ACTION);
96
97
98             if(action.equals("session")){
99                 HttpSession session=request.getSession();
100                 response.setContentType("text/plain");
101                 PrintWriter out=response.getWriter();
102                 if(session==null){
103                     out.println("No session");
104                 }
105                 else{
106                     //out.print("The session is valid for:");
107
long creationtime=session.getCreationTime();
108                     long curenttime= new java.util.Date JavaDoc().getTime();
109                     int diference=(int)((curenttime-creationtime)/1000);
110
111
112                     out.println(session.getMaxInactiveInterval()-diference);
113                 }
114                 out.close();
115                 return null;
116
117             }
118
119
120             GResFile gresfile=null;
121             GlobalResources gres=new GlobalResources();
122
123             if(request.getParameter(FileMgrConstants.HTTPGET_PARAM_FILEID)!=null)
124             {
125                 /**
126                  *we should return the file based on his id
127                  */

128
129                 String JavaDoc fileid=request.getParameter(FileMgrConstants.HTTPGET_PARAM_FILEID);
130                 gresfile=gres.loadFile( Integer.parseInt(fileid) );
131             }
132             else
133             if(request.getParameter(FileMgrConstants.HTTPGET_PARAM_FILENAME)!=null)
134             {
135                 String JavaDoc filename=request.getParameter(FileMgrConstants.HTTPGET_PARAM_FILENAME);
136                 gresfile=gres.loadFile( filename );
137             }
138
139             if(gresfile==null)
140             {
141                 /**
142                  *No good file specified in request parameters
143                  *returning empty response.
144                  *A forward to an unknownfile.jsp is not good solution in case of images or downloads.
145                  *
146                  **/

147                 ServletOutputStream JavaDoc out=response.getOutputStream();
148                 out.close();
149             }
150
151
152
153             if(action==null)
154             {
155                 /**
156                  *action not specified we decide the action based on filetype
157                  */

158                 if( gresfile.getType().compareToIgnoreCase(GlobalResources.FILETYPE_PICTURE)==0 )
159                 {
160                     action=FileMgrConstants.VMODULE_ACTION_DISPLAY;
161                 }
162                 else
163                 {
164                     action=FileMgrConstants.VMODULE_ACTION_DOWNLOAD;
165                 }
166             }
167
168
169
170             if( action.compareToIgnoreCase(FileMgrConstants.VMODULE_ACTION_DOWNLOAD)==0 ){
171                 /**
172                  *The action is download we must set the content-disposition header to force
173                  *the download file dialog box to appair
174                  */

175                 response.setContentType("application/unknown");
176                 response.setHeader("content-disposition"," attachment; filename="+gresfile.getFileName());
177             }
178             else
179             {
180                 /**
181                  *The action is display
182                  */

183                 setContentType(gresfile.getFileName(),response);
184             }
185
186             /**
187              *Reading the file from disk, returning as response
188              */

189             ServletOutputStream JavaDoc out=response.getOutputStream();
190             FileInputStream stream=new FileInputStream( repository+gresfile.getType()+File.separator+gresfile.getFileName() );
191
192             int bytesRead = 0;
193             byte[] buffer = new byte[8192];
194             while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
195             {
196                 out.write(buffer, 0, bytesRead);
197             }
198             out.close();
199             stream.close();
200
201
202         }
203         catch(Exception JavaDoc e){LoggingManager.log(e.getMessage(),this);}
204
205         /**
206          *we return null becuase we send the response so no forward should be used
207          */

208          return null;
209     }
210
211     /**
212      *Method for setting the content-type based on the file extension.
213      *It would be better to have the contenttype in the database ?
214      */

215     private void setContentType(String JavaDoc filename,HttpServletResponse response){
216         if(filename.endsWith(".gif") || filename.endsWith(".GIF") ) response.setContentType("image/gif");
217         else if(filename.endsWith(".jpg") || filename.endsWith(".JPG") ) response.setContentType("image/jpeg");
218         else if(filename.endsWith(".png") || filename.endsWith(".PNG") ) response.setContentType("image/png");
219         else if(filename.endsWith(".bmp") || filename.endsWith(".BMP") ) response.setContentType("image/bmp");
220
221
222     }
223
224
225
226
227 }
228
229 // eof
230
Popular Tags