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

added schema validation

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@2757 b35dd754-fafc-0310-a699-88a17e54d16e
parent b9e91c9c
......@@ -86,7 +86,7 @@ public class ImportExportPlugin implements Plugin {
} catch (IOException ioe) {
Log.error(ioe);
throw ioe;
} finally {
} finally {
if (writer != null) {
writer.close();
}
......@@ -101,15 +101,25 @@ public class ImportExportPlugin implements Plugin {
return importUsers(document, previousDomain);
}
public boolean validateImportFile(FileItem file) {
try {
return new UserSchemaValidator(file, "messenger-user-schema.xsd.xml").validate();
}
catch (Exception e) {
Log.error(e);
return false;
}
}
private Document exportUsers() {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("JiveMessenger");
Collection<User> users = userManager.getUsers();
for (User user : users) {
for (User user : users) {
Element userElement = root.addElement("User");
String userName = user.getUsername();
userElement.addElement("Username").addText(userName);
userElement.addElement("Username").addText(userName);
try {
userElement.addElement("Password").addText(provider.getPassword(user.getUsername()));
......@@ -117,7 +127,7 @@ public class ImportExportPlugin implements Plugin {
catch (UserNotFoundException e) {
//this should never happen
Log.info("User not found: " + userName + ", setting password to their username");
userElement.addElement("Password").addText(userName);
userElement.addElement("Password").addText(userName);
}
userElement.addElement("Email").addText(user.getEmail() == null ? "" : user.getEmail());
......@@ -150,7 +160,7 @@ public class ImportExportPlugin implements Plugin {
return document;
}
private List<String> importUsers(Document document, String previousDomain) throws DocumentException {
private List<String> importUsers(Document document, String previousDomain) {
List<String> duplicateUsers = new ArrayList<String>();
UserManager userManager = UserManager.getInstance();
......@@ -177,16 +187,16 @@ public class ImportExportPlugin implements Plugin {
String nameElement = userElement.getName();
if ("Username".equals(nameElement)) {
userName = userElement.getText();
userName = userElement.getText();
}
else if ("Password".equals(nameElement)) {
password = userElement.getText();
password = userElement.getText();
}
else if ("Name".equals(nameElement)) {
name = userElement.getText();
name = userElement.getText();
}
else if ("Email".equals(nameElement)) {
email = userElement.getText();
email = userElement.getText();
}
else if ("Roster".equals(nameElement)) {
Iterator rosterIter = userElement.elementIterator("Item");
......@@ -223,7 +233,7 @@ public class ImportExportPlugin implements Plugin {
}
if ((userName != null) && (password != null)) {
try {
try {
userManager.createUser(userName, password, name, email);
rosterMap.put(userName, rosterItems);
}
......
......@@ -5,6 +5,7 @@ import com.sun.msv.reader.util.IgnoreController;
import com.sun.msv.verifier.DocumentDeclaration;
import com.sun.msv.verifier.Verifier;
import org.apache.commons.fileupload.FileItem;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
......@@ -15,8 +16,7 @@ import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXParseException;
import java.io.File;
import java.net.MalformedURLException;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.SAXParserFactory;
......@@ -25,12 +25,11 @@ public class UserSchemaValidator {
private Document doc;
private String schema;
UserSchemaValidator(String usersFile, String schemaFile) throws MalformedURLException, DocumentException {
URL usersURL = new File(usersFile).toURL();
URL schemaURL = this.getClass().getClassLoader().getResource(schemaFile);
UserSchemaValidator(FileItem usersFile, String schemaFile) throws DocumentException, IOException {
SAXReader reader = new SAXReader();
doc = reader.read(usersURL);
doc = reader.read(usersFile.getInputStream());
URL schemaURL = this.getClass().getClassLoader().getResource(schemaFile);
schema = schemaURL.toExternalForm();
}
......
......@@ -28,31 +28,36 @@
FileItem pd = (FileItem) i.next();
String previousDomain = pd.getString();
try {
if (isEmpty(previousDomain)) {
duplicateUsers.addAll(plugin.importUserData(fi, null));
}
else if (!isEmpty(previousDomain)) {
duplicateUsers.addAll(plugin.importUserData(fi, previousDomain));
if (plugin.validateImportFile(fi)) {
try {
if (isEmpty(previousDomain)) {
duplicateUsers.addAll(plugin.importUserData(fi, null));
}
else if (!isEmpty(previousDomain)) {
duplicateUsers.addAll(plugin.importUserData(fi, previousDomain));
}
else {
errors.put("missingDomain", "missingDomain");
}
if (duplicateUsers.size() == 0) {
response.sendRedirect("import-user-data.jsp?success=true");
return;
}
errors.put("userAlreadyExists", "userAlreadyExists");
}
else {
errors.put("missingDomain", "missingDomain");
catch (MalformedURLException e) {
errors.put("IOException", "IOException");
}
if (duplicateUsers.size() == 0) {
response.sendRedirect("import-user-data.jsp?success=true");
return;
catch (DocumentException e) {
errors.put("DocumentException", "DocumentException");
}
errors.put("userAlreadyExists", "userAlreadyExists");
}
catch (MalformedURLException e) {
errors.put("IOException", "IOException");
}
catch (DocumentException e) {
errors.put("DocumentException", "DocumentException");
else {
errors.put("invalidUserFile", "invalidUserFile");
}
}
}
%>
<jsp:useBean id="pageinfo" scope="request" class="org.jivesoftware.admin.AdminPageBean" />
......@@ -84,8 +89,8 @@
The import file does not match the user schema.
<% } else if (errors.containsKey("userAlreadyExists")) { %>
The following users are already exist in the system and were not loaded:<br>
<%
Iterator iter = duplicateUsers.iterator();
<%
Iterator iter = duplicateUsers.iterator();
while (iter.hasNext()) {
String username = (String) iter.next();
%><%= username %><%
......
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