Merge branch 'master' into BAEL-1105

This commit is contained in:
Mohit Sinha 2017-08-15 14:53:33 +05:30
commit 43d9bf5655
28 changed files with 632 additions and 72 deletions

View File

@ -0,0 +1,13 @@
package com.baeldung.concurrent.volatilekeyword;
public class SharedObject {
private volatile int count=0;
public void increamentCount(){
count++;
}
public int getCount(){
return count;
}
}

View File

@ -0,0 +1,99 @@
package com.baeldung.concurrent.volatilekeyword;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class SharedObjectManualTest {
SharedObject sharedObject;
int valueReadByThread2;
int valueReadByThread3;
@Before
public void setUp(){
sharedObject = new SharedObject();
}
@Test
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
Thread writer = new Thread(){
public void run(){
sharedObject.increamentCount();
}
};
writer.start();
Thread readerOne = new Thread(){
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
valueReadByThread2= sharedObject.getCount();
}
};
readerOne.start();
Thread readerTwo = new Thread(){
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
valueReadByThread3=sharedObject.getCount();
}
};
readerTwo.start();
assertEquals(1,valueReadByThread2);
assertEquals(1,valueReadByThread3);
}
@Test
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
Thread writerOne = new Thread(){
public void run(){
sharedObject.increamentCount();
}
};
writerOne.start();
Thread.sleep(100);
Thread writerTwo = new Thread(){
public void run(){
sharedObject.increamentCount();
}
};
writerTwo.start();
Thread.sleep(100);
Thread readerOne = new Thread(){
public void run(){
valueReadByThread2= sharedObject.getCount();
}
};
readerOne.start();
Thread readerTwo = new Thread(){
public void run(){
valueReadByThread3=sharedObject.getCount();
}
};
readerTwo.start();
assertEquals(2,valueReadByThread2);
assertEquals(2,valueReadByThread3);
}
@After
public void cleanup(){
sharedObject = null;
}
}

View File

@ -0,0 +1,78 @@
package com.baeldung.javanetworking.uriurl;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import static org.junit.Assert.*;
import org.junit.Test;
public class URIvsURLUnitTest {
@Test
public void whenCreatingURIs_thenSameInfo() throws URISyntaxException {
URI firstURI = new URI("somescheme://theuser:thepassword@someauthority:80/some/path?thequery#somefragment");
URI secondURI = new URI("somescheme", "theuser:thepassword", "someuthority", 80, "/some/path", "thequery", "somefragment");
assertEquals(firstURI.getScheme(), secondURI.getScheme());
assertEquals(firstURI.getPath(), secondURI.getPath());
}
@Test
public void whenCreatingURLs_thenSameInfo() throws MalformedURLException {
URL firstURL = new URL("http://theuser:thepassword@somehost:80/path/to/file?thequery#somefragment");
URL secondURL = new URL("http", "somehost", 80, "/path/to/file");
assertEquals(firstURL.getHost(), secondURL.getHost());
assertEquals(firstURL.getPath(), secondURL.getPath());
}
@Test
public void whenCreatingURI_thenCorrect() {
URI uri = URI.create("urn:isbn:1234567890");
assertNotNull(uri);
}
@Test(expected = MalformedURLException.class)
public void whenCreatingURLs_thenException() throws MalformedURLException {
URL theURL = new URL("otherprotocol://somehost/path/to/file");
assertNotNull(theURL);
}
@Test
public void givenObjects_whenConverting_thenCorrect() throws MalformedURLException, URISyntaxException {
String aURIString = "http://somehost:80/path?thequery";
URI uri = new URI(aURIString);
URL url = new URL(aURIString);
URL toURL = uri.toURL();
URI toURI = url.toURI();
assertNotNull(url);
assertNotNull(uri);
assertEquals(toURL.toString(), toURI.toString());
}
@Test(expected = MalformedURLException.class)
public void givenURI_whenConvertingToURL_thenException() throws MalformedURLException, URISyntaxException {
URI uri = new URI("somescheme://someauthority/path?thequery");
URL url = uri.toURL();
assertNotNull(url);
}
@Test
public void givenURL_whenGettingContents_thenCorrect() throws MalformedURLException, IOException {
URL url = new URL("http://courses.baeldung.com");
String contents = IOUtils.toString(url.openStream());
assertTrue(contents.contains("<!DOCTYPE html>"));
}
}

