Commit 50da71d3 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

serializer for UploadedFileEntry implemented

parent 9e7e3e62
......@@ -4,7 +4,10 @@ import java.io.Serializable;
import javax.persistence.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.serializer.model.cache.UploadedFileEntrySerializer;
/**
* Database object representing file uploaded into system.
......@@ -14,6 +17,7 @@ import lcsb.mapviewer.model.user.User;
*/
@Entity
@DiscriminatorValue("UPLOADED_FILE_ENTRY")
@JsonSerialize(using = UploadedFileEntrySerializer.class)
public class UploadedFileEntry extends FileEntry implements Serializable {
/**
......
package lcsb.mapviewer.modelutils.serializer.model.cache;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
public class UploadedFileEntrySerializer extends JsonSerializer<UploadedFileEntry> {
@Override
public void serialize(final UploadedFileEntry fileEntry, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeNumberField("id", fileEntry.getId());
gen.writeStringField("filename", fileEntry.getOriginalFileName());
gen.writeNumberField("length", fileEntry.getLength());
gen.writeStringField("owner", fileEntry.getOwner() == null ? null : fileEntry.getOwner().getLogin());
gen.writeNumberField("uploadedDataLength", fileEntry.getFileContent().length);
gen.writeEndObject();
}
}
\ No newline at end of file
package lcsb.mapviewer.api.files;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PostAuthorize;
......@@ -14,7 +12,6 @@ import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IFileService;
......@@ -37,7 +34,7 @@ public class FileController extends BaseController {
@PreAuthorize("isAuthenticated() and authentication.name != '" + Configuration.ANONYMOUS_LOGIN + "'")
@PostMapping(value = "/")
public Map<String, Object> createFile(
public UploadedFileEntry createFile(
Authentication authentication,
@RequestParam(value = "filename") String filename,
@RequestParam(value = "length") String length) {
......@@ -55,15 +52,15 @@ public class FileController extends BaseController {
}
}
@PostAuthorize("hasAuthority('IS_ADMIN') or returnObject['owner'] == authentication.name")
@PostAuthorize("hasAuthority('IS_ADMIN') or returnObject.owner == authentication.name")
@GetMapping(value = "/{id}")
public Map<String, Object> getFile(@PathVariable(value = "id") Integer id) throws ObjectNotFoundException {
public UploadedFileEntry getFile(@PathVariable(value = "id") Integer id) throws ObjectNotFoundException {
return fileRest.getFile(id);
}
@PreAuthorize("@fileService.getOwnerByFileId(#id)?.login == authentication.name")
@PostMapping(value = "/{id}:uploadContent")
public Map<String, Object> uploadContent(@PathVariable(value = "id") Integer id, @RequestBody byte[] data)
public UploadedFileEntry uploadContent(@PathVariable(value = "id") Integer id, @RequestBody byte[] data)
throws QueryException {
return fileRest.uploadContent(id, data);
}
......
package lcsb.mapviewer.api.files;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.*;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
......@@ -30,27 +24,17 @@ public class FileRestImpl extends BaseRestImpl {
this.fileService = fileService;
}
public Map<String, Object> getFile(Integer id) throws ObjectNotFoundException {
public UploadedFileEntry getFile(Integer id) throws ObjectNotFoundException {
UploadedFileEntry fileEntry = fileService.getById(id);
if (fileEntry == null) {
throw new ObjectNotFoundException("Object not found");
}
return serializeEntry(fileEntry);
}
private Map<String, Object> serializeEntry(UploadedFileEntry fileEntry) {
Map<String, Object> result = new TreeMap<>();
result.put("id", fileEntry.getId());
result.put("filename", fileEntry.getOriginalFileName());
result.put("length", fileEntry.getLength());
result.put("owner", fileEntry.getOwner() == null ? null : fileEntry.getOwner().getLogin());
result.put("uploadedDataLength", fileEntry.getFileContent().length);
return result;
return fileEntry;
}
public Map<String, Object> uploadContent(Integer id, byte[] data) throws QueryException {
public UploadedFileEntry uploadContent(Integer id, byte[] data) throws QueryException {
int fileId = Integer.valueOf(id);
UploadedFileEntry fileEntry = uploadedFileEntryDao.getById(fileId);
UploadedFileEntry fileEntry = fileService.getById(fileId);
if (fileEntry == null) {
throw new ObjectNotFoundException("Object not found");
}
......@@ -62,7 +46,7 @@ public class FileRestImpl extends BaseRestImpl {
byte[] newConent = ArrayUtils.addAll(fileEntry.getFileContent(), data);
fileEntry.setFileContent(newConent);
uploadedFileEntryDao.update(fileEntry);
return serializeEntry(fileEntry);
return fileEntry;
}
public UploadedFileEntryDao getUploadedFileEntryDao() {
......
package lcsb.mapviewer.services.impl;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -22,18 +23,18 @@ public class FileService implements IFileService {
@Override
public UploadedFileEntry getById(Integer id) {
return uploadedFileEntryDao.getById(id);
UploadedFileEntry entry = uploadedFileEntryDao.getById(id);
if (entry != null) {
Hibernate.initialize(entry.getOwner());
}
return entry;
}
@Override
public User getOwnerByFileId(Integer id) {
if (id != null) {
UploadedFileEntry entry = uploadedFileEntryDao.getById(id);
if (entry != null && entry.getOwner() != null) {
// it's lazy initialized
entry.getOwner().getLogin();
return uploadedFileEntryDao.getById(id).getOwner();
}
UploadedFileEntry entry = getById(id);
if (entry != null) {
return uploadedFileEntryDao.getById(id).getOwner();
}
return null;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment