Merge remote-tracking branch 'origin/master'

This commit is contained in:
slavisa-baeldung
2017-04-14 06:29:12 +01:00
15 changed files with 609 additions and 73 deletions
+8 -2
View File
@@ -170,7 +170,13 @@
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>1.1</version>
</dependency>
</dependency>
<dependency>
<groupId>org.owasp.esapi</groupId>
<artifactId>esapi</artifactId>
<version>2.1.0.1</version>
</dependency>
</dependencies>
@@ -391,4 +397,4 @@
</properties>
</project>
</project>
@@ -0,0 +1,28 @@
package com.baeldung.logforging;
import org.owasp.esapi.ESAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogForgingDemo {
private final Logger logger = LoggerFactory.getLogger(LogForgingDemo.class);
public void addLog(String amount) {
logger.info("Amount credited = {}", amount);
}
public static void main(String[] args) {
LogForgingDemo demo = new LogForgingDemo();
demo.addLog(String.valueOf(300));
demo.addLog(String.valueOf(300 + "\n\nweb - 2017-04-12 17:47:08,957 [main] INFO Amount reversed successfully"));
demo.addLog(String.valueOf(encode(300 + "\n\nweb - 2017-04-12 17:47:08,957 [main] INFO Amount reversed successfully")));
}
public static String encode(String message) {
message = message.replace('\n', '_').replace('\r', '_').replace('\t', '_');
message = ESAPI.encoder().encodeForHTML(message);
return message;
}
}
@@ -0,0 +1,17 @@
package com.baeldung.threadlocal;
public class Context {
private final String userName;
public Context(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "Context{" +
"userNameSecret='" + userName + '\'' +
'}';
}
}
@@ -0,0 +1,21 @@
package com.baeldung.threadlocal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class SharedMapWithUserContext implements Runnable {
public final static Map<Integer, Context> userContextPerUserId = new ConcurrentHashMap<>();
private final Integer userId;
private UserRepository userRepository = new UserRepository();
public SharedMapWithUserContext(Integer userId) {
this.userId = userId;
}
@Override
public void run() {
String userName = userRepository.getUserNameForUserId(userId);
userContextPerUserId.put(userId, new Context(userName));
}
}
@@ -0,0 +1,20 @@
package com.baeldung.threadlocal;
public class ThreadLocalWithUserContext implements Runnable {
private static final ThreadLocal<Context> userContext = new ThreadLocal<>();
private final Integer userId;
private UserRepository userRepository = new UserRepository();
public ThreadLocalWithUserContext(Integer userId) {
this.userId = userId;
}
@Override
public void run() {
String userName = userRepository.getUserNameForUserId(userId);
userContext.set(new Context(userName));
System.out.println("thread context for given userId: " + userId + " is: " + userContext.get());
}
}
@@ -0,0 +1,10 @@
package com.baeldung.threadlocal;
import java.util.UUID;
public class UserRepository {
public String getUserNameForUserId(Integer userId) {
return UUID.randomUUID().toString();
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,35 @@
package com.baeldung.threadlocal;
import org.junit.Test;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.assertEquals;
public class ThreadLocalTest {
@Test
public void givenThreadThatStoresContextInAMap_whenStartThread_thenShouldSetContextForBothUsers() throws ExecutionException, InterruptedException {
//when
SharedMapWithUserContext firstUser = new SharedMapWithUserContext(1);
SharedMapWithUserContext secondUser = new SharedMapWithUserContext(2);
new Thread(firstUser).start();
new Thread(secondUser).start();
Thread.sleep(3000);
//then
assertEquals(SharedMapWithUserContext.userContextPerUserId.size(), 2);
}
@Test
public void givenThreadThatStoresContextInThreadLocal_whenStartThread_thenShouldStoreContextInThreadLocal() throws ExecutionException, InterruptedException {
//when
ThreadLocalWithUserContext firstUser = new ThreadLocalWithUserContext(1);
ThreadLocalWithUserContext secondUser = new ThreadLocalWithUserContext(2);
new Thread(firstUser).start();
new Thread(secondUser).start();
Thread.sleep(3000);
}
}
+1
View File
@@ -5,6 +5,7 @@
- [String Processing with Apache Commons Lang 3](http://www.baeldung.com/string-processing-commons-lang)
- [Introduction to Javatuples](http://www.baeldung.com/java-tuples)
- [Introduction to Javassist](http://www.baeldung.com/javassist)
- [Embedded Jetty Server in Java](http://www.baeldung.com/jetty-embedded)
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.
+1 -15
View File
@@ -151,21 +151,7 @@
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils_2.10</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils_2.10</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -6,6 +6,8 @@ import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.thymeleaf.util.StringUtils;
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
@@ -15,17 +17,17 @@ public class ContactInfoValidator implements ConstraintValidator<ContactInfo, St
@Autowired
private ContactInfoExpressionRepository expressionRepository;
@Value("${contactInfoType}")
String expressionType;
@Override
public void initialize(final ContactInfo contactInfo) {
}
@Override
public boolean isValid(final String value, final ConstraintValidatorContext context) {
String expressionType = System.getProperty("contactInfoType");
System.out.println(expressionType);
final ContactInfoExpression expression = expressionRepository.findOne(expressionType);
if (expression != null) {
final String pattern = expression.getPattern();
if (!StringUtils.isEmptyOrWhitespace(expressionType)) {
final String pattern = expressionRepository.findOne(expressionType).map(ContactInfoExpression::getPattern).orElse("");
if (Pattern.matches(pattern, value))
return true;
}
@@ -10,21 +10,13 @@ import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
import com.baeldung.dynamicvalidation.model.Customer;
@Controller
public class CustomerController {
@Autowired
private ContactInfoExpressionRepository expressionRepository;
@GetMapping("/customer")
public String getCustomerPage(Model model) {
model.addAttribute("contactInfoType", System.getProperty("contactInfoType"));
return "customer";
}
@@ -35,20 +27,6 @@ public class CustomerController {
} else {
model.addAttribute("message", "The information is valid!");
}
model.addAttribute("contactInfoType", System.getProperty("contactInfoType"));
return "customer";
}
@PostMapping("/updateContactInfoType")
@ResponseBody
public void updateContactInfoType(@RequestParam final String type) {
System.setProperty("contactInfoType", type);
}
@GetMapping("/contactInfoTypes")
@ResponseBody
public List<ContactInfoExpression> getContactInfoType(Model model) {
return expressionRepository.findAll();
}
}
@@ -1,9 +1,11 @@
package com.baeldung.dynamicvalidation.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
import org.springframework.data.repository.Repository;
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
public interface ContactInfoExpressionRepository extends JpaRepository<ContactInfoExpression, String> {
public interface ContactInfoExpressionRepository extends Repository<ContactInfoExpression, String> {
Optional<ContactInfoExpression> findOne(String id);
}
@@ -43,4 +43,6 @@ servlet.mapping=/dispatcherExampleURL
#banner.image.width= //TODO
#banner.image.height= //TODO
#banner.image.margin= //TODO
#banner.image.invert= //TODO
#banner.image.invert= //TODO
contactInfoType=email
@@ -3,39 +3,14 @@
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Customer Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: "contactInfoTypes"
}).done(function(data) {
var contactInfoType = $('#contactInfoType').text();
$.each(data,function(key, value) {
var o = new Option(value.type, value.type);
if (value.type == contactInfoType) {
o.selected = true;
}
$('#expressions').append(o);
});
});
$('#expressions').change(function(){
$.ajax({
url: "updateContactInfoType",
method:'POST',
data:{type:$('#expressions').val()}
});
});
});
</script>
</head>
<body>
<br />
<form action="customer" method="POST">
Contact Info: <input type="text" name="contactInfo" /> <br />
Contact Info Type: <select id="expressions" ></select> <br />
<input type="submit" value="Submit" />
</form>
<br /><br />
<span th:text="${message}"></span><br />
<span id="contactInfoType" th:text="${contactInfoType}" style="visibility:hidden"></span><br />
</body>
</html>