Skip to content

Commit b9281ce

Browse files
authored
Merge pull request #5 from tswlun002/feature/27-03-2024-document-service
Feature/27 03 2024 document service
2 parents a4e4d31 + bf68712 commit b9281ce

25 files changed

+479
-69
lines changed

document-service/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.pdfeditor</groupId>
8+
<artifactId>pdf-editor</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>document-service</artifactId>
13+
<packaging>jar</packaging>
14+
<name>document-service</name>
15+
<url>https://maven.apache.org</url>
16+
<properties>
17+
<maven.compiler.source>17</maven.compiler.source>
18+
<maven.compiler.target>17</maven.compiler.target>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-devtools</artifactId>
29+
<scope>runtime</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.itextpdf</groupId>
37+
<artifactId>itextpdf</artifactId>
38+
<version>5.5.13.2</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>jakarta.persistence</groupId>
42+
<artifactId>jakarta.persistence-api</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.apache.httpcomponents.client5</groupId>
46+
<artifactId>httpclient5</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.apache.httpcomponents</groupId>
50+
<artifactId>httpclient</artifactId>
51+
<version>4.5.13</version>
52+
</dependency>
53+
</dependencies>
54+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.documentservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DocumentServiceApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(DocumentServiceApplication.class, args);
10+
}
11+
}

user-service/src/main/java/com/userservice/email/EmailService.java renamed to document-service/src/main/java/com/documentservice/email/EmailService.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
package com.userservice.email;
1+
package com.documentservice.email;
22

3-
import com.itextpdf.text.DocumentException;
4-
import com.userservice.pdffile.PDF;
5-
import com.userservice.user.IUser;
6-
import com.userservice.user.User;
3+
import com.documentservice.exception.InternalServerError;
4+
import com.documentservice.pdf.PDF;
75
import jakarta.validation.Valid;
86
import lombok.RequiredArgsConstructor;
97
import lombok.extern.slf4j.Slf4j;
@@ -12,25 +10,21 @@
1210
import org.springframework.util.LinkedMultiValueMap;
1311
import org.springframework.util.MultiValueMap;
1412
import org.springframework.validation.annotation.Validated;
15-
import org.springframework.web.client.HttpClientErrorException;
16-
import org.springframework.web.client.HttpServerErrorException;
1713
import org.springframework.web.client.RestTemplate;
1814

