service layout 관련코드 core로 옮김

This commit is contained in:
ParkSeongMin
2015-10-13 08:32:50 +00:00
parent fc676830e8
commit a29108d6a9
2 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package com.nexacro.spring.servicelayout;
import java.util.Set;
import org.springframework.web.bind.annotation.RequestMethod;
public class ServiceLayout {
private String pattern;
private Set<RequestMethod> methods;
private Set<String> parameters;
public ServiceLayout(String pattern, Set<RequestMethod> methods,
Set<String> parameters) {
this.pattern = pattern;
this.methods = methods;
this.parameters = parameters;
}
public String getPattern() {
return pattern;
}
public Set<RequestMethod> getMethods() {
return methods;
}
public Set<String> getParameters() {
return parameters;
}
public String getParameterString() {
StringBuilder builder = new StringBuilder();
int loopCnt = 1;
for (String parameter : parameters) {
builder.append(parameter + "=");
if (loopCnt < parameters.size()) {
builder.append("&");
}
loopCnt++;
}
return builder.toString();
}
}

View File

@@ -0,0 +1,87 @@
package com.nexacro.spring.servicelayout;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Controller
public class ServiceLayoutController {
// ---------- getter/setter------------
// @Value("#{config}")
// private Properties config;
private final RequestMappingHandlerMapping handlerMapping;
@Autowired
public ServiceLayoutController(RequestMappingHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}
@RequestMapping("/serviceLayout.do")
public String execute(HttpServletRequest request, Model model)
throws IllegalAccessException {
// session check..
// LoginUser loginUser = (LoginUser)
// request.getSession().getAttribute("loginUser");
ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
List<ServiceLayout> serviceLayouts = new ArrayList<ServiceLayout>();
Map<RequestMappingInfo, HandlerMethod> handlerMethods = this.handlerMapping.getHandlerMethods();
Set<RequestMappingInfo> reqMappingInfoKeys = handlerMethods.keySet();
for (RequestMappingInfo requestMappingInfoKey : reqMappingInfoKeys) {
HandlerMethod handlerMethod = handlerMethods.get(requestMappingInfoKey);
Annotation[][] paramAnnotaions = handlerMethod.getMethod().getParameterAnnotations();
String[] parameterNames = parameterNameDiscoverer.getParameterNames(handlerMethod.getMethod());
Set<String> parameters = new HashSet<String>();
for (int i = 0; i < paramAnnotaions.length; i++) {
Annotation[] annotations = paramAnnotaions[i];
for (int j = 0; j < annotations.length; j++) {
Annotation annotation = annotations[j];
if (annotation instanceof RequestParam) {
parameters.add(parameterNames[i]);
}
}
}
Set<String> patterns = requestMappingInfoKey.getPatternsCondition()
.getPatterns();
for (String pattern : patterns) {
ServiceLayout api = new ServiceLayout(pattern,
requestMappingInfoKey.getMethodsCondition()
.getMethods(), parameters);
serviceLayouts.add(api);
}
}
model.addAttribute("apis", serviceLayouts);
return "serviceLayout.html";
}
}