Search in sources :

Example 1 with EJBAccessException

use of jakarta.ejb.EJBAccessException in project tomee by apache.

the class TestSecurityTemplateInvocationContextProvider method invocationContext.

private TestTemplateInvocationContext invocationContext(String role, boolean authorized) {
    return new TestTemplateInvocationContext() {

        private boolean ejbAccessThrown = false;

        @Override
        public String getDisplayName(int invocationIndex) {
            if (role.equals(TestSecurity.UNAUTHENTICATED)) {
                return "unauthenticated";
            }
            return role;
        }

        @Override
        public List<Extension> getAdditionalExtensions() {
            List<Extension> extensions = new ArrayList<>();
            extensions.add(new TestExecutionExceptionHandler() {

                @Override
                public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable {
                    if (!authorized) {
                        if (throwable instanceof EJBAccessException) {
                            // ok - this would be expected here, do not fail the test!
                            ejbAccessThrown = true;
                            return;
                        } else {
                            throw throwable;
                        }
                    }
                    throw throwable;
                }
            });
            extensions.add(new AfterEachCallback() {

                @Override
                public void afterEach(ExtensionContext extensionContext) throws Exception {
                    if (!authorized) {
                        if (!ejbAccessThrown) {
                            throw new RuntimeException("Expected 'EJBAccessException' but caught none.");
                        }
                    }
                }
            });
            extensions.add(new OpenEjbExtension(role));
            return extensions;
        }
    };
}
Also used : OpenEjbExtension(org.apache.openejb.junit5.OpenEjbExtension) ArrayList(java.util.ArrayList) EJBAccessException(jakarta.ejb.EJBAccessException) EJBAccessException(jakarta.ejb.EJBAccessException) OpenEjbExtension(org.apache.openejb.junit5.OpenEjbExtension)

Example 2 with EJBAccessException

use of jakarta.ejb.EJBAccessException in project tomee by apache.

the class SingletonContainer method invoke.

@Override
public Object invoke(final Object deployID, InterfaceType type, final Class callInterface, final Method callMethod, final Object[] args, final Object primKey) throws OpenEJBException {
    final BeanContext beanContext = this.getBeanContext(deployID);
    if (beanContext == null) {
        throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
    }
    // Use the backup way to determine call type if null was supplied.
    if (type == null) {
        type = beanContext.getInterfaceType(callInterface);
    }
    final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    final CurrentCreationalContext currentCreationalContext = beanContext.get(CurrentCreationalContext.class);
    Object runAs = null;
    try {
        if (oldCallContext != null) {
            final BeanContext oldBc = oldCallContext.getBeanContext();
            if (oldBc.getRunAsUser() != null || oldBc.getRunAs() != null) {
                runAs = AbstractSecurityService.class.cast(securityService).overrideWithRunAsContext(callContext, beanContext, oldBc);
            }
        }
        final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type);
        if (!authorized) {
            throw new org.apache.openejb.ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
        }
        final Class declaringClass = callMethod.getDeclaringClass();
        if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
            if (callMethod.getName().startsWith("create")) {
                return createEJBObject(beanContext, callMethod);
            } else {
                // EJBHome.remove( ) and other EJBHome methods are not process by the container
                return null;
            }
        } else if (EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) {
            // EJBObject.remove( ) and other EJBObject methods are not process by the container
            return null;
        }
        final Instance instance = instanceManager.getInstance(callContext);
        callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
        callContext.setCurrentAllowedStates(null);
        callContext.set(Method.class, runMethod);
        callContext.setInvokedInterface(callInterface);
        if (currentCreationalContext != null) {
            // noinspection unchecked
            currentCreationalContext.set(instance.creationalContext);
        }
        return _invoke(callMethod, runMethod, args, instance, callContext, type);
    } finally {
        if (runAs != null) {
            try {
                securityService.associate(runAs);
            } catch (final LoginException e) {
            // no-op
            }
        }
        ThreadContext.exit(oldCallContext);
        if (currentCreationalContext != null) {
            currentCreationalContext.remove();
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) EJBHome(jakarta.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) Method(java.lang.reflect.Method) EJBAccessException(jakarta.ejb.EJBAccessException) EJBLocalHome(jakarta.ejb.EJBLocalHome) CurrentCreationalContext(org.apache.openejb.cdi.CurrentCreationalContext) BeanContext(org.apache.openejb.BeanContext) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) LoginException(javax.security.auth.login.LoginException) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject)

