Coverage report

  %line %branch
org.apache.jetspeed.layout.impl.ExportJetspeedSchema
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  0
     protected Log log = LogFactory.getLog(GetFolderAction.class);
 45  
 
 46  
     protected PageManager castorPageManager;
 47  
 
 48  
     protected JetspeedSerializerFactory serializerFactory;
 49  
 
 50  
     protected String pageRoot;
 51  
 
 52  
     // categories of export
 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  0
     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  0
         super(template, errorTemplate, pageManager, securityBehavior);
 70  0
         this.serializerFactory = serializerFactory;
 71  0
         this.pageRoot = dir;
 72  0
     }
 73  
 
 74  
     public boolean run(RequestContext requestContext, Map resultMap)
 75  
     {
 76  0
         boolean success = true;
 77  0
         String status = "success";
 78  0
         String userName = requestContext.getUserPrincipal().toString();
 79  0
         Map settings = new HashMap();
 80  0
         String exportFileName = getUserFolder(userName, false) + pathSeprator
 81  
                 + "ldapExport.xml";
 82  
         try
 83  
         {
 84  0
             resultMap.put(ACTION, "export");
 85  0
             if (false == checkAccess(requestContext, JetspeedActions.VIEW))
 86  
             {
 87  0
                 success = false;
 88  0
                 resultMap.put(REASON, "Insufficient access to get portlets");
 89  0
                 return success;
 90  
             }
 91  0
             boolean processPrefs = getNonNullActionParameter(requestContext, PREFS).equalsIgnoreCase("y") ? true : false;
 92  0
             if (!processPrefs)
 93  
             {
 94  0
                 settings.put(JetspeedSerializer.KEY_PROCESS_USERS, 
 95  
                         getNonNullActionParameter(requestContext, USERS).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
 96  0
                 settings.put(JetspeedSerializer.KEY_PROCESS_PERMISSIONS, 
 97  
                         getNonNullActionParameter(requestContext, PERMISSIONS).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
 98  0
                 settings.put(JetspeedSerializer.KEY_PROCESS_PROFILER, 
 99  
                         getNonNullActionParameter(requestContext, PROFILES).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
 100  0
                 settings.put(JetspeedSerializer.KEY_PROCESS_CAPABILITIES, 
 101  
                         getNonNullActionParameter(requestContext, CAPABILITIES).equalsIgnoreCase("y") ? Boolean.TRUE : Boolean.FALSE);
 102  
             }
 103  
             else
 104  
             {
 105  0
                 settings.put(JetspeedSerializer.KEY_PROCESS_PREFERENCES, Boolean.TRUE);
 106  
             }
 107  0
             if (!cleanUserFolder(userName)) 
 108  
             {
 109  0
                 resultMap.put(STATUS, "failure");
 110  0
                 resultMap.put(REASON, "Could not create temp files on disk.");
 111  0
                 success = false;
 112  0
                 return success;
 113  
             }
 114  0
             settings.put(JetspeedSerializer.KEY_OVERWRITE_EXISTING,
 115  
                     Boolean.TRUE);
 116  0
             settings.put(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS,
 117  
                     Boolean.FALSE);
 118  0
             JetspeedSerializer serializer = null;
 119  0
             if (processPrefs)
 120  0
                 serializer = serializerFactory.create(JetspeedSerializerFactory.SECONDARY);
 121  
             else
 122  0
                 serializer = serializerFactory.create(JetspeedSerializerFactory.PRIMARY);
 123  0
             serializer.setDefaultIndent("\t");
 124  0
             serializer.exportData("jetspeedadmin_export_process", exportFileName, settings);
 125  0
             requestContext.getRequest().getSession().setAttribute("file", userName + "_ldapExport.xml");
 126  0
             resultMap.put("link", getDownloadLink(requestContext, "tmpExport.xml", userName));
 127  
 
 128  0
             resultMap.put(STATUS, status);
 129  0
         } catch (Exception e)
 130  
         {
 131  
             // Log the exception
 132  0
             e.printStackTrace();
 133  0
             log.error("exception while getting folder info", e);
 134  0
             resultMap.put(STATUS, "failure");
 135  0
             resultMap.put(REASON, e.getMessage());
 136  
             // Return a failure indicator
 137  0
             success = false;
 138  0
         }
 139  0
         return success;
 140  
     }
 141  
 
 142  
     private String getDownloadLink(RequestContext requestContext,
 143  
             String ObjectName, String userName) throws Exception
 144  
     {
 145  0
         String link = "";
 146  0
         String basePath = requestContext.getRequest().getContextPath()
 147  
                 + "/fileserver/_content/";
 148  0
         link = basePath + userName + "/" + ObjectName;
 149  0
         return link;
 150  
     }
 151  
 
 152  
     private boolean cleanUserFolder(String userName)
 153  
     {
 154  0
         boolean success = false;
 155  0
         synchronized (this)
 156  
         {
 157  0
             String folder = getUserFolder(userName, false);
 158  0
             File dir = new File(pageRoot + pathSeprator + userName + ".zip");
 159  0
             if (dir.exists()) dir.delete();
 160  
 
 161  0
             dir = new File(folder);
 162  0
             if (dir.exists())
 163  
             {
 164  0
                 success = deleteDir(dir);
 165  
             }
 166  0
             success = dir.mkdir();
 167  0
         }
 168  0
         return success;
 169  
     }
 170  
 
 171  
     private boolean deleteDir(File dir)
 172  
     {
 173  0
         if (dir.exists())
 174  
         {
 175  0
             File[] files = dir.listFiles();
 176  0
             for (int i = 0; i < files.length; i++)
 177  
             {
 178  0
                 if (files[i].isDirectory())
 179  
                 {
 180  0
                     deleteDir(files[i]);
 181  
                 } else
 182  
                 {
 183  0
                     files[i].delete();
 184  
                 }
 185  
             }
 186  
         }
 187  0
         return (dir.delete());
 188  
     }
 189  
 
 190  
     private String getUserFolder(String userName, boolean fullPath)
 191  
     {
 192  0
         if (pathSeprator == null || pathSeprator.equals(""))
 193  0
             pathSeprator = "/";
 194  0
         if (fullPath)
 195  
         {
 196  0
             return userName + pathSeprator;
 197  
         } else
 198  
         {
 199  0
             return pageRoot + pathSeprator + userName;
 200  
         }
 201  
     }
 202  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.