Simplified API, refactoring

This commit is contained in:
Armin Friedl 2020-06-29 23:21:00 +02:00
parent d3855432b8
commit 77ce39244d
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
38 changed files with 1598 additions and 1339 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="19"> <profiles version="19">
<profile kind="CodeFormatterProfile" name="GoogleStyle" version="19"> <profile kind="CodeFormatterProfile" name="FlingStyle" version="19">
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_ellipsis" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_after_ellipsis" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression" value="do not insert"/>
@ -142,7 +142,7 @@
<setting id="org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment" value="false"/> <setting id="org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_simple_for_body_on_same_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_simple_for_body_on_same_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment" value="false"/> <setting id="org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_relational_operator" value="0"/> <setting id="org.eclipse.jdt.core.formatter.alignment_for_relational_operator" value="0"/>
@ -301,7 +301,7 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.indent_tag_description" value="false"/> <setting id="org.eclipse.jdt.core.formatter.comment.indent_tag_description" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_record_constructor" value="end_of_line"/> <setting id="org.eclipse.jdt.core.formatter.brace_position_for_record_constructor" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="16"/> <setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="16"/>
@ -315,7 +315,7 @@
<setting id="org.eclipse.jdt.core.formatter.indent_statements_compare_to_body" value="true"/> <setting id="org.eclipse.jdt.core.formatter.indent_statements_compare_to_body" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_multiple_fields" value="16"/> <setting id="org.eclipse.jdt.core.formatter.alignment_for_multiple_fields" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_simple_while_body_on_same_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_simple_while_body_on_same_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_array_initializer" value="end_of_line"/> <setting id="org.eclipse.jdt.core.formatter.brace_position_for_array_initializer" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.wrap_before_logical_operator" value="true"/> <setting id="org.eclipse.jdt.core.formatter.wrap_before_logical_operator" value="true"/>
@ -332,7 +332,7 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_simple_do_while_body_on_same_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_simple_do_while_body_on_same_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_annotation_type_declaration" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_annotation_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line" value="one_line_never"/> <setting id="org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line" value="one_line_never"/>

View file

@ -11,7 +11,7 @@
</parent> </parent>
<groupId>net.friedl</groupId> <groupId>net.friedl</groupId>
<artifactId>fling</artifactId> <artifactId>fling</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<name>fling</name> <name>fling</name>
<description>Simple artifact sharing</description> <description>Simple artifact sharing</description>
@ -90,19 +90,20 @@
<artifactId>mapstruct</artifactId> <artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version> <version>${mapstruct.version}</version>
</dependency> </dependency>
<!-- OpenAPI Generator -->
<dependency> <dependency>
<groupId>org.mapstruct</groupId> <groupId>org.springdoc</groupId>
<artifactId>mapstruct-processor</artifactId> <artifactId>springdoc-openapi-ui</artifactId>
<version>${mapstruct.version}</version> <version>1.2.32</version>
<scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<resources> <resources>
<resource> <resource>
<!-- Replace @spring.profiles.active@ in application.yml by setting in <!-- Replace @spring.profiles.active@ in application.yml by setting
maven profile See also: profiles section --> in maven profile See also: profiles section -->
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
@ -111,7 +112,23 @@
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
<!-- Generators -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
@ -137,6 +154,29 @@
</annotationProcessorPaths> </annotationProcessorPaths>
</configuration> </configuration>
</plugin> </plugin>
<!-- OpenApi -->
<plugin>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFileName>flingapi.json</outputFileName>
<outputDir>${project.build.directory}/openapi-spec</outputDir>
<!-- Attach api doc to deployed artifact -->
<attachArtifact>true</attachArtifact>
</configuration>
</plugin>
<!-- Deployment -->
<plugin> <plugin>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<executions> <executions>
@ -165,7 +205,8 @@
<id>local</id> <id>local</id>
<properties> <properties>
<spring.profiles.active>local</spring.profiles.active> <spring.profiles.active>local</spring.profiles.active>
<!-- automatically run annotation processors within the incremental compilation --> <!-- automatically run annotation processors within the incremental
compilation -->
<m2e.apt.activation>jdt_apt</m2e.apt.activation> <m2e.apt.activation>jdt_apt</m2e.apt.activation>
</properties> </properties>
<dependencies> <dependencies>

View file

@ -1,5 +1,6 @@
package net.friedl.fling; package net.friedl.fling;
import java.nio.file.Path;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -8,7 +9,10 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import net.friedl.fling.model.json.PathDeserializer;
import net.friedl.fling.model.json.PathSerializer;
@Configuration @Configuration
public class FlingConfiguration { public class FlingConfiguration {
@ -19,12 +23,18 @@ public class FlingConfiguration {
@Bean @Bean
public ObjectMapper objectMapper() { public ObjectMapper objectMapper() {
SimpleModule simpleModule = new SimpleModule();
simpleModule
.addDeserializer(Path.class, new PathDeserializer())
.addSerializer(Path.class, new PathSerializer());
ObjectMapper objectMapper = new ObjectMapper() ObjectMapper objectMapper = new ObjectMapper()
.setSerializationInclusion(Include.NON_ABSENT) .setSerializationInclusion(Include.NON_ABSENT)
.registerModule(new JavaTimeModule()) .registerModule(new JavaTimeModule())
// Handle instant as milliseconds // Handle instant as milliseconds
.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false) .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.registerModule(simpleModule);
return objectMapper; return objectMapper;
} }

View file

@ -1,6 +1,8 @@
package net.friedl.fling.controller; package net.friedl.fling.controller;
import java.util.List; import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
@ -10,73 +12,59 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import net.friedl.fling.model.dto.ArtifactDto; import net.friedl.fling.model.dto.ArtifactDto;
import net.friedl.fling.persistence.archive.ArchiveException;
import net.friedl.fling.service.ArtifactService; import net.friedl.fling.service.ArtifactService;
import net.friedl.fling.service.archive.ArchiveService;
@Slf4j
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api/artifacts")
public class ArtifactController { public class ArtifactController {
private ArtifactService artifactService; private ArtifactService artifactService;
private ArchiveService archiveService;
@Autowired @Autowired
public ArtifactController(ArtifactService artifactService) { public ArtifactController(ArtifactService artifactService, ArchiveService archiveService) {
this.artifactService = artifactService; this.artifactService = artifactService;
this.archiveService = archiveService;
} }
@GetMapping(path = "/artifacts", params = "flingId") @GetMapping(path = "/{id}")
public List<ArtifactDto> getArtifacts(@RequestParam Long flingId) { public ArtifactDto getArtifact(@PathVariable UUID id) {
return artifactService.findAllArtifacts(flingId); return artifactService.getById(id);
} }
@GetMapping(path = "/artifacts", params = "artifactId") @DeleteMapping(path = "/{id}")
public ResponseEntity<ArtifactDto> getArtifact(@RequestParam Long artifactId) { public void deleteArtifact(@PathVariable UUID id) {
return ResponseEntity.of(artifactService.findArtifact(artifactId)); artifactService.delete(id);
} }
@PostMapping("/artifacts/{flingId}") @PostMapping(path = "/{id}/data")
public ArtifactDto postArtifact(@PathVariable Long flingId, HttpServletRequest request) public void uploadArtifactData(@PathVariable UUID id, HttpServletRequest request) {
throws Exception { try {
return artifactService.storeArtifact(flingId, request.getInputStream()); archiveService.storeArtifact(id, request.getInputStream());
} catch (IOException e) {
log.error("Could not read input from stream", e);
throw new UncheckedIOException(e);
}
} }
@PatchMapping(path = "/artifacts/{artifactId}", consumes = MediaType.APPLICATION_JSON_VALUE) @GetMapping(path = "/{id}/data")
public ArtifactDto patchArtifact(@PathVariable Long artifactId, @RequestBody String body) { public ResponseEntity<Resource> downloadArtifact(@PathVariable UUID id) {
return artifactService.mergeArtifact(artifactId, body); ArtifactDto artifactDto = artifactService.getById(id);
} InputStreamResource data = new InputStreamResource(archiveService.getArtifact(id));
@DeleteMapping(path = "/artifacts/{artifactId}")
public void deleteArtifact(@PathVariable Long artifactId) throws ArchiveException {
artifactService.deleteArtifact(artifactId);
}
@GetMapping(path = "/artifacts/{artifactId}/downloadid")
public String getDownloadId(@PathVariable Long artifactId) {
return artifactService.generateDownloadId(artifactId);
}
@GetMapping(path = "/artifacts/{artifactId}/{downloadId}/download")
public ResponseEntity<Resource> downloadArtifact(@PathVariable Long artifactId,
@PathVariable String downloadId)
throws ArchiveException {
var artifact = artifactService.findArtifact(artifactId).orElseThrow();
var stream = new InputStreamResource(artifactService.downloadArtifact(downloadId));
return ResponseEntity.ok() return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, .header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=\"" + artifact.getName() + "\"") "attachment;filename=\"" + artifactDto.getPath().getFileName() + "\"")
.contentLength(artifact.getSize())
.contentType(MediaType.APPLICATION_OCTET_STREAM) .contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(stream); .body(data);
} }
} }

