KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > RepositoryUploadHandler


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.upload.FormFile;
31
32 import com.sslexplorer.boot.Repository;
33 import com.sslexplorer.boot.RepositoryFactory;
34 import com.sslexplorer.boot.RepositoryStore;
35 import com.sslexplorer.boot.Util;
36 import com.sslexplorer.vfs.UploadDetails;
37
38 public class RepositoryUploadHandler implements UploadHandler {
39     public static final String JavaDoc TYPE_REPOSITORY = "REPOSITORY";
40     
41     public RepositoryUploadHandler() {
42         super();
43     }
44
45     public ActionForward performUpload(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, UploadDetails upload, FormFile uploadFile)
46                     throws Exception JavaDoc {
47         try {
48
49             /* If an upload was attempted after session timeout, the login
50              * screen would have interrupted the upload. In this case just go
51              * straight back to the home page.
52              */

53             if(upload.getResourcePath() == null || upload.getResourcePath().length()==0) {
54                 return upload.getUploadedForward();
55             }
56             
57             if(uploadFile.getFileName() == null || uploadFile.getFileName().trim().equals("")) {
58                 return upload.getUploadedForward();
59             }
60
61             // Get the repository store
62
Repository repository = RepositoryFactory.getRepository();
63             if(upload.getExtraAttribute1() == null || upload.getExtraAttribute1().equals("")) {
64                 throw new Exception JavaDoc("No store name provided.");
65             }
66             RepositoryStore store = repository.getStore(upload.getExtraAttribute1());
67             
68             // Do the upload
69
InputStream JavaDoc in = uploadFile.getInputStream();
70             try {
71                 OutputStream JavaDoc out = store.getEntryOutputStream(upload.getResourcePath()) ;
72                 try {
73                     Util.copy(in, out);
74                 }
75                 finally {
76                     Util.closeStream(out);
77                 }
78             }
79             finally {
80                 Util.closeStream(in);
81             }
82             return upload.getUploadedForward();
83         }
84         catch(Exception JavaDoc e) {
85             /* Close the stream so the client gets an error straight away rather
86              * than having to wait for the file to upload
87              */

88             try {
89                 request.getInputStream().close();
90             }
91             catch(IOException JavaDoc ioe) {
92                 throw ioe;
93             }
94             throw e ;
95         }
96     }
97
98     public boolean checkFileToUpload(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, UploadDetails fileUpload, FormFile file) throws IOException JavaDoc, Exception JavaDoc {
99         // TODO Auto-generated method stub
100
return false;
101     }
102     
103 }
104
Popular Tags