You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2009/02/28 17:25:46 UTC

svn commit: r748882 - in /directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration: ./ model/ model/v150/ model/v151/ model/v152/ model/v153/ model/v154/

Author: seelmann
Date: Sat Feb 28 16:25:46 2009
New Revision: 748882

URL: http://svn.apache.org/viewvc?rev=748882&view=rev
Log:
DIRSTUDIO-436: Use an EntityResolver to prevent lookups for external entities

Added:
    directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/StudioEntityResolver.java
Modified:
    directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/AbstractServerXmlIO.java
    directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v150/ServerXmlIOV150.java
    directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v151/ServerXmlIOV151.java
    directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v152/ServerXmlIOV152.java
    directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v153/ServerXmlIOV153.java
    directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v154/ServerXmlIOV154.java

Added: directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/StudioEntityResolver.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/StudioEntityResolver.java?rev=748882&view=auto
==============================================================================
--- directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/StudioEntityResolver.java (added)
+++ directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/StudioEntityResolver.java Sat Feb 28 16:25:46 2009
@@ -0,0 +1,65 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.studio.apacheds.configuration;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+
+/**
+ * This class implements an {@link EntityResolver} used for XML parsing. 
+ * 
+ * Currently a zero-length character stream is returned. This will prevent 
+ * lookups to the internet for public entities (i.e. DTDs) in XML documents
+ * that are passed to the {@link ApacheDSConfigurationContentDescriber}.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class StudioEntityResolver implements EntityResolver
+{
+
+    public InputSource resolveEntity( String publicId, String systemId ) throws SAXException, IOException
+    {
+        InputSource is;
+        if ( publicId.equalsIgnoreCase( "-//SPRING//DTD BEAN//EN" ) ) //$NON-NLS-1$
+        {
+            // Assigning the Spring Beans DTD to an entity resolver
+            // (This will prevent the parser to try to get it online)
+            InputStream in = ApacheDSConfigurationPlugin.class.getResourceAsStream( "spring-beans.dtd" ); //$NON-NLS-1$
+            is = new InputSource( in );
+        }
+        else
+        {
+            is = new InputSource( new StringReader( "" ) );
+        }
+
+        is.setSystemId( systemId );
+        is.setPublicId( publicId );
+        return is;
+    }
+
+}

Modified: directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/AbstractServerXmlIO.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/AbstractServerXmlIO.java?rev=748882&r1=748881&r2=748882&view=diff
==============================================================================
--- directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/AbstractServerXmlIO.java (original)
+++ directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/AbstractServerXmlIO.java Sat Feb 28 16:25:46 2009
@@ -22,6 +22,8 @@
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
 import java.io.StringReader;
 import java.util.Iterator;
 
@@ -36,10 +38,12 @@
 import org.apache.directory.shared.ldap.ldif.LdifReader;
 import org.apache.directory.shared.ldap.util.StringTools;
 import org.apache.directory.studio.apacheds.configuration.ApacheDSConfigurationPlugin;
+import org.apache.directory.studio.apacheds.configuration.StudioEntityResolver;
 import org.dom4j.Document;
 import org.dom4j.Element;
 import org.dom4j.io.DocumentResult;
 import org.dom4j.io.DocumentSource;
+import org.dom4j.io.SAXReader;
 import org.eclipse.osgi.util.NLS;
 
 
@@ -278,4 +282,53 @@
         Document transformedDoc = result.getDocument();
         return transformedDoc;
     }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.InputStream)
+     */
+    public final boolean isValid( InputStream is )
+    {
+        try
+        {
+            SAXReader saxReader = new SAXReader();
+            saxReader.setEntityResolver( new StudioEntityResolver() );
+
+            return isValid( saxReader.read( is ) );
+        }
+        catch ( Exception e )
+        {
+            return false;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.Reader)
+     */
+    public final boolean isValid( Reader reader )
+    {
+        try
+        {
+            SAXReader saxReader = new SAXReader();
+            saxReader.setEntityResolver( new StudioEntityResolver() );
+
+            return isValid( saxReader.read( reader ) );
+        }
+        catch ( Exception e )
+        {
+            return false;
+        }
+    }
+
+
+    /**
+     * Checks if the Document is valid.
+     *
+     * @param document
+     *      the Document
+     * @return
+     *      true if the Document is valid, false if not
+     */
+    protected abstract boolean isValid( Document document );
 }

Modified: directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v150/ServerXmlIOV150.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v150/ServerXmlIOV150.java?rev=748882&r1=748881&r2=748882&view=diff
==============================================================================
--- directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v150/ServerXmlIOV150.java (original)
+++ directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v150/ServerXmlIOV150.java Sat Feb 28 16:25:46 2009
@@ -21,7 +21,6 @@
 
 
 import java.io.InputStream;
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -33,6 +32,7 @@
 import javax.naming.directory.BasicAttributes;
 import javax.xml.transform.TransformerException;
 
+import org.apache.directory.studio.apacheds.configuration.StudioEntityResolver;
 import org.apache.directory.studio.apacheds.configuration.model.AbstractServerXmlIO;
 import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
 import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO;
@@ -52,42 +52,6 @@
  */
 public class ServerXmlIOV150 extends AbstractServerXmlIO implements ServerXmlIO
 {
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.InputStream)
-     */
-    public boolean isValid( InputStream is )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( is ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.Reader)
-     */
-    public boolean isValid( Reader reader )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( reader ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
     /**
      * Checks if the Document is valid.
      *
@@ -96,7 +60,7 @@
      * @return
      *      true if the Document is valid, false if not
      */
-    private boolean isValid( Document document )
+    protected boolean isValid( Document document )
     {
         for ( Iterator<?> i = document.getRootElement().elementIterator( "bean" ); i.hasNext(); ) //$NON-NLS-1$
         {
@@ -127,6 +91,7 @@
         try
         {
             SAXReader reader = new SAXReader();
+            reader.setEntityResolver( new StudioEntityResolver() );
             Document document = reader.read( is );
 
             ServerConfigurationV150 serverConfiguration = new ServerConfigurationV150();

Modified: directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v151/ServerXmlIOV151.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v151/ServerXmlIOV151.java?rev=748882&r1=748881&r2=748882&view=diff
==============================================================================
--- directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v151/ServerXmlIOV151.java (original)
+++ directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v151/ServerXmlIOV151.java Sat Feb 28 16:25:46 2009
@@ -21,7 +21,6 @@
 
 
 import java.io.InputStream;
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -33,7 +32,7 @@
 import javax.naming.directory.BasicAttributes;
 import javax.xml.transform.TransformerException;
 
-import org.apache.directory.studio.apacheds.configuration.ApacheDSConfigurationPlugin;
+import org.apache.directory.studio.apacheds.configuration.StudioEntityResolver;
 import org.apache.directory.studio.apacheds.configuration.model.AbstractServerXmlIO;
 import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
 import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO;
@@ -42,8 +41,6 @@
 import org.dom4j.DocumentHelper;
 import org.dom4j.Element;
 import org.dom4j.io.SAXReader;
-import org.xml.sax.EntityResolver;
-import org.xml.sax.InputSource;
 
 
 /**
@@ -55,42 +52,6 @@
  */
 public class ServerXmlIOV151 extends AbstractServerXmlIO implements ServerXmlIO
 {
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.InputStream)
-     */
-    public boolean isValid( InputStream is )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( is ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.Reader)
-     */
-    public boolean isValid( Reader reader )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( reader ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
     /**
      * Checks if the Document is valid.
      *
@@ -99,7 +60,7 @@
      * @return
      *      true if the Document is valid, false if not
      */
-    private boolean isValid( Document document )
+    protected boolean isValid( Document document )
     {
         for ( Iterator<?> i = document.getRootElement().elementIterator( "bean" ); i.hasNext(); ) //$NON-NLS-1$
         {
@@ -129,24 +90,9 @@
     {
         try
         {
-            // Assigning the Spring Beans DTD to an entity resoler
-            // (This will prevent the parser to try to get it online)
-            EntityResolver resolver = new EntityResolver()
-            {
-                public InputSource resolveEntity( String publicId, String systemId )
-                {
-                    if ( publicId.equalsIgnoreCase( "-//SPRING//DTD BEAN//EN" ) ) //$NON-NLS-1$
-                    {
-                        InputStream in = ApacheDSConfigurationPlugin.class.getResourceAsStream( "spring-beans.dtd" ); //$NON-NLS-1$
-                        return new InputSource( in );
-                    }
-                    return null;
-                }
-            };
-
             // Reading and creating the document
             SAXReader reader = new SAXReader();
-            reader.setEntityResolver( resolver );
+            reader.setEntityResolver( new StudioEntityResolver() );
             Document document = reader.read( is );
 
             // Parsing the document

Modified: directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v152/ServerXmlIOV152.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v152/ServerXmlIOV152.java?rev=748882&r1=748881&r2=748882&view=diff
==============================================================================
--- directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v152/ServerXmlIOV152.java (original)
+++ directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v152/ServerXmlIOV152.java Sat Feb 28 16:25:46 2009
@@ -21,7 +21,6 @@
 
 
 import java.io.InputStream;
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -32,6 +31,7 @@
 import javax.naming.directory.Attributes;
 import javax.xml.transform.TransformerException;
 
+import org.apache.directory.studio.apacheds.configuration.StudioEntityResolver;
 import org.apache.directory.studio.apacheds.configuration.model.AbstractServerXmlIO;
 import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
 import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO;
@@ -145,42 +145,6 @@
     private static final String VALUE_EXAMPLE_DOT_COM = "example.com"; //$NON-NLS-1$
 
 
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.InputStream)
-     */
-    public boolean isValid( InputStream is )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( is ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.Reader)
-     */
-    public boolean isValid( Reader reader )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( reader ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
     /**
      * Checks if the Document is valid.
      *
@@ -189,7 +153,7 @@
      * @return
      *      true if the Document is valid, false if not
      */
-    private boolean isValid( Document document )
+    protected boolean isValid( Document document )
     {
         Element rootElement = document.getRootElement();
 
@@ -217,6 +181,7 @@
         {
             // Reading and creating the document
             SAXReader reader = new SAXReader();
+            reader.setEntityResolver( new StudioEntityResolver() );
             Document document = reader.read( is );
 
             // Parsing the document

Modified: directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v153/ServerXmlIOV153.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v153/ServerXmlIOV153.java?rev=748882&r1=748881&r2=748882&view=diff
==============================================================================
--- directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v153/ServerXmlIOV153.java (original)
+++ directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v153/ServerXmlIOV153.java Sat Feb 28 16:25:46 2009
@@ -21,7 +21,6 @@
 
 
 import java.io.InputStream;
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -32,6 +31,7 @@
 import javax.naming.directory.Attributes;
 import javax.xml.transform.TransformerException;
 
+import org.apache.directory.studio.apacheds.configuration.StudioEntityResolver;
 import org.apache.directory.studio.apacheds.configuration.model.AbstractServerXmlIO;
 import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
 import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO;
@@ -154,42 +154,6 @@
     private static final String VALUE_EXAMPLE_DOT_COM = "example.com"; //$NON-NLS-1$
 
 
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.InputStream)
-     */
-    public boolean isValid( InputStream is )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( is ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.Reader)
-     */
-    public boolean isValid( Reader reader )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( reader ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
     /**
      * Checks if the Document is valid.
      *
@@ -198,7 +162,7 @@
      * @return
      *      true if the Document is valid, false if not
      */
-    private boolean isValid( Document document )
+    protected boolean isValid( Document document )
     {
         Element rootElement = document.getRootElement();
 
@@ -232,6 +196,7 @@
         {
             // Reading and creating the document
             SAXReader reader = new SAXReader();
+            reader.setEntityResolver( new StudioEntityResolver() );
             Document document = reader.read( is );
 
             // Parsing the document

Modified: directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v154/ServerXmlIOV154.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v154/ServerXmlIOV154.java?rev=748882&r1=748881&r2=748882&view=diff
==============================================================================
--- directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v154/ServerXmlIOV154.java (original)
+++ directory/studio/trunk/apacheds-configuration/src/main/java/org/apache/directory/studio/apacheds/configuration/model/v154/ServerXmlIOV154.java Sat Feb 28 16:25:46 2009
@@ -21,13 +21,13 @@
 
 
 import java.io.InputStream;
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
 import javax.xml.transform.TransformerException;
 
+import org.apache.directory.studio.apacheds.configuration.StudioEntityResolver;
 import org.apache.directory.studio.apacheds.configuration.model.AbstractServerXmlIO;
 import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
 import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO;
@@ -143,42 +143,6 @@
     private static final String VALUE_EXAMPLE_DOT_COM = "example.com"; //$NON-NLS-1$
 
 
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.InputStream)
-     */
-    public boolean isValid( InputStream is )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( is ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
-    /* (non-Javadoc)
-     * @see org.apache.directory.studio.apacheds.configuration.model.ServerXmlIO#isValid(java.io.Reader)
-     */
-    public boolean isValid( Reader reader )
-    {
-        try
-        {
-            SAXReader saxReader = new SAXReader();
-
-            return isValid( saxReader.read( reader ) );
-        }
-        catch ( Exception e )
-        {
-            return false;
-        }
-    }
-
-
     /**
      * Checks if the Document is valid.
      *
@@ -187,7 +151,7 @@
      * @return
      *      true if the Document is valid, false if not
      */
-    private boolean isValid( Document document )
+    protected boolean isValid( Document document )
     {
         Element rootElement = document.getRootElement();
 
@@ -222,6 +186,7 @@
         {
             // Reading and creating the document
             SAXReader reader = new SAXReader();
+            reader.setEntityResolver( new StudioEntityResolver() );
             Document document = reader.read( is );
 
             // Parsing the document