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 *  
019 */
020package org.apache.directory.shared.kerberos.messages;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.EncoderException;
026import org.apache.directory.api.asn1.ber.tlv.TLV;
027import org.apache.directory.shared.kerberos.KerberosConstants;
028import org.apache.directory.shared.kerberos.KerberosMessageType;
029import org.apache.directory.shared.kerberos.components.KdcReq;
030
031
032/**
033 * AS-REQ message. It's just a KDC-REQ message with a message type set to 10.
034 *  It will store the object described by the ASN.1 grammar :
035 * <pre>
036 * AS-REQ          ::= [APPLICATION 10] &lt;KDC-REQ&gt;
037 * </pre>
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class AsReq extends KdcReq
041{
042    // Storage for computed lengths
043    private int kdcReqLength;
044
045
046    /**
047     * Creates a new instance of AS-REQ.
048     */
049    public AsReq()
050    {
051        super( KerberosMessageType.AS_REQ );
052    }
053
054
055    /**
056     * Compute the AS-REQ length
057     * <pre>
058     * AS-REQ :
059     * 
060     * 0x6A L1 AS-REQ message
061     *  |
062     *  +--&gt;  0x30 L2 KDC-REQ sequence
063     * </pre>
064     */
065    @Override
066    public int computeLength()
067    {
068        kdcReqLength = super.computeLength();
069        return 1 + TLV.getNbBytes( kdcReqLength ) + kdcReqLength;
070    }
071
072
073    /**
074     * Encode the AS-REQ component
075     * 
076     * @param buffer The buffer containing the encoded result
077     * @return The encoded component
078     * @throws EncoderException If the encoding failed
079     */
080    @Override
081    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
082    {
083        if ( buffer == null )
084        {
085            buffer = ByteBuffer.allocate( computeLength() );
086        }
087
088        // The AS-REQ SEQ Tag
089        buffer.put( ( byte ) KerberosConstants.AS_REQ_TAG );
090        buffer.put( TLV.getBytes( kdcReqLength ) );
091
092        // The KDC-REQ --------------------------------------------------------
093        super.encode( buffer );
094
095        return buffer;
096    }
097}