diff options
author | BharathS24 <BS00493532@techmahindra.com> | 2018-03-27 19:03:40 +0530 |
---|---|---|
committer | BharathS24 <BS00493532@techmahindra.com> | 2018-03-27 19:03:54 +0530 |
commit | a0cd2159e2dbfde7613ea7dda8db1eacced4ca69 (patch) | |
tree | f7c66341abe5f3ffbae1441ce23ebb170b9c6033 /snmpmapper/src | |
parent | d4ee5e4e99611a20c091fca6782ded05b560504f (diff) |
Added snmpmapper in Mapper
Commiting new Module in Mapper
Change-Id: I6fe49f5bf980bfc68abca89a7494f640942fe474
Issue-ID: DCAEGEN2-338
Signed-off-by: BharathS24 <BS00493532@techmahindra.com>
Diffstat (limited to 'snmpmapper/src')
12 files changed, 589 insertions, 0 deletions
diff --git a/snmpmapper/src/main/java/org/onap/dcae/mapper/FileUploadController.java b/snmpmapper/src/main/java/org/onap/dcae/mapper/FileUploadController.java new file mode 100644 index 0000000..fce8dfa --- /dev/null +++ b/snmpmapper/src/main/java/org/onap/dcae/mapper/FileUploadController.java @@ -0,0 +1,128 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.dcae.mapper; + +import java.io.IOException; +import java.net.URL; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; + +import org.onap.dcae.mapper.storage.StorageFileNotFoundException; +import org.onap.dcae.mapper.storage.StorageService; + +/** + * @author BS00493532 This Controller shifts control according to the uri + * accessed by the user. Accordingly control shifts to get and post + * methods. + * + */ +@Controller +public class FileUploadController { + + private final StorageService storageService; + + @Autowired + public FileUploadController(StorageService storageService) { + this.storageService = storageService; + } + + @GetMapping("/") + public String listUploadedFiles(Model model) throws IOException { + + model.addAttribute("files", + storageService.loadAll() + .map(path -> MvcUriComponentsBuilder + .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString()) + .build().toString()) + .collect(Collectors.toList())); + + return "uploadForm"; + + /** + * @author BS00493532 + * + * Returns the upload form like a html page + */ + + } + + @GetMapping("/files/{filename:.+}") + @ResponseBody + public ResponseEntity<Resource> serveFile(@PathVariable String filename) { + + Resource file = storageService.loadAsResource(filename); + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"") + .body(file); + } + + @GetMapping("/fileAsString/{filename:.+}") + @ResponseBody + public String serveFileAsString(@PathVariable String filename) { + + Resource file = storageService.loadAsResource(filename); + // URL url = Resources.getResource(file. getFilename());// + // getResource(file.getURL()); + String fileContent = null; + try { + fileContent = Resources.toString(file.getURL(), Charsets.UTF_8); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return fileContent; + } + + @PostMapping("/") + public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { + System.out.println("1"); + + storageService.store(file); + redirectAttributes.addFlashAttribute("message", + "You successfully uploaded " + file.getOriginalFilename() + "!"); + + return "redirect:/"; + } + + @ExceptionHandler(StorageFileNotFoundException.class) + public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc) { + return ResponseEntity.notFound().build(); + } + +} diff --git a/snmpmapper/src/main/java/org/onap/dcae/mapper/SnmpmapperApplication.java b/snmpmapper/src/main/java/org/onap/dcae/mapper/SnmpmapperApplication.java new file mode 100644 index 0000000..4893a2a --- /dev/null +++ b/snmpmapper/src/main/java/org/onap/dcae/mapper/SnmpmapperApplication.java @@ -0,0 +1,46 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.dcae.mapper; + +import org.onap.dcae.mapper.storage.StorageProperties; +import org.onap.dcae.mapper.storage.StorageService; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +@EnableConfigurationProperties(StorageProperties.class) +public class SnmpmapperApplication { + + public static void main(String[] args) { + SpringApplication.run(SnmpmapperApplication.class, args); + } + + @Bean + CommandLineRunner init(StorageService storageService) { + return (args) -> { + // storageService.deleteAll(); + storageService.init(); + }; + } +} diff --git a/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/FileSystemStorageService.java b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/FileSystemStorageService.java new file mode 100644 index 0000000..5cee706 --- /dev/null +++ b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/FileSystemStorageService.java @@ -0,0 +1,130 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.dcae.mapper.storage; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.stream.Stream; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; +import org.springframework.stereotype.Service; +import org.springframework.util.FileSystemUtils; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +@Service +public class FileSystemStorageService implements StorageService { + + private final Path rootLocation; + + @Autowired + public FileSystemStorageService(StorageProperties properties) { + this.rootLocation = Paths.get(properties.getLocation()); + } + + @Override + public void store(MultipartFile file) { + String filename = StringUtils.cleanPath(file.getOriginalFilename()); + try { + if (file.isEmpty()) { + throw new StorageException("Failed to store empty file " + filename); + } + + /** + * @author BS00493532 Checks the contents of the file. If empty, throws + * exception + */ + if (filename.contains("..")) { + // This is a security check + throw new StorageException( + "Cannot store file with relative path outside current directory " + filename); + } + Files.copy(file.getInputStream(), this.rootLocation.resolve(filename), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + throw new StorageException("Failed to store file " + filename, e); + } + } + + @Override + public Stream<Path> loadAll() { + try { + return Files.walk(this.rootLocation, 1).filter(path -> !path.equals(this.rootLocation)) + .map(path -> this.rootLocation.relativize(path)); + } + + /** + * @author BS00493532 Loads all the files in the given path + * + */ + + catch (IOException e) { + throw new StorageException("Failed to read stored files", e); + } + + } + + @Override + public Path load(String filename) { + return rootLocation.resolve(filename); + } + + @Override + public Resource loadAsResource(String filename) { + try { + Path file = load(filename); + Resource resource = new UrlResource(file.toUri()); + if (resource.exists() || resource.isReadable()) { + return resource; + } else { + throw new StorageFileNotFoundException("Could not read file: " + filename); + + } + } catch (MalformedURLException e) { + throw new StorageFileNotFoundException("Could not read file: " + filename, e); + } + } + + /* + * @Override public void deleteAll() { + * FileSystemUtils.deleteRecursively(rootLocation.toFile()); } + */ + + @Override + public void init() { + try { + Files.createDirectories(rootLocation); + } + + /** + * @author BS00493532 Initializes Storage + */ + + catch (IOException e) { + throw new StorageException("Could not initialize storage", e); + } + } +} diff --git a/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageException.java b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageException.java new file mode 100644 index 0000000..dd2675d --- /dev/null +++ b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageException.java @@ -0,0 +1,32 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.dcae.mapper.storage; + +public class StorageException extends RuntimeException { + + public StorageException(String message) { + super(message); + } + + public StorageException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageFileNotFoundException.java b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageFileNotFoundException.java new file mode 100644 index 0000000..ef88087 --- /dev/null +++ b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageFileNotFoundException.java @@ -0,0 +1,32 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.dcae.mapper.storage; + +public class StorageFileNotFoundException extends StorageException { + + public StorageFileNotFoundException(String message) { + super(message); + } + + public StorageFileNotFoundException(String message, Throwable cause) { + super(message, cause); + } +}
\ No newline at end of file diff --git a/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageProperties.java b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageProperties.java new file mode 100644 index 0000000..a2f5396 --- /dev/null +++ b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageProperties.java @@ -0,0 +1,44 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.dcae.mapper.storage; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("storage") +public class StorageProperties { + + /** + * Folder location for storing files + */ + // private String location = "upload-dir"; + @Value("${fileService.rootPath}") + private String location; + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + +} diff --git a/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageService.java b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageService.java new file mode 100644 index 0000000..cb278f2 --- /dev/null +++ b/snmpmapper/src/main/java/org/onap/dcae/mapper/storage/StorageService.java @@ -0,0 +1,43 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.dcae.mapper.storage; + +import java.nio.file.Path; +import java.util.stream.Stream; + +import org.springframework.core.io.Resource; +import org.springframework.web.multipart.MultipartFile; + +public interface StorageService { + + void init(); + + void store(MultipartFile file); + + Stream<Path> loadAll(); + + Path load(String filename); + + Resource loadAsResource(String filename); + + // void deleteAll(); + +} diff --git a/snmpmapper/src/main/resources/application.properties b/snmpmapper/src/main/resources/application.properties new file mode 100644 index 0000000..bee02d9 --- /dev/null +++ b/snmpmapper/src/main/resources/application.properties @@ -0,0 +1,2 @@ +fileService.rootPath=D:/configFiles +server.port=8888
\ No newline at end of file diff --git a/snmpmapper/src/main/resources/templates/uploadForm.html b/snmpmapper/src/main/resources/templates/uploadForm.html new file mode 100644 index 0000000..22ffc68 --- /dev/null +++ b/snmpmapper/src/main/resources/templates/uploadForm.html @@ -0,0 +1,19 @@ +<html xmlns:th="http://www.thymeleaf.org"> +<body> + + <div th:if="${message}"> + <h2 th:text="${message}"/> + </div> + + <div> + <form method="POST" enctype="multipart/form-data" action="/"> + <table> + <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr> + <tr><td></td><td><input type="submit" value="Upload" /></td></tr> + </table> + </form> + </div> + + +</body> +</html> diff --git a/snmpmapper/src/test/java/org/onap/dcae/mapper/StorageExceptionTest.java b/snmpmapper/src/test/java/org/onap/dcae/mapper/StorageExceptionTest.java new file mode 100644 index 0000000..1f4783d --- /dev/null +++ b/snmpmapper/src/test/java/org/onap/dcae/mapper/StorageExceptionTest.java @@ -0,0 +1,37 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ +package org.onap.dcae.mapper; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.onap.dcae.mapper.storage.StorageException; + +public class StorageExceptionTest { + + StorageException se = new StorageException("message"); + StorageException se1 = new StorageException("message", se); + + @Test + public void test() { + + +} +} diff --git a/snmpmapper/src/test/java/org/onap/dcae/mapper/StorageFileNotFoundExceptionTest.java b/snmpmapper/src/test/java/org/onap/dcae/mapper/StorageFileNotFoundExceptionTest.java new file mode 100644 index 0000000..39fbeac --- /dev/null +++ b/snmpmapper/src/test/java/org/onap/dcae/mapper/StorageFileNotFoundExceptionTest.java @@ -0,0 +1,37 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ +package org.onap.dcae.mapper; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.onap.dcae.mapper.storage.StorageFileNotFoundException; + +public class StorageFileNotFoundExceptionTest { + + StorageFileNotFoundException sfnfe = new StorageFileNotFoundException("message"); + StorageFileNotFoundException sfnfe1 = new StorageFileNotFoundException("message", sfnfe); + + @Test + public void test() { + + } + +} diff --git a/snmpmapper/src/test/java/org/onap/dcae/mapper/StoragePropertiesTest.java b/snmpmapper/src/test/java/org/onap/dcae/mapper/StoragePropertiesTest.java new file mode 100644 index 0000000..64915f5 --- /dev/null +++ b/snmpmapper/src/test/java/org/onap/dcae/mapper/StoragePropertiesTest.java @@ -0,0 +1,39 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DCAE +* ================================================================================ +* Copyright 2018 TechMahindra +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ +package org.onap.dcae.mapper; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.dcae.mapper.storage.StorageProperties; + +public class StoragePropertiesTest { + + StorageProperties sp= new StorageProperties(); + + +@Test +public void testStorageProperties() { + + sp.setLocation("location"); + assertEquals(sp.getLocation(), "location"); + +} +}
\ No newline at end of file |