Search in sources :

Example 1 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() throws Exception {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {

        @Override
        public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
            assertTrue(invocation instanceof TestRemoteInvocation);
            assertNull(invocation.getAttributes());
            assertNull(invocation.getAttribute("myKey"));
            return super.invoke(invocation, targetObject);
        }
    });
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {

        @Override
        public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
            RemoteInvocation invocation = new TestRemoteInvocation(methodInvocation);
            assertNull(invocation.getAttributes());
            assertNull(invocation.getAttribute("myKey"));
            return invocation;
        }
    });
    pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {

        @Override
        protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
            assertEquals("http://myurl", config.getServiceUrl());
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
            request.setContent(baos.toByteArray());
            exporter.handleRequest(request, response);
            return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) DefaultRemoteInvocationExecutor(org.springframework.remoting.support.DefaultRemoteInvocationExecutor) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteInvocationFactory(org.springframework.remoting.support.RemoteInvocationFactory) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.

the class RmiSupportTests method remoteInvocation.

@Test
public void remoteInvocation() throws NoSuchMethodException {
    // let's see if the remote invocation object works
    final RemoteBean rb = new RemoteBean();
    final Method setNameMethod = rb.getClass().getDeclaredMethod("setName", String.class);
    MethodInvocation mi = new MethodInvocation() {

        @Override
        public Method getMethod() {
            return setNameMethod;
        }

        @Override
        public Object[] getArguments() {
            return new Object[] { "bla" };
        }

        @Override
        public Object proceed() throws Throwable {
            throw new UnsupportedOperationException();
        }

        @Override
        public Object getThis() {
            return rb;
        }

        @Override
        public AccessibleObject getStaticPart() {
            return setNameMethod;
        }
    };
    RemoteInvocation inv = new RemoteInvocation(mi);
    assertEquals("setName", inv.getMethodName());
    assertEquals("bla", inv.getArguments()[0]);
    assertEquals(String.class, inv.getParameterTypes()[0]);
    // this is a bit BS, but we need to test it
    inv = new RemoteInvocation();
    inv.setArguments(new Object[] { "bla" });
    assertEquals("bla", inv.getArguments()[0]);
    inv.setMethodName("setName");
    assertEquals("setName", inv.getMethodName());
    inv.setParameterTypes(new Class<?>[] { String.class });
    assertEquals(String.class, inv.getParameterTypes()[0]);
    inv = new RemoteInvocation("setName", new Class<?>[] { String.class }, new Object[] { "bla" });
    assertEquals("bla", inv.getArguments()[0]);
    assertEquals("setName", inv.getMethodName());
    assertEquals(String.class, inv.getParameterTypes()[0]);
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 3 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class TestVoter method vote.

public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> config) {
    MethodInvocation mi = (MethodInvocation) object;
    mi.getMethod().getParameterAnnotations();
    return ACCESS_GRANTED;
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation)

Example 4 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security-oauth by spring-projects.

the class OAuth2MethodSecurityExpressionHandlerTests method testScopes.

@Test
public void testScopes() throws Exception {
    OAuth2Request clientAuthentication = RequestTokenFactory.createOAuth2Request("foo", false, Collections.singleton("read"));
    Authentication userAuthentication = null;
    OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
    MethodInvocation invocation = new SimpleMethodInvocation(this, ReflectionUtils.findMethod(getClass(), "testOauthClient"));
    EvaluationContext context = handler.createEvaluationContext(oAuth2Authentication, invocation);
    Expression expression = handler.getExpressionParser().parseExpression("#oauth2.hasAnyScope('read','write')");
    assertTrue((Boolean) expression.getValue(context));
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) Expression(org.springframework.expression.Expression) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Example 5 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security-oauth by spring-projects.

the class OAuth2MethodSecurityExpressionHandlerTests method testNonOauthClient.

@Test
public void testNonOauthClient() throws Exception {
    Authentication clientAuthentication = new UsernamePasswordAuthenticationToken("foo", "bar");
    MethodInvocation invocation = new SimpleMethodInvocation(this, ReflectionUtils.findMethod(getClass(), "testNonOauthClient"));
    EvaluationContext context = handler.createEvaluationContext(clientAuthentication, invocation);
    Expression expression = handler.getExpressionParser().parseExpression("#oauth2.clientHasAnyRole()");
    assertFalse((Boolean) expression.getValue(context));
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) Expression(org.springframework.expression.Expression) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)118 Test (org.junit.jupiter.api.Test)50 Test (org.junit.Test)35 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)26 Method (java.lang.reflect.Method)22 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 ArrayList (java.util.ArrayList)11 Log (org.apache.commons.logging.Log)11 Authentication (org.springframework.security.core.Authentication)10 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 List (java.util.List)7 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 MyThrowsHandler (org.springframework.aop.testfixture.advice.MyThrowsHandler)7 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 Promise (ratpack.exec.Promise)5