View File

@ -1,19 +1,23 @@
package com.baeldung.jackson.test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Test;
import com.baeldung.jackson.deserialization.ItemDeserializer;
import com.baeldung.jackson.dtos.Item;
import com.baeldung.jackson.dtos.ItemWithSerializer;
import com.baeldung.jackson.dtos.MyDto;
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
@ -21,6 +25,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.module.SimpleModule;
@ -165,4 +170,35 @@ public class JacksonDeserializationUnitTest {
assertThat(readValue, notNullValue());
}
@Test
public void whenDeserialisingZonedDateTimeWithDefaults_thenTimeZoneIsNotPreserved() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// construct a new instance of ZonedDateTime
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
String converted = objectMapper.writeValueAsString(now);
// restore an instance of ZonedDateTime from String
ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
System.out.println("serialized: " + now);
System.out.println("restored: " + restored);
assertThat(now, is(not(restored)));
}
@Test
public void whenDeserialisingZonedDateTimeWithFeaturesDisabled_thenTimeZoneIsPreserved() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
// construct a new instance of ZonedDateTime
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
String converted = objectMapper.writeValueAsString(now);
// restore an instance of ZonedDateTime from String
ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
System.out.println("serialized: " + now);
System.out.println("restored: " + restored);
assertThat(now, is(restored));
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.spring_batch_intro;
package org.baeldung.batch;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;

View File

