Coverage Report - org.apache.giraph.comm.netty.handler.ResponseEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
ResponseEncoder
0%
0/28
0%
0/12
8
 
 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 package org.apache.giraph.comm.netty.handler;
 20  
 
 21  
 import io.netty.buffer.ByteBufOutputStream;
 22  
 /*if[HADOOP_NON_SECURE]
 23  
 else[HADOOP_NON_SECURE]*/
 24  
 import org.apache.giraph.comm.requests.RequestType;
 25  
 /*end[HADOOP_NON_SECURE]*/
 26  
 import org.apache.giraph.comm.requests.WritableRequest;
 27  
 import org.apache.log4j.Logger;
 28  
 import io.netty.buffer.ByteBuf;
 29  
 import io.netty.channel.ChannelPromise;
 30  
 import io.netty.channel.ChannelHandlerContext;
 31  
 import io.netty.channel.ChannelOutboundHandlerAdapter;
 32  
 
 33  
 import static org.apache.giraph.utils.ByteUtils.SIZE_OF_INT;
 34  
 
 35  
 /**
 36  
  * How a server should respond to a client. Currently only used for
 37  
  * responding to client's SASL messages, and removed after client
 38  
  * authenticates.
 39  
  */
 40  0
 public class ResponseEncoder extends ChannelOutboundHandlerAdapter {
 41  
   /** Class logger. */
 42  0
   private static final Logger LOG = Logger.getLogger(ResponseEncoder.class);
 43  
 
 44  
   @Override
 45  
   public void write(ChannelHandlerContext ctx, Object msg,
 46  
     ChannelPromise promise) throws Exception {
 47  0
     if (LOG.isDebugEnabled()) {
 48  0
       LOG.debug("write(" + ctx + "," + msg);
 49  
     }
 50  
 
 51  0
     if (!(msg instanceof WritableRequest)) {
 52  0
       throw new IllegalArgumentException(
 53  0
           "encode: cannot encode message of type " + msg.getClass() +
 54  
               " since it is not an instance of an implementation of " +
 55  
               " WritableRequest.");
 56  
     }
 57  
     @SuppressWarnings("unchecked")
 58  0
     WritableRequest writableRequest = (WritableRequest) msg;
 59  
 
 60  0
     ByteBuf buf = ctx.alloc().buffer(10);
 61  0
     ByteBufOutputStream output = new ByteBufOutputStream(buf);
 62  
 
 63  0
     if (LOG.isDebugEnabled()) {
 64  0
       LOG.debug("encode: Encoding a message of type " + msg.getClass());
 65  
     }
 66  
 
 67  
     // Space is reserved now to be filled later by the serialize request size
 68  0
     output.writeInt(0);
 69  
     // write type of object.
 70  0
     output.writeByte(writableRequest.getType().ordinal());
 71  
     // write the object itself.
 72  0
     writableRequest.write(output);
 73  
 
 74  0
     output.flush();
 75  0
     output.close();
 76  
 
 77  
     // Set the correct size at the end.
 78  0
     buf.setInt(0, buf.writerIndex() - SIZE_OF_INT);
 79  
 
 80  0
     if (LOG.isDebugEnabled()) {
 81  0
       LOG.debug("encode: Encoding a message of type " + msg.getClass());
 82  
     }
 83  0
     ctx.write(buf, promise);
 84  
 /*if[HADOOP_NON_SECURE]
 85  
 else[HADOOP_NON_SECURE]*/
 86  0
     if (writableRequest.getType() == RequestType.SASL_COMPLETE_REQUEST) {
 87  
       // We are sending to the client a SASL_COMPLETE response (created by
 88  
       // the SaslServer handler). The SaslServer handler has removed itself
 89  
       // from the pipeline after creating this response, and now it's time for
 90  
       // the ResponseEncoder to remove itself also.
 91  0
       if (LOG.isDebugEnabled()) {
 92  0
         LOG.debug("encode: Removing RequestEncoder handler: no longer needed," +
 93  0
             " since client: " + ctx.channel().remoteAddress() + " has " +
 94  
             "completed authenticating.");
 95  
       }
 96  0
       ctx.pipeline().remove(this);
 97  
     }
 98  
 /*end[HADOOP_NON_SECURE]*/
 99  0
     ctx.write(buf, promise);
 100  0
   }
 101  
 }
 102