Skip to content

Commit 2688cb9

Browse files
Merge remote-tracking branch 'origin/master'
2 parents e021ca1 + 50990c6 commit 2688cb9

16 files changed

Lines changed: 421 additions & 0 deletions

File tree

‎core-java/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@
123123
<version>${mockito.version}</version>
124124
<scope>test</scope>
125125
</dependency>
126+
127+
<dependency>
128+
<groupId>commons-codec</groupId>
129+
<artifactId>commons-codec</artifactId>
130+
<version>1.10</version>
131+
</dependency>
126132

127133
</dependencies>
128134

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.baeldung.java.md5;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.security.MessageDigest;
9+
import java.security.NoSuchAlgorithmException;
10+
11+
import javax.xml.bind.DatatypeConverter;
12+
13+
import org.apache.commons.codec.digest.DigestUtils;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
17+
import com.google.common.hash.HashCode;
18+
import com.google.common.hash.Hashing;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.*;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
26+
public class JavaMD5Test {
27+
28+
29+
String filename = "src/test/resources/test_md5.txt";
30+
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
31+
32+
String hash = "35454B055CC325EA1AF2126E27707052";
33+
String password = "ILoveJava";
34+
35+
36+
37+
@Test
38+
public void givenPassword_whenHashing_thenVerifying() throws NoSuchAlgorithmException {
39+
String hash = "35454B055CC325EA1AF2126E27707052";
40+
String password = "ILoveJava";
41+
42+
MessageDigest md = MessageDigest.getInstance("MD5");
43+
md.update(password.getBytes());
44+
byte[] digest = md.digest();
45+
String myHash = DatatypeConverter.printHexBinary(digest).toUpperCase();
46+
47+
assertThat(myHash.equals(hash)).isTrue();
48+
}
49+
50+
@Test
51+
public void givenFile_generatingChecksum_thenVerifying() throws NoSuchAlgorithmException, IOException {
52+
String filename = "src/test/resources/test_md5.txt";
53+
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
54+
55+
MessageDigest md = MessageDigest.getInstance("MD5");
56+
md.update(Files.readAllBytes(Paths.get(filename)));
57+
byte[] digest = md.digest();
58+
String myChecksum = DatatypeConverter
59+
.printHexBinary(digest).toUpperCase();
60+
61+
assertThat(myChecksum.equals(checksum)).isTrue();
62+
}
63+
64+
@Test
65+
public void givenPassword_whenHashingUsingCommons_thenVerifying() {
66+
String hash = "35454B055CC325EA1AF2126E27707052";
67+
String password = "ILoveJava";
68+
69+
String md5Hex = DigestUtils
70+
.md5Hex(password).toUpperCase();
71+
72+
assertThat(md5Hex.equals(hash)).isTrue();
73+
}
74+
75+
76+
@Test
77+
public void givenFile_whenChecksumUsingGuava_thenVerifying() throws IOException {
78+
String filename = "src/test/resources/test_md5.txt";
79+
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
80+
81+
HashCode hash = com.google.common.io.Files
82+
.hash(new File(filename), Hashing.md5());
83+
String myChecksum = hash.toString()
84+
.toUpperCase();
85+
86+
assertThat(myChecksum.equals(checksum)).isTrue();
87+
}
88+
89+
90+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<module>spring-mvc-java</module>
9797
<module>spring-mvc-no-xml</module>
9898
<module>spring-mvc-xml</module>
99+
<module>spring-mvc-tiles</module>
99100
<module>spring-openid</module>
100101
<module>spring-protobuf</module>
101102
<module>spring-quartz</module>

‎spring-mvc-tiles/pom.xml‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-mvc-tiles</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<name>spring-mvc-tiles</name>
9+
<description>Integrating Spring MVC with Apache Tiles</description>
10+
11+
<properties>
12+
<springframework.version>4.3.2.RELEASE</springframework.version>
13+
<apachetiles.version>3.0.5</apachetiles.version>
14+
</properties>
15+
16+
<dependencies>
17+
<!-- Spring -->
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-core</artifactId>
21+
<version>${springframework.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-web</artifactId>
26+
<version>${springframework.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-webmvc</artifactId>
31+
<version>${springframework.version}</version>
32+
</dependency>
33+
<!-- Apache Tiles -->
34+
<dependency>
35+
<groupId>org.apache.tiles</groupId>
36+
<artifactId>tiles-jsp</artifactId>
37+
<version>${apachetiles.version}</version>
38+
</dependency>
39+
40+
<!-- Servlet+JSP+JSTL -->
41+
<dependency>
42+
<groupId>javax.servlet</groupId>
43+
<artifactId>javax.servlet-api</artifactId>
44+
<version>3.1.0</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>javax.servlet.jsp</groupId>
48+
<artifactId>javax.servlet.jsp-api</artifactId>
49+
<version>2.3.1</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>javax.servlet</groupId>
53+
<artifactId>jstl</artifactId>
54+
<version>1.2</version>
55+
</dependency>
56+
57+
</dependencies>
58+
59+
<build>
60+
<pluginManagement>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>3.2</version>
66+
<configuration>
67+
<source>1.7</source>
68+
<target>1.7</target>
69+
</configuration>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-war-plugin</artifactId>
74+
<version>2.4</version>
75+
<configuration>
76+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
77+
<warName>spring-mvc-tiles</warName>
78+
<failOnMissingWebXml>false</failOnMissingWebXml>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</pluginManagement>
83+
<finalName>spring-mvc-tiles</finalName>
84+
</build>
85+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.tiles.springmvc;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
8+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
11+
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
12+
13+
@Configuration
14+
@EnableWebMvc
15+
@ComponentScan(basePackages = "com.baeldung.tiles.springmvc")
16+
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
17+
18+
/**
19+
* Configure TilesConfigurer.
20+
*/
21+
@Bean
22+
public TilesConfigurer tilesConfigurer() {
23+
TilesConfigurer tilesConfigurer = new TilesConfigurer();
24+
tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/views/**/tiles.xml" });
25+
tilesConfigurer.setCheckRefresh(true);
26+
return tilesConfigurer;
27+
}
28+
29+
/**
30+
* Configure ViewResolvers to deliver views.
31+
*/
32+
@Override
33+
public void configureViewResolvers(ViewResolverRegistry registry) {
34+
TilesViewResolver viewResolver = new TilesViewResolver();
35+
registry.viewResolver(viewResolver);
36+
}
37+
38+
/**
39+
* Configure ResourceHandlers to serve static resources
40+
*/
41+
42+
@Override
43+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
44+
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
45+
}
46+
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.tiles.springmvc;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.ModelMap;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
@Controller
9+
@RequestMapping("/")
10+
public class ApplicationController {
11+
12+
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
13+
public String homePage(ModelMap model) {
14+
return "home";
15+
}
16+
17+
@RequestMapping(value = { "/apachetiles" }, method = RequestMethod.GET)
18+
public String productsPage(ModelMap model) {
19+
return "apachetiles";
20+
}
21+
22+
@RequestMapping(value = { "/springmvc" }, method = RequestMethod.GET)
23+
public String contactUsPage(ModelMap model) {
24+
return "springmvc";
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.tiles.springmvc;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return new Class[] { ApplicationConfiguration.class };
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return null;
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[] { "/" };
20+
}
21+
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Apache Tiles</title>
8+
</head>
9+
<body>
10+
<h2>Tiles with Spring MVC Demo</h2>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Home</title>
8+
</head>
9+
<body>
10+
<h2>Welcome to Apache Tiles integration with Spring MVC</h2>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Spring MVC</title>
8+
</head>
9+
<body>
10+
<h2>Spring MVC configured to work with Apache Tiles</h2>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)