SUPPORT-8381: fix by review (2)

This commit is contained in:
Alexandr Shalaginov 2024-07-17 10:04:54 +03:00
parent b6717f44b7
commit de14cbcea6
11 changed files with 57 additions and 116 deletions

View file

@ -1,4 +1,4 @@
package ervu.client.fileupload.impl;
package ervu.client.fileupload;
import java.io.BufferedOutputStream;
import java.io.IOException;
@ -6,7 +6,6 @@ import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import ervu.client.fileupload.ErvuFileUploadClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@ -17,17 +16,13 @@ import org.springframework.web.multipart.MultipartFile;
* @author Alexandr Shalaginov
*/
@Component
public class ErvuFileUploadClientImpl implements ErvuFileUploadClient {
private static final Logger logger = LoggerFactory.getLogger(ErvuFileUploadClientImpl.class);
public class EmployeeInformationFileWebDavUploadClient {
private static final Logger logger = LoggerFactory.getLogger(EmployeeInformationFileWebDavUploadClient.class);
@Value("${file.upload.url:#{null}}")
@Value("${file.webdav.upload.url:localhost:5757}")
private String url;
@Override
public boolean uploadFile(String filename, MultipartFile multipartFile) {
if (this.url == null) {
throw new RuntimeException("Property file.upload.url is null");
}
public boolean webDavUploadFile(String filename, MultipartFile multipartFile) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(this.url + "/" + filename).openConnection();
connection.setDoInput(true);

View file

@ -1,10 +0,0 @@
package ervu.client.fileupload;
import org.springframework.web.multipart.MultipartFile;
/**
* @author Alexandr Shalaginov
*/
public interface ErvuFileUploadClient {
boolean uploadFile(String filename, MultipartFile multipartFile);
}

View file

@ -1,8 +0,0 @@
package ervu.client.kafka;
/**
* @author Alexandr Shalaginov
*/
public interface KafkaClient {
boolean sendMessage(String message);
}

View file

@ -1,6 +1,5 @@
package ervu.client.kafka.impl;
package ervu.client.kafka;
import ervu.client.kafka.KafkaClient;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,22 +11,19 @@ import org.springframework.stereotype.Component;
* @author Alexandr Shalaginov
*/
@Component
public class KafkaClientImpl implements KafkaClient {
private static final Logger logger = LoggerFactory.getLogger(KafkaClientImpl.class);
public class KafkaSendMessageClient {
private static final Logger logger = LoggerFactory.getLogger(KafkaSendMessageClient.class);
@Value("${kafka.send.topic:#{null}}")
private String kafkaTopic;
@Value("${kafka.send.message.topic.name:messages-topic}")
private String kafkaTopicName;
private final KafkaTemplate<String, String> kafkaTemplate;
public KafkaClientImpl(KafkaTemplate<String, String> kafkaTemplate) {
public KafkaSendMessageClient(KafkaTemplate<String, String> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
public boolean sendMessage(String message) {
if (this.kafkaTopic == null) {
throw new RuntimeException("Property kafka.send.topic is null");
}
ProducerRecord<String, String> record = new ProducerRecord<>(this.kafkaTopic, message);
ProducerRecord<String, String> record = new ProducerRecord<>(this.kafkaTopicName, message);
try {
this.kafkaTemplate.send(record).get();
logger.debug("Success send record: {}", record);

View file

@ -0,0 +1,31 @@
package ervu.controller;
import ervu.service.fileupload.EmployeeInformationFileUploadService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* @author Alexandr Shalaginov
*/
@RestController
public class EmployeeInformationFileUploadController {
private final EmployeeInformationFileUploadService fileUploadService;
public EmployeeInformationFileUploadController(EmployeeInformationFileUploadService fileUploadService) {
this.fileUploadService = fileUploadService;
}
@RequestMapping(value = "/file/upload/employee-information", method = RequestMethod.POST)
public ResponseEntity<?> saveEmployeeInformationFile(@RequestParam("file") MultipartFile multipartFile) {
if (this.fileUploadService.saveEmployeeInformationFile(multipartFile)) {
return ResponseEntity.ok("File successfully uploaded.");
}
else {
return ResponseEntity.internalServerError().body("An error occurred while uploading file.");
}
}
}

View file

@ -1,35 +0,0 @@
package ervu.controller;
import ervu.service.fileupload.FileUploadV2Service;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ru.cg.webbpm.modules.standard_annotations.validation.NotNull;
import ru.cg.webbpm.modules.webkit.beans.Behavior;
/**
* @author Alexandr Shalaginov
*/
@RestController
public class FileUploadV2Controller extends Behavior {
@NotNull
public final FileUploadV2Service fileUploadV2Service;
public FileUploadV2Controller(FileUploadV2Service fileUploadV2Service) {
this.fileUploadV2Service = fileUploadV2Service;
}
@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
public ResponseEntity<?> saveFile(@RequestParam("file") MultipartFile multipartFile) {
if (this.fileUploadV2Service.saveFile(multipartFile)) {
return ResponseEntity.ok("File successfully uploaded.");
}
else {
return ResponseEntity.internalServerError().body("An error occurred while uploading file.");
}
}
}

View file

@ -1,12 +1,11 @@
package ervu.service.fileupload.impl;
package ervu.service.fileupload;
import java.util.UUID;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ervu.client.fileupload.ErvuFileUploadClient;
import ervu.client.kafka.KafkaClient;
import ervu.service.fileupload.FileUploadV2Service;
import ervu.client.fileupload.EmployeeInformationFileWebDavUploadClient;
import ervu.client.kafka.KafkaSendMessageClient;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -19,24 +18,23 @@ import ru.cg.webbpm.modules.security.api.runtime.SecurityContext;
* @author Alexandr Shalaginov
*/
@Service
public class ErvuFileUploadV2ServiceImpl implements FileUploadV2Service {
private static final Logger logger = LoggerFactory.getLogger(ErvuFileUploadV2ServiceImpl.class);
private final KafkaClient kafkaClient;
public class EmployeeInformationFileUploadService {
private static final Logger logger = LoggerFactory.getLogger(EmployeeInformationFileUploadService.class);
private final KafkaSendMessageClient kafkaClient;
private final SecurityContext securityContext;
private final ErvuFileUploadClient ervuFileUploadClient;
private final EmployeeInformationFileWebDavUploadClient fileWebDavUploadClient;
public ErvuFileUploadV2ServiceImpl(ErvuFileUploadClient ervuFileUploadClient,
KafkaClient kafkaClient, SecurityContext securityContext) {
this.ervuFileUploadClient = ervuFileUploadClient;
public EmployeeInformationFileUploadService(EmployeeInformationFileWebDavUploadClient fileWebDavUploadClient,
KafkaSendMessageClient kafkaClient, SecurityContext securityContext) {
this.fileWebDavUploadClient = fileWebDavUploadClient;
this.kafkaClient = kafkaClient;
this.securityContext = securityContext;
}
@Override
public boolean saveFile(MultipartFile multipartFile) {
public boolean saveEmployeeInformationFile(MultipartFile multipartFile) {
String newFilename = getNewFilename(multipartFile.getOriginalFilename());
if (this.ervuFileUploadClient.uploadFile(newFilename, multipartFile)) {
if (this.fileWebDavUploadClient.webDavUploadFile(newFilename, multipartFile)) {
String jsonMessage = getJsonKafkaMessage(
new KafkaMessage(
securityContext.getCurrentUsername(),

View file

@ -1,10 +0,0 @@
package ervu.service.fileupload;
import org.springframework.web.multipart.MultipartFile;
/**
* @author Alexandr Shalaginov
*/
public interface FileUploadV2Service {
boolean saveFile(MultipartFile multipartFile);
}

View file

@ -1,4 +1,4 @@
package ervu.service.fileupload.impl;
package ervu.service.fileupload;
/**
* @author Alexandr Shalaginov

View file

@ -49,7 +49,7 @@ export class FileUploadV2 extends InputControl {
protected isProgressBarVisible: boolean = false;
private fileInputEl: any;
private url: string = '/backend/file/upload';
private url: string = '/backend/file/upload/employee-information';
private messagesService: MessagesService;
constructor(el: ElementRef, cd: ChangeDetectorRef) {

View file

@ -44,21 +44,5 @@
</entry>
</properties>
</scripts>
<scripts id="cfc09c36-7334-4fe4-bfca-e59e1c5a4fcc">
<classRef type="JAVA">
<className>FileUploadV2Controller</className>
<packageName>ervu.controller</packageName>
</classRef>
<enabled>true</enabled>
<expanded>true</expanded>
<properties>
<entry>
<key>fileUploadV2Service</key>
<value>
<implRef/>
</value>
</entry>
</properties>
</scripts>
</rootObject>
</xmlComponent>