001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.layout.impl;
018:
019: import java.io.File;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.jetspeed.JetspeedActions;
026: import org.apache.jetspeed.ajax.AjaxAction;
027: import org.apache.jetspeed.ajax.AjaxBuilder;
028: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
029: import org.apache.jetspeed.page.PageManager;
030: import org.apache.jetspeed.request.RequestContext;
031: import org.apache.jetspeed.serializer.JetspeedSerializer;
032: import org.apache.jetspeed.serializer.JetspeedSerializerFactory;
033:
034: /**
035: * Exporting the object using Ajax command
036: *
037: * @author <a href="mailto:firevelocity@gmail.com">Vivek Kumar</a>
038: * @version $Id$
039: */
040: public class ExportJetspeedSchema extends BaseGetResourceAction
041: implements AjaxAction, AjaxBuilder, Constants {
042:
043: protected Log log = LogFactory.getLog(GetFolderAction.class);
044:
045: protected PageManager castorPageManager;
046:
047: protected JetspeedSerializerFactory serializerFactory;
048:
049: protected String pageRoot;
050:
051: // categories of export
052: private static final String USERS = "users";
053: private static final String GROUPS = "groups";
054: private static final String ROLES = "roles";
055: private static final String PERMISSIONS = "permissions";
056: private static final String PROFILES = "profiles";
057: private static final String CAPABILITIES = "capabilities";
058: private static final String PREFS = "prefs";
059:
060: String pathSeprator = System.getProperty("file.separator");
061:
062: public ExportJetspeedSchema(String template, String errorTemplate,
063: PageManager pageManager,
064: PortletActionSecurityBehavior securityBehavior,
065: JetspeedSerializerFactory serializerFactory, String dir) {
066: super (template, errorTemplate, pageManager, securityBehavior);
067: this .serializerFactory = serializerFactory;
068: this .pageRoot = dir;
069: }
070:
071: public boolean run(RequestContext requestContext, Map resultMap) {
072: boolean success = true;
073: String status = "success";
074: String userName = requestContext.getUserPrincipal().toString();
075: Map settings = new HashMap();
076: String exportFileName = getUserFolder(userName, false)
077: + pathSeprator + "ldapExport.xml";
078: try {
079: resultMap.put(ACTION, "export");
080: if (false == checkAccess(requestContext,
081: JetspeedActions.VIEW)) {
082: success = false;
083: resultMap.put(REASON,
084: "Insufficient access to get portlets");
085: return success;
086: }
087: boolean processPrefs = getNonNullActionParameter(
088: requestContext, PREFS).equalsIgnoreCase("y") ? true
089: : false;
090: if (!processPrefs) {
091: settings
092: .put(
093: JetspeedSerializer.KEY_PROCESS_USERS,
094: getNonNullActionParameter(
095: requestContext, USERS)
096: .equalsIgnoreCase("y") ? Boolean.TRUE
097: : Boolean.FALSE);
098: settings
099: .put(
100: JetspeedSerializer.KEY_PROCESS_PERMISSIONS,
101: getNonNullActionParameter(
102: requestContext, PERMISSIONS)
103: .equalsIgnoreCase("y") ? Boolean.TRUE
104: : Boolean.FALSE);
105: settings
106: .put(
107: JetspeedSerializer.KEY_PROCESS_PROFILER,
108: getNonNullActionParameter(
109: requestContext, PROFILES)
110: .equalsIgnoreCase("y") ? Boolean.TRUE
111: : Boolean.FALSE);
112: settings
113: .put(
114: JetspeedSerializer.KEY_PROCESS_CAPABILITIES,
115: getNonNullActionParameter(
116: requestContext, CAPABILITIES)
117: .equalsIgnoreCase("y") ? Boolean.TRUE
118: : Boolean.FALSE);
119: } else {
120: settings.put(
121: JetspeedSerializer.KEY_PROCESS_PREFERENCES,
122: Boolean.TRUE);
123: }
124: if (!cleanUserFolder(userName)) {
125: resultMap.put(STATUS, "failure");
126: resultMap.put(REASON,
127: "Could not create temp files on disk.");
128: success = false;
129: return success;
130: }
131: settings.put(JetspeedSerializer.KEY_OVERWRITE_EXISTING,
132: Boolean.TRUE);
133: settings.put(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS,
134: Boolean.FALSE);
135: JetspeedSerializer serializer = null;
136: if (processPrefs)
137: serializer = serializerFactory
138: .create(JetspeedSerializerFactory.SECONDARY);
139: else
140: serializer = serializerFactory
141: .create(JetspeedSerializerFactory.PRIMARY);
142: serializer.setDefaultIndent("\t");
143: serializer.exportData("jetspeedadmin_export_process",
144: exportFileName, settings);
145: requestContext.getRequest().getSession().setAttribute(
146: "file", userName + "_ldapExport.xml");
147: resultMap.put("link", getDownloadLink(requestContext,
148: "tmpExport.xml", userName));
149:
150: resultMap.put(STATUS, status);
151: } catch (Exception e) {
152: // Log the exception
153: e.printStackTrace();
154: log.error("exception while getting folder info", e);
155: resultMap.put(STATUS, "failure");
156: resultMap.put(REASON, e.getMessage());
157: // Return a failure indicator
158: success = false;
159: }
160: return success;
161: }
162:
163: private String getDownloadLink(RequestContext requestContext,
164: String ObjectName, String userName) throws Exception {
165: String link = "";
166: String basePath = requestContext.getRequest().getContextPath()
167: + "/fileserver/_content/";
168: link = basePath + userName + "/" + ObjectName;
169: return link;
170: }
171:
172: private boolean cleanUserFolder(String userName) {
173: boolean success = false;
174: synchronized (this ) {
175: String folder = getUserFolder(userName, false);
176: File dir = new File(pageRoot + pathSeprator + userName
177: + ".zip");
178: if (dir.exists())
179: dir.delete();
180:
181: dir = new File(folder);
182: if (dir.exists()) {
183: success = deleteDir(dir);
184: }
185: success = dir.mkdir();
186: }
187: return success;
188: }
189:
190: private boolean deleteDir(File dir) {
191: if (dir.exists()) {
192: File[] files = dir.listFiles();
193: for (int i = 0; i < files.length; i++) {
194: if (files[i].isDirectory()) {
195: deleteDir(files[i]);
196: } else {
197: files[i].delete();
198: }
199: }
200: }
201: return (dir.delete());
202: }
203:
204: private String getUserFolder(String userName, boolean fullPath) {
205: if (pathSeprator == null || pathSeprator.equals(""))
206: pathSeprator = "/";
207: if (fullPath) {
208: return userName + pathSeprator;
209: } else {
210: return pageRoot + pathSeprator + userName;
211: }
212: }
213: }
|