Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
2ca92772b7
5
jee7/.gitignore
vendored
Normal file
5
jee7/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
/classes/
|
||||
/.idea/
|
||||
/target/
|
||||
/jee7.iml
|
@ -6,8 +6,10 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jee7</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<description>JavaEE 7 Arquillian Archetype Sample</description>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
@ -174,6 +176,7 @@
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>webapp</warSourceDirectory>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
23
jee7/src/main/webapp/WEB-INF/spring/security.xml
Normal file
23
jee7/src/main/webapp/WEB-INF/spring/security.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<b:beans xmlns="http://www.springframework.org/schema/security"
|
||||
xmlns:b="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
|
||||
|
||||
<authentication-manager>
|
||||
<authentication-provider>
|
||||
<user-service>
|
||||
<user name="user" password="user123" authorities="ROLE_USER" />
|
||||
</user-service>
|
||||
</authentication-provider>
|
||||
</authentication-manager>
|
||||
|
||||
<http auto-config='true' use-expressions="true">
|
||||
<form-login default-target-url="/secure.jsp" />
|
||||
<intercept-url pattern="/" access="isAnonymous()" />
|
||||
<intercept-url pattern="/index.jsp" access="isAnonymous()" />
|
||||
<intercept-url pattern="/secure.jsp" access="hasRole('ROLE_USER')" />
|
||||
</http>
|
||||
|
||||
</b:beans>
|
@ -32,6 +32,33 @@
|
||||
<!-- If you go to http://host/project/ (with no file name), it will
|
||||
try index.jsf first, welcome.jsf next, and so forth.
|
||||
-->
|
||||
|
||||
|
||||
<!-- UNCOMMENT THE FOLLOWING SECTION FOR SPRING SECURITY XML CONFIGURATION-->
|
||||
|
||||
<!--<context-param>-->
|
||||
<!--<param-name>contextConfigLocation</param-name>-->
|
||||
<!--<param-value>-->
|
||||
<!--/WEB-INF/spring/*.xml-->
|
||||
<!--</param-value>-->
|
||||
<!--</context-param>-->
|
||||
|
||||
<!--<filter>-->
|
||||
<!--<filter-name>springSecurityFilterChain</filter-name>-->
|
||||
<!--<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>-->
|
||||
<!--</filter>-->
|
||||
|
||||
<!--<filter-mapping>-->
|
||||
<!--<filter-name>springSecurityFilterChain</filter-name>-->
|
||||
<!--<url-pattern>/*</url-pattern>-->
|
||||
<!--</filter-mapping>-->
|
||||
|
||||
<!--<listener>-->
|
||||
<!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
|
||||
<!--</listener>-->
|
||||
|
||||
<!-- END SPRING SECURITY XML CONFIGURATION-->
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.jsf</welcome-file>
|
||||
<welcome-file>welcome.jsf</welcome-file>
|
||||
|
11
jee7/src/main/webapp/index.jsp
Normal file
11
jee7/src/main/webapp/index.jsp
Normal file
@ -0,0 +1,11 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Index Page</title>
|
||||
</head>
|
||||
<body>
|
||||
Non-secured Index Page
|
||||
<br>
|
||||
<a href="/login">Login</a>
|
||||
</body>
|
||||
</html>
|
24
jee7/src/main/webapp/secure.jsp
Normal file
24
jee7/src/main/webapp/secure.jsp
Normal file
@ -0,0 +1,24 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
||||
pageEncoding="UTF-8"%>
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>Home Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Home Page</h3>
|
||||
|
||||
<p>
|
||||
Hello <b><c:out value="${pageContext.request.remoteUser}"/></b><br>
|
||||
Roles: <b><sec:authentication property="principal.authorities" /></b>
|
||||
</p>
|
||||
|
||||
<form action="logout" method="post">
|
||||
<input type="submit" value="Logout" />
|
||||
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
19
kotlin/src/main/kotlin/com/baeldung/kotlin/Sealed.kt
Normal file
19
kotlin/src/main/kotlin/com/baeldung/kotlin/Sealed.kt
Normal file
@ -0,0 +1,19 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
sealed class Result<out S, out F> {
|
||||
abstract fun <R> map(func: (S) -> R) : Result<R, F>
|
||||
abstract fun <R> mapFailure(func: (F) -> R) : Result<S, R>
|
||||
abstract fun get() : S?
|
||||
}
|
||||
|
||||
data class Success<out S, out F>(val success: S) : Result<S, F>() {
|
||||
override fun <R> map(func: (S) -> R) : Result<R, F> = Success(func(success))
|
||||
override fun <R> mapFailure(func: (F) -> R): Result<S, R> = Success(success)
|
||||
override fun get(): S? = success
|
||||
}
|
||||
|
||||
data class Failure<out S, out F>(val failure: F) : Result<S, F>() {
|
||||
override fun <R> map(func: (S) -> R) : Result<R, F> = Failure(failure)
|
||||
override fun <R> mapFailure(func: (F) -> R): Result<S, R> = Failure(func(failure))
|
||||
override fun get(): S? = null
|
||||
}
|
84
kotlin/src/test/kotlin/com/baeldung/kotlin/SealedTest.kt
Normal file
84
kotlin/src/test/kotlin/com/baeldung/kotlin/SealedTest.kt
Normal file
@ -0,0 +1,84 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
|
||||
class SealedTest {
|
||||
fun divide(a: Int, b: Int) : Result<Float, String> = when (b) {
|
||||
0 -> Failure("Division by zero")
|
||||
else -> Success(a.toFloat() / b)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSuccess() {
|
||||
val result = divide(10, 5)
|
||||
Assert.assertEquals(Success<Float, String>(2.0f), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testError() {
|
||||
val result = divide(10, 0)
|
||||
Assert.assertEquals(Failure<Float, String>("Division by zero"), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMatchOnSuccess() {
|
||||
val result = divide(10, 5)
|
||||
when (result) {
|
||||
is Success -> {
|
||||
// Expected
|
||||
}
|
||||
is Failure -> Assert.fail("Expected Success")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMatchOnError() {
|
||||
val result = divide(10, 0)
|
||||
when (result) {
|
||||
is Failure -> {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetSuccess() {
|
||||
val result = divide(10, 5)
|
||||
Assert.assertEquals(2.0f, result.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetError() {
|
||||
val result = divide(10, 0)
|
||||
Assert.assertNull(result.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMapOnSuccess() {
|
||||
val result = divide(10, 5)
|
||||
.map { "Result: $it" }
|
||||
Assert.assertEquals(Success<String, String>("Result: 2.0"), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMapOnError() {
|
||||
val result = divide(10, 0)
|
||||
.map { "Result: $it" }
|
||||
Assert.assertEquals(Failure<Float, String>("Division by zero"), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMapFailureOnSuccess() {
|
||||
val result = divide(10, 5)
|
||||
.mapFailure { "Failure: $it" }
|
||||
Assert.assertEquals(Success<Float, String>(2.0f), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMapFailureOnError() {
|
||||
val result = divide(10, 0)
|
||||
.mapFailure { "Failure: $it" }
|
||||
Assert.assertEquals(Failure<Float, String>("Failure: Division by zero"), result)
|
||||
}
|
||||
}
|
@ -27,6 +27,7 @@
|
||||
<org.thymeleaf-version>3.0.7.RELEASE</org.thymeleaf-version>
|
||||
<groovy.version>2.4.12</groovy.version>
|
||||
<freemarker.version>2.3.23</freemarker.version>
|
||||
<jade.version>1.2.5</jade.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -115,6 +116,13 @@
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- jade dependency -->
|
||||
<dependency>
|
||||
<groupId>de.neuland-bfi</groupId>
|
||||
<artifactId>spring-jade4j</artifactId>
|
||||
<version>${jade.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
|
@ -0,0 +1,39 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import de.neuland.jade4j.JadeConfiguration;
|
||||
import de.neuland.jade4j.spring.template.SpringTemplateLoader;
|
||||
import de.neuland.jade4j.spring.view.JadeViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
|
||||
public class JadeTemplateConfiguration {
|
||||
@Bean
|
||||
public SpringTemplateLoader templateLoader() {
|
||||
SpringTemplateLoader templateLoader = new SpringTemplateLoader();
|
||||
templateLoader.setBasePath("/WEB-INF/views/");
|
||||
templateLoader.setSuffix(".jade");
|
||||
return templateLoader;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JadeConfiguration jadeConfiguration() {
|
||||
JadeConfiguration configuration = new JadeConfiguration();
|
||||
configuration.setCaching(false);
|
||||
configuration.setTemplateLoader(templateLoader());
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
JadeViewResolver viewResolver = new JadeViewResolver();
|
||||
viewResolver.setConfiguration(jadeConfiguration());
|
||||
return viewResolver;
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ public class WebInitializer implements WebApplicationInitializer {
|
||||
//ctx.register(ThymeleafConfiguration.class);
|
||||
//ctx.register(FreemarkerConfiguration.class);
|
||||
//ctx.register(GroovyConfiguration.class);
|
||||
//ctx.register(JadeTemplateConfiguration.class);
|
||||
ctx.setServletContext(container);
|
||||
|
||||
// Manage the lifecycle of the root application context
|
||||
|
@ -35,6 +35,12 @@ public class UserController {
|
||||
return "registration-groovy";
|
||||
}
|
||||
|
||||
@GetMapping("/registration-jade")
|
||||
public String getRegistrationJade(Model model) {
|
||||
model.addAttribute("user", new User());
|
||||
return "registration-jade";
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
@ResponseBody
|
||||
public void register(User user){
|
||||
|
@ -0,0 +1,11 @@
|
||||
doctype html
|
||||
html
|
||||
head
|
||||
title User Registration
|
||||
body
|
||||
form(action="register" method="post" )
|
||||
label(for="email") Emailaaaaaaaa:
|
||||
input(type="text" name="email")
|
||||
label(for="password") Password:
|
||||
input(type="password" name="password")
|
||||
input(type="submit" value="Submit")
|
Loading…
x
Reference in New Issue
Block a user