From d21a7b9e5cb8eca833717ea2982d8c833d8c1537 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sun, 1 Dec 2019 18:29:04 +0200 Subject: [PATCH] Initial commit --- ninja/pom.xml | 197 ++++++++++++++++++ ninja/src/main/java/assets/css/custom.css | 3 + ninja/src/main/java/conf/Filters.java | 30 +++ ninja/src/main/java/conf/Module.java | 37 ++++ ninja/src/main/java/conf/Routes.java | 47 +++++ ninja/src/main/java/conf/application.conf | 26 +++ ninja/src/main/java/conf/messages.properties | 21 ++ .../controllers/ApplicationController.java | 36 ++++ ninja/src/main/java/ehcache.xml | 15 ++ ninja/src/main/java/logback.xml | 33 +++ ninja/src/main/java/services/UserService.java | 7 + .../main/java/services/UserServiceImpl.java | 10 + .../ApplicationController/index.ftl.html | 9 + .../java/views/layout/defaultLayout.ftl.html | 58 ++++++ .../main/java/views/layout/footer.ftl.html | 5 + .../main/java/views/layout/header.ftl.html | 18 ++ .../java/views/system/403forbidden.ftl.html | 18 ++ .../java/views/system/404notFound.ftl.html | 18 ++ ninja/src/main/webapp/WEB-INF/web.xml | 41 ++++ .../ApiControllerDocTesterTest.java | 78 +++++++ 20 files changed, 707 insertions(+) create mode 100644 ninja/pom.xml create mode 100644 ninja/src/main/java/assets/css/custom.css create mode 100644 ninja/src/main/java/conf/Filters.java create mode 100644 ninja/src/main/java/conf/Module.java create mode 100644 ninja/src/main/java/conf/Routes.java create mode 100644 ninja/src/main/java/conf/application.conf create mode 100644 ninja/src/main/java/conf/messages.properties create mode 100644 ninja/src/main/java/controllers/ApplicationController.java create mode 100644 ninja/src/main/java/ehcache.xml create mode 100644 ninja/src/main/java/logback.xml create mode 100644 ninja/src/main/java/services/UserService.java create mode 100644 ninja/src/main/java/services/UserServiceImpl.java create mode 100644 ninja/src/main/java/views/ApplicationController/index.ftl.html create mode 100644 ninja/src/main/java/views/layout/defaultLayout.ftl.html create mode 100644 ninja/src/main/java/views/layout/footer.ftl.html create mode 100644 ninja/src/main/java/views/layout/header.ftl.html create mode 100644 ninja/src/main/java/views/system/403forbidden.ftl.html create mode 100644 ninja/src/main/java/views/system/404notFound.ftl.html create mode 100644 ninja/src/main/webapp/WEB-INF/web.xml create mode 100644 ninja/src/test/java/controllers/ApiControllerDocTesterTest.java diff --git a/ninja/pom.xml b/ninja/pom.xml new file mode 100644 index 0000000000..962dabf1bd --- /dev/null +++ b/ninja/pom.xml @@ -0,0 +1,197 @@ + + + 4.0.0 + + ninja + + + + + jar + com.baeldung + 1.0.0 + + http://www.ninjaframework.org + + + 6.5.0 + 9.4.18.v20190429 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-enforcer-plugin + 1.3.1 + + + enforce-banned-dependencies + + enforce + + + + + + commons-logging + + + + true + + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + / + + stop + 8889 + 1 + automatic + + + target/classes + + **/* + + + **/*.ftl.html + assets/** + + + + + + ninja.mode + dev + + + + + + + + + org.ninjaframework + ninja-maven-plugin + ${ninja.version} + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + true + + + + org.apache.maven.plugins + maven-shade-plugin + 2.2 + + true + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + package + + shade + + + + + + ninja.standalone.NinjaJetty + + + + + + + + + + src/main/java + + **/* + + + **/*.java + + + + src/main/resources + + **/* + + + + + + + org.webjars + bootstrap + 3.3.4 + + + org.webjars + jquery + 2.1.3 + + + com.h2database + h2 + 1.4.186 + + + + + + + org.ninjaframework + ninja-standalone + ${ninja.version} + + + org.ninjaframework + ninja-test-utilities + ${ninja.version} + test + + + \ No newline at end of file diff --git a/ninja/src/main/java/assets/css/custom.css b/ninja/src/main/java/assets/css/custom.css new file mode 100644 index 0000000000..41d249d3cb --- /dev/null +++ b/ninja/src/main/java/assets/css/custom.css @@ -0,0 +1,3 @@ +/* Add additional stylesheets below +-------------------------------------------------- */ + diff --git a/ninja/src/main/java/conf/Filters.java b/ninja/src/main/java/conf/Filters.java new file mode 100644 index 0000000000..cfd04e8ec2 --- /dev/null +++ b/ninja/src/main/java/conf/Filters.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed 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 conf; + +import java.util.List; +import ninja.Filter; + +public class Filters implements ninja.application.ApplicationFilters { + + @Override + public void addFilters(List> filters) { + // Add your application - wide filters here + // filters.add(MyFilter.class); + } +} diff --git a/ninja/src/main/java/conf/Module.java b/ninja/src/main/java/conf/Module.java new file mode 100644 index 0000000000..e5ec39d2de --- /dev/null +++ b/ninja/src/main/java/conf/Module.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed 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 conf; + +import com.google.inject.AbstractModule; +import com.google.inject.Singleton; + +import services.UserService; +import services.UserServiceImpl; + +@Singleton +public class Module extends AbstractModule { + + + protected void configure() { + + // bind your injections here! + bind(UserService.class).to(UserServiceImpl.class); + + } + +} diff --git a/ninja/src/main/java/conf/Routes.java b/ninja/src/main/java/conf/Routes.java new file mode 100644 index 0000000000..7db2d7a107 --- /dev/null +++ b/ninja/src/main/java/conf/Routes.java @@ -0,0 +1,47 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed 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 conf; + + +import ninja.AssetsController; +import ninja.Router; +import ninja.application.ApplicationRoutes; +import controllers.ApplicationController; + +public class Routes implements ApplicationRoutes { + + @Override + public void init(Router router) { + + router.GET().route("/").with(ApplicationController::index); + router.GET().route("/hello_world.json").with(ApplicationController::helloWorldJson); + + + /////////////////////////////////////////////////////////////////////// + // Assets (pictures / javascript) + /////////////////////////////////////////////////////////////////////// + router.GET().route("/assets/webjars/{fileName: .*}").with(AssetsController::serveWebJars); + router.GET().route("/assets/{fileName: .*}").with(AssetsController::serveStatic); + + /////////////////////////////////////////////////////////////////////// + // Index / Catchall shows index page + /////////////////////////////////////////////////////////////////////// + router.GET().route("/.*").with(ApplicationController::index); + } + +} diff --git a/ninja/src/main/java/conf/application.conf b/ninja/src/main/java/conf/application.conf new file mode 100644 index 0000000000..3b2e96f702 --- /dev/null +++ b/ninja/src/main/java/conf/application.conf @@ -0,0 +1,26 @@ +# The main properties file to configure your application +# +# Properties can be prefixed by "%". +# "%"" matches a mode you can set as system property. For instance when you +# are using maven you can do: mvn -Dmode=test jetty:run +# +# Using that the following property: +# %test.myproperty=test property +# would overwrite the property +# myproperty=my property +# +# You can define as many environments as you want. Simply set them in the mode. +application.name=baeldung ninja application + +application.cookie.prefix=NINJA + +#ISO Language Code, optionally followed by a valid ISO Country Code. +application.languages=en + +application.session.expire_time_in_seconds=3600 +application.session.send_only_if_changed=true +application.session.transferred_over_https_only=false + +# enable ssl with self-signed cert in dev & test modes +ninja.ssl.port=8443 +application.secret = fxSjSL9Q017BSL7gBnkyo2Prln7uXaXIT35gotXRIED8c46OSa8s4QdoIQdTsEtj diff --git a/ninja/src/main/java/conf/messages.properties b/ninja/src/main/java/conf/messages.properties new file mode 100644 index 0000000000..f163d33260 --- /dev/null +++ b/ninja/src/main/java/conf/messages.properties @@ -0,0 +1,21 @@ +# +# Copyright (C) 2012 the original author or authors. +# +# Licensed 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. +# + +# This file is utf-8 +header.title=Hello World! +header.home=BAM! +hello.world=Hello World! +hello.world.json=There is also Json rendering you can check out! \ No newline at end of file diff --git a/ninja/src/main/java/controllers/ApplicationController.java b/ninja/src/main/java/controllers/ApplicationController.java new file mode 100644 index 0000000000..bd62e5fb84 --- /dev/null +++ b/ninja/src/main/java/controllers/ApplicationController.java @@ -0,0 +1,36 @@ +package controllers; + +import ninja.Result; +import ninja.Results; +import services.UserService; +import javax.inject.Inject; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.google.inject.Singleton; + +@Singleton +public class ApplicationController { + + private static Log logger = LogFactory.getLog(ApplicationController.class); + + @Inject + UserService userService; + + public Result index() { + return Results.html(); + } + + public Result helloWorldJson() { + SimplePojo simplePojo = new SimplePojo(); + simplePojo.content = "Hello World! Hello Json!"; + logger.info(userService.getUserName()); + return Results.json().render(simplePojo); + } + + public static class SimplePojo { + public String content; + } + +} diff --git a/ninja/src/main/java/ehcache.xml b/ninja/src/main/java/ehcache.xml new file mode 100644 index 0000000000..b401b61a36 --- /dev/null +++ b/ninja/src/main/java/ehcache.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/ninja/src/main/java/logback.xml b/ninja/src/main/java/logback.xml new file mode 100644 index 0000000000..ebdcf56f76 --- /dev/null +++ b/ninja/src/main/java/logback.xml @@ -0,0 +1,33 @@ + + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + diff --git a/ninja/src/main/java/services/UserService.java b/ninja/src/main/java/services/UserService.java new file mode 100644 index 0000000000..100761a2ab --- /dev/null +++ b/ninja/src/main/java/services/UserService.java @@ -0,0 +1,7 @@ +package services; + +public interface UserService { + + String getUserName(); + +} diff --git a/ninja/src/main/java/services/UserServiceImpl.java b/ninja/src/main/java/services/UserServiceImpl.java new file mode 100644 index 0000000000..a0dec61084 --- /dev/null +++ b/ninja/src/main/java/services/UserServiceImpl.java @@ -0,0 +1,10 @@ +package services; + +public class UserServiceImpl implements UserService { + + @Override + public String getUserName() { + return "Eugen Parashic"; + } + +} diff --git a/ninja/src/main/java/views/ApplicationController/index.ftl.html b/ninja/src/main/java/views/ApplicationController/index.ftl.html new file mode 100644 index 0000000000..ab2edaf69b --- /dev/null +++ b/ninja/src/main/java/views/ApplicationController/index.ftl.html @@ -0,0 +1,9 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Home page"> + + +

${i18n("hello.world")}

+

${i18n("hello.world.json")}

+Hello World Json + + \ No newline at end of file diff --git a/ninja/src/main/java/views/layout/defaultLayout.ftl.html b/ninja/src/main/java/views/layout/defaultLayout.ftl.html new file mode 100644 index 0000000000..a61edd19e6 --- /dev/null +++ b/ninja/src/main/java/views/layout/defaultLayout.ftl.html @@ -0,0 +1,58 @@ +<#macro myLayout title="Layout example"> + + + + + ${title} + + + + + + + + + + + + + + + + + + +
+ + <#include "header.ftl.html"/> + + <#if (flash.error)??> +
+ ${flash.error} +
+ + + <#if (flash.success)??> +
+ ${flash.success} +
+ + + <#nested/> + + <#include "footer.ftl.html"/> + +
+ + + + + \ No newline at end of file diff --git a/ninja/src/main/java/views/layout/footer.ftl.html b/ninja/src/main/java/views/layout/footer.ftl.html new file mode 100644 index 0000000000..0a2cb2721e --- /dev/null +++ b/ninja/src/main/java/views/layout/footer.ftl.html @@ -0,0 +1,5 @@ +
+ + diff --git a/ninja/src/main/java/views/layout/header.ftl.html b/ninja/src/main/java/views/layout/header.ftl.html new file mode 100644 index 0000000000..8794ee5c68 --- /dev/null +++ b/ninja/src/main/java/views/layout/header.ftl.html @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/ninja/src/main/java/views/system/403forbidden.ftl.html b/ninja/src/main/java/views/system/403forbidden.ftl.html new file mode 100644 index 0000000000..0f4efd160a --- /dev/null +++ b/ninja/src/main/java/views/system/403forbidden.ftl.html @@ -0,0 +1,18 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Error. Forbidden."> +
+
+
+
+

