/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.qpid.proton4j.engine.impl;

import org.apache.qpid.proton4j.amqp.security.SaslPerformative;
import org.apache.qpid.proton4j.amqp.transport.AMQPHeader;
import org.apache.qpid.proton4j.amqp.transport.Performative;
import org.apache.qpid.proton4j.buffer.ProtonBuffer;
import org.apache.qpid.proton4j.common.logging.ProtonLogger;
import org.apache.qpid.proton4j.common.logging.ProtonLoggerFactory;
import org.apache.qpid.proton4j.engine.EngineHandler;
import org.apache.qpid.proton4j.engine.EngineHandlerContext;
import org.apache.qpid.proton4j.engine.HeaderFrame;
import org.apache.qpid.proton4j.engine.ProtocolFrame;
import org.apache.qpid.proton4j.engine.SaslFrame;

/**
 * Handler that will log incoming and outgoing Frames
 */
public class ProtonFrameLoggingHandler implements EngineHandler {

    private static ProtonLogger LOG = ProtonLoggerFactory.getLogger(ProtonFrameLoggingHandler.class);

    // TODO - Possible that this should also have configuration for on / off and even looks at
    //        env for PN_TRACE_FRM for legacy reasons.

    @Override
    public void handlerAdded(EngineHandlerContext context) throws Exception {
    }

    @Override
    public void handlerRemoved(EngineHandlerContext context) throws Exception {
    }

    @Override
    public void handleRead(EngineHandlerContext context, ProtonBuffer buffer) {
        // TODO Could trace out bytes here, would need a pretty print helper.
        context.fireRead(buffer);
    }

    @Override
    public void handleRead(EngineHandlerContext context, HeaderFrame header) {
        LOG.trace("<- Header: {}", header);
        context.fireRead(header);
    }

    @Override
    public void handleRead(EngineHandlerContext context, SaslFrame frame) {
        LOG.trace("<- SASL: {}", frame);
        context.fireRead(frame);
    }

    @Override
    public void handleRead(EngineHandlerContext context, ProtocolFrame frame) {
        LOG.trace("<- AMQP: {}", frame);
        context.fireRead(frame);
    }

    @Override
    public void transportEncodingError(EngineHandlerContext context, Throwable e) {
        LOG.warn("-> Error while encoding: {}", e);
        context.fireEncodingError(e);
    }

    @Override
    public void transportDecodingError(EngineHandlerContext context, Throwable e) {
        LOG.warn("-> Error while decoding: {}", e);
        context.fireDecodingError(e);
    }

    @Override
    public void transportFailed(EngineHandlerContext context, Throwable e) {
        LOG.error("-> Unrecoverable Transport error: {}", e);
        context.fireFailed(e);
    }

    @Override
    public void handleWrite(EngineHandlerContext context, AMQPHeader header) {
        LOG.trace("-> AMQP: {}", header);
        context.fireWrite(header);
    }

    @Override
    public void handleWrite(EngineHandlerContext context, Performative performative, short channel, ProtonBuffer payload, Runnable payloadToLarge) {
        LOG.trace("-> AMQP: {}", performative);  // TODO - Payload ?
        context.fireWrite(performative, channel, payload, payloadToLarge);
    }

    @Override
    public void handleWrite(EngineHandlerContext context, SaslPerformative performative) {
        LOG.trace("-> SASL: {}", performative);
        context.fireWrite(performative);
    }

    @Override
    public void handleWrite(EngineHandlerContext context, ProtonBuffer buffer) {
        // TODO Could trace out bytes here, would need a pretty print helper.
        context.fireWrite(buffer);
    }
}

@@ -1261,18 +1261,25 @@
 eHandler
+Adapter
 ;%0A
-
 import o
@@ -1593,17 +1593,14 @@
 ler 
-implement
+extend
 s En
@@ -1610,16 +1610,23 @@
 eHandler
+Adapter
  %7B%0A%0A    
@@ -2079,221 +2079,8 @@
  %7D%0A%0A
-    @Override%0A    public void handleRead(EngineHandlerContext context, ProtonBuffer buffer) %7B%0A        // TODO Could trace out bytes here, would need a pretty print helper.%0A        context.fireRead(buffer);%0A    %7D%0A%0A
     
@@ -2603,589 +2603,8 @@
  %7D%0A%0A
-    @Override%0A    public void transportEncodingError(EngineHandlerContext context, Throwable e) %7B%0A        LOG.warn(%22-%3E Error while encoding: %7B%7D%22, e);%0A        context.fireEncodingError(e);%0A    %7D%0A%0A    @Override%0A    public void transportDecodingError(EngineHandlerContext context, Throwable e) %7B%0A        LOG.warn(%22-%3E Error while decoding: %7B%7D%22, e);%0A        context.fireDecodingError(e);%0A    %7D%0A%0A    @Override%0A    public void transportFailed(EngineHandlerContext context, Throwable e) %7B%0A        LOG.error(%22-%3E Unrecoverable Transport error: %7B%7D%22, e);%0A        context.fireFailed(e);%0A    %7D%0A%0A
     
@@ -3239,24 +3239,24 @@
 formative);%0A
+
         cont
@@ -3293,222 +3293,7 @@
    %7D
-%0A%0A    @Override%0A    public void handleWrite(EngineHandlerContext context, ProtonBuffer buffer) %7B%0A        // TODO Could trace out bytes here, would need a pretty print helper.%0A        context.fireWrite(buffer);%0A    %7D
 %0A%7D%0A

Clean up frame logging handler code