org.springframework.aop.framework.ReflectiveMethodInvocation Java Examples

The following examples show how to use org.springframework.aop.framework.ReflectiveMethodInvocation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PostTpicAsyncBeanPostProcessor.java    From tracee with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	if (invocation instanceof ReflectiveMethodInvocation) {
		final ReflectiveMethodInvocation methodInvocation = (ReflectiveMethodInvocation) invocation;
		final Object tpicObj = methodInvocation.getUserAttribute(TraceeConstants.TPIC_HEADER);
		if (tpicObj instanceof Map) {
			@SuppressWarnings("unchecked")
			final Map<? extends String, ? extends String> tpic = (Map<? extends String, ? extends String>) tpicObj;
			backend.putAll(tpic);
		}
	}

	try {
		return invocation.proceed();
	} finally {
		backend.clear();
	}
}
 
Example #2
Source File: ReauthenticatingAdvice.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Object invoke(MethodInvocation mi) throws Throwable 
{
    while (true)
    {
        try
        {
            MethodInvocation clone = ((ReflectiveMethodInvocation)mi).invocableClone();
            return clone.proceed();
        }
        catch (AuthenticationException ae)
        {
            // Sleep for an interval and try again.
            try
            {
                Thread.sleep(fRetryInterval);
            }
            catch (InterruptedException ie)
            {
                // Do nothing.
            }
            try
            {
                // Reauthenticate.
                fAuthService.authenticate(fUser, fPassword.toCharArray());
                String ticket = fAuthService.getCurrentTicket();
                fTicketHolder.setTicket(ticket);
                // Modify the ticket argument.
                mi.getArguments()[0] = ticket;
            }
            catch (Exception e)
            {
                // Do nothing.
            }
        }
    }
}
 
Example #3
Source File: PathAuthorizationAuditListener.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
private void onAuthenticationCredentialsNotFoundEvent(AuthenticationCredentialsNotFoundEvent event) {
    Map<String, Object> data = new HashMap<>();
    data.put("type", event.getCredentialsNotFoundException().getClass().getName());
    data.put("message", event.getCredentialsNotFoundException().getMessage());
    if (event.getSource() instanceof FilterInvocation)
        data.put("requestUrl", ((FilterInvocation)event.getSource()).getRequestUrl());
    else if (event.getSource() instanceof ReflectiveMethodInvocation)
        data.put("source", event.getSource());
    publish(new AuditEvent("<unknown>", AuthenticationAuditListener.AUTHENTICATION_FAILURE, data));
}
 
Example #4
Source File: PathAuthorizationAuditListener.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
private void onAuthorizationFailureEvent(AuthorizationFailureEvent event) {
    Map<String, Object> data = new HashMap<>();
    data.put("authorities", event.getAuthentication().getAuthorities());
    data.put("type", event.getAccessDeniedException().getClass().getName());
    data.put("message", event.getAccessDeniedException().getMessage());
    if (event.getSource() instanceof FilterInvocation)
        data.put("requestUrl", ((FilterInvocation)event.getSource()).getRequestUrl());
    else if (event.getSource() instanceof ReflectiveMethodInvocation)
        data.put("source", event.getSource());
    if (event.getAuthentication().getDetails() != null) {
        data.put("details", event.getAuthentication().getDetails());
    }

    publish(new AuditEvent(event.getAuthentication().getName(), AuthorizationAuditListener.AUTHORIZATION_FAILURE, data));
}
 
Example #5
Source File: PreTpicAsyncBeanPostProcessor.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	if (invocation instanceof ReflectiveMethodInvocation) {
		final Map<String, String> tpic = backend.copyToMap();
		((ReflectiveMethodInvocation) invocation).setUserAttribute(TraceeConstants.TPIC_HEADER, tpic);
	}

	return invocation.proceed();
}
 
Example #6
Source File: PreTpicAsyncBeanPostProcessorTest.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldStoreTpicToInvocationMetadata() throws Throwable {
	final DelegateTpicToAsyncInterceptor interceptor = new DelegateTpicToAsyncInterceptor(mock(Executor.class), backend);
	final ReflectiveMethodInvocation mockedInvocation = mock(ReflectiveMethodInvocation.class);
	final Map<String, String> tpic = new HashMap<>();
	tpic.put("myInvoc", "storeThisToAsync");
	backend.putAll(tpic);
	interceptor.invoke(mockedInvocation);
	verify(mockedInvocation).setUserAttribute(eq(TraceeConstants.TPIC_HEADER), eq(tpic));
}
 
