Initial commit

This commit is contained in:
Anshul BANSAL 2019-12-01 18:29:04 +02:00
parent 3685d331d9
commit d21a7b9e5c
20 changed files with 707 additions and 0 deletions

197
ninja/pom.xml Normal file
View File

@ -0,0 +1,197 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ninja</artifactId>
<!-- If you want to deploy to a war please -->
<!-- replace "jar" with "war" and -->
<!-- comment ninja-standalone dependency and -->
<!-- uncomment the dependency for ninja-servlet -->
<packaging>jar</packaging>
<groupId>com.baeldung</groupId>
<version>1.0.0</version>
<url>http://www.ninjaframework.org</url>
<properties>
<ninja.version>6.5.0</ninja.version>
<jetty.version>9.4.18.v20190429</jetty.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>enforce-banned-dependencies</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedDependencies>
<excludes>
<exclude>commons-logging</exclude>
</excludes>
</bannedDependencies>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<webApp>
<contextPath>/</contextPath>
</webApp>
<stopKey>stop</stopKey>
<stopPort>8889</stopPort>
<scanIntervalSeconds>1</scanIntervalSeconds>
<reload>automatic</reload>
<scanTargetPatterns>
<scanTargetPattern>
<directory>target/classes</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.ftl.html</exclude>
<exclude>assets/**</exclude>
</excludes>
</scanTargetPattern>
</scanTargetPatterns>
<systemProperties>
<systemProperty>
<name>ninja.mode</name>
<value>dev</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
<!-- Allows you to run Ninja via the SuperDevMode. -->
<!-- run "mvn ninja:run" on the command line for the best -->
<!-- development experience. -->
<plugin>
<groupId>org.ninjaframework</groupId>
<artifactId>ninja-maven-plugin</artifactId>
<version>${ninja.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ninja.standalone.NinjaJetty</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.186</version>
</dependency>
<!-- If you want to deploy to a war please -->
<!-- comment ninja-standalone dependency and -->
<!-- uncomment the dependency for ninja-servlet -->
<!--
<dependency>
<groupId>org.ninjaframework</groupId>
<artifactId>ninja-servlet</artifactId>
<version>${ninja.version}</version>
</dependency>
-->
<dependency>
<groupId>org.ninjaframework</groupId>
<artifactId>ninja-standalone</artifactId>
<version>${ninja.version}</version>
</dependency>
<dependency>
<groupId>org.ninjaframework</groupId>
<artifactId>ninja-test-utilities</artifactId>
<version>${ninja.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,3 @@
/* Add additional stylesheets below
-------------------------------------------------- */

View File

@ -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<Class<? extends Filter>> filters) {
// Add your application - wide filters here
// filters.add(MyFilter.class);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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!

View File

@ -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;
}
}

View File

@ -0,0 +1,15 @@
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>

View File

@ -0,0 +1,33 @@
<configuration>
<!-- Uncomment to enable file-based logging with
daily (or size exceeding) gzip archival.
Also uncomment the "ROLLING" reference in <root>.
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>mylog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>mylog.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
<MaxHistory>30</MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender> -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
<!-- <appender-ref ref="ROLLING" /> -->
</root>
</configuration>

View File

@ -0,0 +1,7 @@
package services;
public interface UserService {
String getUserName();
}

View File

@ -0,0 +1,10 @@
package services;
public class UserServiceImpl implements UserService {
@Override
public String getUserName() {
return "Eugen Parashic";
}
}

View File

@ -0,0 +1,9 @@
<#import "../layout/defaultLayout.ftl.html" as layout>
<@layout.myLayout "Home page">
<h1>${i18n("hello.world")}</h1>
<p>${i18n("hello.world.json")}</p>
<a href="/hello_world.json">Hello World Json</a>
</@layout.myLayout>

View File

@ -0,0 +1,58 @@
<#macro myLayout title="Layout example">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="/assets/webjars/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<!-- Optional theme -->
<link rel="stylesheet" href="/assets/webjars/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.error-template {padding: 40px 15px;text-align: center;}
</style>
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="https://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<#include "header.ftl.html"/>
<#if (flash.error)??>
<div class="alert alert-danger">
${flash.error}
</div>
</#if>
<#if (flash.success)??>
<div class="alert alert-success">
${flash.success}
</div>
</#if>
<#nested/>
<#include "footer.ftl.html"/>
</div> <!-- /container -->
<script type="text/javascript" src="/assets/webjars/jquery/2.1.3/jquery.js"></script>
<script type="text/javascript" src="/assets/webjars/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html>
</#macro>

View File

@ -0,0 +1,5 @@
<hr>
<footer>
Built with <a href="http://www.ninjaframework.org">Ninja framework</a>.
</footer>

View File

@ -0,0 +1,18 @@
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">${i18n("header.title")}</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">${i18n("header.home")}</a></li>
</ul>
</div>
<!--/.navbar-collapse -->
</div>
</div>

View File

@ -0,0 +1,18 @@
<#import "../layout/defaultLayout.ftl.html" as layout>
<@layout.myLayout "Error. Forbidden.">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="error-template">
<h1>
Oops!</h1>
<h2>
403 Forbidden</h2>
<div class="error-details">
Sorry, an error has occured. Requested page is forbidden!
</div>
</div>
</div>
</div>
</div>
</@layout.myLayout>

View File

@ -0,0 +1,18 @@
<#import "../layout/defaultLayout.ftl.html" as layout>
<@layout.myLayout "Error. Not found.">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="error-template">
<h1>
Oops!</h1>
<h2>
404 Not Found</h2>
<div class="error-details">
Sorry, an error has occured. Requested page not found!
</div>
</div>
</div>
</div>
</div>
</@layout.myLayout>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>ninja</display-name>
<listener>
<listener-class>ninja.servlet.NinjaServletListener</listener-class>
</listener>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

View File

@ -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!"));
}
}