Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Spring Boot service #48

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ aws-infrastructure/aws_ssh_key
aws-infrastructure/aws_ssh_key.pub
aws-infrastructure/.terraform.tfstate.lock.info
aws-infrastructure/instance-env.sh
spring-file/target
spring-file/*.iml
*.json
2 changes: 2 additions & 0 deletions spring-file/LICENSE.header
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The MIT License (MIT) Copyright (c) 2020-2022 artipie.com
https://github.com/artipie/benchmarks/blob/master/spring-file/LICENCE.header
21 changes: 21 additions & 0 deletions spring-file/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020-2022 artipie.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
108 changes: 108 additions & 0 deletions spring-file/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-file</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.artipie</groupId>
<artifactId>ppom</artifactId>
<version>1.1.0</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<qulice.license>${project.basedir}/LICENSE.header</qulice.license>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>${basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
<testResource>
<directory>${basedir}/src/test/resources-binary</directory>
<filtering>false</filtering>
</testResource>
</testResources>
<resources>
<resource>
<directory>${basedir}/src/test/resources-binary</directory>
<excludes>
<exclude>**/repodata/**</exclude>
</excludes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<configuration>
<excludes combine.children="append">
<exclude>checkstyle:/src/test/resources-binary/.*</exclude>
<exclude>checkstyle:/src/test/resources-binary/rpms/.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<executions>
<execution>
<id>jcabi-versionalize-packages</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>spring-file</finalName>
<archive>
<manifest>
<mainClass>com.artipie.spring.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</project>
25 changes: 25 additions & 0 deletions spring-file/src/main/java/com/artipie/spring/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2022 artipie.com
* https://github.com/artipie/benchmarks/blob/master/spring-file/LICENCE.header
*/
package com.artipie.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Entry point of application.
* @since 0.1
* @checkstyle HideUtilityClassConstructorCheck (500 lines)
*/
@SpringBootApplication
@SuppressWarnings({"PMD.UseUtilityClass", "PMD.ProhibitPublicStaticMethods"})
public class Application {
/**
* Entry point of application.
* @param args Application args
*/
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
93 changes: 93 additions & 0 deletions spring-file/src/main/java/com/artipie/spring/DataController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2022 artipie.com
* https://github.com/artipie/benchmarks/blob/master/spring-file/LICENCE.header
*/
package com.artipie.spring;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Controller which contains basic operations for work with data.
* @since 0.1
*/
@RestController
public final class DataController {
/**
* Data service.
*/
private final DataService dataservice;

/**
* Ctor.
* @param dataservice Data service
*/
@Autowired
public DataController(final DataService dataservice) {
this.dataservice = dataservice;
}

/**
* Upload binary data by specified key.
* @param key Key to data
* @param request Request with binary data
* @return Response
*/
@PutMapping(value = "upload/{key}", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<String> uploadData(
final @PathVariable("key") String key, final HttpServletRequest request
) {
ResponseEntity<String> res;
try {
this.dataservice.save(request.getInputStream(), key);
res = ResponseEntity.status(HttpStatus.CREATED)
.body(String.format("Uploaded by key '%s'", key));
} catch (final UncheckedIOException | IOException exc) {
res = ResponseEntity.internalServerError()
.body(String.format("Failed to upload by key '%s'", key));
}
return res;
}

/**
* Clean root directory from all data.
* @return Return OK status if cleaning was successful.
*/
@GetMapping("clean")
public ResponseEntity<HttpStatus> cleanRoot() {
this.dataservice.deleteAll();
return ResponseEntity.ok().build();
}

/**
* Download data by specfied key.
* @param key Key to resource
* @return Resource by specified key
*/
@GetMapping("/files/{key}")
public ResponseEntity<Resource> data(final @PathVariable("key") String key) {
ResponseEntity<Resource> res;
try {
final Resource data = this.dataservice.data(key);
res = ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
.body(data);
} catch (final MalformedURLException | FileNotFoundException exc) {
res = ResponseEntity.badRequest().build();
}
return res;
}
}
117 changes: 117 additions & 0 deletions spring-file/src/main/java/com/artipie/spring/DataService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2022 artipie.com
* https://github.com/artipie/benchmarks/blob/master/spring-file/LICENCE.header
*/
package com.artipie.spring;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;

/**
* Service interface for crucial operation to interact with data.
* @since 0.1
*/
public interface DataService {
/**
* Deletes all data from root path.
*/
void deleteAll();

/**
* Saves data to the root by specified key.
* @param data Stream with data for saving
* @param key Path to data
*/
void save(InputStream data, String key);

/**
* Obtains resource from root by specified key.
* @param key Key for obtaining resource
* @return Resource by specified key.
* @throws MalformedURLException In case of malformed key.
* @throws FileNotFoundException In case of absence file by specified key.
*/
Resource data(String key) throws MalformedURLException, FileNotFoundException;

/**
* Basic implementation for {@link DataService}.
* @since 0.1
*/
@Service
class Simple implements DataService {
/**
* Root path for saving files.
*/
private final Path root;

/**
* Ctor with random root path.
*/
public Simple() {
this(String.format("uploads%s", UUID.randomUUID()));
}

/**
* Ctor with specified root path.
* @param root Root path
*/
public Simple(final String root) {
this(Simple.init(root));
}

/**
* Ctor with specified root path.
* @param root Root path
*/
public Simple(final Path root) {
this.root = root;
}

@Override
public void deleteAll() {
FileSystemUtils.deleteRecursively(this.root.toFile());
}

@Override
public void save(final InputStream data, final String name) {
try {
Files.copy(data, this.root.resolve(name));
} catch (final IOException exc) {
throw new UncheckedIOException(String.format("Could not store '%s'", name), exc);
}
}

@Override
public Resource data(final String key) throws MalformedURLException, FileNotFoundException {
final Path target = this.root.resolve(key);
final Resource resource = new UrlResource(target.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
}
throw new FileNotFoundException(String.format("Could not read data by '%s'", key));
}

/**
* Initializes a temporary directory by specified key.
* @param prefix Prefix for temporary folder
* @return Created temporary folder.
*/
private static Path init(final String prefix) {
try {
return Files.createTempDirectory(prefix);
} catch (final IOException exc) {
throw new UncheckedIOException("Could not initialize folder for upload", exc);
}
}
}
}
10 changes: 10 additions & 0 deletions spring-file/src/main/java/com/artipie/spring/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2022 artipie.com
* https://github.com/artipie/benchmarks/blob/master/spring-file/LICENCE.header
*/

/**
* Files of Spring web-server.
* @since 0.1
*/
package com.artipie.spring;
Loading