Example #7
Source File: PostTpicAsyncBeanPostProcessorTest.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldRestoreTpicFromMetadataWhenSet() throws Throwable {
	final PostTpicAsyncBeanPostProcessor.DelegateTpicToThreadInterceptor interceptor = new PostTpicAsyncBeanPostProcessor.DelegateTpicToThreadInterceptor(mock(Executor.class), backend);
	final ReflectiveMethodInvocation mockedInvocation = mock(ReflectiveMethodInvocation.class);
	final Map<String, String> tpic = new HashMap<>();
	tpic.put("myInvoc", "storeThisToAsync");
	when(mockedInvocation.getUserAttribute(TraceeConstants.TPIC_HEADER)).thenReturn(tpic);

	interceptor.invoke(mockedInvocation);
	verify(mockedInvocation).proceed();
	verify(backend).putAll(eq(tpic));
}
 
Example #8
Source File: PostTpicAsyncBeanPostProcessorTest.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldSkipTpicRestoreFromMetadataWhenMetadataIsNull() throws Throwable {
	final PostTpicAsyncBeanPostProcessor.DelegateTpicToThreadInterceptor interceptor = new PostTpicAsyncBeanPostProcessor.DelegateTpicToThreadInterceptor(mock(Executor.class), backend);
	final ReflectiveMethodInvocation mockedInvocation = mock(ReflectiveMethodInvocation.class);
	when(mockedInvocation.getUserAttribute(TraceeConstants.TPIC_HEADER)).thenReturn(null);

	interceptor.invoke(mockedInvocation);
	verify(mockedInvocation).proceed();
	verify(backend, never()).putAll(anyMapOf(String.class, String.class));
}
 
Example #9
Source File: PostTpicAsyncBeanPostProcessorTest.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldClearTpicAfterInvocation() throws Throwable {
	final PostTpicAsyncBeanPostProcessor.DelegateTpicToThreadInterceptor interceptor = new PostTpicAsyncBeanPostProcessor.DelegateTpicToThreadInterceptor(mock(Executor.class), backend);
	final ReflectiveMethodInvocation mockedInvocation = mock(ReflectiveMethodInvocation.class);
	when(mockedInvocation.getUserAttribute(TraceeConstants.TPIC_HEADER)).thenReturn(null);

	interceptor.invoke(mockedInvocation);
	verify(backend).clear();
}
 
Example #10
Source File: RepositoryMethodEntityGraphExtractor.java    From spring-data-jpa-entity-graph with MIT License 4 votes vote down vote up
private Object doInvoke(MethodInvocation invocation) throws Throwable {
  Object[] arguments = invocation.getArguments();
  EntityGraph providedEntityGraph = null;
  for (Object argument : arguments) {
    if (!(argument instanceof EntityGraph)) {
      continue;
    }
    EntityGraph newEntityGraph = (EntityGraph) argument;
    if (providedEntityGraph != null) {
      throw new MultipleEntityGraphException(
          "Duplicate EntityGraphs detected. '"
              + providedEntityGraph
              + "' and '"
              + newEntityGraph
              + "' were passed to method "
              + invocation.getMethod());
    }
    providedEntityGraph = newEntityGraph;
  }

  Class<?> implementationClass;
  if (invocation instanceof ReflectiveMethodInvocation) {
    implementationClass = ((ReflectiveMethodInvocation) invocation).getProxy().getClass();
  } else {
    implementationClass = invocation.getThis().getClass();
  }

  EntityGraphBean entityGraphCandidate =
      buildEntityGraphCandidate(
          providedEntityGraph,
          ResolvableType.forMethodReturnType(invocation.getMethod(), implementationClass));

  if (entityGraphCandidate != null && !entityGraphCandidate.isValid()) {
    if (entityGraphCandidate.isOptional()) {
      LOG.trace("Cannot apply EntityGraph {}", entityGraphCandidate);
      entityGraphCandidate = null;
    } else {
      throw new InapplicableEntityGraphException(
          "Cannot apply EntityGraph " + entityGraphCandidate + " to the the current query");
    }
  }

  EntityGraphBean oldEntityGraphCandidate = currentEntityGraph.get();
  boolean newEntityGraphCandidatePreValidated =
      entityGraphCandidate != null
          && (oldEntityGraphCandidate == null || !oldEntityGraphCandidate.isPrimary());
  if (newEntityGraphCandidatePreValidated) {
    currentEntityGraph.set(entityGraphCandidate);
  }
  try {
    return invocation.proceed();
  } finally {
    if (newEntityGraphCandidatePreValidated) {
      currentEntityGraph.set(oldEntityGraphCandidate);
    }
  }
}
 
