home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java EE 6 example source code file (MySecurityContext.java)

This example Java EE 6 source code file (MySecurityContext.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Java EE 6 tags/keywords

callback, callerprincipalcallback, io, log, logging, mysecuritycontext, mysecuritycontext, password, passwordvalidationcallback, principalimpl, sc, sc, string, string, stringbuffer, subject, subject, util

The Java EE 6 MySecurityContext.java source code

package samples.connectors.mailconnector.ra.inbound;

import org.glassfish.security.common.PrincipalImpl;

import javax.security.auth.Subject;
import javax.security.auth.message.callback.CallerPrincipalCallback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.resource.spi.work.SecurityContext;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.util.logging.Logger;
import javax.security.auth.message.callback.PasswordValidationCallback;

/**
 * @author jagadish
 */
public class MySecurityContext extends SecurityContext {
    private String userName;
    private String password;
    private String principalName;
    private Subject subject;

        static Logger logger =
        Logger.getLogger("samples.connectors.mailconnector.ra.inbound");

    public MySecurityContext(String userName, String password, String principalName){
        this.userName = userName;
        this.password = password;
        this.principalName = principalName;
        logger.fine("[MySecurityContext] constructor");
    }


    public void setupSecurityContext(CallbackHandler callbackHandler, Subject execSubject, Subject serviceSubject) {

        execSubject.getPrincipals().add(new PrincipalImpl(principalName));
        List<Callback> callbacks = new ArrayList();

        CallerPrincipalCallback cpc = new CallerPrincipalCallback(execSubject, new PrincipalImpl(principalName));

        logger.fine("[SC] setting caller principal callback with principal : " + principalName);
        callbacks.add(cpc);

        PasswordValidationCallback pvc = null;
        pvc = new PasswordValidationCallback(execSubject, userName,
                password.toCharArray());
        logger.fine("[SC] setting password validation callback with user [ " + userName + " ] + password [ " + password + " ]");

        callbacks.add(pvc);

        Callback callbackArray[] = new Callback[callbacks.size()];
        try{
            callbackHandler.handle(callbacks.toArray(callbackArray));

        }catch(UnsupportedCallbackException e){
            debug("exception occured : " + e.getMessage());
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
            debug("exception occured : " + e.getMessage());
        }


        if(!pvc.getResult()){
            logger.info("[SC] Password validation callback failure for user : " + userName);
            throw new IllegalStateException("User [ " + userName + " ] not authorized to send message");
        }else{
            logger.info("[SC] Password validation callback succeded for user : " + userName);
        }
    }

    public String toString(){
        StringBuffer toString = new StringBuffer("{");
        toString.append("userName : " + userName);
        toString.append(", password : " + password);
        toString.append(", principalName : " + principalName);
        toString.append("}");
        return toString.toString();
    }

    public void debug(String message){
        logger.fine("[SC]: " + message);
    }
}

Other Java EE 6 examples (source code examples)

Here is a short list of links related to this Java EE 6 MySecurityContext.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.