Skip to content

Commit cf935dc

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 800d72a + e0e6d3c commit cf935dc

10 files changed

Lines changed: 109 additions & 84 deletions

File tree

‎httpclient/pom.xml‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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">
1+
<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">
32
<modelVersion>4.0.0</modelVersion>
43
<groupId>org.baeldung</groupId>
54
<artifactId>httpclient</artifactId>
@@ -36,11 +35,11 @@
3635
</exclusion>
3736
</exclusions>
3837
</dependency>
39-
<dependency>
40-
<groupId>org.apache.httpcomponents</groupId>
41-
<artifactId>httpcore</artifactId>
42-
<version>${httpcore.version}</version>
43-
</dependency>
38+
<!-- <dependency> -->
39+
<!-- <groupId>org.apache.httpcomponents</groupId> -->
40+
<!-- <artifactId>httpcore</artifactId> -->
41+
<!-- <version>${httpcore.version}</version> -->
42+
<!-- </dependency> -->
4443

4544
<dependency>
4645
<groupId>org.apache.httpcomponents</groupId>
@@ -61,9 +60,9 @@
6160
</dependency>
6261

6362
<dependency>
64-
<groupId>org.apache.httpcomponents</groupId>
65-
<artifactId>httpasyncclient</artifactId>
66-
<version>4.1-beta1</version>
63+
<groupId>org.apache.httpcomponents</groupId>
64+
<artifactId>httpasyncclient</artifactId>
65+
<version>4.1-beta1</version> <!-- 4.0.2 --> <!-- 4.1-beta1 -->
6766
</dependency>
6867

6968
<!-- logging -->
@@ -179,7 +178,7 @@
179178
<mockito.version>1.10.8</mockito.version>
180179

181180
<httpcore.version>4.3.3</httpcore.version>
182-
<httpclient.version>4.3.6</httpclient.version>
181+
<httpclient.version>4.4-beta1</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
183182

184183
<rest-assured.version>2.4.0</rest-assured.version>
185184

‎httpclient/src/test/java/org/baeldung/httpclient/HttpAsyncClientTest.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import static org.hamcrest.Matchers.equalTo;
44
import static org.junit.Assert.assertThat;
55

6+
import java.io.IOException;
67
import java.security.cert.X509Certificate;
8+
import java.util.concurrent.ExecutionException;
79
import java.util.concurrent.Future;
810