View file

@ -1,7 +1,7 @@
package net.friedl.fling.controller; package net.friedl.fling.controller;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -12,79 +12,73 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import net.friedl.fling.model.dto.ArtifactDto;
import net.friedl.fling.model.dto.FlingDto; import net.friedl.fling.model.dto.FlingDto;
import net.friedl.fling.persistence.archive.ArchiveException; import net.friedl.fling.service.ArtifactService;
import net.friedl.fling.service.FlingService; import net.friedl.fling.service.FlingService;
import net.friedl.fling.service.archive.ArchiveService;
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api/fling")
public class FlingController { public class FlingController {
private FlingService flingService; private FlingService flingService;
private ArtifactService artifactService;
private ArchiveService archiveService;
@Autowired @Autowired
public FlingController(FlingService flingService) { public FlingController(FlingService flingService, ArtifactService artifactService,
ArchiveService archiveService) {
this.flingService = flingService; this.flingService = flingService;
this.artifactService = artifactService;
this.archiveService = archiveService;
} }
@GetMapping("/fling") @GetMapping
public List<FlingDto> getFlings() { public List<FlingDto> getFlings() {
return flingService.findAll(); return flingService.findAll();
} }
@PostMapping("/fling") @PostMapping
public Long postFling(@RequestBody FlingDto flingDto) { public FlingDto postFling(@RequestBody FlingDto flingDto) {
return flingService.createFling(flingDto); return flingService.create(flingDto);
} }
@PutMapping("/fling/{flingId}") @PostMapping("/{id}/artifact")
public void putFling(@PathVariable Long flingId, @RequestBody FlingDto flingDto) { public ArtifactDto postArtifact(@PathVariable UUID id, @RequestBody ArtifactDto artifactDto) {
flingService.mergeFling(flingId, flingDto); return artifactService.create(id, artifactDto);
} }
@GetMapping(path = "/fling", params = "flingId") @GetMapping(path = "/{id}")
public ResponseEntity<FlingDto> getFling(@RequestParam Long flingId) { public FlingDto getFling(@PathVariable UUID id) {
return ResponseEntity.of(flingService.findFlingById(flingId)); return flingService.getById(id);
} }
@GetMapping(path = "/fling", params = "shareId") @GetMapping(path = "/share/{shareId}")
public ResponseEntity<FlingDto> getFlingByShareId(@RequestParam String shareId) { public FlingDto getFlingByShareId(@PathVariable String shareId) {
return ResponseEntity.of(flingService.findFlingByShareId(shareId)); return flingService.getByShareId(shareId);
} }
@GetMapping(path = "/fling/shareExists/{shareId}") @DeleteMapping("/{id}")
public Boolean getShareExists(@PathVariable String shareId) { public void deleteFling(@PathVariable UUID id) {
return flingService.existsShareUrl(shareId); flingService.delete(id);
} }
@DeleteMapping("/fling/{flingId}") @GetMapping(path = "/{id}/data")
public void deleteFling(@PathVariable Long flingId) { public ResponseEntity<Resource> getFlingData(@PathVariable UUID id) {
flingService.deleteFlingById(flingId); FlingDto flingDto = flingService.getById(id);
} InputStreamResource data = new InputStreamResource(archiveService.getFling(id));
@GetMapping(path = "/fling/{flingId}/package")
public String packageFling(@PathVariable Long flingId) throws IOException, ArchiveException {
return flingService.packageFling(flingId);
}
@GetMapping(path = "/fling/{flingId}/download/{downloadId}")
public ResponseEntity<Resource> downloadFling(@PathVariable Long flingId,
@PathVariable String downloadId) throws ArchiveException, IOException {
var fling = flingService.findFlingById(flingId).orElseThrow();
var flingPackage = flingService.downloadFling(downloadId);
var stream = new InputStreamResource(flingPackage.getFirst());
return ResponseEntity.ok() return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, .header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=\"" + fling.getName() + ".zip" + "\"") "attachment;filename=\"" + flingDto.getName() + ".zip" + "\"")
.contentLength(flingPackage.getSecond()) .contentLength(200L) // FIXME
.contentType(MediaType.APPLICATION_OCTET_STREAM) .contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(stream); .body(data);
} }
} }

View file

@ -1,23 +1,30 @@
package net.friedl.fling.model.dto; package net.friedl.fling.model.dto;
import java.nio.file.Path;
import java.time.Instant; import java.time.Instant;
import java.util.UUID;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
@Data @Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ArtifactDto { public class ArtifactDto {
private String name; @NotNull
private UUID id;
private Long id; @NotNull
private Path path;
private String path; @Builder.Default
private Instant uploadTime = Instant.now();
private String doi; private String archiveId;
private Long size; @Builder.Default
private Boolean archived = false;
private Integer version;
private Instant uploadTime;
private FlingDto fling;
} }

View file

@ -1,96 +1,43 @@
package net.friedl.fling.model.dto; package net.friedl.fling.model.dto;
import java.time.Instant; import java.time.Instant;
import java.util.HashMap; import java.util.UUID;
import java.util.Map; import javax.validation.constraints.NotNull;
import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.AllArgsConstructor;
import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
@Data @Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FlingDto { public class FlingDto {
@NotNull
private UUID id;
@NotNull
private String name; private String name;
private Long id; @NotNull
@Builder.Default
private Instant creationTime = Instant.now();
private Instant creationTime; @NotNull
private String shareId;
@JsonIgnore
private Boolean directDownload;
@JsonIgnore
private Boolean allowUpload;
@JsonIgnore
private Boolean shared;
@JsonIgnore
private String shareUrl;
@JsonIgnore
private Integer expirationClicks;
@JsonIgnore
private Instant expirationTime;
private String authCode; private String authCode;
@JsonProperty("sharing") @Builder.Default
private void unpackSharing(Map<String, Object> sharing) { private Boolean directDownload = false;
this.directDownload = (Boolean) sharing.getOrDefault("directDownload", false);
this.allowUpload = (Boolean) sharing.getOrDefault("allowUpload", false);
this.shared = (Boolean) sharing.getOrDefault("shared", true);
this.shareUrl = (String) sharing.getOrDefault("shareUrl", null);
}
@JsonProperty("sharing") @Builder.Default
private Map<String, Object> packSharing() { private Boolean allowUpload = false;
Map<String, Object> sharing = new HashMap<>();
sharing.put("directDownload", this.directDownload);
sharing.put("allowUpload", this.allowUpload);
sharing.put("shared", this.shared);
sharing.put("shareUrl", this.shareUrl);
return sharing; @Builder.Default
} private Boolean shared = true;
@JsonProperty("expiration") private Integer expirationClicks;
private void unpackExpiration(Map<String, Object> expiration) {
String type = (String) expiration.getOrDefault("type", null);
if (type == null)
return;
switch (type) { private Instant expirationTime;
case "time":
this.expirationClicks = null;
// json can only handle int, long must be given as string
// TODO: this back and forth conversion is a bit hack-ish
this.expirationTime =
Instant.ofEpochMilli(Long.valueOf(expiration.get("value").toString()));
break;
case "clicks":
this.expirationTime = null;
this.expirationClicks = Integer.valueOf(expiration.get("value").toString());
break;
default:
throw new IllegalArgumentException("Unexpected value '" + type + "'");
}
}
@JsonProperty("expiration")
private Map<String, Object> packExpiration() {
Map<String, Object> expiration = new HashMap<>();
if (this.expirationClicks != null) {
expiration.put("type", "clicks");
expiration.put("value", this.expirationClicks);
}
if (this.expirationTime != null) {
expiration.put("type", "time");
expiration.put("value", this.expirationTime.toEpochMilli());
}
return expiration;
}
} }

View file

@ -1,12 +0,0 @@
package net.friedl.fling.model.dto;
import lombok.Data;
@Data
public class FlingSharingDto {
private Boolean allowUpload;
private Boolean directDownload;
private String shareUrl;
}

View file

@ -0,0 +1,34 @@
package net.friedl.fling.model.json;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class PathDeserializer extends StdDeserializer<Path> {
private static final long serialVersionUID = 1504807365764537418L;
public PathDeserializer() {
this(String.class);
}
protected PathDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Path deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
ObjectCodec codec = p.getCodec();
JsonNode node = codec.readTree(p);
return Paths.get(node.textValue());
}
}

View file

@ -0,0 +1,26 @@
package net.friedl.fling.model.json;
import java.io.IOException;
import java.nio.file.Path;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class PathSerializer extends StdSerializer<Path> {
public PathSerializer() {
this(Path.class);
}
protected PathSerializer(Class<Path> t) {
super(t);
}
private static final long serialVersionUID = -1003917305429893614L;
@Override
public void serialize(Path value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeString(value.toString());
}
}

View file

@ -1,49 +1,15 @@
package net.friedl.fling.model.mapper; package net.friedl.fling.model.mapper;
import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import lombok.extern.slf4j.Slf4j;
import net.friedl.fling.model.dto.ArtifactDto; import net.friedl.fling.model.dto.ArtifactDto;
import net.friedl.fling.persistence.entities.ArtifactEntity; import net.friedl.fling.persistence.entities.ArtifactEntity;
@Slf4j
@Mapper(componentModel = "spring") @Mapper(componentModel = "spring")
public abstract class ArtifactMapper { public interface ArtifactMapper {
public abstract ArtifactDto map(ArtifactEntity artifactEntity); ArtifactDto map(ArtifactEntity artifactEntity);
ArtifactEntity map(ArtifactDto artifactDto);
public abstract ArtifactEntity map(ArtifactDto artifactDto); List<ArtifactDto> mapEntities(List<ArtifactEntity> artifactEntities);
List<ArtifactEntity> mapDtos(List<ArtifactDto> artifactDtos);
public abstract List<ArtifactDto> map(List<ArtifactEntity> artifactEntities);
public Optional<ArtifactDto> map(Optional<ArtifactEntity> artifactEntity) {
return artifactEntity.map(a -> map(a));
}
public ArtifactDto merge(ArtifactDto originalArtifactDto, Map<String, Object> patch) {
ArtifactDto mergedArtifactDto = new ArtifactDto();
for (Field field : ArtifactDto.class.getDeclaredFields()) {
String fieldName = field.getName();
field.setAccessible(true);
try {
if (patch.containsKey(fieldName)) {
if (field.getType().equals(Long.class)) {
field.set(mergedArtifactDto, ((Number) patch.get(fieldName)).longValue());
}
field.set(mergedArtifactDto, patch.get(fieldName));
} else {
field.set(mergedArtifactDto, field.get(originalArtifactDto));
}
} catch (IllegalArgumentException | IllegalAccessException e) {
log.error("Could not merge {} [value={}] with {}", fieldName, patch.get(fieldName),
originalArtifactDto,
e);
}
}
return mergedArtifactDto;
}
} }

View file

@ -1,7 +1,6 @@
package net.friedl.fling.model.mapper; package net.friedl.fling.model.mapper;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import net.friedl.fling.model.dto.FlingDto; import net.friedl.fling.model.dto.FlingDto;
import net.friedl.fling.persistence.entities.FlingEntity; import net.friedl.fling.persistence.entities.FlingEntity;
@ -9,12 +8,8 @@ import net.friedl.fling.persistence.entities.FlingEntity;
@Mapper(componentModel = "spring") @Mapper(componentModel = "spring")
public interface FlingMapper { public interface FlingMapper {
FlingDto map(FlingEntity flingEntity); FlingDto map(FlingEntity flingEntity);
default Optional<FlingDto> map(Optional<FlingEntity> flingEntity) {
return flingEntity.map(f -> map(f));
}
FlingEntity map(FlingDto flingDto); FlingEntity map(FlingDto flingDto);
List<FlingDto> map(List<FlingEntity> flingEntities); List<FlingDto> mapEntities(List<FlingEntity> flingEntities);
List<FlingEntity> mapDtos(List<FlingDto> flingDtos);
} }

View file

@ -1,40 +0,0 @@
package net.friedl.fling.persistence.archive;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public interface Archive {
/**
* Retrieve an artifact from the archive
*
* @param id The unique artifact id as returned by {@link Archive#store}
* @return An {@link InputStream} for reading the artifact
*/
InputStream get(String id) throws ArchiveException;
/**
* Store an artifact
*
* @param is The artifact represented as {@link InputStream}
* @return A unique archive id for the artifact
* @throws IOException If anything goes wrong while storing the artifact in the archive
*/
String store(InputStream is) throws ArchiveException;
default String store(File file) throws ArchiveException {
try {
return store(new FileInputStream(file));
} catch (IOException ex) {
throw new ArchiveException(ex);
}
}
/**
* Delete an artifact
*
* @param id The unique artifact id as returned by {@link Archive#store}
*/
void remove(String id) throws ArchiveException;
}

View file

