From 3c8eb7c1d10b37c214f37f9459b5ccd0916fc173 Mon Sep 17 00:00:00 2001 From: Mohamed ENNAHDI EL IDRISSI Date: Thu, 7 Nov 2024 20:33:05 +0000 Subject: [PATCH] Endpoints re-organization + files overwriting --- .../app/controller/ObjectmorphController.java | 19 +++---------------- .../app/service/ObjectmorphService.java | 14 +++++++++----- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/controller/ObjectmorphController.java b/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/controller/ObjectmorphController.java index 872e335..f2b8830 100644 --- a/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/controller/ObjectmorphController.java +++ b/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/controller/ObjectmorphController.java @@ -1,20 +1,18 @@ package com.github.mohamedennahdi.objectmorph.app.controller; -import java.util.List; - import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; -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 com.github.mohamedennahdi.objectmorph.app.service.ObjectmorphService; import io.swagger.v3.oas.annotations.Parameter; +import lombok.extern.slf4j.Slf4j; +@Slf4j @RestController public class ObjectmorphController { @@ -25,16 +23,6 @@ public ObjectmorphController(ObjectmorphService objectmorphService) { } @CrossOrigin(origins = "*") - @RequestMapping( value = "/hello", method = RequestMethod.GET ) - public ResponseEntity hello() { - return new ResponseEntity<>( "Hello World", HttpStatus.OK); - } - - @RequestMapping( value = "/test", method = RequestMethod.GET ) - public ResponseEntity test(List files) { - return new ResponseEntity<>( "Hello World", HttpStatus.OK); - } - @GetMapping( value = "/html" ) public ResponseEntity html(@Parameter(name = "sourceCode", description = "Source Code", example = "class SourceCode { int attribute1; int attribute2; SourceCode(){} public int getAttribute1() { return attribute1;}}") @RequestParam("sourceCode") String[] sourceCode) { @@ -43,8 +31,7 @@ public ResponseEntity test(List files) { html = objectmorphService.generateHTML(sourceCode); return new ResponseEntity<>(html, HttpStatus.OK); } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("Error in ObjectmorphController", e); return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/service/ObjectmorphService.java b/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/service/ObjectmorphService.java index 91f4ed6..9b32938 100644 --- a/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/service/ObjectmorphService.java +++ b/objectmorph-app/src/main/java/com/github/mohamedennahdi/objectmorph/app/service/ObjectmorphService.java @@ -1,17 +1,16 @@ package com.github.mohamedennahdi.objectmorph.app.service; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Service; import com.github.mohamedennahdi.objectmorph.app.logic.ObjectmorphComponent; @@ -39,8 +38,12 @@ public String generateHTML(String[] sourceCodes) throws IOException, URISyntaxEx if (matcher.find()) { String fileName = matcher.group() + ".java"; log.info("Filename: " + fileName); - File file = Files.createFile(Paths.get(path + File.separator + fileName)).toFile(); - FileUtils.writeStringToFile(file, sourceCode, StandardCharsets.UTF_8); + File file = new File(path + File.separator + fileName); + try (Writer fileWriter = new FileWriter(file, false)) { + fileWriter.write(sourceCode); + } catch (Exception e) { + throw e; + } files.add(file); } } @@ -49,6 +52,7 @@ public String generateHTML(String[] sourceCodes) throws IOException, URISyntaxEx } catch (Exception e) { throw e; } finally { + log.info("Files removal."); for (File file : files) { Files.delete(file.toPath()); }