1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.layout.impl; |
18 |
|
|
19 |
|
import java.io.File; |
20 |
|
import java.io.FileInputStream; |
21 |
|
import java.io.FileOutputStream; |
22 |
|
import java.util.Iterator; |
23 |
|
import java.util.Map; |
24 |
|
import java.util.zip.ZipEntry; |
25 |
|
import java.util.zip.ZipOutputStream; |
26 |
|
|
27 |
|
import org.apache.commons.logging.Log; |
28 |
|
import org.apache.commons.logging.LogFactory; |
29 |
|
import org.apache.jetspeed.JetspeedActions; |
30 |
|
import org.apache.jetspeed.ajax.AjaxAction; |
31 |
|
import org.apache.jetspeed.ajax.AjaxBuilder; |
32 |
|
import org.apache.jetspeed.exception.JetspeedException; |
33 |
|
import org.apache.jetspeed.layout.PortletActionSecurityBehavior; |
34 |
|
import org.apache.jetspeed.om.folder.Folder; |
35 |
|
import org.apache.jetspeed.om.page.Link; |
36 |
|
import org.apache.jetspeed.om.page.Page; |
37 |
|
import org.apache.jetspeed.page.PageManager; |
38 |
|
import org.apache.jetspeed.request.RequestContext; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
public class ExportObject extends BaseGetResourceAction implements AjaxAction, AjaxBuilder, Constants { |
46 |
0 |
protected Log log = LogFactory.getLog(GetFolderAction.class); |
47 |
|
|
48 |
|
protected PageManager castorPageManager; |
49 |
|
|
50 |
|
protected String pageRoot; |
51 |
|
|
52 |
|
private static final String OBJECT_NAME = "objName"; |
53 |
|
|
54 |
|
private static final String OBJECT_TYPE = "objType"; |
55 |
|
|
56 |
|
private static final String OBJECT_PATH = "objPath"; |
57 |
|
|
58 |
|
private static final String RECURSIVE = "exptRecusive"; |
59 |
0 |
String pathSeprator = System.getProperty("file.separator"); |
60 |
|
public ExportObject(String template, String errorTemplate, PageManager pageManager, |
61 |
|
PortletActionSecurityBehavior securityBehavior, PageManager castorpagemanager, String dir) { |
62 |
0 |
super(template, errorTemplate, pageManager, securityBehavior); |
63 |
0 |
this.castorPageManager = castorpagemanager; |
64 |
0 |
this.pageRoot = dir; |
65 |
0 |
} |
66 |
|
|
67 |
|
public boolean run(RequestContext requestContext, Map resultMap) { |
68 |
0 |
boolean success = true; |
69 |
0 |
String status = "success"; |
70 |
0 |
String userName = requestContext.getUserPrincipal().toString(); |
71 |
|
try { |
72 |
0 |
resultMap.put(ACTION, "export"); |
73 |
0 |
if (false == checkAccess(requestContext, JetspeedActions.VIEW)) { |
74 |
0 |
success = false; |
75 |
0 |
resultMap.put(REASON, "Insufficient access to get portlets"); |
76 |
0 |
return success; |
77 |
|
} |
78 |
0 |
String objectName = getActionParameter(requestContext, OBJECT_NAME); |
79 |
0 |
String objectType = getActionParameter(requestContext, OBJECT_TYPE); |
80 |
0 |
String objectPath = getActionParameter(requestContext, OBJECT_PATH); |
81 |
0 |
String recursive = getActionParameter(requestContext, RECURSIVE); |
82 |
0 |
boolean isRecursive = recursive != null && recursive.equals("1") ? true : false; |
83 |
|
|
84 |
0 |
if (!cleanUserFolder(userName)) |
85 |
0 |
success = false; |
86 |
0 |
if (success) { |
87 |
0 |
if (objectType.equalsIgnoreCase("folder")) { |
88 |
0 |
Folder folder = pageManager.getFolder(objectPath); |
89 |
0 |
if (isRecursive) { |
90 |
0 |
importFolder(folder,userName,getRealPath(folder.getPath())); |
91 |
|
} else { |
92 |
0 |
Folder destFolder = castorPageManager.copyFolder(folder, getUserFolder(userName, true) |
93 |
|
+ objectName); |
94 |
0 |
castorPageManager.updateFolder(destFolder); |
95 |
|
} |
96 |
0 |
} else if (objectType.equalsIgnoreCase("page")) { |
97 |
0 |
objectPath = getParentPath(objectPath); |
98 |
0 |
Folder folder = pageManager.getFolder(objectPath); |
99 |
0 |
Page page = folder.getPage(objectName); |
100 |
0 |
Page destPage = castorPageManager.copyPage(page, getUserFolder(userName, true) + objectName); |
101 |
0 |
castorPageManager.updatePage(destPage); |
102 |
0 |
} else if (objectType.equalsIgnoreCase("link")) { |
103 |
0 |
objectPath = getParentPath(objectPath); |
104 |
0 |
Folder folder = pageManager.getFolder(objectPath); |
105 |
0 |
Link link = folder.getLink(objectName); |
106 |
0 |
Link destLink = castorPageManager.copyLink(link, getUserFolder(userName, true) + objectName); |
107 |
0 |
castorPageManager.updateLink(destLink); |
108 |
|
} |
109 |
0 |
String link = userName + "_" + objectName; |
110 |
0 |
if (objectType.equalsIgnoreCase("folder")) link = userName + ".zip"; |
111 |
0 |
requestContext.getRequest().getSession().setAttribute("file", link); |
112 |
0 |
resultMap.put("link", getDownloadLink(requestContext, objectName, userName, objectType)); |
113 |
|
} |
114 |
0 |
if (!success) |
115 |
0 |
status = "failure"; |
116 |
|
|
117 |
0 |
resultMap.put(STATUS, status); |
118 |
0 |
} catch (Exception e) { |
119 |
|
|
120 |
0 |
e.printStackTrace(); |
121 |
0 |
log.error("exception while getting folder info", e); |
122 |
|
|
123 |
|
|
124 |
0 |
success = false; |
125 |
0 |
} |
126 |
|
|
127 |
0 |
return success; |
128 |
|
} |
129 |
|
|
130 |
|
private String getDownloadLink(RequestContext requestContext, String ObjectName, String userName, String objectType) |
131 |
|
throws Exception { |
132 |
0 |
String link = ""; |
133 |
0 |
String basePath = requestContext.getRequest().getContextPath() + "/fileserver/_content/"; |
134 |
0 |
if (objectType.equalsIgnoreCase("folder")) { |
135 |
0 |
String sourcePath = getUserFolder(userName, false); |
136 |
0 |
String target = sourcePath + ".zip"; |
137 |
0 |
boolean success = zipObject(sourcePath, target); |
138 |
0 |
if (!success) |
139 |
0 |
throw new Exception("Error Occurered in zipping the file"); |
140 |
|
|
141 |
0 |
link = basePath + ObjectName+".zip"; |
142 |
0 |
} else { |
143 |
0 |
link = basePath + userName + "/" + ObjectName; |
144 |
|
} |
145 |
0 |
return link; |
146 |
|
} |
147 |
|
|
148 |
|
private boolean cleanUserFolder(String userName) { |
149 |
0 |
boolean success = false; |
150 |
0 |
synchronized (this) { |
151 |
0 |
String folder = getUserFolder(userName, false); |
152 |
0 |
File dir = new File(pageRoot+pathSeprator+userName+".zip"); |
153 |
0 |
if(dir.exists()) dir.delete(); |
154 |
|
|
155 |
0 |
dir = new File(folder); |
156 |
0 |
if (dir.exists()) { |
157 |
0 |
success = deleteDir(dir); |
158 |
|
} |
159 |
0 |
success = dir.mkdir(); |
160 |
0 |
} |
161 |
0 |
return success; |
162 |
|
} |
163 |
|
|
164 |
|
private boolean deleteDir(File dir) { |
165 |
0 |
if( dir.exists() ) { |
166 |
0 |
File[] files = dir.listFiles(); |
167 |
0 |
for(int i=0; i<files.length; i++) { |
168 |
0 |
if(files[i].isDirectory()) { |
169 |
0 |
deleteDir(files[i]); |
170 |
|
} |
171 |
|
else { |
172 |
0 |
files[i].delete(); |
173 |
|
} |
174 |
|
} |
175 |
|
} |
176 |
0 |
return( dir.delete() ); |
177 |
|
} |
178 |
|
|
179 |
|
private String getUserFolder(String userName, boolean fullPath) { |
180 |
0 |
if (pathSeprator == null || pathSeprator.equals("")) |
181 |
0 |
pathSeprator = "/"; |
182 |
0 |
if (fullPath) { |
183 |
0 |
return userName + pathSeprator; |
184 |
|
} else { |
185 |
0 |
return pageRoot + pathSeprator + userName; |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
|
private String getParentPath(String path) { |
190 |
0 |
int index = path.lastIndexOf("/"); |
191 |
|
|
192 |
0 |
if (index == 0) { |
193 |
0 |
return "/"; |
194 |
|
} else { |
195 |
0 |
return path.substring(0, index); |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
private boolean zipObject(String sourcePath, String target) { |
200 |
0 |
ZipOutputStream cpZipOutputStream = null; |
201 |
|
try { |
202 |
0 |
File cpFile = new File(sourcePath); |
203 |
0 |
if (!cpFile.isDirectory()) { |
204 |
0 |
return false; |
205 |
|
} |
206 |
0 |
cpZipOutputStream = new ZipOutputStream(class="keyword">new FileOutputStream(target)); |
207 |
0 |
cpZipOutputStream.setLevel(9); |
208 |
0 |
zipFiles(cpFile, sourcePath, cpZipOutputStream); |
209 |
0 |
cpZipOutputStream.finish(); |
210 |
0 |
cpZipOutputStream.close(); |
211 |
0 |
} catch (Exception e) { |
212 |
0 |
e.printStackTrace(); |
213 |
0 |
return false; |
214 |
|
} |
215 |
0 |
finally{ |
216 |
0 |
} |
217 |
0 |
return true; |
218 |
|
} |
219 |
|
|
220 |
|
private void zipFiles(File cpFile, String sourcePath, ZipOutputStream cpZipOutputStream) { |
221 |
|
|
222 |
0 |
if (cpFile.isDirectory()) { |
223 |
0 |
File[] fList = cpFile.listFiles(); |
224 |
0 |
for (int i = 0; i < fList.length; i++) { |
225 |
0 |
zipFiles(fList[i], sourcePath, cpZipOutputStream); |
226 |
|
} |
227 |
0 |
} else { |
228 |
|
try { |
229 |
0 |
String strAbsPath = cpFile.getAbsolutePath(); |
230 |
0 |
String strZipEntryName = strAbsPath.substring(sourcePath.length() + 1, strAbsPath.length()); |
231 |
0 |
byte[] b = new byte[(int) (cpFile.length())]; |
232 |
0 |
FileInputStream cpFileInputStream = new FileInputStream(cpFile); |
233 |
0 |
int i = cpFileInputStream.read(b, 0, (class="keyword">int) cpFile.length()); |
234 |
0 |
ZipEntry cpZipEntry = new ZipEntry(strZipEntryName); |
235 |
0 |
cpZipOutputStream.putNextEntry(cpZipEntry); |
236 |
0 |
cpZipOutputStream.write(b, 0, (int) cpFile.length()); |
237 |
0 |
cpZipOutputStream.closeEntry(); |
238 |
0 |
cpFileInputStream.close(); |
239 |
0 |
} catch (Exception e) { |
240 |
0 |
e.printStackTrace(); |
241 |
0 |
} |
242 |
|
} |
243 |
0 |
} |
244 |
|
|
245 |
|
private Folder importFolder(Folder srcFolder,String userName,String destination) throws JetspeedException { |
246 |
0 |
String newPath=""; |
247 |
0 |
Folder dstFolder = lookupFolder(srcFolder.getPath()); |
248 |
0 |
dstFolder = castorPageManager.copyFolder(srcFolder, getUserFolder(userName, true) + destination); |
249 |
0 |
castorPageManager.updateFolder(dstFolder); |
250 |
|
|
251 |
0 |
Iterator pages = srcFolder.getPages().iterator(); |
252 |
0 |
while (pages.hasNext()) { |
253 |
0 |
Page srcPage = (Page) pages.next(); |
254 |
0 |
Page dstPage = lookupPage(srcPage.getPath()); |
255 |
0 |
newPath = getUserFolder(userName, true) +destination+ getRealPath(srcPage.getPath()); |
256 |
0 |
dstPage = castorPageManager.copyPage(srcPage, newPath); |
257 |
0 |
castorPageManager.updatePage(dstPage); |
258 |
0 |
} |
259 |
|
|
260 |
0 |
Iterator links = srcFolder.getLinks().iterator(); |
261 |
0 |
while (links.hasNext()) { |
262 |
0 |
Link srcLink = (Link) links.next(); |
263 |
0 |
Link dstLink = lookupLink(srcLink.getPath()); |
264 |
0 |
newPath = getUserFolder(userName, true) +destination+ getRealPath(srcLink.getPath()); |
265 |
0 |
dstLink = castorPageManager.copyLink(srcLink,newPath); |
266 |
0 |
castorPageManager.updateLink(dstLink); |
267 |
0 |
} |
268 |
0 |
Iterator folders = srcFolder.getFolders().iterator(); |
269 |
0 |
while (folders.hasNext()) { |
270 |
0 |
Folder folder = (Folder) folders.next(); |
271 |
0 |
newPath = destination+getRealPath(folder.getPath()); |
272 |
0 |
importFolder(folder,userName,newPath); |
273 |
0 |
} |
274 |
|
|
275 |
0 |
return dstFolder; |
276 |
|
} |
277 |
|
|
278 |
|
private Page lookupPage(String path) { |
279 |
|
try { |
280 |
0 |
return castorPageManager.getPage(path); |
281 |
0 |
} catch (Exception e) { |
282 |
0 |
return null; |
283 |
|
} |
284 |
|
} |
285 |
|
|
286 |
|
private Link lookupLink(String path) { |
287 |
|
try { |
288 |
0 |
return castorPageManager.getLink(path); |
289 |
0 |
} catch (Exception e) { |
290 |
0 |
return null; |
291 |
|
} |
292 |
|
} |
293 |
|
|
294 |
|
private Folder lookupFolder(String path) { |
295 |
|
try { |
296 |
0 |
return castorPageManager.getFolder(path); |
297 |
0 |
} catch (Exception e) { |
298 |
0 |
return null; |
299 |
|
} |
300 |
|
} |
301 |
|
private String getRealPath(String path){ |
302 |
0 |
int index = path.lastIndexOf("/"); |
303 |
0 |
if (index>0) |
304 |
|
{ |
305 |
0 |
return path.substring(index); |
306 |
|
} |
307 |
0 |
return path; |
308 |
|
|
309 |
|
} |
310 |
|
} |