Example 3 with EJBAccessException

use of jakarta.ejb.EJBAccessException in project tomee by apache.

the class EjbHomeProxyHandler method _invoke.

@Override
protected Object _invoke(final Object proxy, final Class interfce, final Method method, final Object[] args) throws Throwable {
    final String methodName = method.getName();
    if (logger.isDebugEnabled()) {
        logger.debug("EjbHomeProxyHandler: invoking method " + methodName + " on " + deploymentID);
    }
    try {
        final Object retValue;
        final MethodType operation = dispatchTable.get(methodName);
        if (operation == null) {
            retValue = homeMethod(interfce, method, args, proxy);
        } else {
            switch(operation) {
                /*-- CREATE ------------- <HomeInterface>.create(<x>) ---*/
                case CREATE:
                    retValue = create(interfce, method, args, proxy);
                    break;
                case FIND:
                    retValue = findX(interfce, method, args, proxy);
                    break;
                /*-- GET EJB METADATA ------ EJBHome.getEJBMetaData() ---*/
                case META_DATA:
                    retValue = getEJBMetaData(method, args, proxy);
                    break;
                /*-- GET HOME HANDLE -------- EJBHome.getHomeHandle() ---*/
                case HOME_HANDLE:
                    retValue = getHomeHandle(method, args, proxy);
                    break;
                /*-- REMOVE ------------------------ EJBHome.remove() ---*/
                case REMOVE:
                    {
                        final Class type = method.getParameterTypes()[0];
                        /*-- HANDLE ------- EJBHome.remove(Handle handle) ---*/
                        if (Handle.class.isAssignableFrom(type)) {
                            retValue = removeWithHandle(interfce, method, args, proxy);
                        } else {
                            /*-- PRIMARY KEY ----- EJBHome.remove(Object key) ---*/
                            retValue = removeByPrimaryKey(interfce, method, args, proxy);
                        }
                        break;
                    }
                default:
                    throw new OpenEJBRuntimeException("Inconsistent internal state: value " + operation + " for operation " + methodName);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("EjbHomeProxyHandler: finished invoking method " + method.getName() + ". Return value:" + retValue);
        }
        return retValue;
    /*
            * The ire is thrown by the container system and propagated by
            * the server to the stub.
            */
    } catch (final RemoteException re) {
        if (interfaceType.isLocal()) {
            throw new EJBException(re.getMessage()).initCause(re.detail);
        } else {
            throw re;
        }
    } catch (final InvalidateReferenceException ire) {
        Throwable cause = ire.getRootCause();
        if (cause instanceof RemoteException && interfaceType.isLocal()) {
            final RemoteException re = (RemoteException) cause;
            final Throwable detail = re.detail != null ? re.detail : re;
            cause = new EJBException(re.getMessage()).initCause(detail);
        }
        throw cause;
    /*
            * Application exceptions must be reported dirctly to the client. They
            * do not impact the viability of the proxy.
            */
    } catch (final ApplicationException ae) {
        final Throwable exc = ae.getRootCause() != null ? ae.getRootCause() : ae;
        if (exc instanceof EJBAccessException) {
            if (interfaceType.isBusiness()) {
                throw exc;
            } else {
                if (interfaceType.isLocal()) {
                    throw (AccessLocalException) new AccessLocalException(exc.getMessage()).initCause(exc);
                } else {
                    try {
                        throw new AccessException(exc.getMessage()).initCause(exc);
                    } catch (final IllegalStateException vmbug) {
                        // bug affects using initCause on any RemoteException subclasses in Sun 1.5_07 or lower
                        throw new AccessException(exc.getMessage(), (Exception) exc);
                    }
                }
            }
        }
        throw exc;
    /*
            * A system exception would be highly unusual and would indicate a sever
            * problem with the container system.
            */
    } catch (final SystemException se) {
        if (interfaceType.isLocal()) {
            throw new EJBException("Container has suffered a SystemException").initCause(se.getRootCause());
        } else {
            throw new RemoteException("Container has suffered a SystemException", se.getRootCause());
        }
    } catch (final OpenEJBException oe) {
        if (interfaceType.isLocal()) {
            throw new EJBException("Unknown Container Exception").initCause(oe.getRootCause());
        } else {
            throw new RemoteException("Unknown Container Exception", oe.getRootCause());
        }
    } catch (final Throwable t) {
        logger.debug("EjbHomeProxyHandler: finished invoking method " + method.getName() + " with exception:" + t, t);
        throw t;
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) AccessLocalException(jakarta.ejb.AccessLocalException) EJBAccessException(jakarta.ejb.EJBAccessException) Handle(jakarta.ejb.Handle) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) AccessException(java.rmi.AccessException) EJBAccessException(jakarta.ejb.EJBAccessException) SystemException(org.apache.openejb.SystemException) RemoteException(java.rmi.RemoteException) EJBException(jakarta.ejb.EJBException) OpenEJBException(org.apache.openejb.OpenEJBException)

Example 4 with EJBAccessException

use of jakarta.ejb.EJBAccessException in project tomee by apache.

the class EjbObjectProxyHandler method _invoke.

@Override
public Object _invoke(final Object p, final Class interfce, final Method m, final Object[] a) throws Throwable {
    Object retValue = null;
    Throwable exc = null;
    final String methodName = m.getName();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("EjbObjectProxyHandler: invoking method " + methodName + " on " + deploymentID + " with identity " + primaryKey);
        }
        Integer operation = dispatchTable.get(methodName);
        if (operation != null) {
            if (operation == 3) {
                if (m.getParameterTypes()[0] != EJBObject.class && m.getParameterTypes()[0] != EJBLocalObject.class) {
                    operation = null;
                }
            } else {
                operation = m.getParameterTypes().length == 0 ? operation : null;
            }
        }
        if (operation == null || !interfaceType.isComponent()) {
            retValue = businessMethod(interfce, m, a, p);
        } else {
            switch(operation) {
                case 1:
                    retValue = getHandle(m, a, p);
                    break;
                case 2:
                    retValue = getPrimaryKey(m, a, p);
                    break;
                case 3:
                    retValue = isIdentical(m, a, p);
                    break;
                case 4:
                    retValue = remove(interfce, m, a, p);
                    break;
                case 5:
                    retValue = getEJBHome(m, a, p);
                    break;
                case 6:
                    retValue = getEJBLocalHome(m, a, p);
                    break;
                default:
                    throw new OpenEJBRuntimeException("Inconsistent internal state");
            }
        }
        return retValue;
    /*
            * The ire is thrown by the container system and propagated by
            * the server to the stub.
            */
    } catch (final InvalidateReferenceException ire) {
        invalidateAllHandlers(getRegistryId());
        exc = ire.getRootCause() != null ? ire.getRootCause() : new RemoteException("InvalidateReferenceException: " + ire);
        throw exc;
    /*
            * Application exceptions must be reported dirctly to the client. They
            * do not impact the viability of the proxy.
            */
    } catch (final ApplicationException ae) {
        exc = ae.getRootCause() != null ? ae.getRootCause() : ae;
        if (exc instanceof EJBAccessException) {
            if (interfaceType.isBusiness()) {
                throw exc;
            } else {
                if (interfaceType.isLocal()) {
                    throw new AccessLocalException(exc.getMessage()).initCause(exc.getCause());
                } else {
                    throw new AccessException(exc.getMessage());
                }
            }
        }
        throw exc;
    /*
            * A system exception would be highly unusual and would indicate a sever
            * problem with the container system.
            */
    } catch (final SystemException se) {
        invalidateReference();
        exc = se.getRootCause() != null ? se.getRootCause() : se;
        logger.debug("The container received an unexpected exception: ", exc);
        throw new RemoteException("Container has suffered a SystemException", exc);
    } catch (final OpenEJBException oe) {
        exc = oe.getRootCause() != null ? oe.getRootCause() : oe;
        logger.debug("The container received an unexpected exception: ", exc);
        throw new RemoteException("Unknown Container Exception", oe.getRootCause());
    } finally {
        if (logger.isDebugEnabled()) {
            if (exc == null) {
                String ret = "void";
                if (null != retValue) {
                    try {
                        ret = retValue.toString();
                    } catch (final Exception e) {
                        ret = "toString() failed on (" + e.getMessage() + ")";
                    }
                }
                logger.debug("EjbObjectProxyHandler: finished invoking method " + methodName + ". Return value:" + ret);
            } else {
                logger.debug("EjbObjectProxyHandler: finished invoking method " + methodName + " with exception " + exc);
            }
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) AccessLocalException(jakarta.ejb.AccessLocalException) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBAccessException(jakarta.ejb.EJBAccessException) AccessException(java.rmi.AccessException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(jakarta.ejb.EJBAccessException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) ObjectStreamException(java.io.ObjectStreamException) SystemException(org.apache.openejb.SystemException) AccessLocalException(jakarta.ejb.AccessLocalException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) AccessException(java.rmi.AccessException) EJBAccessException(jakarta.ejb.EJBAccessException) SystemException(org.apache.openejb.SystemException) EJBObject(jakarta.ejb.EJBObject) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject) RemoteException(java.rmi.RemoteException)

Example 5 with EJBAccessException

use of jakarta.ejb.EJBAccessException in project tomee by apache.

the class EntityContainer method invoke.

@Override
public Object invoke(final Object deployID, InterfaceType type, final Class callInterface, final Method callMethod, final Object[] args, final Object primKey) throws OpenEJBException {
    final BeanContext beanContext = this.getBeanContext(deployID);
    if (beanContext == null) {
        throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
    }
    // Use the backup way to determine call type if null was supplied.
    if (type == null) {
        type = beanContext.getInterfaceType(callInterface);
    }
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type);
        if (!authorized) {
            throw new ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
        }
        final Class declaringClass = callMethod.getDeclaringClass();
        final String methodName = callMethod.getName();
        if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
            if (declaringClass != EJBHome.class && declaringClass != EJBLocalHome.class) {
                if (methodName.startsWith("create")) {
                    return createEJBObject(callMethod, args, callContext, type);
                } else if (methodName.startsWith("find")) {
                    return findMethod(callMethod, args, callContext, type);
                } else {
                    return homeMethod(callMethod, args, callContext, type);
                }
            } else if (methodName.equals("remove")) {
                removeEJBObject(callMethod, args, callContext, type);
                return null;
            }
        } else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) {
            removeEJBObject(callMethod, args, callContext, type);
            return null;
        }
        callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
        final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
        callContext.set(Method.class, runMethod);
        return invoke(type, callMethod, runMethod, args, callContext);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) EJBHome(jakarta.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) Method(java.lang.reflect.Method) EJBAccessException(jakarta.ejb.EJBAccessException) EJBLocalHome(jakarta.ejb.EJBLocalHome)

Aggregations

EJBAccessException (jakarta.ejb.EJBAccessException)11 OpenEJBException (org.apache.openejb.OpenEJBException)5 ApplicationException (org.apache.openejb.ApplicationException)4 EJBHome (jakarta.ejb.EJBHome)3 EJBLocalHome (jakarta.ejb.EJBLocalHome)3 EJBLocalObject (jakarta.ejb.EJBLocalObject)3 EJBObject (jakarta.ejb.EJBObject)3 Method (java.lang.reflect.Method)3 BeanContext (org.apache.openejb.BeanContext)3 ThreadContext (org.apache.openejb.core.ThreadContext)3 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)3 AccessLocalException (jakarta.ejb.AccessLocalException)2 ServletOutputStream (jakarta.servlet.ServletOutputStream)2 AccessException (java.rmi.AccessException)2 RemoteException (java.rmi.RemoteException)2 Principal (java.security.Principal)2 InitialContext (javax.naming.InitialContext)2 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 SystemException (org.apache.openejb.SystemException)2