[jboss-cvs] JBossAS SVN: r99922 - in projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src: test/java/org/jboss/test/security/config and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Jan 25 16:26:07 EST 2010
Author: anil.saldhana at jboss.com
Date: 2010-01-25 16:26:06 -0500 (Mon, 25 Jan 2010)
New Revision: 99922
Added:
projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/SecurityActions.java
Modified:
projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/StaxBasedConfigParser.java
projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java
Log:
take care of schema validation
Added: projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/SecurityActions.java
===================================================================
--- projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/SecurityActions.java (rev 0)
+++ projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/SecurityActions.java 2010-01-25 21:26:06 UTC (rev 99922)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.security.config.parser;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+/**
+ * Privileged Blocks
+ * @author Anil.Saldhana at redhat.com
+ * @since November 19, 2008
+ */
+class SecurityActions
+{
+
+ @SuppressWarnings("unchecked")
+ static ClassLoader getContextClassLoader() throws PrivilegedActionException
+ {
+ return (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction()
+ {
+ public Object run() throws Exception
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ });
+ }
+
+ static String getSystemProperty(final String key, final String defaultValue)
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<String>()
+ {
+ public String run()
+ {
+ return System.getProperty(key, defaultValue);
+ }
+ });
+ }
+
+ static void setSystemProperty(final String key, final String value)
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ System.setProperty(key, value);
+ return null;
+ }
+ });
+ }
+}
\ No newline at end of file
Modified: projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/StaxBasedConfigParser.java
===================================================================
--- projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/StaxBasedConfigParser.java 2010-01-25 21:25:40 UTC (rev 99921)
+++ projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/StaxBasedConfigParser.java 2010-01-25 21:26:06 UTC (rev 99922)
@@ -21,7 +21,9 @@
*/
package org.jboss.security.config.parser;
+import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
import java.util.List;
import javax.security.auth.login.Configuration;
@@ -31,9 +33,18 @@
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Validator;
+import org.jboss.logging.Logger;
import org.jboss.security.config.ApplicationPolicy;
import org.jboss.security.config.ApplicationPolicyRegistration;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
/**
* Configuration Parser based on Stax
@@ -42,36 +53,32 @@
*/
public class StaxBasedConfigParser
{
+ private static Logger log = Logger.getLogger(StaxBasedConfigParser.class);
+ private boolean trace = log.isTraceEnabled();
+
+ private String schemaFile = "schema/security-config_5_0.xsd";
+
/**
- * Whether schema validation is needed
+ * Validate the input file against a schema
+ * @param configStream
+ * @throws SAXException
+ * @throws IOException
*/
- protected boolean shouldValidate = true;
-
- /**
- * Return the flag indicating whether
- * schema validation is enabled
- * @return
- */
- public boolean isShouldValidate()
+ public void schemaValidate(InputStream configStream) throws SAXException, IOException
{
- return shouldValidate;
+ Validator validator = schemaValidator();
+ Source xmlSource = new StreamSource(configStream);
+ validator.validate(xmlSource);
}
-
- /**
- * Set the schema validation flag
- * @param shouldValidate
- */
- public void setShouldValidate(boolean shouldValidate)
- {
- this.shouldValidate = shouldValidate;
- }
/**
* Parse the Input stream of configuration
* @param configStream
* @throws XMLStreamException
+ * @throws IOException
+ * @throws SAXException
*/
- public void parse(InputStream configStream) throws XMLStreamException
+ public void parse(InputStream configStream) throws XMLStreamException, SAXException, IOException
{
Configuration config = Configuration.getConfiguration();
if(config instanceof ApplicationPolicyRegistration == false)
@@ -80,7 +87,8 @@
ApplicationPolicyRegistration appPolicyRegistration = (ApplicationPolicyRegistration) config;
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
- XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(configStream);
+ //XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(xmlSource);
+ XMLEventReader xmlEventReader = getXMLEventReader(configStream);
xmlEventReader = xmlInputFactory.createFilteredReader(xmlEventReader, new EventFilter()
{
@@ -109,4 +117,75 @@
}
}
}
+
+ private Validator schemaValidator()
+ {
+ try
+ {
+ ClassLoader tcl = SecurityActions.getContextClassLoader();
+ URL schemaURL = tcl.getResource(schemaFile);
+ if(schemaURL == null)
+ throw new RuntimeException("Cannot find schema :" + schemaFile);
+ SchemaFactory schemaFactory = SchemaFactory.newInstance( "http://www.w3.org/2001/XMLSchema" );
+ Schema schemaGrammar = schemaFactory.newSchema( schemaURL );
+
+ Validator schemaValidator = schemaGrammar.newValidator();
+ schemaValidator.setErrorHandler( new ErrorHandler()
+ {
+
+ public void error(SAXParseException ex) throws SAXException
+ {
+ logException(ex);
+ }
+
+ public void fatalError(SAXParseException ex) throws SAXException
+ {
+ logException(ex);
+ }
+
+ public void warning(SAXParseException ex) throws SAXException
+ {
+ logException(ex);
+ }
+
+ private void logException(SAXParseException sax)
+ {
+ StringBuilder builder = new StringBuilder();
+
+ if(trace)
+ {
+ builder.append("[").append(sax.getLineNumber()).append(",").append(sax.getColumnNumber()).append("]");
+ builder.append(":").append(sax.getLocalizedMessage());
+ log.trace(builder.toString());
+ }
+ }
+ });
+ return schemaValidator;
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private XMLEventReader getXMLEventReader(InputStream is)
+ {
+ XMLInputFactory xmlInputFactory = null;
+ XMLEventReader xmlEventReader = null;
+ try
+ {
+ xmlInputFactory = XMLInputFactory.newInstance();
+ xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
+ xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
+ xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
+ xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
+
+ xmlEventReader = xmlInputFactory.createXMLEventReader(is);
+ }
+ catch (Exception ex)
+ {
+ throw new RuntimeException(ex);
+ }
+ return xmlEventReader;
+ }
}
\ No newline at end of file
Modified: projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java
===================================================================
--- projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java 2010-01-25 21:25:40 UTC (rev 99921)
+++ projects/security/picketbox/fresh/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java 2010-01-25 21:26:06 UTC (rev 99922)
@@ -69,6 +69,10 @@
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
InputStream is = tcl.getResourceAsStream("config/securityConfig5.xml");
StaxBasedConfigParser parser = new StaxBasedConfigParser();
+
+ parser.schemaValidate(is);
+
+ is = tcl.getResourceAsStream("config/securityConfig5.xml");
parser.parse(is);
TestSecurityConfig5.validateJAASConfiguration();
@@ -83,6 +87,9 @@
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
InputStream is = tcl.getResourceAsStream("config/identitytrust-config.xml");
StaxBasedConfigParser parser = new StaxBasedConfigParser();
+ parser.schemaValidate(is);
+
+ is = tcl.getResourceAsStream("config/identitytrust-config.xml");
parser.parse(is);
TestIdentityTrustConfig.testConfJavaEE();
More information about the jboss-cvs-commits
mailing list