001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.directory.api.ldap.model.message.controls;
020
021
022import org.apache.directory.api.ldap.model.name.Dn;
023import org.apache.directory.api.util.Strings;
024
025
026/**
027 * Simple ProxiedAuthz implementation class.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 * @version $Rev$, $Date$
031 */
032public class ProxiedAuthzImpl extends AbstractControl implements ProxiedAuthz
033{
034    /**
035     * The authzId used to authorize the user.
036     */
037    private String authzId;
038
039
040    /**
041     * Default constructor.
042     */
043    public ProxiedAuthzImpl()
044    {
045        super( OID );
046
047        // The criticality must be true
048        setCritical( true );
049    }
050
051
052    /**
053     * @return the authzId
054     */
055    @Override
056    public String getAuthzId()
057    {
058        return authzId;
059    }
060
061
062    /**
063     * The authzId syntax is given by the RFC 2829 :
064     * 
065     * <pre>
066     * authzId    = dnAuthzId / uAuthzId / &lt;empty&gt;
067     * dnAuthzId  = "dn:" dn
068     * dn         = utf8string
069     * uAuthzId   = "u:" userid
070     * userid     = utf8string
071     * </pre>
072     * @param authzId the authzId to set
073     */
074    @Override
075    public void setAuthzId( String authzId )
076    {
077        // We should have a valid authzId
078        if ( authzId == null )
079        {
080            throw new RuntimeException( "Invalid proxied authz value : cannot be null" );
081        }
082
083        if ( !Strings.isEmpty( authzId ) )
084        {
085            String lowercaseAuthzId = Strings.toLowerCaseAscii( authzId );
086
087            if ( lowercaseAuthzId.startsWith( "dn:" ) )
088            {
089                String dn = authzId.substring( 3 );
090
091                if ( !Dn.isValid( dn ) )
092                {
093                    throw new RuntimeException( "Invalid proxied authz value : the DN is not valid" );
094                }
095            }
096            else if ( !lowercaseAuthzId.startsWith( "u:" ) )
097            {
098                throw new RuntimeException( "Invalid proxied authz value : should start with 'dn:' or 'u:'" );
099            }
100        }
101
102        this.authzId = authzId;
103    }
104
105
106    /**
107     * @see Object#hashCode()
108     */
109    @Override
110    public int hashCode()
111    {
112        int h = super.hashCode();
113
114        if ( authzId != null )
115        {
116            h = h * 37 + authzId.hashCode();
117        }
118
119        return h;
120    }
121
122
123    /**
124     * @see Object#equals(Object)
125     */
126    @Override
127    public boolean equals( Object o )
128    {
129        if ( !super.equals( o ) )
130        {
131            return false;
132        }
133
134        ProxiedAuthz otherControl = ( ProxiedAuthz ) o;
135
136        return ( authzId == otherControl.getAuthzId() )
137            || ( ( authzId != null ) && authzId.equals( otherControl.getAuthzId() ) );
138    }
139
140
141    /**
142     * Return a String representing this PagedSearchControl.
143     */
144    @Override
145    public String toString()
146    {
147        StringBuilder sb = new StringBuilder();
148
149        sb.append( "    Proxied Authz Control\n" );
150        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
151        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
152        sb.append( "        authzid   : '" ).append( authzId ).append( "'\n" );
153
154        return sb.toString();
155    }
156}