Commit 578cbb14 authored by Matija Obreza's avatar Matija Obreza
Browse files

Source code formatted

parent d6b68a34
......@@ -27,7 +27,7 @@ public abstract class FileRepositoryException extends Exception {
public FileRepositoryException() {
}
public FileRepositoryException(String message) {
public FileRepositoryException(final String message) {
super(message);
}
......
......@@ -23,7 +23,7 @@ public class InvalidRepositoryPathException extends FileRepositoryException {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
public InvalidRepositoryPathException(String message) {
public InvalidRepositoryPathException(final String message) {
super(message);
}
}
......@@ -71,7 +71,7 @@ public abstract class BaseEntity {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + (id == null ? 0 : id.hashCode());
return result;
}
......@@ -82,18 +82,23 @@ public abstract class BaseEntity {
*/
@Override
public boolean equals(final Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
}
if (obj == null) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
}
final BaseEntity other = (BaseEntity) obj;
if (id == null) {
if (other.id != null)
if (other.id != null) {
return false;
} else if (!id.equals(other.id))
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
}
......@@ -180,9 +180,10 @@ public class RepositoryFile extends BaseEntity implements BaseMetadata {
// not persisted
@Transient
public String getFilename() {
if (uuid == null)
if (uuid == null) {
return null;
}
final StringBuffer sb = new StringBuffer();
sb.append(uuid.toString());
if (extension != null) {
......@@ -590,7 +591,7 @@ public class RepositoryFile extends BaseEntity implements BaseMetadata {
*
* @param sha1Sum the new sha1 sum
*/
public void setSha1Sum(String sha1Sum) {
public void setSha1Sum(final String sha1Sum) {
this.sha1Sum = sha1Sum;
}
......@@ -608,7 +609,7 @@ public class RepositoryFile extends BaseEntity implements BaseMetadata {
*
* @param md5Sum the new md5 sum
*/
public void setMd5Sum(String md5Sum) {
public void setMd5Sum(final String md5Sum) {
this.md5Sum = md5Sum;
}
}
/*
* Copyright 2016 Global Crop Diversity Trust
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
......@@ -115,63 +115,63 @@ public class RepositoryFileData implements BaseMetadata, Serializable {
return title;
}
public void setAccessRights(String accessRights) {
public void setAccessRights(final String accessRights) {
this.accessRights = accessRights;
}
public void setBibliographicCitation(String bibliographicCitation) {
public void setBibliographicCitation(final String bibliographicCitation) {
this.bibliographicCitation = bibliographicCitation;
}
public void setCreated(String created) {
public void setCreated(final String created) {
this.created = created;
}
public void setCreator(String creator) {
public void setCreator(final String creator) {
this.creator = creator;
}
public void setDateSubmitted(Date dateSubmitted) {
public void setDateSubmitted(final Date dateSubmitted) {
this.dateSubmitted = dateSubmitted;
}
public void setDescription(String description) {
public void setDescription(final String description) {
this.description = description;
}
public void setExtent(String extent) {
public void setExtent(final String extent) {
this.extent = extent;
}
public void setFormat(String format) {
public void setFormat(final String format) {
this.format = format;
}
public void setIdentifier(String identifier) {
public void setIdentifier(final String identifier) {
this.identifier = identifier;
}
public void setLicense(String license) {
public void setLicense(final String license) {
this.license = license;
}
public void setModified(Date modified) {
public void setModified(final Date modified) {
this.modified = modified;
}
public void setRightsHolder(String rightsHolder) {
public void setRightsHolder(final String rightsHolder) {
this.rightsHolder = rightsHolder;
}
public void setSubject(String subject) {
public void setSubject(final String subject) {
this.subject = subject;
}
public void setTitle(String title) {
public void setTitle(final String title) {
this.title = title;
}
public void applyTo(RepositoryFile repositoryFile) {
public void applyTo(final RepositoryFile repositoryFile) {
repositoryFile.setAccessRights(accessRights);
repositoryFile.setBibliographicCitation(bibliographicCitation);
repositoryFile.setCreated(created);
......@@ -186,7 +186,7 @@ public class RepositoryFileData implements BaseMetadata, Serializable {
repositoryFile.setContentType(contentType);
}
public void setOriginalFilename(String originalFilename) {
public void setOriginalFilename(final String originalFilename) {
this.originalFilename = originalFilename;
}
......@@ -194,11 +194,12 @@ public class RepositoryFileData implements BaseMetadata, Serializable {
return originalFilename;
}
public static RepositoryFileData from(RepositoryFile repositoryFile) {
if (repositoryFile == null)
public static RepositoryFileData from(final RepositoryFile repositoryFile) {
if (repositoryFile == null) {
return null;
}
RepositoryFileData rd = new RepositoryFileData();
final RepositoryFileData rd = new RepositoryFileData();
rd.title = repositoryFile.getTitle();
rd.subject = repositoryFile.getSubject();
rd.description = repositoryFile.getDescription();
......@@ -218,7 +219,7 @@ public class RepositoryFileData implements BaseMetadata, Serializable {
return rd;
}
public void setContentType(String contentType) {
public void setContentType(final String contentType) {
this.contentType = contentType;
}
......
/*
* Copyright 2016 Global Crop Diversity Trust
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
......@@ -45,19 +45,19 @@ public class RepositoryImageData extends RepositoryFileData implements ImageMeta
return width;
}
public void setHeight(int height) {
public void setHeight(final int height) {
this.height = height;
}
public void setOrientation(Orientation orientation) {
public void setOrientation(final Orientation orientation) {
this.orientation = orientation;
}
public void setWidth(int width) {
public void setWidth(final int width) {
this.width = width;
}
public void applyTo(RepositoryImage repositoryImage) {
public void applyTo(final RepositoryImage repositoryImage) {
super.applyTo(repositoryImage);
repositoryImage.setWidth(width);
repositoryImage.setHeight(height);
......
......@@ -60,7 +60,7 @@ public interface RepositoryFilePersistence extends JpaRepository<RepositoryFile,
/**
* Find files with missing hashes
*
*
* @return the list
*/
@Query("select rf from RepositoryFile rf where rf.sha1Sum is null or rf.md5Sum is null")
......
......@@ -179,7 +179,7 @@ public interface RepositoryService {
/**
* Remove repository image
*
*
* @param repositoryImage
* @return the original metadata
* @throws NoSuchRepositoryFileException when file is not available in the repository
......
......@@ -131,8 +131,9 @@ public class ImageGalleryAspects {
* @param repositoryImage the repository image
*/
private void addImageToGallery(final RepositoryImage repositoryImage) {
if (repositoryImage == null)
if (repositoryImage == null) {
return;
}
final ImageGallery imageGallery = imageGalleryService.loadImageGallery(repositoryImage.getPath());
......@@ -164,8 +165,9 @@ public class ImageGalleryAspects {
* @param repositoryImage the repository image
*/
private void removeImageFromGallery(final RepositoryImage repositoryImage) {
if (repositoryImage == null)
if (repositoryImage == null) {
return;
}
final ImageGallery imageGallery = imageGalleryService.loadImageGallery(repositoryImage.getPath());
......@@ -177,8 +179,9 @@ public class ImageGalleryAspects {
}
final List<RepositoryImage> imagesList = imageGallery.getImages();
if (imagesList == null)
if (imagesList == null) {
return;
}
if (LOG.isTraceEnabled()) {
LOG.trace("Images size=" + imagesList.size());
......
......@@ -23,7 +23,7 @@ public class FilenameValidator {
/**
* Checks if the filename is acceptable.
*
*
* @param filename
* @return true when OK, false otherwise.
*/
......
......@@ -74,14 +74,16 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
*/
// Do not do any null checks, let it fail.
protected void sanityCheck() throws BytesStorageException {
if (!repoDir.isDirectory())
if (!repoDir.isDirectory()) {
throw new BytesStorageException("Base path is not a directory");
}
// Base directory must contain the file FILEREPOSITORY (so we don't
// create things all over the place)
final File repositoryMarker = new File(repoDir, "FILEREPOSITORY");
if (!repositoryMarker.exists() || !repositoryMarker.isFile())
if (!repositoryMarker.exists() || !repositoryMarker.isFile()) {
throw new BytesStorageException("Missing FILEREPOSITORY marker in " + repoDir.getAbsolutePath());
}
}
/*
......@@ -93,16 +95,18 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
public void upsert(final String path, final String filename, final byte[] data) throws FileNotFoundException, IOException {
final File destinationDir = new File(repoDir, path);
if (!destinationDir.getCanonicalPath().startsWith(repoDir.getCanonicalPath()))
if (!destinationDir.getCanonicalPath().startsWith(repoDir.getCanonicalPath())) {
throw new IOException("Not within repository path: " + destinationDir.getAbsolutePath());
else if (!destinationDir.exists()) {
} else if (!destinationDir.exists()) {
destinationDir.mkdirs();
} else if (!destinationDir.isDirectory())
} else if (!destinationDir.isDirectory()) {
throw new IOException("Exists, not a directory: " + destinationDir.getAbsolutePath());
}
final File destinationFile = new File(destinationDir, filename);
if (!destinationFile.getCanonicalPath().startsWith(repoDir.getCanonicalPath()))
if (!destinationFile.getCanonicalPath().startsWith(repoDir.getCanonicalPath())) {
throw new IOException("Not within repository path: " + destinationFile.getAbsolutePath());
}
try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(destinationFile, false))) {
IOUtils.write(data, output);
......@@ -119,8 +123,9 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
final File destinationDir = new File(repoDir, path);
final File destinationFile = new File(destinationDir, filename);
if (!destinationFile.getCanonicalPath().startsWith(repoDir.getCanonicalPath()))
if (!destinationFile.getCanonicalPath().startsWith(repoDir.getCanonicalPath())) {
throw new IOException("Not within repository path: " + destinationFile.getAbsolutePath());
}
if (destinationFile.exists()) {
destinationFile.delete();
......@@ -129,7 +134,7 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
// Delete empty dir
if (destinationDir.exists() && destinationDir.isDirectory()) {
String[] dirContents = destinationDir.list();
final String[] dirContents = destinationDir.list();
if (dirContents != null && dirContents.length == 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("DELETING " + destinationDir.getAbsolutePath());
......@@ -149,8 +154,9 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
final File destinationDir = new File(repoDir, path);
final File destinationFile = new File(destinationDir, filename);
if (!destinationFile.getCanonicalPath().startsWith(repoDir.getCanonicalPath()))
if (!destinationFile.getCanonicalPath().startsWith(repoDir.getCanonicalPath())) {
throw new IOException("Not within repository path: " + destinationFile.getAbsolutePath());
}
byte[] data = null;
if (destinationFile.exists()) {
......@@ -197,8 +203,9 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
LOG.debug("Listing filenames in dir=" + destinationDir.getAbsolutePath());
}
final File[] files = destinationDir.listFiles();
if (files == null)
if (files == null) {
return Collections.emptyList();
}
return Arrays.stream(files).filter(file -> !file.isDirectory()).map(file -> file.getName()).collect(Collectors.toList());
}
}
......@@ -138,7 +138,7 @@ public class ImageGalleryServiceImpl implements ImageGalleryService {
LOG.error("Failed to remove bytes path=" + thumbPath + " filename=" + filename, e);
}
});
} catch (InvalidRepositoryPathException e) {
} catch (final InvalidRepositoryPathException e) {
// Shouldn't have a gallery with invalid path, but hell
}
......
......@@ -35,7 +35,7 @@ public class PathValidator {
/**
* Checks if path is valid and throws InvalidPathException when path is not valid
*
*
* @param path
* @throws InvalidRepositoryPathException when path is not valid.
*/
......
......@@ -79,16 +79,16 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
*/
@Transactional
public void addMissingHashSums() {
for (RepositoryFile repositoryFile : repositoryFilePersistence.findByMissingHashSums()) {
for (final RepositoryFile repositoryFile : repositoryFilePersistence.findByMissingHashSums()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Updating SHA-1 and MD5 for file uuid={}", repositoryFile.getUuid());
}
try {
byte[] bytes = getFileBytes(repositoryFile.getPath(), repositoryFile.getFilename());
final byte[] bytes = getFileBytes(repositoryFile.getPath(), repositoryFile.getFilename());
repositoryFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
repositoryFile.setMd5Sum(DigestUtils.md5Hex(bytes));
repositoryFilePersistence.save(repositoryFile);
} catch (NoSuchRepositoryFileException e) {
} catch (final NoSuchRepositoryFileException e) {
LOG.warn("Failed to generate hash sums: {}", e.getMessage());
}
}
......@@ -107,8 +107,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
PathValidator.checkValidPath(repositoryPath);
if (originalFilename == null || contentType == null || bytes == null)
if (originalFilename == null || contentType == null || bytes == null) {
throw new InvalidRepositoryFileDataException();
}
RepositoryFile repositoryFile = new RepositoryFile();
......@@ -151,8 +152,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
PathValidator.checkValidPath(repositoryPath);
if (originalFilename == null || contentType == null || bytes == null)
if (originalFilename == null || contentType == null || bytes == null) {
throw new InvalidRepositoryFileDataException();
}
RepositoryImage repositoryImage = new RepositoryImage();
......@@ -211,12 +213,14 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Override
public RepositoryFile getFile(final UUID fileUuid) throws NoSuchRepositoryFileException {
RepositoryFile file = repositoryFilePersistence.findByUuid(fileUuid);
if (file != null)
if (file != null) {
return file;
}
file = repositoryImagePersistence.findByUuid(fileUuid);
if (file == null)
if (file == null) {
throw new NoSuchRepositoryFileException();
}
return file;
}
......@@ -261,11 +265,12 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Transactional
public RepositoryFile updateMetadata(final UUID uuid, final RepositoryFileData fileData) throws NoSuchRepositoryFileException {
RepositoryFile repositoryFile = repositoryFilePersistence.findByUuid(uuid);
if (repositoryFile == null)
if (repositoryFile == null) {
throw new NoSuchRepositoryFileException();
}
fileData.applyTo(repositoryFile);
if (repositoryFile.getContentType().startsWith("image")) {
repositoryFile = repositoryImagePersistence.save((RepositoryImage) repositoryFile);
} else {
......@@ -281,10 +286,11 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* @see org.genesys2.server.filerepository.service.RepositoryService#updateMetadata(org.genesys2.server.filerepository. model.RepositoryImage)
*/
@Override
public RepositoryImage updateMetadata(UUID uuid, RepositoryImageData imageData) throws NoSuchRepositoryFileException {
public RepositoryImage updateMetadata(final UUID uuid, final RepositoryImageData imageData) throws NoSuchRepositoryFileException {
RepositoryImage repositoryImage = repositoryImagePersistence.findByUuid(uuid);
if (repositoryImage == null)
if (repositoryImage == null) {
throw new NoSuchRepositoryFileException();
}
imageData.applyTo(repositoryImage);
repositoryImage = repositoryImagePersistence.save(repositoryImage);
......@@ -300,8 +306,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Override
@Transactional
public RepositoryFile updateBytes(final RepositoryFile repositoryFile, final String contentType, final byte[] bytes) throws NoSuchRepositoryFileException, IOException {
if (repositoryFile == null)
if (repositoryFile == null) {
throw new NoSuchRepositoryFileException();
}
// Calculate SHA-1 and MD5 sums
repositoryFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
......@@ -321,8 +328,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Override
@Transactional
public RepositoryImage updateBytes(final RepositoryImage repositoryImage, final String contentType, final byte[] bytes) throws NoSuchRepositoryFileException, IOException {
if (repositoryImage == null)
if (repositoryImage == null) {
throw new NoSuchRepositoryFileException();
}
// Calculate SHA-1 and MD5 sums
repositoryImage.setSha1Sum(DigestUtils.sha1Hex(bytes));
......@@ -351,8 +359,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Override
@Transactional
public RepositoryFile removeFile(final RepositoryFile repositoryFile) throws NoSuchRepositoryFileException, IOException {
if (repositoryFile == null)
if (repositoryFile == null) {
throw new NoSuchRepositoryFileException();
}
bytesStorageService.remove(repositoryFile.getPath(), repositoryFile.getFilename());
......@@ -367,7 +376,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Override
@Transactional
public RepositoryImage removeImage(RepositoryImage repositoryImage) throws NoSuchRepositoryFileException, IOException {
public RepositoryImage removeImage(final RepositoryImage repositoryImage) throws NoSuchRepositoryFileException, IOException {
removeFile(repositoryImage);
return repositoryImage;
}
......@@ -383,8 +392,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
PathValidator.checkValidPath(newPath);
if (repositoryFile == null)
if (repositoryFile == null) {
throw new NoSuchRepositoryFileException();
}
repositoryFile.setPath(newPath);
......
......@@ -89,20 +89,23 @@ public class S3StorageServiceImpl implements BytesStorageService {
/*
* (non-Javadoc)
*
*
* @see org.genesys2.server.filerepository.service.BytesStorageService#upsert (java.lang.String, java.lang.String, byte[])
*/
@Override
public void upsert(final String path, final String filename, final byte[] data) throws FileNotFoundException, IOException {
if (!PathValidator.isValidPath(path))
if (!PathValidator.isValidPath(path)) {
throw new IOException("Path is not valid");
}