private void handleSetUGI(TUGIContainingTransport ugiTrans,
                            ThriftHiveMetastore.Processor.set_ugi<Iface> fn, TMessage msg, TProtocol iprot, TProtocol oprot)
      throws TException, SecurityException, NoSuchMethodException, IllegalArgumentException,
      IllegalAccessException, InvocationTargetException{

    UserGroupInformation clientUgi = ugiTrans.getClientUGI();
    if( null != clientUgi){
      throw new TException(new IllegalStateException("UGI is already set. Resetting is not " +
      "allowed. Current ugi is: " + clientUgi.getUserName()));
    }

    set_ugi_args args = fn.getEmptyArgsInstance();
    try {
      args.read(iprot);
    } catch (TProtocolException e) {
      iprot.readMessageEnd();
      TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR,
          e.getMessage());
      oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
      x.write(oprot);
      oprot.writeMessageEnd();
      oprot.getTransport().flush();
      return;
    }
    iprot.readMessageEnd();
    set_ugi_result result = fn.getResult(iface, args);
    List<String> principals = result.getSuccess();
    // Store the ugi in transport and then continue as usual.
    ugiTrans.setClientUGI(UserGroupInformation.createRemoteUser(principals.remove(principals.size()-1)));
    oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.REPLY, msg.seqid));
    result.write(oprot);
    oprot.writeMessageEnd();
    oprot.getTransport().flush();
  }
TUGIBasedProcessor is used in unsecure mode for thrift metastore client server communication.
 This processor checks whether the first rpc call after connection is set up is set_ugi()
 through which client sends ugi to server. Processor then perform all subsequent rpcs on the
 connection using ugi.doAs() so all actions are performed in client user context.
 Note that old clients will never call set_ugi() and thus ugi will never be received on server
 side, in which case server exhibits previous behavior and continues as usual.
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/TUGIBasedProcessor.java