19-
import java.util.Optional;
2015
@Slf4j
2116
@RequiredArgsConstructor
2217
@Validated
2318
@Service
2419
public class EmailService implements IEmail {
25-
private final IUser user;
2620
private final RestTemplate restTemplate;
2721

2822
@Override
29-
public Optional<Boolean> sendEmail( String username) {
30-
return user.findUserByEmail(username).map(this::getResponse);
23+
public boolean sendEmail(PDF pdf) {
24+
return getResponse(pdf);
3125
}
3226

33-
private @Valid Boolean getResponse(@Valid User user) {
27+
private @Valid Boolean getResponse(PDF pdf) {
3428
ResponseEntity<String> response = null;
3529
record EmailRequest(
3630
String recipient
@@ -43,27 +37,15 @@ record EmailRequest(
4337
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
4438
MultiValueMap<String, Object> body
4539
= new LinkedMultiValueMap<>();
46-
body.add("pdf", PDF.generatePdfStream().toByteArray());
47-
body.add("email", new EmailRequest(user.getEmail()));
40+
body.add("pdf", pdf.getImage());
41+
body.add("email", new EmailRequest(pdf.getEmail()));
4842
HttpEntity<MultiValueMap<String, Object>> requestEntity
4943
= new HttpEntity<>(body, headers);
5044
response = restTemplate.postForEntity("http://localhost:8081/pdf-editor/email/send",requestEntity, String.class);
5145

52-
} catch (HttpClientErrorException e) {
53-
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
54-
// Handle 404 error
55-
log.info("Error:-----------> message:{},status code:{}", e.getMessage(),4004);
56-
throw new RuntimeException(e);
57-
}
58-
} catch (HttpServerErrorException | DocumentException e) {
59-
if (e instanceof HttpServerErrorException ex)
60-
if(ex.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR) {
61-
// Handle 500 error
46+
} catch (Exception e) {
6247
log.info("Error:-----------> message:{},status code:{}", e.getMessage(),500);
63-
throw new RuntimeException(ex);
64-
}
65-
else throw new RuntimeException(e);
66-
48+
throw new InternalServerError("Internal server error.");
6749
}
6850
return response != null && response.getStatusCode() == HttpStatus.OK;
6951
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.documentservice.email;
2+
3+
import com.documentservice.pdf.PDF;
4+
import org.springframework.stereotype.Service;
5+
@Service("doc-email")
6+
public interface IEmail {
7+
8+
boolean sendEmail(PDF doc);
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.documentservice.exception;
2+
3+
import lombok.Builder;
4+
5+
import java.time.LocalDateTime;
6+
7+
@Builder
8+
public record AppException(
9+
int statusCode,
10+
String statusCodeMessage,
11+
String message,
12+
LocalDateTime time,String path
13+
) {
14+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.documentservice.exception;
2+
3+
import jakarta.validation.ConstraintViolationException;
4+
import lombok.NonNull;
5+
import org.springframework.context.support.DefaultMessageSourceResolvable;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.HttpStatusCode;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.HttpRequestMethodNotSupportedException;
11+
import org.springframework.web.bind.MethodArgumentNotValidException;
12+
import org.springframework.web.bind.annotation.ExceptionHandler;
13+
import org.springframework.web.bind.annotation.RestControllerAdvice;
14+
import org.springframework.web.context.request.WebRequest;
15+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
16+
17+
import java.time.LocalDateTime;
18+
19+
20+
@RestControllerAdvice
21+
public class AppExceptionHandler extends ResponseEntityExceptionHandler {
22+
23+
24+
@Override
25+
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
26+
@NonNull HttpHeaders headers,
27+
@NonNull HttpStatusCode status, @NonNull WebRequest request) {
28+
29+
var message = String.join( ",", ex.getAllErrors()
30+
.stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
31+
.toList()
32+
);
33+
var exc = AppException.builder()
34+
.statusCode(status.value())
35+
.statusCodeMessage(HttpStatus.METHOD_NOT_ALLOWED.name())
36+
.message(message).
37+
path(request.getContextPath())
38+
.time(LocalDateTime.now())
39+
.build();
40+
return new ResponseEntity<>(exc, HttpStatus.METHOD_NOT_ALLOWED);
41+
}
42+
@ExceptionHandler({ConstraintViolationException.class})
43+
public ResponseEntity<?> constrainsFailed(ConstraintViolationException exception, WebRequest request){
44+
45+
var message = String.join( ",", exception.getConstraintViolations()
46+
.stream().map(c-> c.getInvalidValue()+" : "+c.getMessage())
47+
.toList()
48+
);
49+
var exc = AppException.builder()
50+
.statusCode(HttpStatus.BAD_REQUEST.value())
51+
.statusCodeMessage(HttpStatus.BAD_REQUEST.name())
52+
.message(message).
53+
path(request.getContextPath())
54+
.time(LocalDateTime.now())
55+
.build();
56+
return new ResponseEntity<>(exc, HttpStatus.BAD_REQUEST);
57+
}
58+
@Override
59+
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
60+
@NonNull HttpHeaders headers,
61+
@NonNull HttpStatusCode status,
62+
@NonNull WebRequest request) {
63+
64+
var exc = AppException.builder()
65+
.statusCode(status.value())
66+
.statusCodeMessage(HttpStatus.METHOD_NOT_ALLOWED.name())
67+
.message(ex.getMessage()).
68+
path(request.getContextPath())
69+
.time(LocalDateTime.now())
70+
.build();
71+
return new ResponseEntity<>(exc, HttpStatus.METHOD_NOT_ALLOWED);
72+
}
73+
@ExceptionHandler({InternalServerError.class})
74+
public ResponseEntity<?> InternalException(InternalServerError exception, WebRequest request){
75+
var exc = AppException.builder()
76+
.statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
77+
.statusCodeMessage(HttpStatus.INTERNAL_SERVER_ERROR.name())
78+
.message(exception.getMessage()).
79+
path(request.getContextPath())
80+
.time(LocalDateTime.now())
81+
.build();
82+
return new ResponseEntity<>(exc, HttpStatus.INTERNAL_SERVER_ERROR);
83+
84+
}
85+
86+
@ExceptionHandler(value = {EntityNotFoundException.class})
87+
public ResponseEntity<?> userNotFound(EntityNotFoundException exception
88+
,WebRequest request){
89+
var exc = AppException.builder()
90+
.statusCode(HttpStatus.NOT_FOUND.value())
91+
.statusCodeMessage(HttpStatus.NOT_FOUND.name())
92+
.message(exception.getMessage()).
93+
path(request.getContextPath())
94+
.time(LocalDateTime.now())
95+
.build();
96+
return new ResponseEntity<>(exc, HttpStatus.NOT_FOUND);
97+
}
98+
@ExceptionHandler(value = {EntityAlreadyExistException.class})
99+
public ResponseEntity<?> Exits(EntityAlreadyExistException exception, WebRequest request){
100+
var exc = AppException.builder()
101+
.statusCode(HttpStatus.CONFLICT.value())
102+
.statusCodeMessage(HttpStatus.CONFLICT.name())
103+
.message(exception.getMessage()).
104+
path(request.getContextPath())
105+
.time(LocalDateTime.now())
106+
.build();
107+
return new ResponseEntity<>(exc, HttpStatus.CONFLICT);
108+
}
109+
110+
@ExceptionHandler(value = {InvalidDocument.class})
111+
public ResponseEntity<?> Invalid(InvalidDocument exception, WebRequest request){
112+
var exc = AppException.builder()
113+
.statusCode(HttpStatus.BAD_REQUEST.value())
114+
.statusCodeMessage(HttpStatus.BAD_REQUEST.name())
115+
.message(exception.getMessage()).
116+
path(request.getContextPath())
117+
.time(LocalDateTime.now())
118+
.build();
119+
return new ResponseEntity<>(exc, HttpStatus.BAD_REQUEST);
120+
}
121+
122+
123+
124+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.documentservice.exception;
2+
3+
public class EntityAlreadyExistException extends RuntimeException {
4+
public EntityAlreadyExistException( String s) {
5+
6+
super(s);
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.documentservice.exception;
2+
3+
public class EntityNotFoundException extends RuntimeException {
4+
public EntityNotFoundException(String s ) {
5+
super(s);
6+
}
7+
8+
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.documentservice.exception;
2+
3+
public class InternalServerError extends InternalError{
4+
public InternalServerError(String message){
5+
super(message);
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.documentservice.exception;
2+
3+
import java.io.InvalidClassException;
4+
5+
public class InvalidDocument extends InvalidClassException {
6+
public InvalidDocument(String s) {
7+
super(s);
8+
}
9+
}

0 commit comments

Comments
 (0)