summaryrefslogtreecommitdiffstats
path: root/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/util/JNDIUtil.java
blob: be19f0b6c46dbc76f4215ff320048d30c4f17ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package org.apache.tuscany.das.ldap.util;

import javax.naming.NamingException;
import javax.naming.directory.DirContext;

/**
 * The Class JNDIUtil.
 */
public class JNDIUtil
{
    /**
     * Calculate DN components.
     * 
     * @param context the context
     * 
     * @return the string[] containing the components of the DN
     * 
     * @throws NamingException the naming exception
     */
    public static String[] calculateDNComponents(
        DirContext context) 
    throws NamingException
    {
        String contextDN                  = 
            context.getNameInNamespace();
        
        String[] initialContextComponents = 
            contextDN.split( "[,]" );

        return initialContextComponents;
    }

    
    /**
     * Calculate DN components.
     * 
     * @param contextDN the context DN
     * 
     * @return the string[] containing the components of the DN
     * 
     * @throws NamingException the naming exception
     */
    public static String[] calculateDNComponents(
        String contextDN) 
    throws NamingException
    {
        String[] initialContextComponents = 
            contextDN.split( "[,]" );

        return initialContextComponents;
    }
    
    /**
     * Gets the parent context.
     * 
     * @param partitionContext the partition context
     * @param childContext the child context
     * 
     * @return the parent context (Null if the child context is the root)
     * 
     * @throws NamingException the naming exception
     */
    public static DirContext getParentContext(
        DirContext childContext, 
        DirContext partitionContext) 
    throws NamingException
    {
        String[] childContextDNComponents = 
            calculateDNComponents( childContext );
        
        String[] partitionDNComponents    = 
            calculateDNComponents( partitionContext );
        
        DirContext parentContext          = null; 
        
        if (childContextDNComponents.length == partitionDNComponents.length)
        {
            return null;
        }
        else if ( (childContextDNComponents.length - 1) == partitionDNComponents.length)
        {
            return partitionContext;
        }
        else
        {
            String parentLookupDN = 
                calculateParentRelativeDN( 
                    childContext, partitionContext.getNameInNamespace() );
            
            
            
            parentContext = 
                ( DirContext ) partitionContext.lookup( parentLookupDN );
        }
        return parentContext;
    }

    
    /**
     * Calculate parent relative DN.
     * The parent relative DN is the
     * DN of the parent context, relative
     * to the partition context.
     * 
     * So if the partition context is
     * <i>ou=system</i> and
     * the child context has DN
     * <i>cn=accounts, cn=users, cn=example, ou=system</i>
     * 
     * then the parent relative DN is
     * <i>cn=users, cn=example</i>
     * 
     * @param childContext the child context
     * @param partitionDN the partition DN
     * 
     * @return the string
     * 
     * @throws NamingException the naming exception
     */
    public static String calculateParentRelativeDN(
        DirContext childContext, 
        String partitionDN) 
    throws NamingException
    {
        String parentRelativeDN           = null;
        
        String[] childContextDNComponents = 
            calculateDNComponents( childContext );
        
        String[] partitionDNComponents    = 
            calculateDNComponents( partitionDN );
        
        if (childContextDNComponents.length == 
            partitionDNComponents.length)
        {
            return null;
        }
        else if (childContextDNComponents.length ==
             partitionDNComponents.length + 1)
        {
            String childRDN            = childContextDNComponents[0];
            String childContextDN      = childContext.getNameInNamespace();
            
            int beginIndex = childRDN.length() + 1;
            int endIndex   = childContextDN.length();

            parentRelativeDN = childContextDN.substring( 
                beginIndex, endIndex );
            
            return parentRelativeDN;
        }
        else
        {
            String childRDN            = childContextDNComponents[0];
            String childContextDN      = childContext.getNameInNamespace();
            
            int beginIndex = childRDN.length() + 1;
            int endIndex   = 
                childContextDN.length() - 
                ( partitionDN.length() +1);
            
            parentRelativeDN = childContextDN.substring( 
                beginIndex, endIndex );
        }
        return parentRelativeDN;
    }
}