+ Oops!

+

+ 403 Forbidden

+
+ Sorry, an error has occured. Requested page is forbidden! +
+
+
+
+
+ \ No newline at end of file diff --git a/ninja/src/main/java/views/system/404notFound.ftl.html b/ninja/src/main/java/views/system/404notFound.ftl.html new file mode 100644 index 0000000000..c10e7c3949 --- /dev/null +++ b/ninja/src/main/java/views/system/404notFound.ftl.html @@ -0,0 +1,18 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Error. Not found."> +
+
+
+
+

+ Oops!

+

+ 404 Not Found

+
+ Sorry, an error has occured. Requested page not found! +
+
+
+
+
+ \ No newline at end of file diff --git a/ninja/src/main/webapp/WEB-INF/web.xml b/ninja/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..fe4b2c95bf --- /dev/null +++ b/ninja/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,41 @@ + + + + + + ninja + + + ninja.servlet.NinjaServletListener + + + + guiceFilter + com.google.inject.servlet.GuiceFilter + + + guiceFilter + /* + + + diff --git a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java new file mode 100644 index 0000000000..af4f5ba85f --- /dev/null +++ b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java @@ -0,0 +1,78 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed 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. + */ + +/** + * Copyright (C) 2013 the original author or authors. + * + * Licensed 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 controllers; + + +import org.junit.Test; + +import ninja.NinjaDocTester; +import org.doctester.testbrowser.Request; +import org.doctester.testbrowser.Response; +import org.hamcrest.CoreMatchers; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + +public class ApiControllerDocTesterTest extends NinjaDocTester { + + String URL_INDEX = "/"; + String URL_HELLO_WORLD_JSON = "/hello_world.json"; + + @Test + public void testGetIndex() { + + Response response = makeRequest( + Request.GET().url( + testServerUrl().path(URL_INDEX))); + + assertThat(response.payload, containsString("Hello World!")); + assertThat(response.payload, containsString("BAM!")); + + + } + + @Test + public void testGetHelloWorldJson() { + + Response response = makeRequest( + Request.GET().url( + testServerUrl().path(URL_HELLO_WORLD_JSON))); + + ApplicationController.SimplePojo simplePojo + = response.payloadJsonAs(ApplicationController.SimplePojo.class); + + assertThat(simplePojo.content, CoreMatchers.equalTo("Hello World! Hello Json!")); + + + } + +}