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.mina.filter.codec;
021
022import org.apache.mina.core.buffer.IoBuffer;
023
024/**
025 * An exception that is thrown when {@link ProtocolDecoder}
026 * cannot understand or failed to validate the specified {@link IoBuffer}
027 * content.
028 *
029 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
030 */
031public class ProtocolDecoderException extends ProtocolCodecException {
032    private static final long serialVersionUID = 3545799879533408565L;
033
034    private String hexdump;
035
036    /**
037     * Constructs a new instance.
038     */
039    public ProtocolDecoderException() {
040        // Do nothing
041    }
042
043    /**
044     * Constructs a new instance with the specified message.
045     * 
046     * @param message The detail message
047     */
048    public ProtocolDecoderException(String message) {
049        super(message);
050    }
051
052    /**
053     * Constructs a new instance with the specified cause.
054     * 
055     * @param cause The Exception's cause
056     */
057    public ProtocolDecoderException(Throwable cause) {
058        super(cause);
059    }
060
061    /**
062     * Constructs a new instance with the specified message and the specified
063     * cause.
064     * 
065     * @param message The detail message
066     * @param cause The Exception's cause
067     */
068    public ProtocolDecoderException(String message, Throwable cause) {
069        super(message, cause);
070    }
071
072    /**
073     * @return the message and the hexdump of the unknown part.
074     */
075    @Override
076    public String getMessage() {
077        String message = super.getMessage();
078
079        if (message == null) {
080            message = "";
081        }
082
083        if (hexdump != null) {
084            return message + (message.length() > 0 ? " " : "") + "(Hexdump: " + hexdump + ')';
085        }
086
087        return message;
088    }
089
090    /**
091     * @return the hexdump of the unknown message part.
092     */
093    public String getHexdump() {
094        return hexdump;
095    }
096
097    /**
098     * Sets the hexdump of the unknown message part.
099     * 
100     * @param hexdump The hexadecimal String representation of the message 
101     */
102    public void setHexdump(String hexdump) {
103        if (this.hexdump != null) {
104            throw new IllegalStateException("Hexdump cannot be set more than once.");
105        }
106        
107        this.hexdump = hexdump;
108    }
109}