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.i18n.I18n;
023import org.apache.directory.api.ldap.model.name.Dn;
024import org.apache.directory.api.util.Strings;
025
026
027/**
028 * Simple ProxiedAuthz implementation class.
029 *
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 * @version $Rev$, $Date$
032 */
033public class ProxiedAuthzImpl extends AbstractControl implements ProxiedAuthz
034{
035    /**
036     * The authzId used to authorize the user.
037     */
038    private String authzId;
039
040
041    /**
042     * Default constructor.
043     */
044    public ProxiedAuthzImpl()
045    {
046        super( OID );
047
048        // The criticality must be true
049        setCritical( true );
050    }
051
052
053    /**
054     * @return the authzId
055     */
056    @Override
057    public String getAuthzId()
058    {
059        return authzId;
060    }
061
062
063    /**
064     * The authzId syntax is given by the RFC 2829 :
065     * 
066     * <pre>
067     * authzId    = dnAuthzId / uAuthzId / &lt;empty&gt;
068     * dnAuthzId  = "dn:" dn
069     * dn         = utf8string
070     * uAuthzId   = "u:" userid
071     * userid     = utf8string
072     * </pre>
073     * @param authzId the authzId to set
074     */
075    @Override
076    public void setAuthzId( String authzId )
077    {
078        // We should have a valid authzId
079        if ( authzId == null )
080        {
081            throw new RuntimeException( I18n.err( I18n.ERR_13511_INVALID_PROXIED_AUTHZ_NULL ) );
082        }
083
084        if ( !Strings.isEmpty( authzId ) )
085        {
086            String lowercaseAuthzId = Strings.toLowerCaseAscii( authzId );
087
088            if ( lowercaseAuthzId.startsWith( "dn:" ) )
089            {
090                String dn = authzId.substring( 3 );
091
092                if ( !Dn.isValid( dn ) )
093                {
094                    throw new RuntimeException( I18n.err( I18n.ERR_13512_INVALID_PROXIED_AUTHZ_BAD_DN ) );
095                }
096            }
097            else if ( !lowercaseAuthzId.startsWith( "u:" ) )
098            {
099                throw new RuntimeException( I18n.err( I18n.ERR_13513_INVALID_PROXIED_AUTHZ_NO_DN_OR_U ) );
100            }
101        }
102
103        this.authzId = authzId;
104    }
105
106
107    /**
108     * @see Object#hashCode()
109     */
110    @Override
111    public int hashCode()
112    {
113        int h = super.hashCode();
114
115        if ( authzId != null )
116        {
117            h = h * 37 + authzId.hashCode();
118        }
119
120        return h;
121    }
122
123
124    /**
125     * @see Object#equals(Object)
126     */
127    @Override
128    public boolean equals( Object o )
129    {
130        if ( this == o )
131        {
132            return true;
133        }
134
135        if ( !( o instanceof ProxiedAuthz ) )
136        {
137            return false;
138        }
139        
140        ProxiedAuthz otherControl = ( ProxiedAuthz ) o;
141
142        return super.equals( o )
143            && ( ( authzId == otherControl.getAuthzId() ) || ( ( authzId != null ) && authzId.equals( otherControl.getAuthzId() ) ) );
144    }
145
146
147    /**
148     * Return a String representing this PagedSearchControl.
149     */
150    @Override
151    public String toString()
152    {
153        StringBuilder sb = new StringBuilder();
154
155        sb.append( "    Proxied Authz Control\n" );
156        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
157        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
158        sb.append( "        authzid   : '" ).append( authzId ).append( "'\n" );
159
160        return sb.toString();
161    }
162}