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 */ 020 package org.apache.directory.server.changepw.io; 021 022 023 import java.io.ByteArrayOutputStream; 024 import java.io.IOException; 025 import java.nio.ByteBuffer; 026 027 import org.apache.directory.server.changepw.value.ChangePasswordData; 028 import org.apache.directory.server.kerberos.shared.io.encoder.PrincipalNameEncoder; 029 import org.apache.directory.shared.asn1.der.ASN1OutputStream; 030 import org.apache.directory.shared.asn1.der.DERGeneralString; 031 import org.apache.directory.shared.asn1.der.DEROctetString; 032 import org.apache.directory.shared.asn1.der.DERSequence; 033 import org.apache.directory.shared.asn1.der.DERTaggedObject; 034 035 036 /** 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 * @version $Rev$, $Date$ 039 */ 040 public class ChangePasswordDataEncoder 041 { 042 /** 043 * Encodes a {@link ChangePasswordData} into a byte array. 044 * 045 * @param data 046 * @return The byte array. 047 * @throws IOException 048 */ 049 public byte[] encode( ChangePasswordData data ) throws IOException 050 { 051 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 052 ASN1OutputStream aos = new ASN1OutputStream( baos ); 053 054 DERSequence dataSequence = encodeDataSequence( data ); 055 aos.writeObject( dataSequence ); 056 057 aos.close(); 058 059 return baos.toByteArray(); 060 } 061 062 063 /** 064 * Encodes a {@link ChangePasswordData} into a {@link ByteBuffer}. 065 * 066 * @param data 067 * @param out 068 * @throws IOException 069 */ 070 public void encode( ChangePasswordData data, ByteBuffer out ) throws IOException 071 { 072 ASN1OutputStream aos = new ASN1OutputStream( out ); 073 074 DERSequence sequence = encodeDataSequence( data ); 075 aos.writeObject( sequence ); 076 077 aos.close(); 078 } 079 080 081 private DERSequence encodeDataSequence( ChangePasswordData data ) 082 { 083 DERSequence sequence = new DERSequence(); 084 sequence.add( new DERTaggedObject( 0, new DEROctetString( data.getPassword() ) ) ); 085 086 // OPTIONAL 087 if ( data.getPrincipalName() != null ) 088 { 089 sequence.add( new DERTaggedObject( 1, PrincipalNameEncoder.encode( data.getPrincipalName() ) ) ); 090 } 091 092 // OPTIONAL 093 if ( data.getRealm() != null ) 094 { 095 sequence.add( new DERTaggedObject( 2, DERGeneralString.valueOf( data.getRealm() ) ) ); 096 } 097 098 return sequence; 099 } 100 }