Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
38989db
code for gRPC
rockoder Jul 28, 2017
b25f17f
Merge branch 'master' of https://github.com/rockoder/tutorials into r…
slavisa-baeldung Jul 31, 2017
6fcbc9b
BAEL-1022 - minor fixes
slavisa-baeldung Aug 1, 2017
b0e0563
rxjava custom operator (#2352)
Doha2012 Aug 1, 2017
8214c8d
Refactor
pivovarit Aug 1, 2017
4ff0768
Refactor
pivovarit Aug 1, 2017
aeec8f5
Merge pull request #2353 from eugenp/BAEL-1022-grpc-ganesh
slavisa-baeldung Aug 1, 2017
5e8022f
spring template engines (#2326)
lor6 Aug 2, 2017
5ba675b
Merge pull request #2355 from eugenp/rxjava_ref
Doha2012 Aug 2, 2017
43ce43c
Rxjava refactor (#2358)
pivovarit Aug 2, 2017
b6b077b
minor fix (#2359)
Doha2012 Aug 2, 2017
f02d117
BAEL-887 How to collect a Java Stream to an immutable collection? (#2…
yasin3061 Aug 2, 2017
8137c20
Delegated Properties in Kotlin (#2357)
sazzer Aug 2, 2017
8e9c36c
fix spring config (#2364)
Doha2012 Aug 3, 2017
71f1a2b
add jade engine example (#2365)
lor6 Aug 3, 2017
dc78aac
BAEL-884 Spring Security in JEE App (#2362)
SeunMatt Aug 4, 2017
b5835c5
Updated PR for BAEL-884 (#2372)
SeunMatt Aug 4, 2017
6a9e13c
BAEL-892: Example of Sealed Classes (#2373)
sazzer Aug 4, 2017
891b4eb
Remove jooq module (#2375)
pivovarit Aug 5, 2017
7114e39
fix spring config (#2374)
Doha2012 Aug 5, 2017
f913859
package fixes
Aug 5, 2017
4d8f92e
formatting cleanup
Aug 5, 2017
67448de
enabling jee7 in the main build
Aug 5, 2017
ae827f3
Refactor hashcode() samples (#2377)
pivovarit Aug 5, 2017
40b3cc7
Bootique module (#2376)
AbhinabKanrar Aug 5, 2017
b6e59c2
Refactor bootique (#2380)
pivovarit Aug 6, 2017
5383b2b
add valueswithdefaults example (#2369)
chrisoberle Aug 6, 2017
ddee04f
BAEL-644: RSS with Rome (#2385)
KevinGilmore Aug 7, 2017
a1388b6
BAEL-378 A Guide to Activiti with Java (#2245)
mansi2392 Aug 7, 2017
d11d9ad
fix spring config (#2386)
Doha2012 Aug 7, 2017
2263b24
BAEL-764 Renaming Modules as per the review (#2371)
roman-seleznov Aug 7, 2017
1824320
[BAEL-1066] Implementing Singleton pattern in Java (#2361)
Aug 7, 2017
53433a0
README updates (#2389)
KevinGilmore Aug 7, 2017
415b80d
BAEL-1072 - Difference between Proxy, Decorator, Adapter, and Bridge …
ramansahasi Aug 8, 2017
2036138
BAEL-1044 - Introduction to NoException (#2394)
egoebelbecker Aug 9, 2017
69f3e11
minor fix (#2398)
Doha2012 Aug 9, 2017
b5478f7
Build opt 9 08 2017 (#2397)
pivovarit Aug 9, 2017
1a001a5
Activiti test as integration (#2399)
pivovarit Aug 9, 2017
ca70e9f
Changes for BAEL-332 (#2396)
khatwaniNikhil Aug 9, 2017
71ef6b2
code for Java 9 Objects API (#2408)
sanaulla123 Aug 10, 2017
146b633
New artifacts for BAEL-1081. (#2392)
Aug 10, 2017
c3de3db
BAEL 1035 - Introduction to Eclipse Collections (#2395)
Aug 10, 2017
accb430
java 9 objects api sample (#2413)
sanaulla123 Aug 10, 2017
69d8c10
BAEL-1044 - Introduction to NoException (#2412)
egoebelbecker Aug 10, 2017
127dd73
BAEL-243 Spring Batch using Partitioner (#2419)
parthkaria Aug 11, 2017
dc3589a
fix spring-boot module (#2422)
Doha2012 Aug 11, 2017
a4132df
fix pom (#2423)
Doha2012 Aug 12, 2017
310e89e
BAEL-1035 Introduction to Eclipse Collections (#2421)
Aug 12, 2017
9b2e8ec
BAEL-1035 Introduction to Eclipse Collections (#2425)
Aug 12, 2017
e13204c
Introduction to EJB JNDI Lookup on WildFly Application Server - Aleja…
Aug 13, 2017
299432d
updating Java9ObjectsAPIUnitTest (#2430)
sanaulla123 Aug 13, 2017
290e759
BAEL1053 - deep20jain@gmail.com - Broadcasting and Multicasting in Ja…
deep20jain Aug 14, 2017
87cdf5d
Spring Data Ldap
Aug 14, 2017
43357b0
Partitioner Refactor (#2434)
pivovarit Aug 14, 2017
8d9a7b0
BAEL-764 Automatic Property Expansion with Spring Boot (#2435)
roman-seleznov Aug 14, 2017
c39abbf
Merge pull request #2432 from half-wit4u/master
slavisa-baeldung Aug 14, 2017
99eee4d
BAEL-1077 Guide to volatile keyword (#2433)
baljeet20 Aug 14, 2017
58469d7
BAEL-864 Difference between URL and URI (#2438)
juarezjaramillo Aug 14, 2017
0de69c3
add dependency management (#2382)
lor6 Aug 14, 2017
dc9ecc1
Added test cases for BAEL-897 (#2436)
khatwaniNikhil Aug 14, 2017
bcc122b
Refactor Batch (#2439)
pivovarit Aug 14, 2017
9308bae
BAEL-1105: Apache Commons CSV, Test cases
mohitsinha Aug 15, 2017
43d9bf5
Merge branch 'master' into BAEL-1105
mohitsinha Aug 15, 2017
299be4a
BAEL-1105: Apache Commons CSV, Changed name of testcase
mohitsinha Aug 15, 2017
00e3483
Bael 1053 - Removing java from package name (#2443)
deep20jain Aug 15, 2017
1a9c33f
Commit source code for BAEL-698 (#2429)
buddhini81 Aug 15, 2017
3b5d958
Example Code for Apache Shiro (#2441)
SeunMatt Aug 15, 2017
6db067d
Refactor volatile (#2444)
pivovarit Aug 15, 2017
5d20115
Merge pull request #2446 from mohitsinha/BAEL-1105
evandor Aug 15, 2017
7ec1db1
Sample code for article - JIRA: (BAEL-1079) Java 8 Streams distinctBy…
Aug 16, 2017
da3273a
Binary Search algorithm (#2448)
techPackets Aug 16, 2017
eeb89df
Merge pull request #2447 from earth001/master
evandor Aug 16, 2017
491fc88
Update Apache Shiro Example code (#2453)
SeunMatt Aug 16, 2017
4226100
moved test file to correct location (#2437)
nbosecker Aug 16, 2017
dc105bc
BAEL-812: List of Rules Engines in Java (#2319)
felipeazv Aug 16, 2017
c8c0c1e
undertow (#2426)
AbhinabKanrar Aug 16, 2017
04689cc
pull 16.08 (#2454)
cody756 Aug 17, 2017
ee9152a
BAEL-1014 [Spring MVC with Kotlin] (#2459)
Euniceadu Aug 19, 2017
f2efbd9
BAEL-812: moving examples to rule-engines folder (#2461)
felipeazv Aug 19, 2017
a3cbc25
Undertow refactor (#2464)
pivovarit Aug 19, 2017
b65bcca
Bael 817 (#2411)
dimitarsazdovski Aug 20, 2017
a0198e1
upgrade jackson (#2465)
Doha2012 Aug 20, 2017
a02e758
thread pools examples (#2400)
lor6 Aug 20, 2017
b7ad275
minor fix (#2468)
Doha2012 Aug 20, 2017
e6c2fd3
Revert "BAEL-812: List of Rules Engines in Java (#2319)" (#2455)
adamd1985 Aug 20, 2017
def5758
easy code added for simplicity (#2473)
AbhinabKanrar Aug 21, 2017
82c8d88
simplifying (#2476)
AbhinabKanrar Aug 21, 2017
84cbc58
Undertow refactor (#2462)
pivovarit Aug 21, 2017
6d7fa19
BAEL-1014 [Spring MVC with Kotlin] (#2475)
Euniceadu Aug 21, 2017
04fa178
reladomo example (#2469)
lor6 Aug 21, 2017
706b3d6
Example for a GraphQL Application in Spring Boot (#2477)
sazzer Aug 21, 2017
802c0b0
#BAEL-1025 (#2445)
aietcn Aug 21, 2017
822e868
lambda example (#2480)
lor6 Aug 21, 2017
8c0ce48
BAEL-1045 Lambda Behave (#2456)
mansi2392 Aug 21, 2017
8a34433
Optimize libraries (#2458)
pivovarit Aug 22, 2017
dc86767
logback example app (#2354)
lor6 Aug 22, 2017
ccba3de
Delete and add new test for BAEL-698 (#2479)
buddhini81 Aug 23, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
26 changes: 26 additions & 0 deletions algorithms/src/main/java/com/baeldung/algorithms/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class BinarySearch {

public int runBinarySearch() {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;

int low = 0;
int high = sortedArray.length - 1;
int index = Integer.MAX_VALUE;

while (low <= high) {

int mid = (low + high) / 2;

if (sortedArray[mid] < key) {
low = mid + 1;
} else if (sortedArray[mid] > key) {
high = mid - 1;
} else if (sortedArray[mid] == key) {
index = mid;
break;
}
}
return index;
}
}
14 changes: 14 additions & 0 deletions algorithms/src/test/java/algorithms/BinarySearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import org.junit.Assert;
import org.junit.Test;


public class BinarySearchTest {

@Test
public void givenASortedArrayOfIntegers_whenBinarySearchRunForANumber_thenGetIndexOfTheNumber() {
BinarySearch binSearch = new BinarySearch();
int expectedIndexForSearchKey = 7;
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearch());
}

}
4 changes: 4 additions & 0 deletions apache-shiro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/.idea/
/target/
/apache-shiro.iml
Empty file added apache-shiro/README.md
Empty file.
65 changes: 65 additions & 0 deletions apache-shiro/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.baeldung</groupId>
<artifactId>apache-shiro</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<properties>
<apache-shiro-core-version>1.4.0</apache-shiro-core-version>
<log4j-version>1.2.17</log4j-version>
<slf4j-version>1.7.25</slf4j-version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${apache-shiro-core-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>


</project>
85 changes: 85 additions & 0 deletions apache-shiro/src/main/java/com/baeldung/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.baeldung;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

private static final transient Logger log = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {

Realm realm = new MyCustomRealm();
SecurityManager securityManager = new DefaultSecurityManager(realm);

SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();

if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token
= new UsernamePasswordToken("user", "password");
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
log.error("Username Not Found!", uae);
} catch (IncorrectCredentialsException ice) {
log.error("Invalid Credentials!", ice);
} catch (LockedAccountException lae) {
log.error("Your Account is Locked!", lae);
} catch (AuthenticationException ae) {
log.error("Unexpected Error!", ae);
}
}

log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

if (currentUser.hasRole("admin")) {
log.info("Welcome Admin");
} else if(currentUser.hasRole("editor")) {
log.info("Welcome, Editor!");
} else if(currentUser.hasRole("author")) {
log.info("Welcome, Author");
} else {
log.info("Welcome, Guest");
}

if(currentUser.isPermitted("articles:compose")) {
log.info("You can compose an article");
} else {
log.info("You are not permitted to compose an article!");
}

if(currentUser.isPermitted("articles:save")) {
log.info("You can save articles");
} else {
log.info("You can not save articles");
}

if(currentUser.isPermitted("articles:publish")) {
log.info("You can publish articles");
} else {
log.info("You can not publish articles");
}

Session session = currentUser.getSession();
session.setAttribute("key", "value");
String value = (String) session.getAttribute("key");
if (value.equals("value")) {
log.info("Retrieved the correct value! [" + value + "]");
}

currentUser.logout();

System.exit(0);
}

}
102 changes: 102 additions & 0 deletions apache-shiro/src/main/java/com/baeldung/MyCustomRealm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.baeldung;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

public class MyCustomRealm extends JdbcRealm {

private Map<String, String> credentials = new HashMap<>();
private Map<String, Set<String>> roles = new HashMap<>();
private Map<String, Set<String>> perm = new HashMap<>();

{
credentials.put("user", "password");
credentials.put("user2", "password2");
credentials.put("user3", "password3");

roles.put("user", new HashSet<>(Arrays.asList("admin")));
roles.put("user2", new HashSet<>(Arrays.asList("editor")));
roles.put("user3", new HashSet<>(Arrays.asList("author")));

perm.put("admin", new HashSet<>(Arrays.asList("*")));
perm.put("editor", new HashSet<>(Arrays.asList("articles:*")));
perm.put("author",
new HashSet<>(Arrays.asList("articles:compose",
"articles:save")));

}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
throws AuthenticationException {

UsernamePasswordToken uToken = (UsernamePasswordToken) token;

if(uToken.getUsername() == null
|| uToken.getUsername().isEmpty()
|| !credentials.containsKey(uToken.getUsername())
) {
throw new UnknownAccountException("username not found!");
}


return new SimpleAuthenticationInfo(
uToken.getUsername(), credentials.get(uToken.getUsername()),
getName());
}

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
Set<String> roleNames = new HashSet<>();
Set<String> permissions = new HashSet<>();

principals.forEach(p -> {
try {
Set<String> roles = getRoleNamesForUser(null, (String) p);
roleNames.addAll(roles);
permissions.addAll(getPermissions(null, null,roles));
} catch (SQLException e) {
e.printStackTrace();
}
});

SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
info.setStringPermissions(permissions);
return info;
}

@Override
protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException {
if(!roles.containsKey(username)) {
throw new SQLException("username not found!");
}

return roles.get(username);
}

@Override
protected Set<String> getPermissions(Connection conn, String username, Collection<String> roleNames) throws SQLException {
for (String role : roleNames) {
if (!perm.containsKey(role)) {
throw new SQLException("role not found!");
}
}

Set<String> finalSet = new HashSet<>();
for (String role : roleNames) {
finalSet.addAll(perm.get(role));
}

return finalSet;
}

}
12 changes: 12 additions & 0 deletions apache-shiro/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n

log4j.logger.org.apache=WARN

log4j.logger.org.apache.shiro=INFO

log4j.logger.org.apache.shiro.util.ThreadContext=WARN
log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN
9 changes: 9 additions & 0 deletions apache-shiro/src/main/resources/shiro.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[users]
user = password,admin
user2 = password2,editor
user3 = password3,author

[roles]
admin = *
editor = articles:*
author = articles:compose,articles:save
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource.loader=webapp
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
webapp.resource.loader.path = .
webapp.resource.loader.path =
webapp.resource.loader.cache = true
3 changes: 2 additions & 1 deletion asciidoctor/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### Relevant articles

- [Introduction to Asciidoctor](http://www.baeldung.com/introduction-to-asciidoctor)
- [Introduction to Asciidoctor](http://www.baeldung.com/introduction-to-asciidoctor)
- [Generating a Book with Asciidoctor](http://www.baeldung.com/asciidoctor-book)
11 changes: 11 additions & 0 deletions bootique/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
log:
level: warn
appenders:
- type: file
logFormat: '%c{20}: %m%n'
file: /home/logger.log

jetty:
context: /hello
connector:
port: 10001
Loading