@ -1,80 +0,0 @@
package net.friedl.fling.persistence.archive.impl;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import net.friedl.fling.persistence.archive.Archive;
import net.friedl.fling.persistence.archive.ArchiveException;
@Component("fileSystemArchive")
public class FileSystemArchive implements Archive {
private MessageDigest fileStoreDigest;
private FileSystemArchiveConfiguration configuration;
@Autowired
public FileSystemArchive(MessageDigest fileStoreDigest,
FileSystemArchiveConfiguration configuration) {
this.fileStoreDigest = fileStoreDigest;
this.configuration = configuration;
}
@Override
public InputStream get(String id) throws ArchiveException {
try {
var path = Paths.get(configuration.getDirectory(), id);
FileInputStream fis = new FileInputStream(path.toFile());
return fis;
} catch (FileNotFoundException ex) {
throw new ArchiveException(ex);
}
}
@Override
public String store(InputStream is) throws ArchiveException {
try {
byte[] fileBytes = is.readAllBytes();
is.close();
String fileStoreId = hexEncode(fileStoreDigest.digest(fileBytes));
FileChannel fc = FileChannel.open(Paths.get(configuration.getDirectory(), fileStoreId),
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
fc.write(ByteBuffer.wrap(fileBytes));
fc.close();
return fileStoreId;
} catch (IOException ex) {
throw new ArchiveException(ex);
}
}
@Override
public void remove(String id) throws ArchiveException {
var path = Paths.get(configuration.getDirectory(), id);
try {
Files.deleteIfExists(path);
} catch (IOException e) {
throw new ArchiveException("Could not delete file at " + path.toString(), e);
}
}
private String hexEncode(byte[] fileStoreId) {
StringBuilder sb = new StringBuilder(fileStoreId.length * 2);
for (byte b : fileStoreId)
sb.append(String.format("%02x", b));
return sb.toString();
}
}

View file

@ -1,41 +0,0 @@
package net.friedl.fling.persistence.archive.impl;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
@ConfigurationProperties("fling.archive.fileystem")
@ConditionalOnBean(FileSystemArchive.class)
@Getter
@Setter
public class FileSystemArchiveConfiguration {
private String directory;
@Bean
public MessageDigest fileStoreDigest() throws NoSuchAlgorithmException {
return MessageDigest.getInstance("SHA-512");
}
@PostConstruct
public void init() throws IOException {
if (directory == null) {
log.info("Directory not configured take temp path");
Path tmpPath = Files.createTempDirectory("fling");
this.directory = tmpPath.toAbsolutePath().toString();
}
log.info("File store directory: {}", directory);
}
}

View file

@ -1,46 +1,37 @@
package net.friedl.fling.persistence.entities; package net.friedl.fling.persistence.entities;
import java.nio.file.Path;
import java.time.Instant; import java.time.Instant;
import java.util.UUID;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Entity @Entity
@Table(name = "Artifact") @Table(name = "Artifact")
@Getter @Getter @Setter
@Setter
public class ArtifactEntity { public class ArtifactEntity {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private UUID id;
private String name; @Column(nullable = false)
private Path path;
private Integer version; @Column(nullable = false)
private Instant uploadTime = Instant.now();
private String path; @Column(unique = true, nullable = true)
private String archiveId;
@Column(unique = true) @Column(nullable = false)
private String doi; private Boolean archived = false;
private Instant uploadTime;
private Long size;
@ManyToOne(optional = false) @ManyToOne(optional = false)
private FlingEntity fling; private FlingEntity fling;
@PrePersist
private void prePersist() {
this.uploadTime = Instant.now();
if (this.version == null)
this.version = -1;
}
} }

View file

@ -2,69 +2,47 @@ package net.friedl.fling.persistence.entities;
import java.time.Instant; import java.time.Instant;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.PostPersist;
import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Entity @Entity
@Table(name = "Fling") @Table(name = "Fling")
@Getter @Getter @Setter
@Setter
public class FlingEntity { public class FlingEntity {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private UUID id;
private String name; private String name;
private Instant creationTime; private Instant creationTime = Instant.now();
private Instant expirationTime; private Instant expirationTime;
private Integer expirationClicks; private Integer expirationClicks;
@Column(nullable = false) @Column(nullable = false)
private Boolean directDownload; private Boolean directDownload = false;
@Column(nullable = false) @Column(nullable = false)
private Boolean allowUpload; private Boolean allowUpload = false;
@Column(nullable = false) @Column(nullable = false)
private Boolean shared; private Boolean shared = true;
@Column(unique = true, nullable = false) @Column(unique = true, nullable = false)
private String shareUrl; private String shareId;
private String authCode; private String authCode;
@OneToMany(mappedBy = "fling", cascade = CascadeType.ALL, orphanRemoval = true) @OneToMany(mappedBy = "fling", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<ArtifactEntity> artifacts; private Set<ArtifactEntity> artifacts;
@PrePersist
private void prePersist() {
if (this.directDownload == null)
this.directDownload = false;
if (this.allowUpload == null)
this.allowUpload = false;
if (this.shared == null)
this.shared = true;
this.creationTime = Instant.now();
}
@PostPersist
private void postPersist() {
System.out.println("ID: " + this.id);
System.out.println("Share Url: " + this.shareUrl);
this.shareUrl = this.id + this.shareUrl;
}
} }

View file

@ -1,14 +1,10 @@
package net.friedl.fling.persistence.repositories; package net.friedl.fling.persistence.repositories;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import net.friedl.fling.persistence.entities.ArtifactEntity; import net.friedl.fling.persistence.entities.ArtifactEntity;
public interface ArtifactRepository extends JpaRepository<ArtifactEntity, Long> { public interface ArtifactRepository extends JpaRepository<ArtifactEntity, UUID> {
Optional<ArtifactEntity> findByDoi(String doi);
List<ArtifactEntity> deleteByDoi(String doi);
List<ArtifactEntity> findAllByFlingId(Long flingId); List<ArtifactEntity> findAllByFlingId(Long flingId);
} }

View file

@ -1,14 +1,15 @@
package net.friedl.fling.persistence.repositories; package net.friedl.fling.persistence.repositories;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import net.friedl.fling.persistence.entities.FlingEntity; import net.friedl.fling.persistence.entities.FlingEntity;
public interface FlingRepository extends JpaRepository<FlingEntity, Long> { public interface FlingRepository extends JpaRepository<FlingEntity, UUID> {
Optional<FlingEntity> findByName(String name); Optional<FlingEntity> findByName(String name);
Optional<FlingEntity> findByShareUrl(String shareUrl); FlingEntity findByShareId(String shareId);
@Query("SELECT COUNT(*) FROM ArtifactEntity a, FlingEntity f where a.fling=f.id and f.id=:flingId") @Query("SELECT COUNT(*) FROM ArtifactEntity a, FlingEntity f where a.fling=f.id and f.id=:flingId")
Long countArtifactsById(Long flingId); Long countArtifactsById(Long flingId);

View file

@ -0,0 +1,21 @@
package net.friedl.fling.persistence.types;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class PathConverter implements AttributeConverter<Path, String> {
@Override
public String convertToDatabaseColumn(Path attribute) {
return attribute.toString();
}
@Override
public Path convertToEntityAttribute(String dbData) {
return Paths.get(dbData);
}
}

View file

@ -1,89 +1,71 @@
package net.friedl.fling.security; package net.friedl.fling.security;
import java.util.NoSuchElementException; import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AbstractAuthenticationToken; import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.friedl.fling.security.authentication.FlingToken; import net.friedl.fling.security.authentication.FlingToken;
import net.friedl.fling.security.authentication.dto.UserAuthDto; import net.friedl.fling.security.authentication.dto.UserAuthDto;
import net.friedl.fling.service.ArtifactService;
import net.friedl.fling.service.FlingService; import net.friedl.fling.service.FlingService;
@Slf4j @Slf4j
@Service @Service
public class AuthorizationService { public class AuthorizationService {
private FlingService flingService; private FlingService flingService;
private ArtifactService artifactService;
@Autowired @Autowired
public AuthorizationService(FlingService flingService, ArtifactService artifactService) { public AuthorizationService(FlingService flingService) {
this.flingService = flingService; this.flingService = flingService;
this.artifactService = artifactService;
} }
public boolean allowUpload(Long flingId, AbstractAuthenticationToken token) { public boolean allowUpload(UUID flingId, AbstractAuthenticationToken token) {
if (!(token instanceof FlingToken)) if (!(token instanceof FlingToken)) {
log.warn("Authorization attempt without fling token for {}. Authorization denied.", flingId);
return false; return false;
}
FlingToken flingToken = (FlingToken) token; FlingToken flingToken = (FlingToken) token;
if (flingToken.getGrantedFlingAuthority().getAuthority() if (FlingAuthority.FLING_OWNER.name()
.equals(FlingAuthority.FLING_OWNER.name())) { .equals(flingToken.getGrantedFlingAuthority().getAuthority())) {
log.debug("Owner authorized for upload [id = {}]", flingId);
return true; return true;
} }
var uploadAllowed = flingService.findFlingById(flingId).orElseThrow().getAllowUpload(); boolean uploadAllowed = flingService.getById(flingId).getAllowUpload();
boolean authorized = uploadAllowed
&& flingToken.getGrantedFlingAuthority().getFlingId().equals(flingId);
return uploadAllowed && flingToken.getGrantedFlingAuthority().getFlingId().equals(flingId); log.debug("User {} authorized for upload [id = {}]", authorized ? "" : "not", flingId);
return authorized;
} }
public boolean allowPatchingArtifact(Long artifactId, FlingToken authentication) { public boolean allowFlingAccess(UUID flingId, AbstractAuthenticationToken token) {
var flingId = artifactService.findArtifact(artifactId).orElseThrow().getFling().getId(); if (!(token instanceof FlingToken)) {
return allowUpload(flingId, authentication); log.warn("Authorization attempt without fling token for {}. Authorization denied.", flingId);
}
public boolean allowFlingAccess(UserAuthDto userAuth, String shareUrl) {
return userAuth.getShareId().equals(shareUrl);
}
public boolean allowFlingAccess(Long flingId, AbstractAuthenticationToken token) {
if (!(token instanceof FlingToken))
return false; return false;
}
FlingToken flingToken = (FlingToken) token; FlingToken flingToken = (FlingToken) token;
if (flingToken.getGrantedFlingAuthority().getAuthority() if (FlingAuthority.FLING_OWNER.name()
.equals(FlingAuthority.FLING_OWNER.name())) { .equals(flingToken.getGrantedFlingAuthority().getAuthority())) {
log.debug("Owner authorized for fling access [id = {}]", flingId);
return true; return true;
} }
return flingToken.getGrantedFlingAuthority().getFlingId().equals(flingId); boolean authorized = flingToken.getGrantedFlingAuthority().getFlingId().equals(flingId);
log.debug("User {} authorized for fling access [id = {}]", authorized ? "" : "not", flingId);
return authorized;
} }
public boolean allowFlingAccess(AbstractAuthenticationToken token, HttpServletRequest request) { public boolean allowFlingAccess(UserAuthDto userAuth, String shareId) {
if (!(token instanceof FlingToken)) boolean authorized = userAuth.getShareId().equals(shareId);
return false; log.debug("User {} authorized for fling access [shareId = {}]", authorized ? "" : "not",
shareId);
FlingToken flingToken = (FlingToken) token; return authorized;
if (flingToken.getGrantedFlingAuthority().getAuthority()
.equals(FlingAuthority.FLING_OWNER.name())) {
return true;
} }
var shareId = request.getParameter("shareId");
Long flingId;
try {
flingId = shareId != null
? flingService.findFlingByShareId(shareId).orElseThrow().getId()
: Long.parseLong(request.getParameter("flingId"));
} catch (NumberFormatException | NoSuchElementException e) {
log.warn("Invalid shareId [shareId=\"{}\"] or flingId [flingId=\"{}\"] found",
request.getParameter("shareId"), request.getParameter("flingId"));
flingId = null;
}
return flingToken.getGrantedFlingAuthority().getFlingId().equals(flingId);
}
} }

View file

@ -1,9 +1,7 @@
package net.friedl.fling.security; package net.friedl.fling.security;
import static org.springframework.security.config.Customizer.withDefaults; import static org.springframework.security.config.Customizer.withDefaults;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -12,9 +10,6 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
@ -101,20 +96,6 @@ public class FlingWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
//@formatter:on //@formatter:on
} }
private RequestMatcher modificationMethodsAntMatcher(String antPattern) {
return multiMethodAntMatcher(antPattern,
HttpMethod.PATCH, HttpMethod.PUT,
HttpMethod.POST, HttpMethod.DELETE);
}
private RequestMatcher multiMethodAntMatcher(String antPattern, HttpMethod... httpMethods) {
List<RequestMatcher> antMatchers = Arrays.stream(httpMethods)
.map(m -> new AntPathRequestMatcher(antPattern, m.toString()))
.collect(Collectors.toList());
return new OrRequestMatcher(antMatchers);
}
@Bean @Bean
public CorsConfigurationSource corsConfigurationSource() { public CorsConfigurationSource corsConfigurationSource() {
// see https://stackoverflow.com/a/43559266 // see https://stackoverflow.com/a/43559266

View file

@ -19,7 +19,7 @@ public class AuthenticationController {
this.authenticationService = authenticationService; this.authenticationService = authenticationService;
} }
@PostMapping("/auth/owner") @PostMapping(path = "/auth/owner")
public String authenticateOwner(@RequestBody OwnerAuthDto ownerAuthDto) { public String authenticateOwner(@RequestBody OwnerAuthDto ownerAuthDto) {
return authenticationService.authenticate(ownerAuthDto); return authenticationService.authenticate(ownerAuthDto);
} }

View file

@ -3,6 +3,7 @@ package net.friedl.fling.security.authentication;
import java.security.Key; import java.security.Key;
import java.time.Instant; import java.time.Instant;
import java.util.Date; import java.util.Date;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
@ -12,6 +13,7 @@ import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.JwtParser; import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import net.friedl.fling.model.dto.FlingDto;
import net.friedl.fling.security.FlingAuthority; import net.friedl.fling.security.FlingAuthority;
import net.friedl.fling.security.FlingSecurityConfiguration; import net.friedl.fling.security.FlingSecurityConfiguration;
import net.friedl.fling.security.authentication.dto.OwnerAuthDto; import net.friedl.fling.security.authentication.dto.OwnerAuthDto;
@ -49,17 +51,16 @@ public class AuthenticationService {
} }
public String authenticate(UserAuthDto userAuth) { public String authenticate(UserAuthDto userAuth) {
var fling = flingService.findFlingByShareId(userAuth.getShareId()) FlingDto flingDto = flingService.getByShareId(userAuth.getShareId());
.orElseThrow();
String authCode = userAuth.getCode(); String authCode = userAuth.getCode();
if (!flingService.hasAuthCode(fling.getId(), authCode)) { if (!flingService.validateAuthCode(flingDto.getId(), authCode)) {
throw new AccessDeniedException("Wrong fling code"); throw new AccessDeniedException("Wrong fling code");
} }
return makeBaseBuilder() return makeBaseBuilder()
.setSubject("user") .setSubject("user")
.claim("sid", fling.getShareUrl()) .claim("sid", flingDto.getShareId())
.compact(); .compact();
} }
@ -68,7 +69,7 @@ public class AuthenticationService {
Claims claims = parseClaims(token); Claims claims = parseClaims(token);
FlingAuthority authority; FlingAuthority authority;
Long flingId; UUID flingId;
switch (claims.getSubject()) { switch (claims.getSubject()) {
case "owner": case "owner":
@ -77,8 +78,8 @@ public class AuthenticationService {
break; break;
case "user": case "user":
authority = FlingAuthority.FLING_USER; authority = FlingAuthority.FLING_USER;
var sid = claims.get("sid", String.class); String sid = claims.get("sid", String.class);
flingId = flingService.findFlingByShareId(sid).orElseThrow().getId(); flingId = flingService.getByShareId(sid).getId();
break; break;
default: default:
throw new BadCredentialsException("Invalid token"); throw new BadCredentialsException("Invalid token");

View file

@ -1,5 +1,6 @@
package net.friedl.fling.security.authentication; package net.friedl.fling.security.authentication;
import java.util.UUID;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import net.friedl.fling.security.FlingAuthority; import net.friedl.fling.security.FlingAuthority;
@ -13,14 +14,14 @@ public class GrantedFlingAuthority implements GrantedAuthority {
private static final long serialVersionUID = -1552301479158714777L; private static final long serialVersionUID = -1552301479158714777L;
private FlingAuthority authority; private FlingAuthority authority;
private Long flingId; private UUID flingId;
public GrantedFlingAuthority(FlingAuthority authority, Long flingId) { public GrantedFlingAuthority(FlingAuthority authority, UUID flingId) {
this.authority = authority; this.authority = authority;
this.flingId = flingId; this.flingId = flingId;
} }
public Long getFlingId() { public UUID getFlingId() {
return this.flingId; return this.flingId;
} }

View file

@ -1,89 +1,85 @@
package net.friedl.fling.service; package net.friedl.fling.service;
import java.io.InputStream; import java.util.UUID;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import net.friedl.fling.model.dto.ArtifactDto; import net.friedl.fling.model.dto.ArtifactDto;
import net.friedl.fling.model.mapper.ArtifactMapper; import net.friedl.fling.model.mapper.ArtifactMapper;
import net.friedl.fling.persistence.archive.Archive;
import net.friedl.fling.persistence.archive.ArchiveException;
import net.friedl.fling.persistence.entities.ArtifactEntity; import net.friedl.fling.persistence.entities.ArtifactEntity;
import net.friedl.fling.persistence.entities.FlingEntity;
import net.friedl.fling.persistence.repositories.ArtifactRepository; import net.friedl.fling.persistence.repositories.ArtifactRepository;
import net.friedl.fling.persistence.repositories.FlingRepository; import net.friedl.fling.persistence.repositories.FlingRepository;
import net.friedl.fling.service.archive.ArchiveService;
@Slf4j
@Service @Service
@Transactional @Transactional
public class ArtifactService { public class ArtifactService {
private FlingRepository flingRepository;
private ArtifactRepository artifactRepository; private ArtifactRepository artifactRepository;
private FlingRepository flingRepository;
private ArtifactMapper artifactMapper; private ArtifactMapper artifactMapper;
private Archive archive; private ArchiveService archiveService;
@Autowired @Autowired
public ArtifactService(ArtifactRepository artifactRepository, FlingRepository flingRepository, public ArtifactService(ArtifactRepository artifactRepository, FlingRepository flingRepository,
ArtifactMapper artifactMapper, Archive archive) { ArtifactMapper artifactMapper, ArchiveService archiveService) {
this.artifactRepository = artifactRepository; this.artifactRepository = artifactRepository;
this.flingRepository = flingRepository; this.flingRepository = flingRepository;
this.artifactMapper = artifactMapper; this.artifactMapper = artifactMapper;
this.archive = archive; this.archiveService = archiveService;
} }
public List<ArtifactDto> findAllArtifacts(Long flingId) { /**
return artifactMapper.map(artifactRepository.findAllByFlingId(flingId)); * Fetch an {@link ArtifactDto} by id. Must be called with a valid artifact id, otherwise bails
* out with a {@link RuntimeException}. Synchronization must be done on client side.
*
* @param id A valid {@link UUID} for an existing entity in the database. Not null.
* @return The ArtifactDto corresponding to the {@code id}
*/
@NotNull
public ArtifactDto getById(@NotNull UUID id) {
return artifactMapper.map(artifactRepository.getOne(id));
} }
public ArtifactDto storeArtifact(Long flingId, InputStream artifact) throws ArchiveException { /**
var flingEntity = flingRepository.findById(flingId).orElseThrow(); * Create a new {@link ArtifactEntity} from {@code artifactDto} for the fling {@code flingId}.
var archiveId = archive.store(artifact); *
* @param flingId Id of an existing {@link FlingEntity}
* @param artifactDto The data for the new {@link ArtifactEntity}
* @return The newly created artifact
*/
public ArtifactDto create(UUID flingId, ArtifactDto artifactDto) {
FlingEntity flingEntity = flingRepository.getOne(flingId);
ArtifactEntity artifactEntity = new ArtifactEntity(); ArtifactEntity artifactEntity = artifactMapper.map(artifactDto);
artifactEntity.setDoi(archiveId);
artifactEntity.setFling(flingEntity); artifactEntity.setFling(flingEntity);
artifactEntity = artifactRepository.save(artifactEntity);
artifactRepository.save(artifactEntity);
return artifactMapper.map(artifactEntity); return artifactMapper.map(artifactEntity);
} }
public Optional<ArtifactDto> findArtifact(Long artifactId) { /**
return artifactMapper.map(artifactRepository.findById(artifactId)); * Deletes an artifact identified by {@code id}. NOOP if the artifact cannot be found.
*
* @param id An {@link UUID} that identifies the artifact
*/
public void delete(UUID id) {
if (id == null)
return;
ArtifactEntity artifactEntity = artifactRepository.findById(id).orElse(null);
if (artifactEntity == null) {
log.warn("Cannot delete artifact {}. Artifact not found.", id);
return;
} }
public ArtifactDto mergeArtifact(Long artifactId, String body) { archiveService.deleteArtifact(id);
JsonParser jsonParser = JsonParserFactory.getJsonParser(); artifactRepository.delete(artifactEntity);
Map<String, Object> parsedBody = jsonParser.parseMap(body); log.info("Deleted artifact {}", artifactEntity);
artifactRepository.findById(artifactId)
// map entity to dto
.map(artifactMapper::map)
// merge parsedBody into dto
.map(a -> artifactMapper.merge(a, parsedBody))
// map dto to entity
.map(artifactMapper::map)
.ifPresent(artifactRepository::save);
return artifactMapper.map(artifactRepository.getOne(artifactId));
}
public void deleteArtifact(Long artifactId) throws ArchiveException {
var doi = artifactRepository.getOne(artifactId).getDoi();
artifactRepository.deleteById(artifactId);
archive.remove(doi);
}
public String generateDownloadId(Long artifactId) {
// TODO: This id is not secured! Generate temporary download id
return artifactRepository.getOne(artifactId).getDoi();
}
public InputStream downloadArtifact(String downloadId) throws ArchiveException {
return archive.get(downloadId);
} }
} }

View file

@ -1,24 +1,11 @@
package net.friedl.fling.service; package net.friedl.fling.service;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.security.crypto.codec.Hex; import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.keygen.KeyGenerators; import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -26,11 +13,9 @@ import org.springframework.util.StringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.friedl.fling.model.dto.FlingDto; import net.friedl.fling.model.dto.FlingDto;
import net.friedl.fling.model.mapper.FlingMapper; import net.friedl.fling.model.mapper.FlingMapper;
import net.friedl.fling.persistence.archive.Archive;
import net.friedl.fling.persistence.archive.ArchiveException;
import net.friedl.fling.persistence.entities.ArtifactEntity;
import net.friedl.fling.persistence.entities.FlingEntity; import net.friedl.fling.persistence.entities.FlingEntity;
import net.friedl.fling.persistence.repositories.FlingRepository; import net.friedl.fling.persistence.repositories.FlingRepository;
import net.friedl.fling.service.archive.ArchiveService;
@Slf4j @Slf4j
@Service @Service
@ -39,135 +24,99 @@ public class FlingService {
private FlingRepository flingRepository; private FlingRepository flingRepository;
private FlingMapper flingMapper; private FlingMapper flingMapper;
private Archive archive; private ArchiveService archiveService;
private MessageDigest keyHashDigest; private MessageDigest keyHashDigest;
@Autowired @Autowired
public FlingService(FlingRepository flingRepository, FlingMapper flingMapper, Archive archive, public FlingService(FlingRepository flingRepository, FlingMapper flingMapper,
ArchiveService archiveService,
MessageDigest keyHashDigest) { MessageDigest keyHashDigest) {
this.flingRepository = flingRepository; this.flingRepository = flingRepository;
this.flingMapper = flingMapper; this.flingMapper = flingMapper;
this.archive = archive; this.archiveService = archiveService;
this.keyHashDigest = keyHashDigest; this.keyHashDigest = keyHashDigest;
} }
/**
* Retrieves a list of all flings
*
* @return A list of all flings
*/
public List<FlingDto> findAll() { public List<FlingDto> findAll() {
return flingMapper.map(flingRepository.findAll()); return flingMapper.mapEntities(flingRepository.findAll());
} }
public Long createFling(FlingDto flingDto) { /**
if (!StringUtils.hasText(flingDto.getShareUrl())) { * Get a fling by id
flingDto.setShareUrl(generateShareUrl()); *
* @param id Id of the fling. Must exist.
* @return The fling
*/
public FlingDto getById(UUID id) {
return flingMapper.map(flingRepository.getOne(id));
}
/**
* Creates a new fling entity from {@code flingDto}
*
* @param flingDto Base data from which the new fling should be created
* @return The created fling
*/
public FlingDto create(FlingDto flingDto) {
log.debug("Creating new fling");
FlingEntity flingEntity = flingMapper.map(flingDto);
if (!StringUtils.hasText(flingEntity.getShareId())) {
log.debug("No share id set. Generating random share id");
flingEntity.setShareId(generateShareId());
}
if (StringUtils.hasText(flingEntity.getAuthCode())) {
log.debug("Hashing authentication code for {}", flingEntity.getId());
flingEntity.setAuthCode(hashAuthCode(flingDto.getAuthCode()));
} }
var flingEntity = flingMapper.map(flingDto);
flingEntity.setAuthCode(hashKey(flingEntity.getAuthCode()));
flingEntity = flingRepository.save(flingEntity); flingEntity = flingRepository.save(flingEntity);
return flingEntity.getId(); log.debug("Created new fling {}", flingEntity.getId());
return flingMapper.map(flingEntity);
} }
public Boolean existsShareUrl(String shareUrl) { public FlingDto getByShareId(String shareId) {
return !flingRepository.findByShareUrl(shareUrl).isEmpty(); FlingEntity flingEntity = flingRepository.findByShareId(shareId);
return flingMapper.map(flingEntity);
} }
public void mergeFling(Long flingId, FlingDto flingDto) { public void delete(UUID id) {
var flingEntity = flingRepository.getOne(flingId); archiveService.deleteFling(id);
flingRepository.deleteById(id);
mergeNonEmpty(flingDto::getAllowUpload, flingEntity::setAllowUpload); log.debug("Deleted fling {}", id);
mergeNonEmpty(flingDto::getDirectDownload, flingEntity::setDirectDownload);
mergeWithEmpty(flingDto::getExpirationClicks, flingEntity::setExpirationClicks);
mergeWithEmpty(flingDto::getExpirationTime, flingEntity::setExpirationTime);
mergeNonEmpty(flingDto::getName, flingEntity::setName);
mergeNonEmpty(flingDto::getShared, flingEntity::setShared);
mergeNonEmpty(flingDto::getShareUrl, flingEntity::setShareUrl);
mergeWithEmpty(() -> hashKey(flingDto.getAuthCode()), flingEntity::setAuthCode);
} }
public Optional<FlingDto> findFlingById(Long flingId) { public boolean validateAuthCode(UUID id, String authCode) {
return flingMapper.map(flingRepository.findById(flingId)); FlingEntity flingEntity = flingRepository.getOne(id);
boolean valid = flingEntity.getAuthCode().equals(hashAuthCode(authCode));
log.debug("Provided authentication for {} is {} valid", id, valid ? "" : "not");
return valid;
} }
public Optional<FlingDto> findFlingByShareId(String shareUrl) { private String hashAuthCode(String authCode) {
return flingMapper.map(flingRepository.findByShareUrl(shareUrl)); String hash = new String(Hex.encode(keyHashDigest.digest(authCode.getBytes())));
log.debug("Hashed authentication code to {}", hash);
return hash;
} }
public void deleteFlingById(Long flingId) { /**
flingRepository.deleteById(flingId); * Generates a URL safe share id
} *
* @return A random URL safe share id
public boolean hasAuthCode(Long flingId, String authCode) { */
var fling = flingRepository.getOne(flingId); private String generateShareId() {
byte[] key = KeyGenerators
if (!StringUtils.hasText(fling.getAuthCode()))
return true;
return fling.getAuthCode().equals(hashKey(authCode));
}
public String getShareName(String shareUrl) {
FlingEntity flingEntity = flingRepository.findByShareUrl(shareUrl).orElseThrow();
if (flingEntity.getArtifacts().size() > 1)
return flingEntity.getName();
else if (flingEntity.getArtifacts().size() == 1)
return flingEntity.getArtifacts().stream().findFirst().get().getName();
return null;
}
public Long countArtifacts(Long flingId) {
return flingRepository.countArtifactsById(flingId);
}
public Long getFlingSize(Long flingId) {
var fling = flingRepository.getOne(flingId);
return fling.getArtifacts().stream()
.map(ae -> ae.getSize())
.reduce(0L, (acc, as) -> acc + as);
}
public String packageFling(Long flingId) throws IOException, ArchiveException {
var fling = flingRepository.getOne(flingId);
var tempFile = Files.createTempFile(Long.toString(flingId), ".zip");
try (var zipStream = new ZipOutputStream(new FileOutputStream(tempFile.toFile()))) {
zipStream.setLevel(Deflater.BEST_SPEED);
for (ArtifactEntity artifactEntity : fling.getArtifacts()) {
ZipEntry ze = new ZipEntry(artifactEntity.getName());
zipStream.putNextEntry(ze);
var artifactStream = archive.get(artifactEntity.getDoi());
try (var archiveEntryStream = new BufferedInputStream(artifactStream)) {
int b;
while ((b = archiveEntryStream.read()) != -1) {
zipStream.write(b);
}
} finally {
zipStream.closeEntry();
}
}
}
return tempFile.getFileName().toString();
}
public Pair<InputStream, Long> downloadFling(String fileId) throws IOException, ArchiveException {
var tempFile = Paths.get(System.getProperty("java.io.tmpdir"), fileId).toFile();
var archiveLength = tempFile.length();
var archiveStream = new FileInputStream(tempFile);
return Pair.of(archiveStream, archiveLength);
}
public String generateShareUrl() {
var key = KeyGenerators
.secureRandom(16) .secureRandom(16)
.generateKey(); .generateKey();
return Base64.getUrlEncoder().encodeToString(key) String shareId = Base64.getUrlEncoder().encodeToString(key)
// replace all special chars [=-_] in RFC 4648 // replace all special chars [=-_] in RFC 4648
// "URL and Filename safe" table with characters from // "URL and Filename safe" table with characters from
// [A-Za-z0-9]. Hence, the generated share url will only consist // [A-Za-z0-9]. Hence, the generated share url will only consist
@ -175,23 +124,8 @@ public class FlingService {
.replace('=', 'q') .replace('=', 'q')
.replace('_', 'u') .replace('_', 'u')
.replace('-', 'd'); .replace('-', 'd');
}
public String hashKey(String key) { log.debug("Generated share id {}", shareId);
if (!StringUtils.hasText(key)) return shareId;
return null;
return new String(Hex.encode(keyHashDigest.digest(key.getBytes())));
}
private <T> void mergeNonEmpty(Supplier<T> sup, Consumer<T> con) {
T r = sup.get();
if (r != null)
con.accept(r);
}
private <T> void mergeWithEmpty(Supplier<T> sup, Consumer<T> con) {
T r = sup.get();
con.accept(r);
} }
} }

View file

@ -0,0 +1,42 @@
package net.friedl.fling.service;
public class ServiceException extends Exception {
private static final long serialVersionUID = 2159182914434903969L;
/**
* {@inheritDoc}
*/
public ServiceException() {
super();
}
/**
* {@inheritDoc}
*/
public ServiceException(String message) {
super(message);
}
/**
* {@inheritDoc}
*/
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
/**
* {@inheritDoc}
*/
public ServiceException(Throwable cause) {
super(cause);
}
/**
* {@inheritDoc}
*/
protected ServiceException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View file

@ -1,6 +1,6 @@
package net.friedl.fling.persistence.archive; package net.friedl.fling.service.archive;
public class ArchiveException extends Exception { public class ArchiveException extends RuntimeException {
private static final long serialVersionUID = 6216735865308056261L; private static final long serialVersionUID = 6216735865308056261L;
/** /**

View file

@ -0,0 +1,50 @@
package net.friedl.fling.service.archive;
import java.io.InputStream;
import java.util.UUID;
import java.util.zip.ZipInputStream;
/**
* Interface for persisting artifacts
*
* @author Armin Friedl <dev@friedl.net>
*/
public interface ArchiveService {
/**
* Retrieve an artifact from the archive
*
* @param id The artifact id
* @return An {@link InputStream} for reading the artifact
*/
InputStream getArtifact(UUID artifactId);
/**
* Retrieve a packaged fling from the archive
*
* @param flingId The fling id
* @return An {@link ZipInputStream} representing the fling and its artifacts
*/
ZipInputStream getFling(UUID flingId);
/**
* Store an artifact
*
* @param artifactStream The artifact to store represented as {@link InputStream}
* @param artifactId The id of the artifact. Must be an existing artifact in the DB. Not null.
*/
void storeArtifact(UUID artifactId, InputStream artifactStream);
/**
* Delete an artifact
*
* @param id The unique artifact id
*/
void deleteArtifact(UUID artifactId);
/**
* Delete a fling
*
* @param flingId The unique fling id
*/
void deleteFling(UUID flingId);
}

View file

@ -0,0 +1,217 @@
package net.friedl.fling.service.archive.impl;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.zip.ZipInputStream;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.transaction.Transactional;
import javax.validation.constraints.NotBlank;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.friedl.fling.persistence.entities.ArtifactEntity;
import net.friedl.fling.persistence.repositories.ArtifactRepository;
import net.friedl.fling.service.archive.ArchiveService;
@Slf4j
@Service
@ConfigurationProperties("fling.archive.filesystem")
@Transactional
public class FileSystemArchive implements ArchiveService {
@NotBlank
private Path archivePath;
private ArtifactRepository artifactRepository;
private Map<URI, FileSystem> filesystems;
public FileSystemArchive(ArtifactRepository artifactRepository) {
this.artifactRepository = artifactRepository;
this.filesystems = new HashMap<>();
}
@PostConstruct
public void postConstruct() {
try {
Files.createDirectories(archivePath);
} catch (IOException e) {
log.error("Could not create directory at archive path {}", archivePath);
throw new UncheckedIOException(e);
}
}
@PreDestroy
public void preDestroy() {
filesystems.forEach((uri, zfs) -> {
try {
zfs.close();
} catch (IOException e) {
log.error("Could not close file system for {}", uri);
}
});
}
@Override
@SneakyThrows
public InputStream getArtifact(UUID artifactId) {
log.debug("Reading data for artifact {}", artifactId);
FileSystem zipDisk = getZipDisk(artifactId);
return zipDisk.provider().newInputStream(getZipDiskPath(artifactId, zipDisk),
StandardOpenOption.READ);
// do not close zip disk here or the input stream will be closed as well
}
@Override
@SneakyThrows
public ZipInputStream getFling(UUID flingId) {
log.debug("Reading data for fling {}", flingId);
Path zipDiskPath = archivePath.resolve(flingId.toString() + ".zip");
log.debug("Zip disk path is {}", zipDiskPath);
return new ZipInputStream(new FileInputStream(zipDiskPath.toFile()));
}
@Override
@SneakyThrows
public void storeArtifact(UUID artifactId, InputStream artifactStream) {
log.debug("Storing artifact {}", artifactId);
setArchived(artifactId, false);
FileSystem zipDisk = getZipDisk(artifactId);
Files.copy(artifactStream, getZipDiskPath(artifactId, zipDisk),
StandardCopyOption.REPLACE_EXISTING);
// we need to close the zipDisk in order to flush it to disk
closeZipDisk(artifactId);
setArchived(artifactId, true);
}
@Override
@SneakyThrows
public void deleteArtifact(UUID artifactId) {
log.debug("Deleting artifact {}", artifactId);
FileSystem zipDisk = getZipDisk(artifactId);
Files.delete(getZipDiskPath(artifactId, zipDisk));
// we need to close the zipDisk in order to flush it to disk
closeZipDisk(artifactId);
setArchived(artifactId, false);
}
@Override
@SneakyThrows
public void deleteFling(UUID flingId) {
URI zipDiskUri = resolveFlingUri(flingId);
log.debug("Closing zip disk at {}", zipDiskUri);
// make sure nobody opens the filesystem while it is being closed and deleted
synchronized (filesystems) {
FileSystem zipDisk = filesystems.remove(zipDiskUri);
if (zipDisk != null) {
zipDisk.close();
log.debug("Zip disk closed");
} else {
log.debug("No open zip disk found");
}
Path zipDiskPath = archivePath.resolve(flingId.toString() + ".zip");
log.debug("Deleting fling [.id={}] at {}", flingId, zipDiskPath);
Files.delete(zipDiskPath);
}
}
private void setArchived(UUID artifactId, boolean archived) {
ArtifactEntity artifactEntity = artifactRepository.getOne(artifactId);
artifactEntity.setArchived(archived);
log.debug("Artifact[.id={}] set to {} archived", artifactId, archived ? "" : "not");
}
private Path getZipDiskPath(UUID artifactId, FileSystem zipDisk) {
ArtifactEntity artifactEntity = artifactRepository.getOne(artifactId);
log.debug("Getting zip disk path for {}", artifactEntity.getPath());
Path zipDiskPath = zipDisk.getPath(artifactEntity.getPath().toString());
if (zipDiskPath.getParent() != null && !Files.exists(zipDiskPath.getParent())) {
try {
Files.createDirectories(zipDiskPath.getParent());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
log.debug("Got zip disk path {}", zipDiskPath);
return zipDiskPath;
}
private FileSystem getZipDisk(UUID artifactId) throws IOException {
log.debug("Retrieving zip disk for artifact {}", artifactId);
URI uri = resolveArtifactUri(artifactId);
log.debug("Looking for zip disk at uri {}", uri);
// make sure nobody opens closes, deletes or interleavingly opens the filesystem while it is
// being opened
synchronized (filesystems) {
if (!filesystems.containsKey(uri)) {
log.debug("Zip disk does not exist. Creating zip disk for {}", uri);
FileSystem zipDisk = FileSystems.newFileSystem(uri, Map.of("create", "true"));
filesystems.put(uri, zipDisk);
}
return filesystems.get(uri);
}
}
private void closeZipDisk(UUID artifactId) throws IOException {
log.debug("Closing zip disk for artifact {}", artifactId);
URI uri = resolveArtifactUri(artifactId);
log.debug("Closing zip disk at uri {}", uri);
// make sure nobody opens the filesystem while it is being closed
synchronized (filesystems) {
FileSystem zipDisk = filesystems.remove(uri);
if (zipDisk == null) {
log.warn("Could not close zip disk at {}. Filesystem not found.", uri);
return;
}
zipDisk.close();
}
}
private URI resolveArtifactUri(UUID artifactId) throws IOException {
ArtifactEntity artifactEntity = artifactRepository.getOne(artifactId);
UUID flingId = artifactEntity.getFling().getId();
return resolveFlingUri(flingId);
}
private URI resolveFlingUri(UUID flingId) throws IOException {
Path zipDiskPath = archivePath.resolve(flingId.toString() + ".zip");
return URI.create("jar:file:" + zipDiskPath.toFile().getCanonicalPath());
}
public void setArchivePath(String archivePath) {
this.archivePath = Paths.get(archivePath);
}
}

View file

@ -0,0 +1,52 @@
{
"groups": [
{
"name": "fling.archive.filesystem",
"type": "net.friedl.fling.service.archive.impl.FileSystemArchive",
"sourceType": "net.friedl.fling.service.archive.impl.FileSystemArchive"
},
{
"name": "fling.security",
"type": "net.friedl.fling.security.FlingWebSecurityConfiguration",
"sourceType": "net.friedl.fling.security.FlingWebSecurityConfiguration"
}
],
"properties": [
{
"name": "fling.archive.filesystem.archive-path",
"type": "java.lang.String",
"description": "Directory where FileSystemArchive stores its data",
"sourceType": "net.friedl.fling.service.archive.impl.FileSystemArchive"
},
{
"name": "fling.security.allowed-origins",
"type": "java.util.List",
"description": "Allowed origins for CORS",
"sourceType": "net.friedl.fling.security.FlingWebSecurityConfiguration"
},
{
"name": "fling.security.admin-user",
"type": "java.lang.String",
"description": "Username of the admin user/instance owner",
"sourceType": "net.friedl.fling.security.FlingWebSecurityConfiguration"
},
{
"name": "fling.security.admin-password",
"type": "java.util.String",
"description": "Password of the admin user/instance owner",
"sourceType": "net.friedl.fling.security.FlingWebSecurityConfiguration"
},
{
"name": "fling.security.signing-key",
"type": "java.util.String",
"description": "Key for signing JWT tokens. Must be 256 bits (32 bytes)",
"sourceType": "net.friedl.fling.security.FlingWebSecurityConfiguration"
},
{
"name": "fling.security.jwt-expiration",
"type": "java.util.Long",
"description": "Time until JWT tokens expire",
"sourceType": "net.friedl.fling.security.FlingWebSecurityConfiguration"
}
]
}

View file

@ -21,7 +21,7 @@ logging.level:
# spring.http.log-request-details: true # spring.http.log-request-details: true
fling: fling:
archive.fileystem.directory: "/home/armin/Desktop/fling" archive.filesystem.archive-path: /home/armin/Desktop/fling
security: security:
allowed-origins: allowed-origins:
- "https://friedl.net" - "https://friedl.net"

View file

@ -16,7 +16,7 @@ logging.level:
root: WARN root: WARN
fling: fling:
archive.fileystem.directory: "/var/fling/files" archive.filesystem.archive-path: "/var/fling/files"
security: security:
allowed-origins: allowed-origins:
- "https://fling.friedl.net" - "https://fling.friedl.net"

View file

@ -1,21 +1,9 @@
package net.friedl.fling.controller; package net.friedl.fling.controller;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
import org.springframework.test.web.servlet.MockMvc;
import net.friedl.fling.model.dto.ArtifactDto;
import net.friedl.fling.service.ArtifactService;
@WebMvcTest(controllers = ArtifactController.class, @WebMvcTest(controllers = ArtifactController.class,
// do auto-configure security // do auto-configure security
@ -23,34 +11,34 @@ import net.friedl.fling.service.ArtifactService;
// do not try to create beans in security // do not try to create beans in security
excludeFilters = @Filter(type = FilterType.REGEX, pattern = "net.friedl.fling.security.*")) excludeFilters = @Filter(type = FilterType.REGEX, pattern = "net.friedl.fling.security.*"))
class ArtifactControllerTest { class ArtifactControllerTest {
@Autowired // @Autowired
private MockMvc mvc; // private MockMvc mvc;
//
@MockBean // @MockBean
private ArtifactService artifactService; // private ArtifactService artifactService;
//
@Test // @Test
public void testGetArtifacts_noArtifacts_empty() throws Exception { // public void testGetArtifacts_noArtifacts_empty() throws Exception {
Long flingId = 123L; //// Long flingId = 123L;
////
when(artifactService.findAllArtifacts(flingId)).thenReturn(List.of()); //// when(artifactService.findAllArtifacts(flingId)).thenReturn(List.of());
////
mvc.perform(get("/api/artifacts").param("flingId", flingId.toString())) //// mvc.perform(get("/api/artifacts").param("flingId", flingId.toString()))
.andExpect(jsonPath("$", hasSize(0))); //// .andExpect(jsonPath("$", hasSize(0)));
} // }
//
@Test // @Test
public void testGetArtifacts_hasArtifacts_allArtifacts() throws Exception { // public void testGetArtifacts_hasArtifacts_allArtifacts() throws Exception {
Long flingId = 123L; //// Long flingId = 123L;
String artifactName = "TEST"; //// String artifactName = "TEST";
////
ArtifactDto artifactDto = new ArtifactDto(); //// ArtifactDto artifactDto = new ArtifactDto();
artifactDto.setName(artifactName); //// artifactDto.setName(artifactName);
////
when(artifactService.findAllArtifacts(flingId)).thenReturn(List.of(artifactDto)); //// when(artifactService.findAllArtifacts(flingId)).thenReturn(List.of(artifactDto));
////
mvc.perform(get("/api/artifacts").param("flingId", flingId.toString())) //// mvc.perform(get("/api/artifacts").param("flingId", flingId.toString()))
.andExpect(jsonPath("$", hasSize(1))) //// .andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].name", equalTo(artifactName))); //// .andExpect(jsonPath("$[0].name", equalTo(artifactName)));
} // }
} }

View file

@ -0,0 +1,67 @@
package net.friedl.fling.model;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.file.Paths;
import java.util.Set;
import java.util.UUID;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import net.friedl.fling.model.dto.ArtifactDto;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ModelTestConfiguration.class)
public class ArtifactDtoTest {
@Autowired
private Validator validator;
@Test
void testSetId_null_validationFails() {
ArtifactDto artifactDto = ArtifactDto.builder()
.id(null)
.path(Paths.get("test"))
.build();
Set<ConstraintViolation<ArtifactDto>> constraintViolations = validator.validate(artifactDto);
assertThat(constraintViolations).hasSize(1);
ConstraintViolation<ArtifactDto> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("id");
assertThat(violation.getMessage()).isEqualTo("must not be null");
}
@Test
void testSetPath_null_validationFails() {
ArtifactDto artifactDto = ArtifactDto.builder()
.id(new UUID(0L, 0L))
.path(null)
.build();
Set<ConstraintViolation<ArtifactDto>> constraintViolations = validator.validate(artifactDto);
assertThat(constraintViolations).hasSize(1);
ConstraintViolation<ArtifactDto> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("path");
assertThat(violation.getMessage()).isEqualTo("must not be null");
}
@Test
void testMandatoryFieldsSet_validationOk() {
ArtifactDto artifactDto = ArtifactDto.builder()
.id(new UUID(0L, 0L))
.path(Paths.get("test"))
.build();
Set<ConstraintViolation<ArtifactDto>> constraintViolations = validator.validate(artifactDto);
assertTrue(constraintViolations.isEmpty());
}
}

View file

@ -0,0 +1,109 @@
package net.friedl.fling.model;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Instant;
import java.util.Set;
import java.util.UUID;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import net.friedl.fling.model.dto.FlingDto;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ModelTestConfiguration.class)
public class FlingDtoTest {
@Autowired
private Validator validator;
@Test
void testSetId_null_validationFails() {
FlingDto flingDto = FlingDto.builder()
.id(null)
.name("test")
.creationTime(Instant.EPOCH)
.shareId("test")
.build();
Set<ConstraintViolation<FlingDto>> constraintViolations = validator.validate(flingDto);
assertThat(constraintViolations).hasSize(1);
ConstraintViolation<FlingDto> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("id");
assertThat(violation.getMessage()).isEqualTo("must not be null");
}
@Test
void testSetName_null_validationFails() {
FlingDto flingDto = FlingDto.builder()
.id(new UUID(0L, 0L))
.name(null)
.creationTime(Instant.EPOCH)
.shareId("test")
.build();
Set<ConstraintViolation<FlingDto>> constraintViolations = validator.validate(flingDto);
assertThat(constraintViolations).hasSize(1);
ConstraintViolation<FlingDto> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("name");
assertThat(violation.getMessage()).isEqualTo("must not be null");
}
@Test
void testSetCreationTime_null_validationFails() {
FlingDto flingDto = FlingDto.builder()
.id(new UUID(0L, 0L))
.name("test")
.creationTime(null)
.shareId("test")
.build();
Set<ConstraintViolation<FlingDto>> constraintViolations = validator.validate(flingDto);
assertThat(constraintViolations).hasSize(1);
ConstraintViolation<FlingDto> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("creationTime");
assertThat(violation.getMessage()).isEqualTo("must not be null");
}
@Test
void testSetShareId_null_validationFails() {
FlingDto flingDto = FlingDto.builder()
.id(new UUID(0L, 0L))
.name("test")
.creationTime(Instant.EPOCH)
.shareId(null)
.build();
Set<ConstraintViolation<FlingDto>> constraintViolations = validator.validate(flingDto);
assertThat(constraintViolations).hasSize(1);
ConstraintViolation<FlingDto> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("shareId");
assertThat(violation.getMessage()).isEqualTo("must not be null");
}
@Test
void testSetAllManadatory_validationOk() {
FlingDto flingDto = FlingDto.builder()
.id(new UUID(0L, 0L))
.name("test")
.creationTime(Instant.EPOCH)
.shareId("test")
.build();
Set<ConstraintViolation<FlingDto>> constraintViolations = validator.validate(flingDto);
assertTrue(constraintViolations.isEmpty());
}
}

View file

@ -0,0 +1,17 @@
package net.friedl.fling.model;
import javax.validation.Validator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@Configuration
public class ModelTestConfiguration {
@Bean
public Validator validator() {
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.afterPropertiesSet();
return localValidatorFactoryBean;
}
}