Commit ebef8c42 authored by Ryan Graham's avatar Ryan Graham Committed by ryang

added support for sending user export directly to the screen / need to bulletproof and test


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1517 b35dd754-fafc-0310-a699-88a17e54d16e
parent 787f2628
......@@ -3,6 +3,7 @@ package org.jivesoftware.messenger.plugin;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
......@@ -47,13 +48,34 @@ public class ImportExportPlugin implements Plugin {
private PluginManager pluginManager;
private static UserProvider provider;
private static String serverName;
private static String serverName;
private static String exportDirectory;
public ImportExportPlugin() {
userManager = XMPPServer.getInstance().getUserManager();
provider = UserManager.getUserProvider();
serverName = XMPPServer.getInstance().getServerInfo().getName();
if (exportDirectory == null) {
if (JiveGlobals.getHomeDirectory() != null) {
File messengerHome = new File(JiveGlobals.getHomeDirectory());
if (messengerHome.exists() && messengerHome.canWrite()) {
exportDirectory = (new File(messengerHome, "export")).toString();
}
}
}
if (!exportDirectory.endsWith(File.separator)) {
exportDirectory = exportDirectory + File.separator;
}
// Make sure the export directory exists. If not, make it:
File exportDir = new File(exportDirectory);
if (!exportDir.exists()) {
exportDir.mkdir();
}
}
public void initializePlugin(PluginManager manager, File pluginDirectory) {
......@@ -64,6 +86,8 @@ public class ImportExportPlugin implements Plugin {
userManager = null;
pluginManager = null;
provider = null;
serverName = null;
exportDirectory = null;
}
public boolean isUserProviderReadOnly() {
......@@ -71,38 +95,51 @@ public class ImportExportPlugin implements Plugin {
}
public static String exportDirectory() {
return JiveGlobals.getHomeDirectory() + File.separator + "export";
return exportDirectory;
}
public boolean exportUserData(String file) throws IOException {
if (!createExportDirectory()) {
return false;
}
if (!file.endsWith(".xml")) {
public boolean exportUsersToFile(String file) throws IOException {
if (!file.endsWith(".xml")) {
file += ".xml";
}
String exportFilePath = exportDirectory() + File.separator + file;
String exportFilePath = exportDirectory + file;
XMLWriter writer = new XMLWriter(new FileWriter(exportFilePath), OutputFormat.createPrettyPrint());
writer.write(exportUsers());
writer.close();
XMLWriter writer = null;
try {
writer = new XMLWriter(new FileWriter(exportFilePath), OutputFormat.createPrettyPrint());
writer.write(exportUsers());
} catch (IOException ioe) {
Log.error(ioe);
throw ioe;
} finally {
if (writer != null) {
writer.close();
}
}
return true;
}
private boolean createExportDirectory() {
boolean isDirReady = true;
if (!(new File(exportDirectory())).exists()) {
isDirReady = (new File(exportDirectory())).mkdirs();
public String exportUsersToString() throws IOException {
StringWriter stringWriter = new StringWriter();
XMLWriter writer = null;
try {
writer = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
writer.write(exportUsers());
} catch (IOException ioe) {
Log.error(ioe);
throw ioe;
} finally {
if (writer != null) {
writer.close();
}
}
return isDirReady;
return stringWriter.toString();
}
public boolean validateImportFile(String file) {
String importFilePath = exportDirectory() + File.separator + file;
String importFilePath = exportDirectory + file;
try {
return new UserSchemaValidator(importFilePath, "messenger-user-schema.xsd.xml").validate();
......@@ -114,7 +151,7 @@ public class ImportExportPlugin implements Plugin {
}
public List importUserData(String file) throws MalformedURLException, DocumentException {
String importFilePath = exportDirectory() + File.separator + file;
String importFilePath = exportDirectory + File.separator + file;
SAXReader reader = new SAXReader();
Document document = reader.read(new File(importFilePath).toURL());
......@@ -129,9 +166,20 @@ public class ImportExportPlugin implements Plugin {
for (User user : users) {
Element userElement = root.addElement("User");
String userName = user.getUsername();
userElement.addElement("Jid").addText(userName);
// if (userName != null) {
// try {
// userName = URLEncoder.encode(userName, "UTF-8");
// } catch (UnsupportedEncodingException e) {
// Log.error(e);
// userName = "";
// }
// } else {
// userName = "";
// }
userElement.addElement("Jid").addText(userName);
try {
userElement.addElement("Password").addText(provider.getPassword(userName));
userElement.addElement("Password").addText(provider.getPassword(user.getUsername()));
}
catch (UserNotFoundException e) {
//this should never happen
......@@ -139,7 +187,19 @@ public class ImportExportPlugin implements Plugin {
userElement.addElement("Password").addText(userName);
}
userElement.addElement("Email").addText(user.getEmail() == null ? "" : user.getEmail());
userElement.addElement("Name").addText(user.getName() == null ? "" : user.getName());
String name = user.getName();
// if (name != null) {
// try {
// name = URLEncoder.encode(name, "UTF-8");
// } catch (UnsupportedEncodingException e) {
// Log.error(e);
// name = "";
// }
// } else {
// name = "";
// }
userElement.addElement("Name").addText(name == null ? "" : name);
//creation and modified datte are not used as part of the import process but are exported
//for historical purposes, should they be formatted differently?
......
......@@ -14,31 +14,40 @@
<%
boolean exportUsers = request.getParameter("exportUsers") != null;
boolean success = request.getParameter("success") != null;
boolean exportToFile = ParamUtils.getBooleanParameter(request, "exporttofile", false);
ImportExportPlugin plugin = (ImportExportPlugin) XMPPServer.getInstance().getPluginManager().getPlugin("userimportexport");
String exportText = null;
Map errors = new HashMap();
if (exportUsers) {
String file = ParamUtils.getParameter(request, "exportFile");
if ((file == null) || (file.length() <= 0)) {
errors.put("missingFile","missingFile");
if (exportToFile) {
String file = ParamUtils.getParameter(request, "exportFile");
if ((file == null) || (file.length() <= 0)) {
errors.put("missingFile","missingFile");
}
else {
try {
//todo this could take some, redirect to a progress page?
if (plugin.exportUsersToFile(file)) {
response.sendRedirect("export-user-data.jsp?success=true");
return;
}
else {
errors.put("fileNotCreated","fileNotCreated");
}
}
catch (IOException e) {
errors.put("IOException","IOException");
}
}
}
else {
try {
//todo this could take some, redirect to a progress page?
if (plugin.exportUserData(file)) {
response.sendRedirect("export-user-data.jsp?success=true");
return;
}
else {
errors.put("fileNotCreated","fileNotCreated");
}
}
catch (IOException e) {
errors.put("IOException","IOException");
}
}
exportText = plugin.exportUsersToString();
}
}
%>
......@@ -47,7 +56,7 @@
<% // Title of this page and breadcrumbs
String title = "Export User Data";
pageinfo.setTitle(title);
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "index.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "../../index.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(title, "export-user-data.jsp"));
pageinfo.setPageID("import-export-selection");
%>
......@@ -91,30 +100,49 @@
<% } %>
<form action="export-user-data.jsp?exportUsers" method="post">
<div class="jive-table">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
<tr>
<th>Export Properties</th>
</tr>
</thead>
<tr class="jive-even">
<td style="border-right:1px #ccc solid;">Export Location:</td>
</tr>
<tr class="jive-odd">
<td style="border-right:1px #ccc solid;"><%= plugin.exportDirectory() %></td>
</tr>
<tr class="jive-even">
<td style="border-right:1px #ccc solid;">Export File Name:</td>
</tr>
<tr class="jive-odd">
<td style="border-right:1px #ccc solid;">
<input type="text" size="30" maxlength="150" name="exportFile">
</td>
</tr>
</table>
</div>
<fieldset>
<legend>Export Options</legend>
<div>
<table cellpadding="3" cellspacing="0" border="0" width="100%">
<tbody>
<tr>
<td width="1%">
<input type="radio" name="exporttofile" value="true" selected id="rb01">
</td>
<td width="99%">
<label for="rb01"><b>To File</b></label> - Save user data to the specified file location.
</td>
<tr>
<td width="1%">&nbsp;</td>
<td width="99%">
<%= plugin.exportDirectory() %>
</td>
</tr>
<tr>
<td width="1%">&nbsp;</td>
<td width="99%">Export File Name:&nbsp;<input type="text" size="30" maxlength="150" name="exportFile"></td>
</tr>
</tr>
<tr>
<td width="1%">
<input type="radio" name="exporttofile" value="false" id="rb02">
</td>
<td width="99%">
<label for="rb02"><b>To Screen</b></label> - Dispaly user data in the text area below.
</td>
</tr>
<tr>
<td width="1%">&nbsp;</td>
<td width="99%">
<textarea cols="80" rows="20" wrap=off><%=(exportText == null) ? "" : exportText %></textarea>
</td>
</tr>
</tbody>
</table>
</div>
</fieldset>
<br><br>
<input type="submit" value="Export">
......
......@@ -7,7 +7,7 @@
<% // Title of this page and breadcrumbs
String title = "Import/Export Selection";
pageinfo.setTitle(title);
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "index.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "../../index.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(title, "import-export-selection.jsp"));
pageinfo.setPageID("import-export-selection");
......
......@@ -55,7 +55,7 @@
<% // Title of this page and breadcrumbs
String title = "Import User Data";
pageinfo.setTitle(title);
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "index.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "../../index.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(title, "import-user-data.jsp"));
pageinfo.setPageID("import-export-selection");
%>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment