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.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.jetspeed.JetspeedActions;
26 import org.apache.jetspeed.ajax.AjaxAction;
27 import org.apache.jetspeed.ajax.AjaxBuilder;
28 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
29 import org.apache.jetspeed.page.PageManager;
30 import org.apache.jetspeed.request.RequestContext;
31 import org.apache.jetspeed.serializer.JetspeedSerializer;
32 import org.apache.jetspeed.serializer.JetspeedSerializerFactory;
33
34 /***
35 * Exporting the object using Ajax command
36 *
37 * @author <a href="mailto:firevelocity@gmail.com">Vivek Kumar</a>
38 * @version $Id$
39 */
40 public class ExportJetspeedSchema extends BaseGetResourceAction implements
41 AjaxAction, AjaxBuilder, Constants
42 {
43
44 protected Log log = LogFactory.getLog(GetFolderAction.class);
45
46 protected PageManager castorPageManager;
47
48 protected JetspeedSerializerFactory serializerFactory;
49
50 protected String pageRoot;
51
52
53 private static final String USERS = "users";
54 private static final String GROUPS = "groups";
55 private static final String ROLES = "roles";
56 private static final String PERMISSIONS = "permissions";
57 private static final String PROFILES = "profiles";
58 private static final String CAPABILITIES = "capabilities";
59 private static final String PREFS = "prefs";
60
61 String pathSeprator = System.getProperty("file.separator");
62
63 public ExportJetspeedSchema(String template, String errorTemplate,
64 PageManager pageManager,
65 PortletActionSecurityBehavior securityBehavior,
66 JetspeedSerializerFactory serializerFactory,
67 String dir)
68 {
69 super(template, errorTemplate, pageManager, securityBehavior);
70 this.serializerFactory = serializerFactory;
71 this.pageRoot = dir;
72 }
73
74 public boolean run(RequestContext requestContext, Map resultMap)
75 {
76 boolean success = true;
77 String status = "success";
78 String userName = requestContext.getUserPrincipal().toString();
79 Map settings = new HashMap();
80 String exportFileName = getUserFolder(userName, false) + pathSeprator
81 + "ldapExport.xml";
82 try
83 {
84 resultMap.put(ACTION, "export");
85 if (false == checkAccess(requestContext, JetspeedActions.VIEW))
86 {
87 success = false;
88 resultMap.put(REASON, "Insufficient access to get portlets");
89 return success;
90 }
91 boolean processPrefs = getNonNullActionParameter(requestContext, PREFS).equalsIgnoreCase("y") ? true : false;
92 if (!processPrefs)
93 {
94 settings.put(JetspeedSerializer.KEY_PROCESS_USERS,
95 getNonNullActionParameter(requestContext, USERS).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
96 settings.put(JetspeedSerializer.KEY_PROCESS_PERMISSIONS,
97 getNonNullActionParameter(requestContext, PERMISSIONS).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
98 settings.put(JetspeedSerializer.KEY_PROCESS_PROFILER,
99 getNonNullActionParameter(requestContext, PROFILES).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
100 settings.put(JetspeedSerializer.KEY_PROCESS_CAPABILITIES,
101 getNonNullActionParameter(requestContext, CAPABILITIES).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
102 }
103 else
104 {
105 settings.put(JetspeedSerializer.KEY_PROCESS_PREFERENCES, Boolean.TRUE);
106 }
107 if (!cleanUserFolder(userName))
108 {
109 resultMap.put(STATUS, "failure");
110 resultMap.put(REASON, "Could not create temp files on disk.");
111 success = false;
112 return success;
113 }
114 settings.put(JetspeedSerializer.KEY_OVERWRITE_EXISTING,
115 Boolean.TRUE);
116 settings.put(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS,
117 Boolean.FALSE);
118 JetspeedSerializer serializer = null;
119 if (processPrefs)
120 serializer = serializerFactory.create(JetspeedSerializerFactory.SECONDARY);
121 else
122 serializer = serializerFactory.create(JetspeedSerializerFactory.PRIMARY);
123 serializer.setDefaultIndent("\t");
124 serializer.exportData("jetspeedadmin_export_process", exportFileName, settings);
125 requestContext.getRequest().getSession().setAttribute("file", userName + "_ldapExport.xml");
126 resultMap.put("link", getDownloadLink(requestContext, "tmpExport.xml", userName));
127
128 resultMap.put(STATUS, status);
129 } catch (Exception e)
130 {
131
132 e.printStackTrace();
133 log.error("exception while getting folder info", e);
134 resultMap.put(STATUS, "failure");
135 resultMap.put(REASON, e.getMessage());
136
137 success = false;
138 }
139 return success;
140 }
141
142 private String getDownloadLink(RequestContext requestContext,
143 String ObjectName, String userName) throws Exception
144 {
145 String link = "";
146 String basePath = requestContext.getRequest().getContextPath()
147 + "/fileserver/_content/";
148 link = basePath + userName + "/" + ObjectName;
149 return link;
150 }
151
152 private boolean cleanUserFolder(String userName)
153 {
154 boolean success = false;
155 synchronized (this)
156 {
157 String folder = getUserFolder(userName, false);
158 File dir = new File(pageRoot + pathSeprator + userName + ".zip");
159 if (dir.exists()) dir.delete();
160
161 dir = new File(folder);
162 if (dir.exists())
163 {
164 success = deleteDir(dir);
165 }
166 success = dir.mkdir();
167 }
168 return success;
169 }
170
171 private boolean deleteDir(File dir)
172 {
173 if (dir.exists())
174 {
175 File[] files = dir.listFiles();
176 for (int i = 0; i < files.length; i++)
177 {
178 if (files[i].isDirectory())
179 {
180 deleteDir(files[i]);
181 } else
182 {
183 files[i].delete();
184 }
185 }
186 }
187 return (dir.delete());
188 }
189
190 private String getUserFolder(String userName, boolean fullPath)
191 {
192 if (pathSeprator == null || pathSeprator.equals(""))
193 pathSeprator = "/";
194 if (fullPath)
195 {
196 return userName + pathSeprator;
197 } else
198 {
199 return pageRoot + pathSeprator + userName;
200 }
201 }
202 }