Skip to content

Instantly share code, notes, and snippets.

@guangtuan
Created December 2, 2019 01:08
Show Gist options
  • Save guangtuan/b57fd6fe651df68e67b705c8fc58977a to your computer and use it in GitHub Desktop.
Save guangtuan/b57fd6fe651df68e67b705c8fc58977a to your computer and use it in GitHub Desktop.
WebServiceProvider
package cn.agilean.valuekanban.sync.protrait;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class WebServiceProvider {
private List<WebService> webServices = new ArrayList<>();
static class WebService {
String prefix;
String url;
Method method;
Object context;
HttpMethod httpMethod;
}
private ConfigurableApplicationContext configurableApplicationContext;
public void setConfigurableApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
this.configurableApplicationContext = configurableApplicationContext;
}
public void handle(HttpServletRequest request) {
String requestURI = request.getRequestURI();
String method = request.getMethod();
Optional<WebService> webServiceOptional = webServices.stream().filter(webService -> {
String url = removeFrontAndBackSlash(webService.url);
String prefix = removeFrontAndBackSlash(webService.prefix);
return webService.httpMethod.name().equals(method) && String.join("/", prefix, url).equals(requestURI);
}).findFirst();
if (webServiceOptional.isPresent()) {
try {
webServiceOptional.get().method.invoke(request);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
private static String removeFrontAndBackSlash(String s) {
String result = s;
if (result.startsWith("/")) {
result = result.substring(1);
}
if (s.endsWith("/")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
public static void main(String[] args) {
System.out.println(removeFrontAndBackSlash("123/"));
}
public void init() {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
String[] beanNamesForAnnotation = configurableApplicationContext.getBeanFactory().getBeanNamesForAnnotation(RestController.class);
for (String s : beanNamesForAnnotation) {
try {
Class<?> controllerClass = classLoader.loadClass(s);
String prefix = controllerClass.getAnnotation(RequestMapping.class).value()[0];
Constructor<?>[] declaredConstructors = controllerClass.getConstructors();
if (declaredConstructors.length == 0) {
return;
}
Constructor<?> defaultConstructor = declaredConstructors[0];
Object controller = defaultConstructor.newInstance();
List<WebService> webServiceList = Stream
.of(controllerClass.getDeclaredMethods())
.map(m -> {
WebService webService = new WebService();
webService.context = controller;
webService.prefix = prefix;
if (m.isAnnotationPresent(RequestMapping.class)) {
webService.httpMethod = lookupHttpMethod(m.getAnnotation(RequestMapping.class).method()[0].name());
webService.url = m.getAnnotation(RequestMapping.class).value()[0];
} else if (m.isAnnotationPresent(GetMapping.class)) {
webService.httpMethod = HttpMethod.GET;
webService.url = m.getAnnotation(GetMapping.class).value()[0];
} else if (m.isAnnotationPresent(PostMapping.class)) {
webService.httpMethod = HttpMethod.POST;
webService.url = m.getAnnotation(PostMapping.class).value()[0];
} else if (m.isAnnotationPresent(PutMapping.class)) {
webService.httpMethod = HttpMethod.PUT;
webService.url = m.getAnnotation(PutMapping.class).value()[0];
} else if (m.isAnnotationPresent(DeleteMapping.class)) {
webService.httpMethod = HttpMethod.DELETE;
webService.url = m.getAnnotation(DeleteMapping.class).value()[0];
} else {
return null;
}
return webService;
})
.collect(Collectors.toList());
webServices.addAll(webServiceList);
} catch (InstantiationException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
private HttpMethod lookupHttpMethod(String name) {
Optional<HttpMethod> first = Stream.of(HttpMethod.values())
.filter(httpMethod -> httpMethod.name().equalsIgnoreCase(name))
.findFirst();
return first.orElse(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment