Skip to content

Commit

Permalink
Merge pull request #54 from Konrad-Abramowski/development
Browse files Browse the repository at this point in the history
Deploy version 1.0.0
  • Loading branch information
Konrad-Abramowski authored Nov 27, 2021
2 parents 3501f73 + ef3a628 commit f2e3eb8
Show file tree
Hide file tree
Showing 67 changed files with 1,657 additions and 12,630 deletions.
14 changes: 14 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM maven:3.6.3-jdk-11 AS build
WORKDIR /usr/local/service
COPY src /usr/local/service/src
COPY pom.xml /usr/local/service
RUN mvn -f /usr/local/service/pom.xml clean package

FROM adoptopenjdk/openjdk11:latest
EXPOSE 8080
WORKDIR /usr/local/service
COPY --from=build /usr/local/service/target/smart-home-0.0.1-SNAPSHOT.jar /usr/local/service/target/smart-home-0.0.1-SNAPSHOT.jar
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install cups-client
ENTRYPOINT ["java", "-jar", "target/smart-home-0.0.1-SNAPSHOT.jar"]
47 changes: 22 additions & 25 deletions backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>jar</packaging>
<groupId>com.server</groupId>
<artifactId>smart-home</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0</version>
<name>smart-home</name>
<description>Smart home management system using IoT</description>
<properties>
Expand All @@ -20,15 +21,15 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -38,15 +39,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand All @@ -58,16 +50,26 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.22</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand All @@ -87,12 +89,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.server.smarthome;

import com.server.smarthome.properties.StorageProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class SmartHomeApplication {

public static void main(String[] args) {
SpringApplication.run(SmartHomeApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.server.smarthome.controller;


import com.server.smarthome.model.File;
import com.server.smarthome.model.PrinterConfiguration;
import com.server.smarthome.model.ResponseFile;
import com.server.smarthome.service.FileServiceImpl;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/")
public class FileController {

private FileServiceImpl fileService;
private String selectedPrinter;

public FileController(final FileServiceImpl fileService) {
this.fileService = fileService;
}

@PostMapping("/upload")
public ResponseEntity<?> uploadFiles(@RequestParam("file") MultipartFile[] files) {
try {
for (MultipartFile file : files) {
fileService.store(file);
}

return ResponseEntity.status(HttpStatus.OK).
body(fileService.getAllFilesAsResponseFileList());
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).
body("Uploading file failed");
}
}

@GetMapping("/files")
public ResponseEntity<List<ResponseFile>> getListFiles() {
return ResponseEntity.status(HttpStatus.OK).
body(fileService.getAllFilesAsResponseFileList());
}

@GetMapping("/files/{id}")
public ResponseEntity<byte[]> getFile(@PathVariable String id) {
File file = fileService.getFile(id);

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
.body(file.getData());
}

@DeleteMapping("files/{id}")
public ResponseEntity deleteFile(@PathVariable String id) {
boolean result = fileService.deleteFileById(id);
if (result) {
return ResponseEntity.status(HttpStatus.OK).
body(fileService.getAllFilesAsResponseFileList());
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@GetMapping("/printer/available-printers")
public ResponseEntity<List<String>> getPrinters() {
return new ResponseEntity<>(Arrays.stream(PrintServiceLookup.lookupPrintServices(null, null))
.map(PrintService::getName).collect(Collectors.toList()), HttpStatus.OK);

}

@PostMapping("/printer/config/{selectedPrinter}")
public ResponseEntity<String> configurePrinter(@PathVariable String selectedPrinter) {
this.selectedPrinter = selectedPrinter;
return new ResponseEntity<>(HttpStatus.OK);
}

@GetMapping("/printer/config")
public ResponseEntity<PrinterConfiguration> getPrinter() {
return new ResponseEntity<>(new PrinterConfiguration(this.selectedPrinter), HttpStatus.OK);
}

@PostMapping("/printAll")
public ResponseEntity printAllFiles() {
List<ResponseFile> files = fileService.getAllFiles().map(dbFile -> {
String fileDownloadUri = ServletUriComponentsBuilder
.fromCurrentContextPath()
.path("/files/")
.path(dbFile.getId())
.toUriString();

return new ResponseFile(
dbFile.getId(),
dbFile.getName(),
fileDownloadUri,
dbFile.getType(),
dbFile.getData().length);
}).collect(Collectors.toList());

try {
PrintService myPrintService = fileService.findPrintService(selectedPrinter);

for (int i = 0; i < files.size(); i++) {
URL url = new URL(files.get(i).getUrl());
java.io.File file = new java.io.File("file" + i + ".pdf");
org.apache.commons.io.FileUtils.copyURLToFile(url, file);
PDDocument document = PDDocument.load(file);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
job.setPrintService(myPrintService);
job.print();
file.deleteOnExit();
document.close();
}

} catch (IOException | PrinterException e) {
e.printStackTrace();
}
return new ResponseEntity<>(HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.server.smarthome.exception;

public class StorageException extends RuntimeException {

public StorageException(String message) {
super(message);
}

public StorageException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.server.smarthome.exception;

public class StorageFileNotFoundException extends StorageException {

public StorageFileNotFoundException(String message) {
super(message);
}

public StorageFileNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
58 changes: 58 additions & 0 deletions backend/src/main/java/com/server/smarthome/model/File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.server.smarthome.model;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity
@Table(name = "files")
public class File {

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;

private String name;

private String type;

@Lob
private byte[] data;

public File() {}

public File(final String name, final String type, final byte[] data) {
this.name = name;
this.type = type;
this.data = data;
}

public void setName(final String name) {
this.name = name;
}

public void setType(final String type) {
this.type = type;
}

public void setData(final byte[] data) {
this.data = data;
}

public String getId() {
return id;
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public byte[] getData() {
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.server.smarthome.model;

public class PrinterConfiguration {

private String name;

public PrinterConfiguration() {
}

public PrinterConfiguration(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Loading

0 comments on commit f2e3eb8

Please sign in to comment.