Skip to content
Snippets Groups Projects
Commit 14c0f1cc authored by Paulo Pimenta's avatar Paulo Pimenta
Browse files

refactor(settings-controller): Added two entry points that allows reading and...

refactor(settings-controller): Added two entry points that allows reading and writing (not yet ready) f2ds settings configuration
parent 76a4ec82
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@
<description>smart-harvester</description>
<properties>
<java.version>1.8</java.version>
<java.version>11</java.version>
<spring.framework.version>5.2.9.RELEASE</spring.framework.version>
</properties>
......@@ -46,6 +46,13 @@
<version>0.9.1</version>
</dependency>
<!--Gson-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- Swagger openapi-->
<dependency>
<groupId>org.springdoc</groupId>
......@@ -103,7 +110,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
com.smartharvester.model.f2ds.F2DSSettings@61d4eec2
......@@ -64,8 +64,7 @@ public class SmartHarvesterAuthController {
if (userRepository.existsByEmail(signUpRequest.getEmail())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Username is already taken!",HttpStatus.BAD_REQUEST)
);
.body(new MessageResponse("Error: Username is already taken!",HttpStatus.BAD_REQUEST));
}
// Create new user's account
......
package com.smartharvester.controller;
import com.smartharvester.model.f2ds.F2DSSettings;
import com.smartharvester.model.login.request.SignupRequest;
import com.smartharvester.model.login.response.MessageResponse;
import com.smartharvester.util.EditResource;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.json.JSONParser;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.io.*;
import static java.nio.charset.StandardCharsets.UTF_8;
@CrossOrigin(origins = "*")
@RequestMapping("/harvester/settings")
@Tag(name="SmartHarvester authentication", description = "User authentication")
@RestController
public class SmartHarvesterF2DSSettingsController {
@Autowired
private EditResource editResource;
@Value("${f2dp.settings.filename}")
private String f2dsFileName;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@PostMapping (value="/update")
public ResponseEntity updateSettings(@RequestBody F2DSSettings f2DSSettings) {
try {
System.out.println("New data : " + f2DSSettings);
editResource.updateResourceFileAsString(String.valueOf(f2DSSettings),f2dsFileName);
return ResponseEntity.ok("Test");
}
catch (Exception e){
return ResponseEntity.unprocessableEntity().body("Could not update F2DS settings : " + e);
}
}
@GetMapping(value = "/load", produces= MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> readSettings() {
try {
String settings = editResource.readResourceFileAsString(f2dsFileName);
return ResponseEntity.ok().body(settings);
} catch (UncheckedIOException e){
return ResponseEntity
.badRequest()
.body(new MessageResponse("Could not load settings for F2DS : " + e.getLocalizedMessage(), HttpStatus.BAD_REQUEST));
}
}
}
package com.smartharvester.model.f2ds;
public class F2DSSettings {
String title;
String fdpUrl;
String fdpEmail;
String fdpPassword;
String smartApiUrl;
String mongoUrl;
String mongoUser;
String mongoPassword;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFdpUrl() {
return fdpUrl;
}
public void setFdpUrl(String fdpUrl) {
this.fdpUrl = fdpUrl;
}
public String getFdpEmail() {
return fdpEmail;
}
public void setFdpEmail(String fdpEmail) {
this.fdpEmail = fdpEmail;
}
public String getFdpPassword() {
return fdpPassword;
}
public void setFdpPassword(String fdpPassword) {
this.fdpPassword = fdpPassword;
}
public String getSmartApiUrl() {
return smartApiUrl;
}
public void setSmartApiUrl(String smartApiUrl) {
this.smartApiUrl = smartApiUrl;
}
public String getMongoUrl() {
return mongoUrl;
}
public void setMongoUrl(String mongoUrl) {
this.mongoUrl = mongoUrl;
}
public String getMongoUser() {
return mongoUser;
}
public void setMongoUser(String mongoUser) {
this.mongoUser = mongoUser;
}
public String getMongoPassword() {
return mongoPassword;
}
public void setMongoPassword(String mongoPassword) {
this.mongoPassword = mongoPassword;
}
}
package com.smartharvester.util;
import com.google.gson.Gson;
import com.smartharvester.model.f2ds.F2DSSettings;
import org.apache.commons.io.FileUtils;
import org.apache.tomcat.util.json.JSONParser;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import java.io.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
@Component("editResource")
public class EditResource {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
ResourceLoader resourceLoader;
public void updateResourceFileAsString(JSONArray newContent, String fileName) {
try{
Resource resourceFile = resourceLoader.getResource("classpath:"+fileName);
File tempFile = new File("src/main/resources/settings.tmp");
tempFile.createNewFile();
FileWriter ffw=new FileWriter(tempFile);
JSONParser jsonParser = new JSONParser();
ffw.write(String.valueOf(newContent));
ffw.write("\n");
ffw.close();
FileUtils.copyInputStreamToFile((InputStream) resourceFile, tempFile);
} catch (Exception e){
//return e.getMessage();
}
// gson.toJson(newSettings, new FileWriter(fileName));
}
public String readResourceFileAsString(String filename) {
Resource resourceFile = resourceLoader.getResource("classpath:"+filename);
try (Reader reader = new InputStreamReader(resourceFile.getInputStream(), UTF_8)) {
logger.info("Resource file '" + filename + "' successfully retrieved");
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
logger.error("Resource file '" + filename + "' could not be retrieved ");
throw new UncheckedIOException(e);
}
}
}
......@@ -39,3 +39,5 @@ springdoc.model-and-view-allowed=false
server.error.whitelabel.enabled=false
server.error.path=/error
#9. F2DS Settings Filename
f2dp.settings.filename=settings.json
\ No newline at end of file
{
"title": "F2DS EOSC-PILLAR",
"fdpurl": "http://51.178.69.147",
"fdpemail": "admin@cines-lab.fr",
"fdppassword": "adminadmin",
"smartapiurl": "http://localhost:8080/",
"mongourl": "51.178.69.150",
"mongouser": "admin",
"mongopassword": "adminadmin"
}
\ No newline at end of file
com.smartharvester.model.f2ds.F2DSSettings@b254fa2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment