diff --git a/src/shogun2-core/src/main/java/de/terrestris/shogun2/service/EndpointDocService.java b/src/shogun2-core/src/main/java/de/terrestris/shogun2/service/EndpointDocService.java new file mode 100644 index 000000000..7756ad111 --- /dev/null +++ b/src/shogun2-core/src/main/java/de/terrestris/shogun2/service/EndpointDocService.java @@ -0,0 +1,38 @@ +package de.terrestris.shogun2.service; + +import java.util.Set; + +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Service; +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +import de.terrestris.shogun2.web.EndpointDocController; + +/** + * Service class for the {@link EndpointDocController}. + * + * @author Christian Mayer + */ +@Service("endpointDocService") +public class EndpointDocService { + + /** + * Returns all RequestMappingInfo instances from type and + * method-level @RequestMapping annotations in @Controller classes. + * + * @param requestMappingHandlerMapping + * The RequestMappingInfo collection of Spring + * @return A set of RequestMappingInfo + */ + @PreAuthorize("hasRole(@configHolder.getSuperAdminRoleName())") + public Set getEndpoints(RequestMappingHandlerMapping requestMappingHandlerMapping) { + + if (requestMappingHandlerMapping.getHandlerMethods() != null) { + return requestMappingHandlerMapping.getHandlerMethods().keySet(); + } + + return null; + } + +} diff --git a/src/shogun2-core/src/main/java/de/terrestris/shogun2/web/EndpointDocController.java b/src/shogun2-core/src/main/java/de/terrestris/shogun2/web/EndpointDocController.java new file mode 100644 index 000000000..59a1fa145 --- /dev/null +++ b/src/shogun2-core/src/main/java/de/terrestris/shogun2/web/EndpointDocController.java @@ -0,0 +1,64 @@ +package de.terrestris.shogun2.web; + +import java.util.Set; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +import de.terrestris.shogun2.service.EndpointDocService; + +/** + * Web-controller for endpoint documentation. + * + * @author Christian mayer + */ +@Controller +public class EndpointDocController { + + /** + * Creates RequestMappingInfo instances from type and + * method-level @RequestMapping annotations in @Controller classes. + */ + @Autowired + private RequestMappingHandlerMapping requestMappingHandlerMapping; + + /** + * The service layer instance + */ + @Autowired + @Qualifier("endpointDocService") + private EndpointDocService service; + + /** + * Provides an overview of all mapped endpoints. + */ + @RequestMapping(value = "/endpointdoc", method = RequestMethod.GET) + public @ResponseBody Set getEndpoints() { + + return this.service.getEndpoints(requestMappingHandlerMapping); + } + + + /** + * @param service + * the service to set + */ + public void setService(EndpointDocService service) { + this.service = service; + } + + /** + * @param requestMappingHandlerMapping + * the requestMappingHandlerMapping to set + */ + public void setRequestMappingHandlerMapping(RequestMappingHandlerMapping requestMappingHandlerMapping) { + this.requestMappingHandlerMapping = requestMappingHandlerMapping; + } + +} diff --git a/src/shogun2-core/src/test/java/de/terrestris/shogun2/web/EndpointDocControllerTest.java b/src/shogun2-core/src/test/java/de/terrestris/shogun2/web/EndpointDocControllerTest.java new file mode 100644 index 000000000..ce3c8467b --- /dev/null +++ b/src/shogun2-core/src/test/java/de/terrestris/shogun2/web/EndpointDocControllerTest.java @@ -0,0 +1,61 @@ +package de.terrestris.shogun2.web; + +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; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +import de.terrestris.shogun2.service.EndpointDocService; + +/** + * + * @author Christian Mayer + */ +public class EndpointDocControllerTest { + + private MockMvc mockMvc; + + /** + * The controller to test + */ + private EndpointDocController endpointDocController; + + @Mock + private EndpointDocService endpointDocServiceMock; + + @Mock + private RequestMappingHandlerMapping requestMappingHandlerMapping; + + @Before + public void setUp() { + + // Process mock annotations + MockitoAnnotations.initMocks(this); + + // init the controller to test. this is necessary as InjectMocks + // annotation will not work with the constructors of the controllers + // (entityClass). see https://goo.gl/jLbMZe + endpointDocController = new EndpointDocController(); + endpointDocController.setService(endpointDocServiceMock); + endpointDocController.setRequestMappingHandlerMapping(requestMappingHandlerMapping); + // Setup Spring test in standalone mode + this.mockMvc = MockMvcBuilders.standaloneSetup(endpointDocController).build(); + } + + @Test + public void findAllRequestMappings_shouldReturnAllRequestMappings() throws Exception { + + // Perform and test the GET-Request + mockMvc.perform(get("/endpointdoc")) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().string("[]")); + } +}