911
import javax.net.ssl.SSLContext;
@@ -44,14 +46,17 @@ public class HttpAsyncClientTest {
4446
private static final String COOKIE_DOMAIN = ".yuilibrary.com"; // ".github.com";
4547
private static final String COOKIE_NAME = "example"; // "JSESSIONID";
4648

49+
// tests
4750

4851
@Test
49-
public void whenUseHttpAsyncClient_thenCorrect() throws Exception {
52+
public void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException {
5053
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
51-
client.start();
54+
// client.start();
5255
final HttpGet request = new HttpGet(HOST);
56+
5357
final Future<HttpResponse> future = client.execute(request, null);
5458
final HttpResponse response = future.get();
59+
5560
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
5661
client.close();
5762
}
@@ -114,7 +119,6 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
114119
client.close();
115120
}
116121

117-
118122
@Test
119123
public void whenUseCookiesWithHttpAsyncClient_thenCorrect() throws Exception {
120124
final BasicCookieStore cookieStore = new BasicCookieStore();
@@ -138,8 +142,8 @@ public void whenUseCookiesWithHttpAsyncClient_thenCorrect() throws Exception {
138142
@Test
139143
public void whenUseAuthenticationWithHttpAsyncClient_thenCorrect() throws Exception {
140144
final CredentialsProvider provider = new BasicCredentialsProvider();
141-
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
142-
provider.setCredentials(AuthScope.ANY, credentials);
145+
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
146+
provider.setCredentials(AuthScope.ANY, creds);
143147
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setDefaultCredentialsProvider(provider).build();
144148

145149
final HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
@@ -175,4 +179,3 @@ public void run() {
175179

176180
}
177181
}
178-

‎spring-security-login-and-registration/src/main/java/org/baeldung/event/listener/RegistrationListener.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
import java.util.UUID;
44

5-
import javax.mail.AuthenticationFailedException;
6-
75
import org.baeldung.event.OnRegistrationCompleteEvent;
86
import org.baeldung.persistence.model.User;
97
import org.baeldung.persistence.service.IUserService;
108
import org.springframework.beans.factory.annotation.Autowired;
119
import org.springframework.context.ApplicationListener;
1210
import org.springframework.context.MessageSource;
13-
import org.springframework.mail.MailAuthenticationException;
1411
import org.springframework.mail.SimpleMailMessage;
1512
import org.springframework.mail.javamail.JavaMailSender;
1613
import org.springframework.stereotype.Component;
@@ -34,7 +31,8 @@ public void onApplicationEvent(OnRegistrationCompleteEvent event) {
3431
private void confirmRegistration(OnRegistrationCompleteEvent event) {
3532
User user = event.getUser();
3633
String token = UUID.randomUUID().toString();
37-
service.addVerificationToken(user, token);
34+
service.createVerificationTokenForUser(user, token);
35+
3836
String recipientAddress = user.getEmail();
3937
String subject = "Registration Confirmation";
4038
String confirmationUrl = event.getAppUrl() + "/regitrationConfirm.html?token=" + token;

‎spring-security-login-and-registration/src/main/java/org/baeldung/persistence/model/User.java‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,43 @@
11
package org.baeldung.persistence.model;
22

33
import javax.persistence.CascadeType;
4-
import javax.persistence.Column;
54
import javax.persistence.Entity;
65
import javax.persistence.FetchType;
76
import javax.persistence.GeneratedValue;
87
import javax.persistence.GenerationType;
98
import javax.persistence.Id;
109
import javax.persistence.OneToOne;
11-
import javax.persistence.Table;
1210

1311
@Entity
14-
@Table
1512
public class User {
1613

1714
@Id
1815
@GeneratedValue(strategy = GenerationType.AUTO)
1916
private Long id;
2017

21-
@Column(name = "firstName")
2218
private String firstName;
2319

24-
@Column(name = "lastName")
2520
private String lastName;
2621

27-
@Column(name = "email")
2822
private String email;
2923

30-
@Column(name = "password")
3124
private String password;
3225

33-
@Column(name = "enabled")
3426
private boolean enabled;
35-
36-
@Column(name = "token_expired")
27+
3728
private boolean tokenExpired;
3829

3930
@OneToOne(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
4031
private Role role;
41-
32+
4233
public User() {
4334
super();
4435
this.enabled = false;
4536
this.tokenExpired = false;
4637
}
38+
39+
//
40+
4741
public Long getId() {
4842
return id;
4943
}
@@ -99,7 +93,7 @@ public boolean isEnabled() {
9993
public void setEnabled(boolean enabled) {
10094
this.enabled = enabled;
10195
}
102-
96+
10397
public boolean isTokenExpired() {
10498
return tokenExpired;
10599
}
@@ -108,6 +102,7 @@ public void setTokenExpired(boolean expired) {
108102
this.tokenExpired = expired;
109103
}
110104

105+
//
111106

112107
@Override
113108
public int hashCode() {
@@ -137,4 +132,5 @@ public String toString() {
137132
builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(email).append("]");
138133
return builder.toString();
139134
}
135+
140136
}

‎spring-security-login-and-registration/src/main/java/org/baeldung/persistence/model/VerificationToken.java‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
import java.util.Calendar;
44
import java.sql.Date;
55
import java.sql.Timestamp;
6-
import javax.persistence.Column;
6+
77
import javax.persistence.Entity;
88
import javax.persistence.FetchType;
99
import javax.persistence.GeneratedValue;
1010
import javax.persistence.GenerationType;
1111
import javax.persistence.Id;
1212
import javax.persistence.JoinColumn;
1313
import javax.persistence.OneToOne;
14-
import javax.persistence.Table;
1514

1615
@Entity
17-
@Table
1816
public class VerificationToken {
1917

2018
private static final int EXPIRATION = 60 * 24;
@@ -23,14 +21,12 @@ public class VerificationToken {
2321
@GeneratedValue(strategy = GenerationType.AUTO)
2422
private Long id;
2523

26-
@Column(name = "token")
2724
private String token;
2825

2926
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER)
3027
@JoinColumn(nullable = false, name = "user_id")
3128
private User user;
3229

33-
@Column(name = "expiry_date")
3430
private Date expiryDate;
3531

3632
public VerificationToken() {
@@ -39,17 +35,21 @@ public VerificationToken() {
3935

4036
public VerificationToken(String token) {
4137
super();
38+
4239
this.token = token;
4340
this.expiryDate = calculateExpiryDate(EXPIRATION);
4441
}
4542

4643
public VerificationToken(String token, User user) {
4744
super();
45+
4846
this.token = token;
4947
this.user = user;
5048
this.expiryDate = calculateExpiryDate(EXPIRATION);
5149
}
5250

51+
//
52+
5353
public String getToken() {
5454
return token;
5555
}
@@ -81,16 +81,41 @@ private Date calculateExpiryDate(int expiryTimeInMinutes) {
8181
return new Date(cal.getTime().getTime());
8282
}
8383

84+
//
85+
86+
@Override
87+
public int hashCode() {
88+
final int prime = 31;
89+
int result = 1;
90+
result = prime * result + ((expiryDate == null) ? 0 : expiryDate.hashCode());
91+
result = prime * result + ((token == null) ? 0 : token.hashCode());
92+
result = prime * result + ((user == null) ? 0 : user.hashCode());
93+
return result;
94+
}
95+
8496
@Override
85-
public boolean equals(final Object obj) {
97+
public boolean equals(Object obj) {
8698
if (this == obj)
8799
return true;
88100
if (obj == null)
89101
return false;
90102
if (getClass() != obj.getClass())
91103
return false;
92-
final VerificationToken verificationToken = (VerificationToken) obj;
93-
if (!token.equals(verificationToken.getToken()))
104+
VerificationToken other = (VerificationToken) obj;
105+
if (expiryDate == null) {
106+
if (other.expiryDate != null)
107+
return false;
108+
} else if (!expiryDate.equals(other.expiryDate))
109+
return false;
110+
if (token == null) {
111+
if (other.token != null)
112+
return false;
113+
} else if (!token.equals(other.token))
114+
return false;
115+
if (user == null) {
116+
if (other.user != null)
117+
return false;
118+
} else if (!user.equals(other.user))
94119
return false;
95120
return true;
96121
}

‎spring-security-login-and-registration/src/main/java/org/baeldung/persistence/service/IUserService.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
public interface IUserService {
88

9-
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException;
9+
User registerNewUserAccount(UserDto accountDto) throws EmailExistsException;
1010

11-
public User getUser(String verificationToken);
11+
User getUser(String verificationToken);
1212

13-
public void saveRegisteredUser(User user);
13+
void saveRegisteredUser(User user);
1414

15-
public void addVerificationToken(User user, String token);
15+
void deleteUser(User user);
1616

17-
public VerificationToken getVerificationToken(String VerificationToken);
17+
void createVerificationTokenForUser(User user, String token);
18+
19+
VerificationToken getVerificationToken(String VerificationToken);
1820

19-
public void deleteUser(User user);
2021
}

‎spring-security-login-and-registration/src/main/java/org/baeldung/persistence/service/UserService.java‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
import org.springframework.stereotype.Service;
1313

1414
@Service
15+
@Transactional
1516
public class UserService implements IUserService {
1617
@Autowired
1718
private UserRepository repository;
1819

1920
@Autowired
2021
private VerificationTokenRepository tokenRepository;
2122

22-
@Transactional
23+
// API
24+
2325
@Override
2426
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException {
2527
if (emailExist(accountDto.getEmail())) {
@@ -34,14 +36,6 @@ public User registerNewUserAccount(UserDto accountDto) throws EmailExistsExcepti
3436
return repository.save(user);
3537
}
3638

37-
private boolean emailExist(String email) {
38-
User user = repository.findByEmail(email);
39-
if (user != null) {
40-
return true;
41-
}
42-
return false;
43-
}
44-
4539
@Override
4640
public User getUser(String verificationToken) {
4741
User user = tokenRepository.findByToken(verificationToken).getUser();
@@ -53,22 +47,30 @@ public VerificationToken getVerificationToken(String VerificationToken) {
5347
return tokenRepository.findByToken(VerificationToken);
5448
}
5549

56-
@Transactional
5750
@Override
5851
public void saveRegisteredUser(User user) {
5952
repository.save(user);
6053
}
6154

62-
@Transactional
6355
@Override
6456
public void deleteUser(User user) {
6557
repository.delete(user);
6658
}
6759

68-
@Transactional
6960
@Override
70-
public void addVerificationToken(User user, String token) {
61+
public void createVerificationTokenForUser(User user, String token) {
7162
VerificationToken myToken = new VerificationToken(token, user);
7263
tokenRepository.save(myToken);
7364
}
65+
66+
//
67+
68+
private boolean emailExist(String email) {
69+
User user = repository.findByEmail(email);
70+
if (user != null) {
71+
return true;
72+
}
73+
return false;
74+
}
75+
7476
}

0 commit comments

Comments
 (0)