FileDocCategorySizeDatePackage
PipeHelper.javaAPI DocGlassfish v2 API18667Tue Jul 24 05:01:34 BST 2007com.sun.enterprise.security.jmac.config

PipeHelper

public class PipeHelper extends ConfigHelper

Fields Summary
private static com.sun.enterprise.security.audit.AuditManager
auditManager
protected static final com.sun.enterprise.util.LocalStringManagerImpl
localStrings
private boolean
isEjbEndpoint
private com.sun.xml.ws.api.model.SEIModel
seiModel
private com.sun.xml.ws.api.SOAPVersion
soapVersion
Constructors Summary
public PipeHelper(String layer, Map map, CallbackHandler cbh)

    
           
        init(layer, getAppCtxt(map), map, cbh);

	this.isEjbEndpoint = processSunDeploymentDescriptor();
	this.seiModel = (SEIModel) map.get(PipeConstants.SEI_MODEL);
        WSBinding binding = (WSBinding)map.get(PipeConstants.BINDING);
        if (binding == null) {
            WSEndpoint endPoint = (WSEndpoint)map.get(PipeConstants.ENDPOINT);
            if (endPoint != null) {
                binding = endPoint.getBinding();
            }
        }
        this.soapVersion = (binding != null) ? binding.getSOAPVersion(): SOAPVersion.SOAP_11;
   
Methods Summary
private static voidaddModel(javax.security.auth.message.MessageInfo info, java.util.Map map)

        Object model = map.get(PipeConstants.WSDL_MODEL);
        if (model != null) {
            info.getMap().put(PipeConstants.WSDL_MODEL,model);
        }
    
public voidauditInvocation(com.sun.xml.ws.api.message.Packet request, javax.security.auth.message.AuthStatus status)


	if (auditManager.isAuditOn()) {

	    String uri = null;
	    if (!isEjbEndpoint && request != null &&
                    request.supports(MessageContext.SERVLET_REQUEST)) {
                HttpServletRequest httpServletRequest =
                    (HttpServletRequest)request.get(
                    MessageContext.SERVLET_REQUEST);
                uri = httpServletRequest.getRequestURI().toString();
	    } 
	    
            String endpointName = null;
            if (map != null) {
                WebServiceEndpoint endpoint = (WebServiceEndpoint)
                       map.get(PipeConstants.SERVICE_ENDPOINT);
                if (endpoint != null) {
                    endpointName = endpoint.getEndpointName();
                }
            }
            if (endpointName == null) {
                endpointName = "(no endpoint)";
            }
            
            if (isEjbEndpoint) {
                auditManager.ejbAsWebServiceInvocation(
                    endpointName, AuthStatus.SUCCESS.equals(status));
            } else {
                auditManager.webServiceInvocation(
                    ((uri==null) ? "(no uri)" : uri), 
                    endpointName, AuthStatus.SUCCESS.equals(status));
            }
	}
    
public voidauthorize(com.sun.xml.ws.api.message.Packet request)


	// SecurityContext constructor should set initiator to
	// unathenticated if Subject is null or empty

	Subject s = (Subject) request.invocationProperties.get
	    (PipeConstants.CLIENT_SUBJECT);

	SecurityContext sC = new SecurityContext(s);

	SecurityContext.setCurrent(sC);

	// we should try to replace this endpoint specific
	// authorization check with a generic web service message check
	// and move the endpoint specific check down stream
	if (isEjbEndpoint) {

	    Switch theSwitch = Switch.getSwitch();
	    InvocationManager invManager= theSwitch.getInvocationManager();
	    Invocation inv= (Invocation) invManager.getCurrentInvocation();
            // one need to copy message here, otherwise the message may be
            // consumed
            inv.setMessage(request.getMessage().copy());
	    Exception ie = null;
            Method m = null;
            if (seiModel != null) {
	        JavaMethod jm = request.getMessage().getMethod(seiModel);
	        m = (jm != null) ? jm.getMethod() : null;
            } else { // WebServiceProvider
               WebServiceEndpoint endpoint = (WebServiceEndpoint)
                   map.get(PipeConstants.SERVICE_ENDPOINT);
               EjbDescriptor ejbDescriptor = endpoint.getEjbComponentImpl();
               if (ejbDescriptor != null) {
                   final String ejbImplClassName = ejbDescriptor.getEjbImplClassName();
                   if (ejbImplClassName != null) {
                       try {
                           m = (Method)AppservAccessController.doPrivileged
                               ( new PrivilegedExceptionAction() {
                                   public Object run() throws Exception {
                                       ClassLoader loader =
                                           Thread.currentThread().getContextClassLoader();
                                       Class clazz =
                                           Class.forName(ejbImplClassName, true, loader);
                                       return clazz.getMethod("invoke",
                                               new Class[] { Object.class });
                                  }
                           });
                       } catch(PrivilegedActionException pae) {
                           throw new RuntimeException(pae.getException());
                       }
                   }
               }

            }
	    
	    if (m != null) {

		Container container = (Container) inv.container;

		try {
		    inv.method = m;
		    if ( !container.authorize(inv) ) {

			ie = new Exception
			    (localStrings.getLocalString
			     ("enterprise.webservice.methodNotAuth",
			      "Client not authorized for invocation of {0}", 
			      new Object[] { inv.method }) );
		    } else {
			// Record the method on which the successful
			// authorization check was performed. 
			inv.setWebServiceMethod(inv.method);
		    }
		} catch(Exception e) {
		    String errorMsg = localStrings.getLocalString
			( "enterprise.webservice.errorUnMarshalMethod",
			  "Error unmarshalling method for ejb {0}", 
			  new Object[] { ejbName() });
		    ie = new UnmarshalException(errorMsg); 
		    ie.initCause(e);
		} 
		
		if ( ie != null ) {
		    inv.exception = ie;
		    throw ie;
		} 

	    } else {
		inv.setWebServiceMethod(null);
	    }
	}
            
	return;
    