Example #11
Source File: AuthorizationFailureEventListener.java    From cia with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(final AuthorizationFailureEvent authorizationFailureEvent) {

	final String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();

	final CreateApplicationEventRequest serviceRequest = new CreateApplicationEventRequest();
	serviceRequest.setSessionId(sessionId);

	serviceRequest.setEventGroup(ApplicationEventGroup.APPLICATION);
	serviceRequest.setApplicationOperation(ApplicationOperationType.AUTHORIZATION);

	serviceRequest.setUserId(UserContextUtil.getUserIdFromSecurityContext());

	final Page currentPageIfAny = Page.getCurrent();
	final String requestUrl = UserContextUtil.getRequestUrl(currentPageIfAny);
	final UI currentUiIfAny = UI.getCurrent();
	String methodInfo = "";

	if (currentPageIfAny != null && currentUiIfAny != null && currentUiIfAny.getNavigator() != null
			&& currentUiIfAny.getNavigator().getCurrentView() != null) {
		serviceRequest.setPage(currentUiIfAny.getNavigator().getCurrentView().getClass().getSimpleName());
		serviceRequest.setPageMode(currentPageIfAny.getUriFragment());
	}

	if (authorizationFailureEvent.getSource() instanceof ReflectiveMethodInvocation) {
		final ReflectiveMethodInvocation methodInvocation = (ReflectiveMethodInvocation) authorizationFailureEvent
				.getSource();
		if (methodInvocation != null && methodInvocation.getThis() != null) {
			methodInfo = new StringBuilder().append(methodInvocation.getThis().getClass().getSimpleName())
					.append('.').append(methodInvocation.getMethod().getName()).toString();
		}
	}

	final Collection<? extends GrantedAuthority> authorities = authorizationFailureEvent.getAuthentication()
			.getAuthorities();
	final Collection<ConfigAttribute> configAttributes = authorizationFailureEvent.getConfigAttributes();

	serviceRequest.setErrorMessage(MessageFormat.format(ERROR_MESSAGE_FORMAT, requestUrl, methodInfo, AUTHORITIES,
			authorities, REQUIRED_AUTHORITIES, configAttributes, authorizationFailureEvent.getSource()));
	serviceRequest.setApplicationMessage(ACCESS_DENIED);

	applicationManager.service(serviceRequest);

	LOGGER.info(LOG_MSG_AUTHORIZATION_FAILURE_SESSION_ID_AUTHORITIES_REQUIRED_AUTHORITIES,
			requestUrl.replaceAll(CRLF, CRLF_REPLACEMENT), methodInfo.replaceAll(CRLF, CRLF_REPLACEMENT),
			sessionId.replaceAll(CRLF, CRLF_REPLACEMENT), authorities, configAttributes);
}
 
Example #12
Source File: ReflectUtil.java    From dog with GNU Lesser General Public License v3.0 3 votes vote down vote up
public static Annotation[][] getParameterAnnotations(ProceedingJoinPoint pjp) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

        MethodInvocationProceedingJoinPoint methodJoinPoint = (MethodInvocationProceedingJoinPoint) pjp;

        Field methodInvocationfield = methodJoinPoint.getClass().getDeclaredField("methodInvocation");

        methodInvocationfield.setAccessible(true);

        Annotation[][] argAnnotations = ((ReflectiveMethodInvocation) methodInvocationfield.get(methodJoinPoint)).getMethod().getParameterAnnotations();

        return argAnnotations;
    }