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.server.kerberos.changepwd.io; 021 022 023import java.nio.ByteBuffer; 024import java.nio.InvalidMarkException; 025 026import org.apache.directory.server.kerberos.changepwd.exceptions.ChangePasswordException; 027import org.apache.directory.server.kerberos.changepwd.messages.AbstractPasswordMessage; 028import org.apache.directory.server.kerberos.changepwd.messages.ChangePasswordError; 029import org.apache.directory.server.kerberos.changepwd.messages.ChangePasswordReply; 030import org.apache.directory.server.kerberos.changepwd.messages.ChangePasswordRequest; 031 032 033/** 034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 035 */ 036public class ChangePasswordDecoder 037{ 038 public static AbstractPasswordMessage decode( ByteBuffer buf, boolean isTcp ) throws ChangePasswordException 039 { 040 if ( isTcp ) 041 { 042 // For TCP transport, there is a 4 octet header in network byte order 043 // that precedes the message and specifies the length of the message. 044 buf.getInt(); 045 buf.mark(); 046 } 047 048 // cause we don't have a special message type value, try decoding as request, reply and error and send whichever succeeds 049 050 try 051 { 052 return ChangePasswordRequest.decode( buf ); 053 } 054 catch ( Exception e ) 055 { 056 resetOrRewind( buf ); 057 } 058 059 try 060 { 061 return ChangePasswordReply.decode( buf ); 062 } 063 catch ( Exception e ) 064 { 065 resetOrRewind( buf ); 066 } 067 068 return ChangePasswordError.decode( buf ); 069 } 070 071 072 private static void resetOrRewind( ByteBuffer buf ) 073 { 074 try 075 { 076 buf.reset(); 077 } 078 catch ( InvalidMarkException e ) 079 { 080 buf.rewind(); 081 } 082 } 083}