public voiddisable()

	listenerWrapper.disableWithRefCount();
    
private java.lang.StringejbName()

 
	WebServiceEndpoint wSE = (WebServiceEndpoint) 
	    getProperty(PipeConstants.SERVICE_ENDPOINT);
	return (wSE == null ? "unknown" : wSE.getEjbComponentImpl().getName());
    
private static java.lang.StringgetAppCtxt(java.util.Map map)


        String rvalue = null;

        WebServiceEndpoint wse = 
            (WebServiceEndpoint) map.get(PipeConstants.SERVICE_ENDPOINT);

        // endpoint
        if (wse != null) {

            rvalue = getServerName(wse) + " " + getEndpointURI(wse);

        // client reference
        } else {
            
            ServiceReferenceDescriptor srd = (ServiceReferenceDescriptor) 
                map.get(PipeConstants.SERVICE_REF);

            rvalue = getClientModuleID(srd) + " " + getRefName(srd,map);

        }

        return rvalue;
    
public javax.security.auth.message.config.ClientAuthContextgetClientAuthContext(javax.security.auth.message.MessageInfo info, javax.security.auth.Subject s)

	ClientAuthConfig c = (ClientAuthConfig)getAuthConfig(false);
	if (c != null) {
            addModel(info, map);
	    return c.getAuthContext(c.getAuthContextID(info),s,map);
	}
	return null;
    
private static java.lang.StringgetClientModuleID(com.sun.enterprise.deployment.ServiceReferenceDescriptor srd)


        String rvalue = "#default-client-context#";

        if (srd != null) {
            ModuleDescriptor md = null;
            BundleDescriptor bd = (BundleDescriptor) srd.getBundleDescriptor();

            if (bd != null) {
                md = bd.getModuleDescriptor();
            }

            Application a = bd.getApplication();
            if (a != null) {
                if (a.isVirtual()) {
                    rvalue = a.getRegistrationName();
                } else if (md != null) {
                    rvalue = FileUtils.makeFriendlyFileName(md.getArchiveUri());
                } 
            } else if (md != null) {
                rvalue = FileUtils.makeFriendlyFileName(md.getArchiveUri());
            }
        } 

        return rvalue;
    
public static javax.security.auth.SubjectgetClientSubject()


	Subject s = null;

	if (Switch.getSwitch().getContainerType() == 
	    Switch.APPCLIENT_CONTAINER) {

	    ClientSecurityContext sc = ClientSecurityContext.getCurrent();
	    if (sc != null) {
		s = sc.getSubject();
	    }

	    if (s == null) {
		s = Subject.getSubject(AccessController.getContext());
	    }

	} else {
	    SecurityContext sc = SecurityContext.getCurrent();
	    if (sc != null && !sc.didServerGenerateCredentials()) {
		// make sure we don't use default unauthenticated subject, 
		// so that module cannot change this important (constant) 
		// subject.
		s = sc.getSubject();
	    }
	}

	if (s == null) {
	    s = new Subject();
	}

	return s;
    
private static java.lang.StringgetEndpointURI(com.sun.enterprise.deployment.WebServiceEndpoint wse)


        String uri = "#default-endpoint-context#";

        if (wse != null) {
            uri = wse.getEndpointAddressUri();
            if (uri != null && (!uri.startsWith("/"))) {
                uri = "/" + uri;
            }

            if (wse.implementedByWebComponent()) {
                WebBundleDescriptor wbd = (WebBundleDescriptor)
                    wse.getBundleDescriptor();
                if (wbd != null) {
                    String contextRoot = wbd.getContextRoot();
                    if (contextRoot != null) {
                        if (!contextRoot.startsWith("/")) {
                            contextRoot = "/" + contextRoot;
                        }
                        uri = contextRoot + uri;
                    }
                }
            }
        }
        return uri;
    
