* 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
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
package com.datatorrent.common.security.auth.callback;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.RealmCallback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.datatorrent.api.Component;
import com.datatorrent.common.security.SecurityContext;

 * The default callback handler handles common callbacks and can be used with most common
 * JAAS login modules. This handler can be extended to create custom callback handlers if
 * needed. When developing a custom handler in this fashion most of time you would need to
 * just override the processCallback method, handle the custom callback and delegate to the
 * base class for the common callbacks.
 * This handler implements Component to interface to receive the login information via the
 * SecurityContext. Custom handlers being implemented from scratch without extending this
 * handler can also extend Component to receive security information.
 * @since 2.0.0
public class DefaultCallbackHandler implements CallbackHandler, Component<SecurityContext>
  private static final Logger logger = LoggerFactory.getLogger(DefaultCallbackHandler.class);

  protected SecurityContext context;

  public void setup(SecurityContext context)
    this.context = context;

  public void teardown()


  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
    for (Callback callback : callbacks) {

  protected void processCallback(Callback callback) throws IOException, UnsupportedCallbackException
    if (callback instanceof NameCallback) {
      NameCallback namecb = (NameCallback)callback;
    } else if (callback instanceof PasswordCallback) {
      PasswordCallback passcb = (PasswordCallback)callback;
    } else if (callback instanceof RealmCallback) {
      RealmCallback realmcb = (RealmCallback)callback;
    } else if (callback instanceof TextOutputCallback) {
      TextOutputCallback textcb = (TextOutputCallback)callback;
      if (textcb.getMessageType() == TextOutputCallback.INFORMATION) {
      } else if (textcb.getMessageType() == TextOutputCallback.WARNING) {
      } else if (textcb.getMessageType() == TextOutputCallback.ERROR) {
      } else {
        logger.debug("Auth message type {}, message {}", textcb.getMessageType(), textcb.getMessage());
    } else {
      throw new UnsupportedCallbackException(callback);