directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r495634 - /directory/trunks/ldapstudio/ldapstudio-browser-core/src/main/java/org/apache/directory/ldapstudio/browser/core/jobs/ExportDsmlJob.java
Date Fri, 12 Jan 2007 16:42:23 GMT
Author: pamarcelot
Date: Fri Jan 12 08:42:22 2007
New Revision: 495634

URL: http://svn.apache.org/viewvc?view=rev&rev=495634
Log:
Updating DSML Export to be able to use the search filter. Substrings filter still needs a
workaround.

Modified:
    directory/trunks/ldapstudio/ldapstudio-browser-core/src/main/java/org/apache/directory/ldapstudio/browser/core/jobs/ExportDsmlJob.java

Modified: directory/trunks/ldapstudio/ldapstudio-browser-core/src/main/java/org/apache/directory/ldapstudio/browser/core/jobs/ExportDsmlJob.java
URL: http://svn.apache.org/viewvc/directory/trunks/ldapstudio/ldapstudio-browser-core/src/main/java/org/apache/directory/ldapstudio/browser/core/jobs/ExportDsmlJob.java?view=diff&rev=495634&r1=495633&r2=495634
==============================================================================
--- directory/trunks/ldapstudio/ldapstudio-browser-core/src/main/java/org/apache/directory/ldapstudio/browser/core/jobs/ExportDsmlJob.java
(original)
+++ directory/trunks/ldapstudio/ldapstudio-browser-core/src/main/java/org/apache/directory/ldapstudio/browser/core/jobs/ExportDsmlJob.java
Fri Jan 12 08:42:22 2007
@@ -31,7 +31,31 @@
 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
 import org.apache.directory.ldapstudio.browser.core.model.SearchParameter;
+import org.apache.directory.ldapstudio.browser.core.model.filter.LdapAndFilterComponent;
+import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilter;
+import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilterComponent;
+import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilterItemComponent;
+import org.apache.directory.ldapstudio.browser.core.model.filter.LdapNotFilterComponent;
+import org.apache.directory.ldapstudio.browser.core.model.filter.LdapOrFilterComponent;
+import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterParser;
+import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterToken;
 import org.apache.directory.ldapstudio.dsmlv2.engine.Dsmlv2Engine;