public com.sun.xml.ws.api.message.PacketgetFaultResponse(com.sun.xml.ws.api.message.Packet request, com.sun.xml.ws.api.message.Packet response, java.lang.Throwable t)

	boolean twoWay = true;
	try {
	    twoWay = isTwoWay(true,request);
	} catch (Exception e) {
	    // exception is consumed, and twoWay is assumed
 	} 
	if (twoWay) {
	    return makeFaultResponse(response,t);
 	} else {
	    return new Packet();
	}
    
protected HandlerContextgetHandlerContext(java.util.Map map)

        String realmName = null;
        WebServiceEndpoint wSE = (WebServiceEndpoint)
                map.get(PipeConstants.SERVICE_ENDPOINT);
        if (wSE != null) {
            Application app = wSE.getBundleDescriptor().getApplication();
            if (app != null) {
                realmName = app.getRealm();
            }
            if (realmName == null) {
                realmName = wSE.getRealm();
            }
        }

        final String fRealmName = realmName;
        return new HandlerContext() {
            public String getRealmName() {
                return fRealmName;
            }
        };
    
public java.lang.ObjectgetModelName()

 
 	WSDLPort wsdlModel = (WSDLPort) getProperty(PipeConstants.WSDL_MODEL);
 	return (wsdlModel == null ? "unknown" : wsdlModel.getName());
    
private static java.lang.StringgetRefName(com.sun.enterprise.deployment.ServiceReferenceDescriptor srd, java.util.Map map)


        String name = null;
        if (srd != null) {
            name = srd.getName();
        }
        if (name == null) {
            EndpointAddress ea = 
                (EndpointAddress) map.get(PipeConstants.ENDPOINT_ADDRESS);
            if (ea != null) {
                URL url = ea.getURL();
                if (url != null) {
                    name = url.toString();
                }
            }
        }
        if (name == null) {
            name = "#default-ref-name#";
        }
        return name;
    
public javax.security.auth.message.config.ServerAuthContextgetServerAuthContext(javax.security.auth.message.MessageInfo info, javax.security.auth.Subject s)

	ServerAuthConfig c = (ServerAuthConfig)getAuthConfig(true);
	if (c != null) {
            addModel(info, map);
	    return c.getAuthContext(c.getAuthContextID(info),s,map);
	}
	return null;
    
private static java.lang.StringgetServerName(com.sun.enterprise.deployment.WebServiceEndpoint wse)

        //XXX FIX ME: need to lookup real hostname
        String hostname = "localhost"; 
        return hostname;
    
public voidgetSessionToken(java.util.Map m, javax.security.auth.message.MessageInfo info, javax.security.auth.Subject s)

	ClientAuthConfig c = (ClientAuthConfig) getAuthConfig(false);    
	if (c != null) {
	    m.putAll(map);
            addModel(info, map);
	    c.getAuthContext(c.getAuthContextID(info),s,m);
	}
	return;
    
public booleanisTwoWay(boolean twoWayIsDefault, com.sun.xml.ws.api.message.Packet request)

 
 	boolean twoWay = twoWayIsDefault;
 	Message m = request.getMessage();
 	if (m != null) {
	    WSDLPort wsdlModel =
		(WSDLPort) getProperty(PipeConstants.WSDL_MODEL);
	    if (wsdlModel != null) {
		twoWay = (m.isOneWay(wsdlModel) ? false : true);
	    }
	}
 	return twoWay;
    
public com.sun.xml.ws.api.message.PacketmakeFaultResponse(com.sun.xml.ws.api.message.Packet response, java.lang.Throwable t)

	// wrap throwable in WebServiceException, if necessary
	if (!(t instanceof WebServiceException)) {
	    t = (Throwable) new WebServiceException(t);
	}
 	if (response == null) {
 	    response = new Packet();
  	} 
	// try to create fault in provided response packet, if an exception
	// is thrown, create new packet, and create fault in it.
	try {
	    return response.createResponse(Messages.create(t, this.soapVersion));
	} catch (Exception e) {
	    response = new Packet();
	}
 	return response.createResponse(Messages.create(t, this.soapVersion));
    
private booleanprocessSunDeploymentDescriptor()


	if (factory == null) {
	    return false;
	}

	MessageSecurityBindingDescriptor binding =
	    AuthMessagePolicy.getMessageSecurityBinding
	    (PipeConstants.SOAP_LAYER,map);

	if (binding != null) {
	    if (!hasExactMatchAuthProvider()) {
		String jmacProviderRegisID = factory.registerConfigProvider(
                    new GFServerConfigProvider(null, null),
                    layer, appCtxt,
                    "GF AuthConfigProvider bound by Sun Specific Descriptor");
                this.setJmacProviderRegisID(jmacProviderRegisID);
	    }
	}

	WebServiceEndpoint e = (WebServiceEndpoint)
	    map.get(PipeConstants.SERVICE_ENDPOINT);

	return (e == null ? false : e.implementedByEjbComponent());