diff --git a/EffectiveJavaExercises/src/StaticFactoryMethods.java b/EffectiveJavaExercises/src/StaticFactoryMethods.java new file mode 100644 index 0000000..9ef3884 --- /dev/null +++ b/EffectiveJavaExercises/src/StaticFactoryMethods.java @@ -0,0 +1,9 @@ +import java.util.Collections; + +public class StaticFactoryMethods { + + public static void main(String args[]){ + Collections.emptyMap(); + } + +} diff --git a/README.md b/README.md deleted file mode 100644 index 739c44e..0000000 --- a/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# SpringBootApplications - -After the application started, you are supposed to see file named 'UploadRepo' under 'C:' directory. -This folder will be used to save the content of the uploaded files. - -src/main/resources/DownloadRepository: Download API will write the file content here. - -I used RestTemplate for client-side HTTP access. Run the Application.java as Spring Boot Application to run RestTemplate tests. - -Future enhancement: - -RestClientTest.java will be filled in to make the same tests in Application.java -ScheduledCronJobs.java will be completed with the related business methods. Template and the annotations are ready. diff --git a/SpringRESTExample/pom.xml b/SpringRESTExample/pom.xml deleted file mode 100644 index dea964b..0000000 --- a/SpringRESTExample/pom.xml +++ /dev/null @@ -1,122 +0,0 @@ - - - 4.0.0 - - com.springboot.restapi - SpringRESTExample - 0.0.1-SNAPSHOT - jar - - SpringRESTExample - Demo project for Spring Boot - - - org.springframework.boot - spring-boot-starter-parent - 1.3.3.RELEASE - - - - - UTF-8 - 1.8 - 3.6.8 - - - - - - org.hsqldb - hsqldb - 2.4.0 - - - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.springframework.boot - spring-boot-starter-data-rest - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - - org.hsqldb - hsqldb - runtime - - - - com.mysema.querydsl - querydsl-jpa - ${querydsl.version} - - - commons-lang - commons-lang - 2.6 - - - org.apache.commons - commons-lang3 - 3.3.2 - - - org.springframework.boot - - spring-boot-starter-cloud-connectors - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - com.mysema.maven - apt-maven-plugin - 1.1.3 - - - - process - - - target/generated-sources/java - com.mysema.query.apt.jpa.JPAAnnotationProcessor - - - - - - com.mysema.querydsl - querydsl-apt - ${querydsl.version} - - - - - - - - diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/Application.java b/SpringRESTExample/src/main/java/com/springboot/restapi/Application.java deleted file mode 100644 index 3497f0b..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/Application.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.springboot.restapi; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.web.client.RestTemplate; - -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - ApplicationContext context = SpringApplication.run(Application.class, - args); - - /** - * This method call will create a the folder "C://UploadRepo". This - * folder will be used to save the content of uploaded files. - */ - ((FileUploadUtil) context.getBean("fileUploadUtil")) - .createUploadRepository(); - - System.out.println(((RESTClient) context.getBean("restClient")) - .getAllFiles()); - - System.out.println(((RESTClient) context.getBean("restClient")) - .uploadFile( - "src/main/resources/UploadFromRepository/TextFile.txt", - null, null)); - - System.out.println(((RESTClient) context.getBean("restClient")) - .getAllFiles()); - - System.out.println(((RESTClient) context.getBean("restClient")) - .uploadFile( - "src/main/resources/UploadFromRepository/TextFile.txt", - "TextFile_1.txt", null)); - - System.out.println(((RESTClient) context.getBean("restClient")) - .uploadFile( - "src/main/resources/UploadFromRepository/TextFile.txt", - "TextFile_2.txt", "Test")); - - System.out.println(((RESTClient) context.getBean("restClient")) - .uploadFile("C://FileRepo/TextFileee.txt", "TextFile_3.txt", - "Test")); - - System.out.println(((RESTClient) context.getBean("restClient")) - .uploadFile("C://FileRepo///", "TextFile_4.txt", "Test")); - - System.out.println(((RESTClient) context.getBean("restClient")) - .uploadFile("C://FileRepo/TextFile.txt", "Text//File_5.txt", - "Test")); - - System.out.println(((RESTClient) context.getBean("restClient")) - .getAllFiles()); - - System.out.println(((RESTClient) context.getBean("restClient")) - .getFileMetaDataById(1)); - - System.out.println(((RESTClient) context.getBean("restClient")) - .getFileMetaDataById(null)); - - System.out.println(((RESTClient) context.getBean("restClient")) - .getFileMetaDataById(100)); - - System.out.println(((RESTClient) context.getBean("restClient")) - .downloadFile(1)); - - System.out.println(((RESTClient) context.getBean("restClient")) - .downloadFile(null)); - - System.out.println(((RESTClient) context.getBean("restClient")) - .downloadFile(100)); - - FileMetaDataSearchCriteria fileMetaDataSearchCriteria = new FileMetaDataSearchCriteria(); - - fileMetaDataSearchCriteria.setName("File_1"); - - System.out.println(((RESTClient) context.getBean("restClient")) - .searchFiles(fileMetaDataSearchCriteria)); - - } - - @Bean - public RestTemplate getRestTemplate() { - return new RestTemplate(); - } - -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/FileController.java b/SpringRESTExample/src/main/java/com/springboot/restapi/FileController.java deleted file mode 100644 index 692845d..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/FileController.java +++ /dev/null @@ -1,206 +0,0 @@ -package com.springboot.restapi; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.Collection; -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -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; - -@RestController -public class FileController { - - @Autowired - private FileRepository repo; - - @Autowired - private FileUploadUtil fileUploadUtil; - - @Autowired - private FileMetaDataDao fileMetaDataDao; - - /** - * This controller method is for fetching all uploaded files in the system - * - * @return - */ - @RequestMapping(value = "/files", method = RequestMethod.GET) - public ResponseEntity getAllFiles() { - List resultList = null; - try { - resultList = repo.findAll(); - } catch (Exception e) { - return new ResponseEntity( - "Exception: getAllFiles has failed.", - HttpStatus.BAD_REQUEST); - } - return new ResponseEntity>(resultList, - HttpStatus.OK); - } - - /** - * This controller method is for searching all uploaded files with a search - * criteria - * - * @param searchCriteria - * @return - */ - @RequestMapping(value = "/searchFile", method = RequestMethod.POST) - public List searchFiles( - @RequestBody FileMetaDataSearchCriteria searchCriteria) { - return fileMetaDataDao.searchFileMetaData(searchCriteria); - } - - /** - * This controller method is for finding a FileMetaData entity with the - * given id - * - * @param id - * @return - */ - @RequestMapping(value = "/metadata/{id}", method = RequestMethod.GET) - public ResponseEntity getFileMetaData(@PathVariable Integer id) { - FileMetaData fileMetaData = repo.findOne(id); - - if (fileMetaData != null) { - try { - Path path = Paths.get(fileMetaData.getPath()); - BasicFileAttributes attr = Files.readAttributes(path, - BasicFileAttributes.class); - if (attr != null) { - fileMetaData.setLastUpdatedDate(fileUploadUtil - .createLocalDateTimeFrom(attr.lastModifiedTime())); - fileMetaData.setLastAccessDate(fileUploadUtil - .createLocalDateTimeFrom(attr.lastAccessTime())); - fileMetaData.setDirectory(attr.isDirectory()); - fileMetaData.setOther(attr.isOther()); - fileMetaData.setRegularFile(attr.isRegularFile()); - fileMetaData.setSymbolicLink(attr.isSymbolicLink()); - fileMetaData.setSize(attr.size()); - repo.saveAndFlush(fileMetaData); - - return new ResponseEntity(fileMetaData, - HttpStatus.OK); - } - - } catch (IOException e) { - e.printStackTrace(); - return new ResponseEntity( - "IOException: Get file meta-data process has failed.", - HttpStatus.BAD_REQUEST); - } - - catch (Exception e) { - e.printStackTrace(); - return new ResponseEntity( - "Exception: Request has failed.", - HttpStatus.BAD_REQUEST); - } - } - - return new ResponseEntity("No FileMetaData found with id: " - + id, HttpStatus.BAD_REQUEST); - } - - /** - * This controller method is for uploading the given file with the given - * meta data information - * - * @param file - * @param name - * @param descr - * @return - */ - @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data") - public ResponseEntity uploadFile( - @RequestParam("file") MultipartFile file, - @RequestParam(value = "name", required = false) String name, - @RequestParam(value = "descr", required = false) String descr) { - - if (file.isEmpty()) { - return new ResponseEntity( - "Please select a file to upload.", HttpStatus.BAD_REQUEST); - } - - String fileName = StringUtils.isEmpty(name) ? file - .getOriginalFilename() : name; - - try { - - if (!StringUtils.isEmpty(fileName)) { - List metaDataList = repo.findByName(fileName); - if (metaDataList != null && metaDataList.size() > 0) { - return new ResponseEntity( - "Duplicate file name - FileMetaData found for the given name: " - + fileName, HttpStatus.BAD_REQUEST); - } - } - - fileUploadUtil.saveFileToDisk(file, fileName); - - FileMetaData fileMetaData = fileUploadUtil.createFileMetaDataFrom( - fileName, descr, file); - repo.saveAndFlush(fileMetaData); - } - - catch (Exception e) { - return new ResponseEntity(e.getClass() - + ": File upload process has failed for the file " - + fileName, HttpStatus.BAD_REQUEST); - } - - return new ResponseEntity(fileName + " successfully uploaded", - new HttpHeaders(), HttpStatus.OK); - } - - /** - * This controller method is for downloading the content of the file saved - * in FileMetaData table with the given id - * - * @param id - * @return - */ - @RequestMapping(value = "/download/{id}", method = RequestMethod.GET) - public ResponseEntity downloadFile(@PathVariable("id") Integer id) { - FileMetaData fileMetaData = repo.findOne(id); - if (fileMetaData != null) { - try { - Path path = Paths.get(fileMetaData.getPath()); - byte[] data = Files.readAllBytes(path); - - Files.write( - Paths.get("src/main/resources/DownloadRepository/" - + fileMetaData.getName()), data); - - return new ResponseEntity( - "File successfully downloaded.", new HttpHeaders(), - HttpStatus.OK); - } - - catch (Exception e) { - e.printStackTrace(); - return new ResponseEntity(e.getClass() - + ": Download file process has failed", - HttpStatus.BAD_REQUEST); - } - } - - return new ResponseEntity("File not found to download", - HttpStatus.BAD_REQUEST); - } - -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaData.java b/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaData.java deleted file mode 100644 index 8f5ba4b..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaData.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.springboot.restapi; - -import java.time.LocalDateTime; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -public class FileMetaData { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; - - @Column - private String name; - - @Column - private String originalName; - - @Column - private String description; - - @Column - private String path; - - @Column - private LocalDateTime createdDate; - - @Column - private String contentType; - - @Column - private Long size; - - @Column - private LocalDateTime lastUpdatedDate; - - @Column - private LocalDateTime lastAccessDate; - - @Column - private boolean directory; - - @Column - private boolean other; - - @Column - private boolean regularFile; - - @Column - private boolean symbolicLink; - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public LocalDateTime getCreatedDate() { - return createdDate; - } - - public void setCreatedDate(LocalDateTime createdDate) { - this.createdDate = createdDate; - } - - public String getContentType() { - return contentType; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - public String getOriginalName() { - return originalName; - } - - public void setOriginalName(String originalName) { - this.originalName = originalName; - } - - public Long getSize() { - return size; - } - - public void setSize(Long size) { - this.size = size; - } - - public LocalDateTime getLastUpdatedDate() { - return lastUpdatedDate; - } - - public void setLastUpdatedDate(LocalDateTime lastUpdatedDate) { - this.lastUpdatedDate = lastUpdatedDate; - } - - public LocalDateTime getLastAccessDate() { - return lastAccessDate; - } - - public void setLastAccessDate(LocalDateTime lastAccessDate) { - this.lastAccessDate = lastAccessDate; - } - - public boolean isDirectory() { - return directory; - } - - public void setDirectory(boolean directory) { - this.directory = directory; - } - - public boolean isOther() { - return other; - } - - public void setOther(boolean other) { - this.other = other; - } - - public boolean isRegularFile() { - return regularFile; - } - - public void setRegularFile(boolean regularFile) { - this.regularFile = regularFile; - } - - public boolean isSymbolicLink() { - return symbolicLink; - } - - public void setSymbolicLink(boolean symbolicLink) { - this.symbolicLink = symbolicLink; - } - -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataDao.java b/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataDao.java deleted file mode 100644 index b5b8bf5..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataDao.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.springboot.restapi; - -import java.util.List; - -public interface FileMetaDataDao { - public List searchFileMetaData( - FileMetaDataSearchCriteria fileMetaDataSearchCriteria); -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataDaoImpl.java b/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataDaoImpl.java deleted file mode 100644 index 8edca40..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataDaoImpl.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.springboot.restapi; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import com.mysema.query.jpa.impl.JPAQuery; - -@Transactional -@Repository -public class FileMetaDataDaoImpl implements FileMetaDataDao { - - @PersistenceContext - private EntityManager entityManager; - - @Override - public List searchFileMetaData( - FileMetaDataSearchCriteria fileMetaDataSearchCriteria) { - - JPAQuery query = new JPAQuery(entityManager); - QFileMetaData fileMetaData_ = QFileMetaData.fileMetaData; - - query = query.from(fileMetaData_); - - if (fileMetaDataSearchCriteria != null) { - if (StringUtils.isNotEmpty(fileMetaDataSearchCriteria.getName())) { - query = query.where(fileMetaData_.name - .containsIgnoreCase(fileMetaDataSearchCriteria - .getName())); - } - - if (StringUtils.isNotEmpty(fileMetaDataSearchCriteria.getDescr())) { - query = query.where(fileMetaData_.description - .containsIgnoreCase(fileMetaDataSearchCriteria - .getDescr())); - } - - if (StringUtils.isNotEmpty(fileMetaDataSearchCriteria.getPath())) { - query = query.where(fileMetaData_.path - .containsIgnoreCase(fileMetaDataSearchCriteria - .getPath())); - } - - if (StringUtils.isNotEmpty(fileMetaDataSearchCriteria - .getContentType())) { - query = query.where(fileMetaData_.contentType - .containsIgnoreCase(fileMetaDataSearchCriteria - .getContentType())); - } - - if (fileMetaDataSearchCriteria.getCreatedDateFrom() != null) { - query = query - .where(fileMetaData_.createdDate - .after(fileMetaDataSearchCriteria - .getCreatedDateFrom())); - } - - if (fileMetaDataSearchCriteria.getCreatedDateTo() != null) { - query = query.where(fileMetaData_.createdDate - .before(fileMetaDataSearchCriteria.getCreatedDateTo())); - } - - if (fileMetaDataSearchCriteria.getLastUpdateDateFrom() != null) { - query = query.where(fileMetaData_.lastUpdatedDate - .after(fileMetaDataSearchCriteria - .getLastUpdateDateFrom())); - } - - if (fileMetaDataSearchCriteria.getLastUpdateDateTo() != null) { - query = query.where(fileMetaData_.lastUpdatedDate - .before(fileMetaDataSearchCriteria - .getLastUpdateDateTo())); - } - - if (fileMetaDataSearchCriteria.getLastAccessDateFrom() != null) { - query = query.where(fileMetaData_.lastAccessDate - .after(fileMetaDataSearchCriteria - .getLastAccessDateFrom())); - } - - if (fileMetaDataSearchCriteria.getLastAccessDateTo() != null) { - query = query.where(fileMetaData_.lastAccessDate - .before(fileMetaDataSearchCriteria - .getLastAccessDateTo())); - } - } - - return query.list(fileMetaData_); - } - -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataSearchCriteria.java b/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataSearchCriteria.java deleted file mode 100644 index d54da5e..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/FileMetaDataSearchCriteria.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.springboot.restapi; - -import java.time.LocalDateTime; - -public class FileMetaDataSearchCriteria { - - private String name; - - private String descr; - - private String path; - - private LocalDateTime createdDateFrom; - - private LocalDateTime createdDateTo; - - private LocalDateTime lastUpdateDateFrom; - - private LocalDateTime lastUpdateDateTo; - - private String contentType; - - private LocalDateTime lastAccessDateFrom; - - private LocalDateTime lastAccessDateTo; - - public FileMetaDataSearchCriteria() { - - } - - public FileMetaDataSearchCriteria(String name, String descr, String path, - LocalDateTime createdDateFrom, LocalDateTime createdDateTo, - LocalDateTime lastUpdateDateFrom, LocalDateTime lastUpdateDateTo, - String contentType, LocalDateTime lastAccessDateFrom, - LocalDateTime lastAccessDateTo) { - this.name = name; - this.descr = descr; - this.path = path; - this.createdDateFrom = createdDateFrom; - this.createdDateTo = createdDateTo; - this.lastUpdateDateFrom = lastUpdateDateFrom; - this.lastUpdateDateTo = lastUpdateDateTo; - this.contentType = contentType; - this.lastAccessDateFrom = lastAccessDateFrom; - this.lastAccessDateTo = lastAccessDateTo; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescr() { - return descr; - } - - public void setDescr(String descr) { - this.descr = descr; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public LocalDateTime getCreatedDateFrom() { - return createdDateFrom; - } - - public void setCreatedDateFrom(LocalDateTime createdDateFrom) { - this.createdDateFrom = createdDateFrom; - } - - public LocalDateTime getCreatedDateTo() { - return createdDateTo; - } - - public void setCreatedDateTo(LocalDateTime createdDateTo) { - this.createdDateTo = createdDateTo; - } - - public LocalDateTime getLastUpdateDateFrom() { - return lastUpdateDateFrom; - } - - public void setLastUpdateDateFrom(LocalDateTime lastUpdateDateFrom) { - this.lastUpdateDateFrom = lastUpdateDateFrom; - } - - public LocalDateTime getLastUpdateDateTo() { - return lastUpdateDateTo; - } - - public void setLastUpdateDateTo(LocalDateTime lastUpdateDateTo) { - this.lastUpdateDateTo = lastUpdateDateTo; - } - - public String getContentType() { - return contentType; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - public LocalDateTime getLastAccessDateFrom() { - return lastAccessDateFrom; - } - - public void setLastAccessDateFrom(LocalDateTime lastAccessDateFrom) { - this.lastAccessDateFrom = lastAccessDateFrom; - } - - public LocalDateTime getLastAccessDateTo() { - return lastAccessDateTo; - } - - public void setLastAccessDateTo(LocalDateTime lastAccessDateTo) { - this.lastAccessDateTo = lastAccessDateTo; - } -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/FileRepository.java b/SpringRESTExample/src/main/java/com/springboot/restapi/FileRepository.java deleted file mode 100644 index f15e2c3..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/FileRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.springboot.restapi; - -import java.util.List; - -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.query.Param; -import org.springframework.data.rest.core.annotation.RepositoryRestResource; - -@RepositoryRestResource -public interface FileRepository extends JpaRepository { - List findByName(@Param("name") String name); -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/FileUploadUtil.java b/SpringRESTExample/src/main/java/com/springboot/restapi/FileUploadUtil.java deleted file mode 100644 index 8cdb10e..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/FileUploadUtil.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.springboot.restapi; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.attribute.BasicFileAttributes; -import java.nio.file.attribute.FileTime; -import java.sql.SQLException; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.util.Calendar; - -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; -import org.springframework.web.multipart.MultipartFile; - -@Component("fileUploadUtil") -public class FileUploadUtil { - - /** - * Contents of uploaded files will be saved in this folder - */ - private static String DEFAULT_UPLOADED_FOLDER = "C://UploadRepo//"; - - public void createUploadRepository() { - File file = new File(DEFAULT_UPLOADED_FOLDER); - if (!file.exists()) { - if (file.mkdir()) { - System.out.println("Default upload directory " - + DEFAULT_UPLOADED_FOLDER + " is created!"); - } else { - System.out - .println("Failed to create the default upload directory " - + DEFAULT_UPLOADED_FOLDER + "!"); - } - } - - System.out - .println("Please see the 'C:' directory for the new created UploadRepo folder that will be used as the default upload repository."); - - } - - /** - * This method is for writing the file content to the disk - * - * @param file - * @param name - * @throws IOException - */ - public void saveFileToDisk(MultipartFile file, String name) - throws IOException { - if (!file.isEmpty()) { - byte[] bytes = file.getBytes(); - - String fileName = StringUtils.isEmpty(name) ? file - .getOriginalFilename() : name; - - String filePath = DEFAULT_UPLOADED_FOLDER + fileName; - - Path path = Paths.get(filePath); - Files.write(path, bytes); - } - } - - /** - * This helper method is for creating a FileMetaData object from the given - * parameters - * - * @param name - * @param descr - * @param multipartFile - * @return - * @throws IOException - * @throws SQLException - */ - public FileMetaData createFileMetaDataFrom(String name, String descr, - MultipartFile multipartFile) throws IOException, SQLException { - - String fileName = StringUtils.isEmpty(name) ? multipartFile - .getOriginalFilename() : name; - - String filePath = DEFAULT_UPLOADED_FOLDER + fileName; - - Path path = Paths.get(filePath); - - FileMetaData fileMetaData = new FileMetaData(); - fileMetaData.setName(fileName); - fileMetaData.setDescription(descr); - fileMetaData.setContentType(multipartFile.getContentType()); - fileMetaData.setCreatedDate(LocalDateTime.now()); - fileMetaData.setPath(DEFAULT_UPLOADED_FOLDER + fileName); - fileMetaData.setOriginalName(multipartFile.getOriginalFilename()); - fileMetaData.setSize(multipartFile.getSize()); - - BasicFileAttributes attr = Files.readAttributes(path, - BasicFileAttributes.class); - - if (attr != null) { - fileMetaData.setLastUpdatedDate(createLocalDateTimeFrom(attr - .lastModifiedTime())); - fileMetaData.setLastAccessDate(createLocalDateTimeFrom(attr - .lastAccessTime())); - fileMetaData.setDirectory(attr.isDirectory()); - fileMetaData.setOther(attr.isOther()); - fileMetaData.setRegularFile(attr.isRegularFile()); - fileMetaData.setSymbolicLink(attr.isSymbolicLink()); - } - - return fileMetaData; - } - - public static LocalDateTime createLocalDateTimeFrom(FileTime fileTime) { - if (fileTime != null) { - Calendar cal = Calendar.getInstance(); - cal.setTimeInMillis(fileTime.toMillis()); - - LocalDateTime ofInstant = LocalDateTime.ofInstant(cal.toInstant(), - ZoneId.systemDefault()); - - return ofInstant; - } - return null; - } - -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/RESTClient.java b/SpringRESTExample/src/main/java/com/springboot/restapi/RESTClient.java deleted file mode 100644 index 2c8ae2d..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/RESTClient.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.springboot.restapi; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.FileSystemResource; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.http.converter.ByteArrayHttpMessageConverter; -import org.springframework.stereotype.Component; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.client.RestTemplate; - -@Component("restClient") -public class RESTClient { - @Autowired - private RestTemplate restTemplate; - - /** - * REST Client to make the request call "http://localhost:8080/files" - * - * @return - */ - public String getAllFiles() { - String result = restTemplate.getForObject( - "http://localhost:8080/files", String.class); - - return result; - } - - /** - * REST Client to make the request call "http://localhost:8080/searchFile" - * with the related parameters - * - * @param fileMetaDataSearchCriteria - * @return - */ - public String searchFiles( - FileMetaDataSearchCriteria fileMetaDataSearchCriteria) { - HttpEntity request = new HttpEntity<>( - fileMetaDataSearchCriteria); - return restTemplate.postForObject("http://localhost:8080/searchFile", - request, List.class).toString(); - } - - /** - * REST Client to make the request call "http://localhost:8080/upload" with - * the related parameters - * - * @param filePath - * @param name - * @param descr - * @return - */ - public String uploadFile(String filePath, String name, String descr) { - try { - MultiValueMap parameters = new LinkedMultiValueMap(); - parameters.add("file", new FileSystemResource(filePath)); - parameters.add("name", name); - parameters.add("descr", descr); - - HttpHeaders headers = new HttpHeaders(); - headers.set("Content-Type", "multipart/form-data"); - headers.set("Accept", "text/plain"); - - String result = restTemplate.postForObject( - "http://localhost:8080/upload", - new HttpEntity>(parameters, - headers), String.class); - - return result; - } catch (Exception e) { - return new ResponseEntity("uploadFile('" + filePath - + "', '" + name + "', '" + descr + "') has failed.", - HttpStatus.BAD_REQUEST).toString(); - } - } - - /** - * REST Client to make the request call - * "http://localhost:8080/metadata/{id}" - * - * @param id - * @return - */ - public String getFileMetaDataById(Integer id) { - try { - String result = restTemplate.getForObject( - "http://localhost:8080/metadata/" + id, String.class); - - return result; - } catch (Exception e) { - return new ResponseEntity("getFileMetaDataById(" + id - + ") has failed.", HttpStatus.BAD_REQUEST).toString(); - - } - } - - /** - * REST Client to make the request call - * "http://localhost:8080/download/{id}" - * - * @param id - * @return - */ - public String downloadFile(Integer id) { - try { - restTemplate.getMessageConverters().add( - new ByteArrayHttpMessageConverter()); - - return restTemplate.getForObject("http://localhost:8080/download/" - + id, String.class); - } - - catch (Exception e) { - return new ResponseEntity("downloadFile(" + id - + ") has failed.", HttpStatus.BAD_REQUEST).toString(); - } - } -} diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/RestClientTest.java b/SpringRESTExample/src/main/java/com/springboot/restapi/RestClientTest.java deleted file mode 100644 index 0194e5e..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/RestClientTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.springboot.restapi; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -/** - * RestTemplate tests in Application.java need to be implemented here. - * - */ -@RunWith(value = SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) -public class RestClientTest { - - @Autowired - private RESTClient restClient; - - @Test - public void testFindByName() { - } -} \ No newline at end of file diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/ScheduledCronJobs.java b/SpringRESTExample/src/main/java/com/springboot/restapi/ScheduledCronJobs.java deleted file mode 100644 index c64ddd3..0000000 --- a/SpringRESTExample/src/main/java/com/springboot/restapi/ScheduledCronJobs.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.springboot.restapi; - -import java.util.concurrent.atomic.AtomicBoolean; - -import org.springframework.scheduling.annotation.Scheduled; - -public class ScheduledCronJobs { - - private AtomicBoolean isRunning = new AtomicBoolean(false); - - /** - * Scheduled job to poll for new items in the last hour and send an email - */ - @Scheduled(cron = "0 0/60 * * *") - public void pollForNewItems() { - - if (isRunning.get()) { - return; - } - - try { - isRunning.set(true); - /** - * Add the business logic here to fetch the items in the last hour - */ - } catch (Exception e) { - e.printStackTrace(); - } finally { - isRunning.set(false); - /** - * Send email if there are any new items added in the last hour - */ - } - } - -} diff --git a/SpringRESTExample/src/main/resources/UploadFromRepository/TextFile.txt b/SpringRESTExample/src/main/resources/UploadFromRepository/TextFile.txt deleted file mode 100644 index f677204..0000000 --- a/SpringRESTExample/src/main/resources/UploadFromRepository/TextFile.txt +++ /dev/null @@ -1,9 +0,0 @@ -What is Spring? --------------- -Framework that makes developing Java applications easier. -Spring has best practices and design patterns built in. Even though I do not understand these patterns, -I will be able to get the benefits of those best practices anyway. - -Commonly used for Java based web applications. -Used as a way to access and manage database transactions in Java programs. -Broken up into pieces/modules. \ No newline at end of file diff --git a/SpringRESTExample/src/test/java/com/javarticle/spring/rest/SpringRestExampleApplicationTests.java b/SpringRESTExample/src/test/java/com/javarticle/spring/rest/SpringRestExampleApplicationTests.java deleted file mode 100644 index b63d4ce..0000000 --- a/SpringRESTExample/src/test/java/com/javarticle/spring/rest/SpringRestExampleApplicationTests.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.javarticle.spring.rest; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; - -import com.springboot.restapi.Application; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) -@WebAppConfiguration -public class SpringRestExampleApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/SpringRESTExample/src/main/resources/application.properties b/SpringWebFluxExample/pom.xml similarity index 100% rename from SpringRESTExample/src/main/resources/application.properties rename to SpringWebFluxExample/pom.xml diff --git a/SpringWebFluxExample/src/com/baeldung/EventProducerController.java b/SpringWebFluxExample/src/com/baeldung/EventProducerController.java new file mode 100644 index 0000000..6ad5256 --- /dev/null +++ b/SpringWebFluxExample/src/com/baeldung/EventProducerController.java @@ -0,0 +1,5 @@ +package com.baeldung; + +public class EventProducerController { + +} diff --git a/SpringWebFluxExample/src/com/baeldung/ReactiveWebClient.java b/SpringWebFluxExample/src/com/baeldung/ReactiveWebClient.java new file mode 100644 index 0000000..61e2453 --- /dev/null +++ b/SpringWebFluxExample/src/com/baeldung/ReactiveWebClient.java @@ -0,0 +1,5 @@ +package com.baeldung; + +public class ReactiveWebClient { + +}