+import org.apache.directory.ldapstudio.dsmlv2.request.SearchRequestDsml;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.ldap.codec.AttributeValueAssertion;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
+import org.apache.directory.shared.ldap.codec.search.AndFilter;
+import org.apache.directory.shared.ldap.codec.search.AttributeValueAssertionFilter;
+import org.apache.directory.shared.ldap.codec.search.Filter;
+import org.apache.directory.shared.ldap.codec.search.NotFilter;
+import org.apache.directory.shared.ldap.codec.search.OrFilter;
+import org.apache.directory.shared.ldap.codec.search.PresentFilter;
+import org.apache.directory.shared.ldap.codec.search.SearchRequest;
+import org.apache.directory.shared.ldap.message.ScopeEnum;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
 
 
 /**
@@ -104,90 +128,80 @@
 
         try
         {
-            // Getting and preparing each parameter for the request        
-            String requestDN = searchParameter.getSearchBase().toString();
+            SearchRequest searchRequest = new SearchRequest();
+            searchRequest.setProtocolOP( searchRequest );
 
-            String requestScope = null;
+            // DN
+            searchRequest.setBaseObject( new LdapDN( searchParameter.getSearchBase().toString()
) );
+
+            // Scope
             int scope = searchParameter.getScope();
             if ( scope == ISearch.SCOPE_OBJECT )
             {
-                requestScope = "baseObject";
+                searchRequest.setScope( ScopeEnum.BASE_OBJECT );
             }
             else if ( scope == ISearch.SCOPE_ONELEVEL )
             {
-                requestScope = "singleLevel";
+                searchRequest.setScope( ScopeEnum.SINGLE_LEVEL );
             }
             else if ( scope == ISearch.SCOPE_SUBTREE )
             {
-                requestScope = "wholeSubtree";
+                searchRequest.setScope( ScopeEnum.WHOLE_SUBTREE );
             }
 
-            String requestDerefAliases = null;
+            // DerefAliases
             int derefAliases = searchParameter.getAliasesDereferencingMethod();
             if ( derefAliases == IConnection.DEREFERENCE_ALIASES_ALWAYS )
             {
-                requestDerefAliases = "derefAlways";
+                searchRequest.setDerefAliases( LdapConstants.DEREF_ALWAYS );
             }
             else if ( derefAliases == IConnection.DEREFERENCE_ALIASES_FINDING )
             {
-                requestDerefAliases = "derefFindingBaseObj";
+                searchRequest.setDerefAliases( LdapConstants.DEREF_FINDING_BASE_OBJ );
             }
             else if ( derefAliases == IConnection.DEREFERENCE_ALIASES_NEVER )
             {
-                requestDerefAliases = "neverDerefAliases";
+                searchRequest.setDerefAliases( LdapConstants.NEVER_DEREF_ALIASES );
             }
             else if ( derefAliases == IConnection.DEREFERENCE_ALIASES_SEARCH )
             {
-                requestDerefAliases = "derefInSearching";
+                searchRequest.setDerefAliases( LdapConstants.DEREF_IN_SEARCHING );
             }
 
-            String requestTimeLimit = null;
+            // Time Limit
             int timeLimit = searchParameter.getTimeLimit();
             if ( timeLimit != 0 )
-                ;
             {
-                requestTimeLimit = "" + timeLimit;
+                searchRequest.setTimeLimit( timeLimit );
             }
 
-            String requestSizeLimit = null;
+            // Size Limit
             int countLimit = searchParameter.getCountLimit();
             if ( countLimit != 0 )
-                ;
             {
-                requestSizeLimit = "" + countLimit;
+                searchRequest.setSizeLimit( countLimit );
             }
 
-            // Constructing the request
-            StringBuffer sb = new StringBuffer();
-            sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
-            sb.append( "<batchRequest>" );
-            sb.append( "<searchRequest dn=\"" + requestDN + "\"" );
-            sb.append( " scope=\"" + requestScope + "\" " );
-            sb.append( " derefAliases=\"" + requestDerefAliases + "\"" );
-            if ( requestTimeLimit != null )
-            {
-                sb.append( " timeLimit=\"" + requestTimeLimit + "\"" );
-            }
-            if ( requestSizeLimit != null )
-            {
-                sb.append( " sizeLimit=\"" + requestSizeLimit + "\"" );
-            }
-            sb.append( ">" );
-            sb.append( "<filter><present name=\"objectclass\"></present></filter>"
);
-            sb.append( "<attributes>" );
+            // Filter
+            //            PresentFilter presentFilter = new PresentFilter();
+            //            presentFilter.setAttributeDescription( "objectclass" );
+            searchRequest.setFilter( convertToSharedLdapFilter( searchParameter.getFilter()
) );
+
+            // Attributes
             String[] returningAttributes = searchParameter.getReturningAttributes();
             for ( int i = 0; i < returningAttributes.length; i++ )
             {
-                sb.append( "<attribute name=\"" + returningAttributes[i] + "\"/>" );
+                searchRequest.addAttribute( returningAttributes[i] );
             }
-            sb.append( "</attributes>" );
-            sb.append( "</searchRequest>" );
-            sb.append( "</batchRequest>" );
 
             // Executing the request
+            Document xmlRequest = DocumentHelper.createDocument();
+            Element rootElement = xmlRequest.addElement( "batchRequest" );
+            SearchRequestDsml searchRequestDsml = new SearchRequestDsml( searchRequest );
+            searchRequestDsml.toDsml( rootElement );
             Dsmlv2Engine engine = new Dsmlv2Engine( connection.getHost(), connection.getPort(),
connection
                 .getBindPrincipal(), connection.getBindPassword() );
-            String response = engine.processDSML( sb.toString() );
+            String response = engine.processDSML( xmlRequest.asXML() );
 
             // Saving the response
             FileOutputStream fout = new FileOutputStream( exportDsmlFilename );
@@ -207,5 +221,162 @@
     protected String getErrorMessage()
     {
         return BrowserCoreMessages.jobs__export_dsml_error;
+    }
+
+
+    /**
+     * Converts a String filter into a Shared LDAP Filter.
+     *
+     * @param filter
+     *      the filter String to convert
+     * @return
+     *      the corresponding Shared LDAP Filter
+     * @throws DecoderException
+     */
+    public static Filter convertToSharedLdapFilter( String filter ) throws DecoderException
+    {
+        LdapFilterParser ldapFilterParser = new LdapFilterParser();
+
+        ldapFilterParser.parse( filter );
+
+        return convertToSharedLdapFilter( ldapFilterParser.getModel() );
+    }
+
+
+    /**
+     * Converts a Browser Core Filter Model into a Shared LDAP Model.
+     *
+     * @param filter
+     *      the filter
+     * @return
+     *      the corresponding filter in the Shared LDAP Model
+     * @throws DecoderException
+     */
+    public static Filter convertToSharedLdapFilter( LdapFilter filter ) throws DecoderException
+    {
+        Filter sharedLdapFilter = null;
+
+        LdapFilterComponent filterComponent = filter.getFilterComponent();
+        if ( filterComponent instanceof LdapAndFilterComponent )
+        {
+            LdapAndFilterComponent andFilterComponent = ( LdapAndFilterComponent ) filterComponent;
+
+            AndFilter andFilter = new AndFilter();
+            sharedLdapFilter = andFilter;
+
+            Filter[] filters = iterateOnFilters( andFilterComponent.getFilters() );
+            for ( int i = 0; i < filters.length; i++ )
+            {
+                andFilter.addFilter( filters[i] );
+            }
+        }
+        else if ( filterComponent instanceof LdapOrFilterComponent )
+        {
+            LdapOrFilterComponent orFilterComponent = ( LdapOrFilterComponent ) filterComponent;
+
+            OrFilter orFilter = new OrFilter();
+            sharedLdapFilter = orFilter;
+
+            Filter[] filters = iterateOnFilters( orFilterComponent.getFilters() );
+            for ( int i = 0; i < filters.length; i++ )
+            {
+                orFilter.addFilter( filters[i] );
+            }
+        }
+        else if ( filterComponent instanceof LdapNotFilterComponent )
+        {
+            LdapNotFilterComponent notFilterComponent = ( LdapNotFilterComponent ) filterComponent;
+
+            NotFilter notFilter = new NotFilter();
+            sharedLdapFilter = notFilter;
+
+            Filter[] filters = iterateOnFilters( notFilterComponent.getFilters() );
+            notFilter.setNotFilter( filters[0] );
+        }
+        else if ( filterComponent instanceof LdapFilterItemComponent )
+        {
+            LdapFilterItemComponent filterItemComponent = ( LdapFilterItemComponent ) filterComponent;
+
+            int filterType = filterItemComponent.getFilterToken().getType();
+            if ( filterType == LdapFilterToken.EQUAL )
+            {
+                AttributeValueAssertionFilter avaFilter = createAttributeValueAssertionFilter(
filterItemComponent,
+                    LdapConstants.EQUALITY_MATCH_FILTER );
+                sharedLdapFilter = avaFilter;
+            }
+            else if ( filterType == LdapFilterToken.GREATER )
+            {
+                AttributeValueAssertionFilter avaFilter = createAttributeValueAssertionFilter(
filterItemComponent,
+                    LdapConstants.GREATER_OR_EQUAL_FILTER );
+                sharedLdapFilter = avaFilter;
+            }
+            else if ( filterType == LdapFilterToken.LESS )
+            {
+                AttributeValueAssertionFilter avaFilter = createAttributeValueAssertionFilter(
filterItemComponent,
+                    LdapConstants.LESS_OR_EQUAL_FILTER );
+                sharedLdapFilter = avaFilter;
+            }
+            else if ( filterType == LdapFilterToken.APROX )
+            {
+                AttributeValueAssertionFilter avaFilter = createAttributeValueAssertionFilter(
filterItemComponent,
+                    LdapConstants.APPROX_MATCH_FILTER );
+                sharedLdapFilter = avaFilter;
+            }
+            else if ( filterType == LdapFilterToken.PRESENT )
+            {
+                PresentFilter presentFilter = new PresentFilter();
+                sharedLdapFilter = presentFilter;
+
+                presentFilter.setAttributeDescription( filterItemComponent.getAttributeToken().getValue()
);
+            }
+        }
+
+        return sharedLdapFilter;
+    }
+
+
+    /**
+     * Iterates the conversion on the given array of Ldap Filters.
+     *
+     * @param filters
+     *      the array of Ldap Filters to convert
+     * @return
+     *      an array containing the conversion for each Ldap Filter into its Shared LDAP
Model
+     * @throws DecoderException
+     */
+    private static Filter[] iterateOnFilters( LdapFilter[] filters ) throws DecoderException
+    {
+        List<Filter> filtersList = new ArrayList<Filter>();
+
+        for ( int c = 0; c < filters.length; c++ )
+        {
+            filtersList.add( convertToSharedLdapFilter( filters[c] ) );
+        }
+
+        return filtersList.toArray( new Filter[0] );
+    }
+
+
+    /**
+     * Create and returns an Attribute Value Assertion Filter from the given LdapFilterItemComponent
ant the given type.
+     *
+     * @param filter
+     *      the filter to convert
+     * @param type
+     *      the type of the Attribute Value Assertion Filter
+     * @return
+     *      the corresponding Attribute Value Assertion Filter
+     */
+    private static AttributeValueAssertionFilter createAttributeValueAssertionFilter( LdapFilterItemComponent
filter,
+        int type )
+    {
+        AttributeValueAssertionFilter avaFilter = new AttributeValueAssertionFilter( type
);
+
+        AttributeValueAssertion assertion = new AttributeValueAssertion();
+        avaFilter.setAssertion( assertion );
+        assertion.setAttributeDesc( filter.getAttributeToken().getValue() );
+        assertion.setAssertionValue( filter.getValueToken().getValue() );
+
+        return avaFilter;
     }
 }



Mime
View raw message