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