Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jan 10, 2025
1 parent e22d28a commit 327d974
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Collection;

import static io.microsphere.spring.beans.BeanUtils.isBeanPresent;
import static io.microsphere.util.ArrayUtils.isNotEmpty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -89,7 +90,8 @@ public void setup() {
this.registerWebEndpointMappings = enableWebMvcExtension.registerWebEndpointMappings();
this.interceptHandlerMethods = enableWebMvcExtension.interceptHandlerMethods();
this.publishEvents = enableWebMvcExtension.publishEvents();
this.registerHandlerInterceptors = enableWebMvcExtension.registerHandlerInterceptors();
this.registerHandlerInterceptors = enableWebMvcExtension.registerHandlerInterceptors() ? true :
isNotEmpty(enableWebMvcExtension.handlerInterceptors());
this.storeRequestBodyArgument = enableWebMvcExtension.storeRequestBodyArgument();
this.storeResponseBodyReturnValue = enableWebMvcExtension.storeResponseBodyReturnValue();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.microsphere.spring.webmvc.annotation;

import io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import static io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor.MOCK_TOKEN_VALUE;
import static io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor.TOKEN_HEADER_NAME;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* {@link EnableWebMvcExtension} Test with interceptors
*
* @author <a href="mailto:[email protected]">Mercy<a/>
* @see EnableWebMvcExtension
* @since 1.0.0
*/
@ContextConfiguration(classes = {
EnableWebMvcExtensionInterceptorsTest.class
})
@EnableWebMvcExtension(handlerInterceptors = {
IdempotentAnnotatedMethodHandlerInterceptor.class
})
public class EnableWebMvcExtensionInterceptorsTest extends AbstractEnableWebMvcExtensionTest {

@Test
@Override
public void test() throws Exception {
this.mockMvc.perform(get("/echo/hello").header(TOKEN_HEADER_NAME, MOCK_TOKEN_VALUE))
.andExpect(status().isOk())
.andExpect(content().json("[ECHO] : hello"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package io.microsphere.spring.webmvc.interceptor;

import io.microsphere.logging.Logger;
import io.microsphere.spring.web.event.HandlerMethodArgumentsResolvedEvent;
import io.microsphere.spring.webmvc.IdempotentException;
import io.microsphere.spring.webmvc.annotation.Idempotent;
Expand All @@ -29,22 +30,30 @@
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;

import static io.microsphere.logging.LoggerFactory.getLogger;
import static java.util.Arrays.asList;

/**
* {@link AnnotatedMethodHandlerInterceptor} for {@link Idempotent} annotation
*
* @author <a href="mailto:[email protected]">Mercy</a>
* @since 1.0.0
*/
public class IdempotentAnnotatedMethodHandlerInterceptor extends AnnotatedMethodHandlerInterceptor<Idempotent> implements ApplicationListener<HandlerMethodArgumentsResolvedEvent> {
public class IdempotentAnnotatedMethodHandlerInterceptor extends AnnotatedMethodHandlerInterceptor<Idempotent>
implements ApplicationListener<HandlerMethodArgumentsResolvedEvent> {

private static final Logger logger = getLogger(IdempotentAnnotatedMethodHandlerInterceptor.class);

public static final String TOKEN_HEADER_NAME = "_token_";

public static final String MOCK_TOKEN_VALUE = UUID.randomUUID().toString();

@Override
protected boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod,
Idempotent idempotent) throws Exception {

System.out.println(handlerMethod);
System.out.println(idempotent);

return true;
}

Expand All @@ -54,19 +63,14 @@ protected boolean preHandle(HttpServletRequest request, HttpServletResponse resp
public void onApplicationEvent(HandlerMethodArgumentsResolvedEvent event) {
Method method = event.getMethod();
Object[] args = event.getArguments();
System.out.println("method : " + method + " , args : " + Arrays.asList(args));
WebRequest webRequest = event.getWebRequest();
logger.trace("The method : {} , args : {} , webRequest : {}", method, asList(args), webRequest);
if (webRequest instanceof ServletWebRequest) {
ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
HttpServletRequest request = servletWebRequest.getNativeRequest(HttpServletRequest.class);
// HttpSession based on Spring Redis
// Spring Session
HttpSession httpSession = request.getSession();
String token = request.getHeader("token");
Object tokenValue = httpSession.getAttribute(token);
if (tokenValue != null) {
//
throw new IdempotentException("");
String token = request.getHeader(TOKEN_HEADER_NAME);
if (!Objects.equals(MOCK_TOKEN_VALUE, token)) {
throw new IdempotentException("Illegal token");
}
}
}
Expand Down

0 comments on commit 327d974

Please sign in to comment.