Repository: metamodel Updated Branches: refs/heads/feature/5.x/swagger-docs [created] 0a82d13b1
Added scaffolding for 'metamodel-as-a-service' module Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/c9f56aa7 Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/c9f56aa7 Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/c9f56aa7 Branch: refs/heads/feature/5.x/swagger-docs Commit: c9f56aa753334b571ab868912c47d0e629e351ed Parents: dd0cff7 Author: Kasper Sørensen <[email protected]> Authored: Mon May 23 21:36:33 2016 -0700 Committer: Kasper Sørensen <[email protected]> Committed: Mon May 23 21:55:51 2016 -0700 ---------------------------------------------------------------------- pom.xml | 30 +++++++ service-webapp/pom.xml | 84 +++++++++++++++++ .../controllers/RootInformationController.java | 95 ++++++++++++++++++++ .../resources/context/application-context.xml | 32 +++++++ service-webapp/src/main/resources/logback.xml | 33 +++++++ .../main/webapp/WEB-INF/dispatcher-servlet.xml | 57 ++++++++++++ service-webapp/src/main/webapp/WEB-INF/web.xml | 66 ++++++++++++++ .../RootInformationControllerTest.java | 61 +++++++++++++ spring/pom.xml | 12 --- 9 files changed, 458 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 6b4b2b3..0e9d696 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,7 @@ under the License. <hadoop.version>2.6.0</hadoop.version> <jackson.version>2.6.3</jackson.version> <easymock.version>3.2</easymock.version> + <spring.version>4.2.6.RELEASE</spring.version> <httpcomponents.version>4.4.1</httpcomponents.version> <checksum-maven-plugin.version>1.2</checksum-maven-plugin.version> <skipTests>false</skipTests> @@ -77,6 +78,7 @@ under the License. <module>full</module> <module>spring</module> <module>neo4j</module> + <module>service-webapp</module> </modules> <issueManagement> <system>Jira</system> @@ -534,6 +536,34 @@ under the License. <artifactId>hsqldb</artifactId> <version>1.8.0.10</version> </dependency> + + <!-- Spring --> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>${spring.version}</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <version>${spring.version}</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-webmvc</artifactId> + <version>${spring.version}</version> + </dependency> <!-- Hadoop --> <dependency> http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/service-webapp/pom.xml ---------------------------------------------------------------------- diff --git a/service-webapp/pom.xml b/service-webapp/pom.xml new file mode 100644 index 0000000..f3836dd --- /dev/null +++ b/service-webapp/pom.xml @@ -0,0 +1,84 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>MetaModel</artifactId> + <groupId>org.apache.metamodel</groupId> + <version>5.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>MetaModel-service-webapp</artifactId> + <name>MetaModel-asâa-service web application</name> + <packaging>war</packaging> + + <build> + <finalName>MetaModel</finalName> + </build> + + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-webmvc</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>1.1.7</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + <version>5.2.4.Final</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl-over-slf4j</artifactId> + </dependency> + + <!-- Provided --> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <version>3.1.0</version> + <scope>provided</scope> + </dependency> + + <!-- Test --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <scope>test</scope> + </dependency> + </dependencies> +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/service-webapp/src/main/java/org/apache/metamodel/service/controllers/RootInformationController.java ---------------------------------------------------------------------- diff --git a/service-webapp/src/main/java/org/apache/metamodel/service/controllers/RootInformationController.java b/service-webapp/src/main/java/org/apache/metamodel/service/controllers/RootInformationController.java new file mode 100644 index 0000000..794f85a --- /dev/null +++ b/service-webapp/src/main/java/org/apache/metamodel/service/controllers/RootInformationController.java @@ -0,0 +1,95 @@ +/** + * 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 org.apache.metamodel.service.controllers; + +import java.io.InputStream; +import java.net.InetAddress; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +import javax.servlet.ServletContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +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.bind.annotation.RestController; + +@RestController +public class RootInformationController { + + private static final Logger logger = LoggerFactory.getLogger(RootInformationController.class); + + @Autowired + ServletContext servletContext; + + @RequestMapping(method = RequestMethod.GET, value = "/", produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + public Map<String, Object> index() { + final Map<String, Object> map = new LinkedHashMap<>(); + map.put("ping", "pong!"); + map.put("application", "Apache MetaModel"); + map.put("version", getVersion()); + map.put("server-time", getServerTime()); + try { + map.put("canonical-hostname", InetAddress.getLocalHost().getCanonicalHostName()); + } catch (Exception e) { + logger.info("Failed to get canonical-hostname", e); + } + return map; + } + + private Map<String, Object> getServerTime() { + final ZonedDateTime now = ZonedDateTime.now(); + final String dateFormatted = now.format( DateTimeFormatter.ISO_INSTANT); + + final Map<String, Object> map = new LinkedHashMap<>(); + map.put("timestamp", new Date().getTime()); + map.put("iso8601", dateFormatted); + return map; + } + + /** + * Does the slightly tedious task of reading the software version from + * META-INF based on maven metadata. + * + * @return + */ + private String getVersion() { + final String groupId = "org.apache.metamodel"; + final String artifactId = "MetaModel-service-webapp"; + final String resourcePath = "/META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties"; + final Properties properties = new Properties(); + try (final InputStream inputStream = servletContext.getResourceAsStream(resourcePath)) { + properties.load(inputStream); + } catch (Exception e) { + logger.error("Failed to load version from manifest: " + e.getMessage()); + } + + final String version = properties.getProperty("version", "UNKNOWN"); + return version; + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/service-webapp/src/main/resources/context/application-context.xml ---------------------------------------------------------------------- diff --git a/service-webapp/src/main/resources/context/application-context.xml b/service-webapp/src/main/resources/context/application-context.xml new file mode 100644 index 0000000..b0c945d --- /dev/null +++ b/service-webapp/src/main/resources/context/application-context.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:security="http://www.springframework.org/schema/security" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" + xmlns:context="http://www.springframework.org/schema/context" + xmlns:mvc="http://www.springframework.org/schema/mvc" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd + http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + + <context:component-scan base-package="org.apache.metamodel.service.app" /> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/service-webapp/src/main/resources/logback.xml ---------------------------------------------------------------------- diff --git a/service-webapp/src/main/resources/logback.xml b/service-webapp/src/main/resources/logback.xml new file mode 100644 index 0000000..de862de --- /dev/null +++ b/service-webapp/src/main/resources/logback.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<configuration> + + <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <logger name="org.apache.metamodel" level="info" /> + + <root level="warn"> + <appender-ref ref="consoleAppender" /> + </root> +</configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/service-webapp/src/main/webapp/WEB-INF/dispatcher-servlet.xml ---------------------------------------------------------------------- diff --git a/service-webapp/src/main/webapp/WEB-INF/dispatcher-servlet.xml b/service-webapp/src/main/webapp/WEB-INF/dispatcher-servlet.xml new file mode 100644 index 0000000..12af16f --- /dev/null +++ b/service-webapp/src/main/webapp/WEB-INF/dispatcher-servlet.xml @@ -0,0 +1,57 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:security="http://www.springframework.org/schema/security" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" + xmlns:context="http://www.springframework.org/schema/context" + xmlns:mvc="http://www.springframework.org/schema/mvc" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd + http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + + <context:component-scan base-package="org.apache.metamodel.service.controllers" /> + + <mvc:annotation-driven> + <mvc:message-converters> + <ref bean="jsonMessageConverter" /> + </mvc:message-converters> + </mvc:annotation-driven> + + <!-- Message converters --> + <bean id="jsonMessageConverter" + class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> + <property name="supportedMediaTypes"> + <list> + <value>application/json;charset=UTF-8</value> + <value>application/json</value> + </list> + </property> + </bean> + + <bean + class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> + <property name="messageConverters"> + <list> + <ref bean="jsonMessageConverter" /> + </list> + </property> + </bean> +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/service-webapp/src/main/webapp/WEB-INF/web.xml ---------------------------------------------------------------------- diff --git a/service-webapp/src/main/webapp/WEB-INF/web.xml b/service-webapp/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..eb3ef51 --- /dev/null +++ b/service-webapp/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + version="3.1" xmlns="http://java.sun.com/xml/ns/javaee"> + + <!-- Spring listener --> + <listener> + <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> + </listener> + <listener> + <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> + </listener> + + <!-- Spring config --> + <context-param> + <param-name>contextConfigLocation</param-name> + <param-value>classpath:context/application-context.xml</param-value> + </context-param> + + <!-- Spring servlet --> + <servlet> + <servlet-name>dispatcher</servlet-name> + <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> + <load-on-startup>1</load-on-startup> + </servlet> + <servlet-mapping> + <servlet-name>dispatcher</servlet-name> + <url-pattern>/*</url-pattern> + </servlet-mapping> + + <filter> + <filter-name>CharacterEncodingFilter</filter-name> + <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> + <init-param> + <param-name>encoding</param-name> + <param-value>UTF-8</param-value> + </init-param> + <init-param> + <param-name>forceEncoding</param-name> + <param-value>true</param-value> + </init-param> + </filter> + + <filter-mapping> + <filter-name>CharacterEncodingFilter</filter-name> + <url-pattern>/*</url-pattern> + </filter-mapping> + +</web-app> http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/service-webapp/src/test/java/org/apache/metamodel/service/controllers/RootInformationControllerTest.java ---------------------------------------------------------------------- diff --git a/service-webapp/src/test/java/org/apache/metamodel/service/controllers/RootInformationControllerTest.java b/service-webapp/src/test/java/org/apache/metamodel/service/controllers/RootInformationControllerTest.java new file mode 100644 index 0000000..58a5ca5 --- /dev/null +++ b/service-webapp/src/test/java/org/apache/metamodel/service/controllers/RootInformationControllerTest.java @@ -0,0 +1,61 @@ +/** + * 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 org.apache.metamodel.service.controllers; + +import static org.junit.Assert.assertEquals; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class RootInformationControllerTest { + + private MockMvc mockMvc; + + @Before + public void init() { + final RootInformationController controller = new RootInformationController(); + controller.servletContext = new MockServletContext(); + mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); + } + + @Test + public void testGenericMessageSuccess() throws Exception { + final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/").contentType( + MediaType.APPLICATION_JSON); + + final MvcResult result = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().is(200)).andReturn(); + + final String content = result.getResponse().getContentAsString(); + + final Map<?, ?> map = new ObjectMapper().readValue(content, Map.class); + assertEquals("pong!", map.get("ping")); + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/c9f56aa7/spring/pom.xml ---------------------------------------------------------------------- diff --git a/spring/pom.xml b/spring/pom.xml index 6792582..b6d09b4 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -27,10 +27,6 @@ under the License. <artifactId>MetaModel-spring</artifactId> <name>MetaModel module for Spring enabled configuration</name> - <properties> - <spring.version>3.0.7.RELEASE</spring.version> - </properties> - <dependencies> <dependency> <groupId>org.apache.metamodel</groupId> @@ -46,14 +42,7 @@ under the License. <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> - <version>${spring.version}</version> <scope>provided</scope> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> </dependency> <!-- test --> @@ -70,7 +59,6 @@ under the License. <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> - <version>${spring.version}</version> <scope>test</scope> </dependency> </dependencies>
