Skip to content

Commit 63c6da7

Browse files
Merge branch 'master' into pr/720-tim-bootstrap-security
2 parents 876710f + d55903a commit 63c6da7

110 files changed

Lines changed: 1259 additions & 461 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,36 @@ public class Baeldung {
3737
}
3838

3939
@GET
40-
@Path("courses/{courseOrder}")
41-
public Course getCourse(@PathParam("courseOrder") int courseOrder) {
42-
return courses.get(courseOrder);
40+
@Path("courses/{courseId}")
41+
public Course getCourse(@PathParam("courseId") int courseId) {
42+
return findById(courseId);
4343
}
4444

4545
@PUT
46-
@Path("courses/{courseOrder}")
47-
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
48-
Course existingCourse = courses.get(courseOrder);
49-
50-
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
51-
courses.put(courseOrder, course);
52-
return Response.ok().build();
46+
@Path("courses/{courseId}")
47+
public Response updateCourse(@PathParam("courseId") int courseId, Course course) {
48+
Course existingCourse = findById(courseId);
49+
if (existingCourse == null) {
50+
return Response.status(Response.Status.NOT_FOUND).build();
51+
}
52+
if (existingCourse.equals(course)) {
53+
return Response.notModified().build();
5354
}
55+
courses.put(courseId, course);
56+
return Response.ok().build();
57+
}
5458

55-
return Response.notModified().build();
59+
@Path("courses/{courseId}/students")
60+
public Course pathToStudent(@PathParam("courseId") int courseId) {
61+
return findById(courseId);
5662
}
5763

58-
@Path("courses/{courseOrder}/students")
59-
public Course pathToStudent(@PathParam("courseOrder") int courseOrder) {
60-
return courses.get(courseOrder);
64+
private Course findById(int id) {
65+
for (Map.Entry<Integer, Course> course : courses.entrySet()) {
66+
if (course.getKey() == id) {
67+
return course.getValue();
68+
}
69+
}
70+
return null;
6171
}
6272
}

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import javax.ws.rs.*;
44
import javax.ws.rs.core.Response;
55
import javax.xml.bind.annotation.XmlRootElement;
6+
67
import java.util.ArrayList;
78
import java.util.List;
89

910
@XmlRootElement(name = "Course")
1011
public class Course {
1112
private int id;
1213
private String name;
13-
private List<Student> students;
14+
private List<Student> students = new ArrayList<>();
1415

1516
public int getId() {
1617
return id;
@@ -35,31 +36,51 @@ public List<Student> getStudents() {
3536
public void setStudents(List<Student> students) {
3637
this.students = students;
3738
}
38-
39+
3940
@GET
40-
@Path("{studentOrder}")
41-
public Student getStudent(@PathParam("studentOrder")int studentOrder) {
42-
return students.get(studentOrder);
41+
@Path("{studentId}")
42+
public Student getStudent(@PathParam("studentId") int studentId) {
43+
return findById(studentId);
4344
}
44-
45+
4546
@POST
46-
public Response postStudent(Student student) {
47-
if (students == null) {
48-
students = new ArrayList<>();
47+
public Response createStudent(Student student) {
48+
for (Student element : students) {
49+
if (element.getId() == student.getId()) {
50+
return Response.status(Response.Status.CONFLICT).build();
51+
}
4952
}
5053
students.add(student);
5154
return Response.ok(student).build();
5255
}
53-
56+
5457
@DELETE
55-
@Path("{studentOrder}")
56-
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
57-
Student student = students.get(studentOrder);
58-
if (student != null) {
59-
students.remove(studentOrder);
60-
return Response.ok().build();
61-
} else {
62-
return Response.notModified().build();
58+
@Path("{studentId}")
59+
public Response deleteStudent(@PathParam("studentId") int studentId) {
60+
Student student = findById(studentId);
61+
if (student == null) {
62+
return Response.status(Response.Status.NOT_FOUND).build();
6363
}
64+
students.remove(student);
65+
return Response.ok().build();
66+
}
67+
68+
private Student findById(int id) {
69+
for (Student student : students) {
70+
if (student.getId() == id) {
71+
return student;
72+
}
73+
}
74+
return null;
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return id + name.hashCode();
80+
}
81+
82+
@Override
83+
public boolean equals(Object obj) {
84+
return (obj instanceof Course) && (id == ((Course) obj).getId()) && (name.equals(((Course) obj).getName()));
6485
}
6586
}

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Student.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public String getName() {
2222
public void setName(String name) {
2323
this.name = name;
2424
}
25+
26+
@Override
27+
public int hashCode() {
28+
return id + name.hashCode();
29+
}
30+
31+
@Override
32+
public boolean equals(Object obj) {
33+
return (obj instanceof Student) && (id == ((Student) obj).getId()) && (name.equals(((Student) obj).getName()));
34+
}
2535
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Course>
2+
<id>2</id>
3+
<name>Apache CXF Support for RESTful</name>
4+
</Course>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Student>
2+
<id>2</id>
3+
<name>Student B</name>
4+
</Student>

apache-cxf/cxf-jaxrs-implementation/src/main/resources/student.xml renamed to apache-cxf/cxf-jaxrs-implementation/src/main/resources/created_student.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<Student>
32
<id>3</id>
43
<name>Student C</name>

apache-cxf/cxf-jaxrs-implementation/src/main/resources/course.xml renamed to apache-cxf/cxf-jaxrs-implementation/src/main/resources/non_existent_course.xml

File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Course>
2+
<id>1</id>
3+
<name>REST with Spring</name>
4+
</Course>

‎apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java‎

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,78 @@ public static void closeClient() throws IOException {
3434
}
3535

3636
@Test
37-
public void whenPutCourse_thenCorrect() throws IOException {
37+
public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse() throws IOException {
3838
HttpPut httpPut = new HttpPut(BASE_URL + "3");
39-
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml");
39+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("non_existent_course.xml");
40+
httpPut.setEntity(new InputStreamEntity(resourceStream));
41+
httpPut.setHeader("Content-Type", "text/xml");
42+
43+
HttpResponse response = client.execute(httpPut);
44+
assertEquals(404, response.getStatusLine().getStatusCode());
45+
}
46+
47+
@Test
48+
public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse() throws IOException {
49+
HttpPut httpPut = new HttpPut(BASE_URL + "1");
50+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("unchanged_course.xml");
51+
httpPut.setEntity(new InputStreamEntity(resourceStream));
52+
httpPut.setHeader("Content-Type", "text/xml");
53+
54+
HttpResponse response = client.execute(httpPut);
55+
assertEquals(304, response.getStatusLine().getStatusCode());
56+
}
57+
58+
@Test
59+
public void whenUpdateValidCourse_thenReceiveOKResponse() throws IOException {
60+
HttpPut httpPut = new HttpPut(BASE_URL + "2");
61+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("changed_course.xml");
4062
httpPut.setEntity(new InputStreamEntity(resourceStream));
4163
httpPut.setHeader("Content-Type", "text/xml");
4264

4365
HttpResponse response = client.execute(httpPut);
4466
assertEquals(200, response.getStatusLine().getStatusCode());
4567

46-
Course course = getCourse(3);
47-
assertEquals(3, course.getId());
68+
Course course = getCourse(2);
69+
assertEquals(2, course.getId());
4870
assertEquals("Apache CXF Support for RESTful", course.getName());
4971
}
72+
73+
@Test
74+
public void whenCreateConflictStudent_thenReceiveConflictResponse() throws IOException {
75+
HttpPost httpPost = new HttpPost(BASE_URL + "1/students");
76+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("conflict_student.xml");
77+
httpPost.setEntity(new InputStreamEntity(resourceStream));
78+
httpPost.setHeader("Content-Type", "text/xml");
79+
80+
HttpResponse response = client.execute(httpPost);
81+
assertEquals(409, response.getStatusLine().getStatusCode());
82+
}
5083

5184
@Test
52-
public void whenPostStudent_thenCorrect() throws IOException {
85+
public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException {
5386
HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
54-
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml");
87+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("created_student.xml");
5588
httpPost.setEntity(new InputStreamEntity(resourceStream));
5689
httpPost.setHeader("Content-Type", "text/xml");
5790

5891
HttpResponse response = client.execute(httpPost);
5992
assertEquals(200, response.getStatusLine().getStatusCode());
6093

61-
Student student = getStudent(2, 0);
94+
Student student = getStudent(2, 3);
6295
assertEquals(3, student.getId());
6396
assertEquals("Student C", student.getName());
6497
}
6598

6699
@Test
67-
public void whenDeleteStudent_thenCorrect() throws IOException {
68-
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
100+
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
101+
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/3");
102+
HttpResponse response = client.execute(httpDelete);
103+
assertEquals(404, response.getStatusLine().getStatusCode());
104+
}
105+
106+
@Test
107+
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
108+
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/1");
69109
HttpResponse response = client.execute(httpDelete);
70110
assertEquals(200, response.getStatusLine().getStatusCode());
71111

‎core-java-8/src/test/java/com/baeldung/encoderdecoder/EncoderDecoder.java‎

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,82 @@
33
import org.hamcrest.CoreMatchers;
44
import org.junit.Assert;
55
import org.junit.Test;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
import java.io.UnsupportedEncodingException;
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
812
import java.net.URLDecoder;
913
import java.net.URLEncoder;
1014
import java.nio.charset.StandardCharsets;
15+
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.function.Function;
19+
import java.util.stream.Collectors;
1120

1221
public class EncoderDecoder {
1322

23+
private static final String URL = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
24+
private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoder.class);
25+
26+
private String encodeValue(String value) {
27+
String encoded = null;
28+
try {
29+
encoded = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
30+
} catch (UnsupportedEncodingException e) {
31+
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
32+
}
33+
return encoded;
34+
}
35+
36+
37+
private String decode(String value) {
38+
String decoded = null;
39+
try {
40+
decoded = URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
41+
} catch (UnsupportedEncodingException e) {
42+
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
43+
}
44+
return decoded;
45+
}
46+
1447
@Test
15-
public void givenPlainURL_whenUsingUTF8EncodingScheme_thenEncodeURL() throws UnsupportedEncodingException {
16-
String plainURL = "http://www.baeldung.com" ;
17-
String encodedURL = URLEncoder.encode(plainURL, StandardCharsets.UTF_8.toString());
48+
public void givenURL_whenAnalyze_thenCorrect() throws Exception {
49+
URL url = new URL(URL);
1850

19-
Assert.assertThat("http%3A%2F%2Fwww.baeldung.com", CoreMatchers.is(encodedURL));
51+
Assert.assertThat(url.getProtocol(), CoreMatchers.is("http"));
52+
Assert.assertThat(url.getHost(), CoreMatchers.is("www.baeldung.com"));
53+
Assert.assertThat(url.getQuery(), CoreMatchers.is("key1=value+1&key2=value%40%21%242&key3=value%253"));
2054
}
2155

2256
@Test
23-
public void givenEncodedURL_whenUsingUTF8EncodingScheme_thenDecodeURL() throws UnsupportedEncodingException {
24-
String encodedURL = "http%3A%2F%2Fwww.baeldung.com" ;
25-
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_8.toString());
57+
public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
58+
Map<String, String> requestParams = new HashMap<>();
59+
requestParams.put("key1", "value 1");
60+
requestParams.put("key2", "value@!$2");
61+
requestParams.put("key3", "value%3");
2662

27-
Assert.assertThat("http://www.baeldung.com", CoreMatchers.is(decodedURL));
63+
String encodedQuery = requestParams.keySet().stream()
64+
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
65+
.collect(Collectors.joining("&"));
66+
String encodedURL = "http://www.baeldung.com?" + encodedQuery;
67+
68+
Assert.assertThat(URL, CoreMatchers.is(encodedURL));
2869
}
2970

3071
@Test
31-
public void givenEncodedURL_whenUsingWrongEncodingScheme_thenDecodeInvalidURL() throws UnsupportedEncodingException {
32-
String encodedURL = "http%3A%2F%2Fwww.baeldung.com";
72+
public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception {
73+
URL url = new URL(URL);
74+
String query = url.getQuery();
3375

34-
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_16.toString());
76+
String decodedQuery = Arrays.stream(query.split("&"))
77+
.map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1]))
78+
.collect(Collectors.joining("&"));
3579

36-
Assert.assertFalse("http://www.baeldung.com".equals(decodedURL));
80+
Assert.assertEquals(
81+
"http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
3782
}
83+
3884
}

0 commit comments

Comments
 (0)