@ -1,11 +1,8 @@
package org.baeldung.spring_batch_intro;
package org.baeldung.batch;
import java.net.MalformedURLException;
import java.text.ParseException;
import org.baeldung.spring_batch_intro.model.Transaction;
import org.baeldung.spring_batch_intro.service.CustomItemProcessor;
import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper;
import org.baeldung.batch.model.Transaction;
import org.baeldung.batch.service.CustomItemProcessor;
import org.baeldung.batch.service.RecordFieldSetMapper;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
@ -26,6 +23,9 @@ import org.springframework.core.io.Resource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import java.net.MalformedURLException;
import java.text.ParseException;
public class SpringBatchConfig {
@Autowired
private JobBuilderFactory jobs;
@ -43,7 +43,7 @@ public class SpringBatchConfig {
public ItemReader<Transaction> itemReader() throws UnexpectedInputException, ParseException {
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
String[] tokens = { "username", "userid", "transactiondate", "amount" };
String[] tokens = {"username", "userid", "transactiondate", "amount"};
tokenizer.setNames(tokens);
reader.setResource(inputCsv);
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>();
@ -71,13 +71,13 @@ public class SpringBatchConfig {
@Bean
public Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[] { Transaction.class });
marshaller.setClassesToBeBound(Transaction.class);
return marshaller;
}
@Bean
protected Step step1(ItemReader<Transaction> reader, ItemProcessor<Transaction, Transaction> processor, ItemWriter<Transaction> writer) {
return steps.get("step1").<Transaction, Transaction> chunk(10).reader(reader).processor(processor).writer(writer).build();
return steps.get("step1").<Transaction, Transaction>chunk(10).reader(reader).processor(processor).writer(writer).build();
}
@Bean(name = "firstBatchJob")

View File

@ -1,4 +1,4 @@
package org.baeldung.spring_batch_intro;
package org.baeldung.batch;
import java.net.MalformedURLException;

View File

@ -1,4 +1,4 @@
package org.baeldung.spring_batch_intro.model;
package org.baeldung.batch.model;
import java.util.Date;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.baeldung.spring_batch_intro.partitioner;
package org.baeldung.batch.partitioner;
import java.util.HashMap;
import java.util.Map;

View File

@ -1,13 +1,7 @@
package org.baeldung.spring_batch_intro.partitioner;
package org.baeldung.batch.partitioner;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;
import javax.sql.DataSource;
import org.baeldung.spring_batch_intro.model.Transaction;
import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper;
import org.baeldung.batch.model.Transaction;
import org.baeldung.batch.service.RecordFieldSetMapper;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@ -33,7 +27,6 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.oxm.Marshaller;
@ -41,6 +34,11 @@ import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;
@Configuration
@EnableBatchProcessing
public class SpringbatchPartitionConfig {
@ -57,26 +55,26 @@ public class SpringbatchPartitionConfig {
@Bean(name = "partitionerJob")
public Job partitionerJob() throws UnexpectedInputException, MalformedURLException, ParseException {
return jobs.get("partitionerJob")
.start(partitionStep())
.build();
.start(partitionStep())
.build();
}
@Bean
public Step partitionStep() throws UnexpectedInputException, MalformedURLException, ParseException {
return steps.get("partitionStep")
.partitioner("slaveStep", partitioner())
.step(slaveStep())
.taskExecutor(taskExecutor())
.build();
.partitioner("slaveStep", partitioner())
.step(slaveStep())
.taskExecutor(taskExecutor())
.build();
}
@Bean
public Step slaveStep() throws UnexpectedInputException, MalformedURLException, ParseException {
return steps.get("slaveStep")
.<Transaction, Transaction> chunk(1)
.reader(itemReader(null))
.writer(itemWriter(marshaller(), null))
.build();
.<Transaction, Transaction>chunk(1)
.reader(itemReader(null))
.writer(itemWriter(marshaller(), null))
.build();
}
@Bean
@ -95,12 +93,12 @@ public class SpringbatchPartitionConfig {
@Bean
@StepScope
public FlatFileItemReader<Transaction> itemReader(@Value("#{stepExecutionContext[fileName]}") String filename) throws UnexpectedInputException, ParseException {
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
String[] tokens = { "username", "userid", "transactiondate", "amount" };
String[] tokens = {"username", "userid", "transactiondate", "amount"};
tokenizer.setNames(tokens);
reader.setResource(new ClassPathResource("input/partitioner/" + filename));
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>();
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
lineMapper.setLineTokenizer(tokenizer);
lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
reader.setLinesToSkip(1);
@ -121,7 +119,7 @@ public class SpringbatchPartitionConfig {
@Bean
public Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[] { Transaction.class });
marshaller.setClassesToBeBound(Transaction.class);
return marshaller;
}
@ -142,16 +140,15 @@ public class SpringbatchPartitionConfig {
// JobRepositoryFactoryBean's methods Throws Generic Exception,
// it would have been better to have a specific one
factory.afterPropertiesSet();
return (JobRepository) factory.getObject();
return factory.getObject();
}
private DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
.addScript("classpath:org/springframework/batch/core/schema-h2.sql")
.build();
return db;
return builder.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
.addScript("classpath:org/springframework/batch/core/schema-h2.sql")
.build();
}
private PlatformTransactionManager getTransactionManager() {

View File

@ -1,4 +1,4 @@
package org.baeldung.spring_batch_intro.partitioner;
package org.baeldung.batch.partitioner;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;

View File

@ -1,6 +1,6 @@
package org.baeldung.spring_batch_intro.service;
package org.baeldung.batch.service;
import org.baeldung.spring_batch_intro.model.Transaction;
import org.baeldung.batch.model.Transaction;
import org.springframework.batch.item.ItemProcessor;
public class CustomItemProcessor implements ItemProcessor<Transaction, Transaction> {

View File

@ -1,9 +1,9 @@
package org.baeldung.spring_batch_intro.service;
package org.baeldung.batch.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.baeldung.spring_batch_intro.model.Transaction;
import org.baeldung.batch.model.Transaction;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

View File

@ -22,14 +22,14 @@
</property>
<property name="fieldSetMapper">
<bean
class="org.baeldung.spring_batch_intro.service.RecordFieldSetMapper" />
class="org.baeldung.batch.service.RecordFieldSetMapper" />
</property>
</bean>
</property>
<property name="linesToSkip" value="1" />
</bean>
<bean id="itemProcessor" class="org.baeldung.spring_batch_intro.service.CustomItemProcessor" />
<bean id="itemProcessor" class="org.baeldung.batch.service.CustomItemProcessor" />
<bean id="itemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" value="file:xml/output.xml" />
@ -40,7 +40,7 @@
<bean id="recordMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>org.baeldung.spring_batch_intro.model.Transaction</value>
<value>org.baeldung.batch.model.Transaction</value>
</list>
</property>
</bean>

View File

@ -17,6 +17,25 @@
<version>1.5.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- add dependency management if you have a different parent pom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -1,2 +1,18 @@
## The Module Holds Sources for the Following Articles
- [Automatic Property Expansion with Spring Boot] (http://www.baeldung.com/property-expansion-spring-boot)
- [Automatic Property Expansion with Spring Boot](http://www.baeldung.com/property-expansion-spring-boot)
### Module property-exp-default-config
This module contains both a standard Maven Spring Boot build and the Gradle build which is Maven compatible.
In order to execute the Maven example, run the following command:
`mvn spring-boot:run`
To execute the Gradle example:
`gradle bootRun`
### Module property-exp-custom-config
This project is not using the standard Spring Boot parent and is configured manually. Run the following command:
`mvn exec:java`

View File

@ -9,7 +9,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>property-exp-custom</artifactId>
<artifactId>property-exp-custom-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

View File

@ -11,7 +11,7 @@
</parent>
<groupId>com.baeldung</groupId>
<artifactId>property-exp-default</artifactId>
<artifactId>property-exp-default-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

View File

@ -1,6 +1,6 @@
<?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">
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>
@ -97,6 +97,18 @@
<scope>test</scope>
</dependency>
<!-- Spring Data LDAP -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-ldap</artifactId>
<version>1.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.6.RELEASE</version>
</dependency>
</dependencies>
<profiles>

View File

@ -0,0 +1,55 @@
package com.baeldung.ldap.data.repository;
import javax.naming.Name;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
@Entry(base = "ou=users", objectClasses = { "person", "inetOrgPerson", "top" })
public class User {
@Id
private Name id;
private @Attribute(name = "cn") String username;
private @Attribute(name = "sn") String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public Name getId() {
return id;
}
public void setId(Name id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return username;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.ldap.data.repository;
import java.util.List;
import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends LdapRepository<User> {
User findByUsername(String username);
User findByUsernameAndPassword(String username, String password);
List<User> findByUsernameLikeIgnoreCase(String username);
}

View File

@ -0,0 +1,87 @@
package com.baeldung.ldap.data.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder;
import javax.naming.Name;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
public class LdapClient {
@Autowired
private Environment env;
@Autowired
private ContextSource contextSource;
@Autowired
private LdapTemplate ldapTemplate;
public void authenticate(final String username, final String password) {
contextSource.getContext("cn=" + username + ",ou=users," + env.getRequiredProperty("ldap.partitionSuffix"), password);
}
public List<String> search(final String username) {
return ldapTemplate.search(
"ou=users",
"cn=" + username,
(AttributesMapper<String>) attrs -> (String) attrs
.get("cn")
.get());
}
public void create(final String username, final String password) {
Name dn = LdapNameBuilder
.newInstance()
.add("ou", "users")
.add("cn", username)
.build();
DirContextAdapter context = new DirContextAdapter(dn);
context.setAttributeValues("objectclass", new String[]{"top", "person", "organizationalPerson", "inetOrgPerson"});
context.setAttributeValue("cn", username);
context.setAttributeValue("sn", username);
context.setAttributeValue("userPassword", digestSHA(password));
ldapTemplate.bind(context);
}
public void modify(final String username, final String password) {
Name dn = LdapNameBuilder
.newInstance()
.add("ou", "users")
.add("cn", username)
.build();
DirContextOperations context = ldapTemplate.lookupContext(dn);
context.setAttributeValues("objectclass", new String[]{"top", "person", "organizationalPerson", "inetOrgPerson"});
context.setAttributeValue("cn", username);
context.setAttributeValue("sn", username);
context.setAttributeValue("userPassword", digestSHA(password));
ldapTemplate.modifyAttributes(context);
}
private String digestSHA(final String password) {
String base64;
try {
MessageDigest digest = MessageDigest.getInstance("SHA");
digest.update(password.getBytes());
base64 = Base64
.getEncoder()
.encodeToString(digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return "{SHA}" + base64;
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.ldap.data.service;
import com.baeldung.ldap.data.repository.User;
import com.baeldung.ldap.data.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.stereotype.Service;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public Boolean authenticate(final String username, final String password) {
User user = userRepository.findByUsernameAndPassword(username, password);
return user != null;
}
public List<String> search(final String username) {
List<User> userList = userRepository.findByUsernameLikeIgnoreCase(username);
if (userList == null) {
return Collections.emptyList();
}
return userList.stream()
.map(User::getUsername)
.collect(Collectors.toList());
}
public void create(final String username, final String password) {
User newUser = new User(username,digestSHA(password));
newUser.setId(LdapUtils.emptyLdapName());
userRepository.save(newUser);
}
public void modify(final String username, final String password) {
User user = userRepository.findByUsername(username);
user.setPassword(password);
userRepository.save(user);
}
private String digestSHA(final String password) {
String base64;
try {
MessageDigest digest = MessageDigest.getInstance("SHA");
digest.update(password.getBytes());
base64 = Base64.getEncoder()
.encodeToString(digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return "{SHA}" + base64;
}
}

View File

@ -1,5 +1,6 @@
package com.baeldung.ldap.javaconfig;
import com.baeldung.ldap.client.LdapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -7,15 +8,15 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.ldap.repository.config.EnableLdapRepositories;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import com.baeldung.ldap.client.LdapClient;
@Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = { "com.baeldung.ldap.*" })
@ComponentScan(basePackages = {"com.baeldung.ldap.*"})
@Profile("default")
@EnableLdapRepositories(basePackages = "com.baeldung.ldap.**")
public class AppConfig {
@Autowired

View File

@ -7,13 +7,13 @@
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<logger name="org.springframework" level="WARN"/>
<logger name="org.springframework.transaction" level="WARN"/>
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<logger name="org.springframework.web.servlet.mvc" level="WARN"/>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -1,7 +1,6 @@
package com.baeldung.ldap.client;
import java.util.List;
import com.baeldung.ldap.javaconfig.TestConfig;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
@ -13,11 +12,11 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.ldap.javaconfig.TestConfig;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("testlive")
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
public class LdapClientLiveTest {
private static final String USER2 = "TEST02";

View File

@ -0,0 +1,67 @@
package com.baeldung.ldap.client;
import com.baeldung.ldap.data.service.UserService;
import com.baeldung.ldap.javaconfig.TestConfig;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("testlive")
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
public class LdapDataRepositoryTest {
private static final String USER2 = "TEST02";
private static final String USER3 = "TEST03";
private static final String USER4 = "TEST04";
private static final String USER2_PWD = "TEST02";
private static final String USER3_PWD = "TEST03";
private static final String USER4_PWD = "TEST04";
private static final String SEARCH_STRING = "TEST*";
@Autowired
private UserService userService;
@Test
public void givenLdapClient_whenCorrectCredentials_thenSuccessfulLogin() {
Boolean isValid = userService.authenticate(USER3, USER3_PWD);
assertEquals(true, isValid);
}
@Test
public void givenLdapClient_whenIncorrectCredentials_thenFailedLogin() {
Boolean isValid = userService.authenticate(USER3, USER2_PWD);
assertEquals(false, isValid);
}
@Test
public void givenLdapClient_whenCorrectSearchFilter_thenEntriesReturned() {
List<String> userList = userService.search(SEARCH_STRING);
assertThat(userList, Matchers.containsInAnyOrder(USER2, USER3));
}
@Test
public void givenLdapClientNotExists_whenDataProvided_thenNewUserCreated() {
userService.create(USER4, USER4_PWD);
userService.authenticate(USER4, USER4_PWD);
}
@Test
public void givenLdapClientExists_whenDataProvided_thenExistingUserModified() {
userService.modify(USER2, USER3_PWD);
userService.authenticate(USER2, USER3_PWD);
}
}

View File

@ -1,5 +1,6 @@
package com.baeldung.ldap.javaconfig;
import com.baeldung.ldap.client.LdapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -8,15 +9,15 @@ import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.ldap.repository.config.EnableLdapRepositories;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.test.TestContextSourceFactoryBean;
import com.baeldung.ldap.client.LdapClient;
@Configuration
@PropertySource("classpath:test_application.properties")
@ComponentScan(basePackages = { "com.baeldung.ldap.*" })
@ComponentScan(basePackages = {"com.baeldung.ldap.*"})
@EnableLdapRepositories(basePackages = "com.baeldung.ldap.**")
@Profile("testlive")
public class TestConfig {
@Autowired