1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20
21 package org.apache.directory.server.changepw.io;
22
23
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26
27 import org.apache.directory.server.changepw.messages.ChangePasswordError;
28 import org.apache.directory.server.kerberos.shared.io.encoder.ErrorMessageEncoder;
29 import org.apache.directory.server.kerberos.shared.messages.ErrorMessage;
30
31
32 /**
33 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34 * @version $Rev: 546375 $, $Date: 2007-06-12 06:05:00 +0200 (Di, 12 Jun 2007) $
35 */
36 public class ChangePasswordErrorEncoder
37 {
38 private static final int HEADER_LENGTH = 6;
39
40
41 /**
42 * Encodes a {@link ChangePasswordError} into a {@link ByteBuffer}.
43 *
44 * @param buf
45 * @param message
46 * @throws IOException
47 */
48 public void encode( ByteBuffer buf, ChangePasswordError message ) throws IOException
49 {
50 // Build error message bytes
51 ErrorMessage errorMessage = message.getErrorMessage();
52 ErrorMessageEncoder errorEncoder = new ErrorMessageEncoder();
53 byte[] errorBytes = errorEncoder.encode( errorMessage );
54
55 short messageLength = ( short ) ( HEADER_LENGTH + errorBytes.length );
56 buf.putShort( messageLength );
57
58 short protocolVersion = 1;
59 buf.putShort( protocolVersion );
60
61 short zeroIndicatesError = 0;
62 buf.putShort( zeroIndicatesError );
63
64 buf.put( errorBytes );
65 }
66 }