Commit 8460335f authored by Matija Obreza's avatar Matija Obreza
Browse files

WIP: Interfacing with Repository

parent 5a43bf5d
......@@ -59,6 +59,18 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
......@@ -119,15 +119,17 @@ public class RepositoryFile extends AuditedVersionedModel implements BaseMetadat
private Date dateRetrieved;
/** Sha1sum hash of the bytes. */
// TODO Update to nullable = false in next release
@Column(length = 40, nullable = true)
@Column(length = 40, nullable = false)
private String sha1Sum;
/** MD5 hash of the bytes. */
// TODO Update to nullable = false in next release
@Column(length = 32, nullable = true)
@Column(length = 32, nullable = false)
private String md5Sum;
/** Byte length */
@Column(nullable = false)
private long size;
/**
* Pre persist.
*/
......@@ -568,6 +570,20 @@ public class RepositoryFile extends AuditedVersionedModel implements BaseMetadat
this.md5Sum = md5Sum;
}
/**
* @return the size
*/
public final long getSize() {
return size;
}
/**
* @param size the size to set
*/
public final void setSize(long size) {
this.size = size;
}
@Override
public RepositoryFile apply(RepositoryFile source) {
......@@ -590,6 +606,7 @@ public class RepositoryFile extends AuditedVersionedModel implements BaseMetadat
this.subject = source.subject;
this.title = source.title;
this.uuid = source.uuid;
this.size = source.size;
return this;
}
......
......@@ -39,7 +39,6 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
// TODO: Auto-generated Javadoc
/**
* The Class DatabaseConfig.
*/
......
......@@ -67,5 +67,20 @@
<classifier>ftp</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>file-repository-core</artifactId>
<version>0.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>file-repository-core</artifactId>
<type>test-jar</type>
<version>0.9-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
......@@ -15,19 +15,133 @@
*/
package org.genesys.filerepository.service.ftp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.ftpserver.ftplet.FileSystemFactory;
import org.apache.ftpserver.ftplet.FileSystemView;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpFile;
import org.apache.ftpserver.ftplet.User;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public class RepositoryFileSystemFactory implements FileSystemFactory {
private final static Logger LOG = LoggerFactory.getLogger(RepositoryFileSystemFactory.class);
@Autowired
private RepositoryService repositoryService;
private RepositoryFtpFile file(RepositoryFile repositoryFile) {
RepositoryFtpFile rff = new RepositoryFtpFile(repositoryFile) {
@Override
public String getOwnerName() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getGroupName() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean mkdir() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean delete() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean move(FtpFile destination) {
LOG.info("Move file={} to dest={}", this.getAbsolutePath(), destination.getAbsolutePath());
return false;
}
@Override
public OutputStream createOutputStream(long offset) throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public InputStream createInputStream(long offset) throws IOException {
return null;
}
};
return rff;
}
private RepositoryFtpDirectory directory(String path) {
RepositoryFtpDirectory rfd = new RepositoryFtpDirectory(path) {
@Override
public boolean move(FtpFile destination) {
LOG.info("Move directory={} to dest={}", this.getAbsolutePath(), destination.getAbsolutePath());
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mkdir() {
LOG.info("Mkdir directory={}", this.getAbsolutePath());
// TODO Auto-generated method stub
return false;
}
@Override
public List<? extends FtpFile> listFiles() {
return repositoryService.getFiles(this.getAbsolutePath()).stream().map(rf -> file(fr));.collect(Collectors::toList);
}
@Override
public String getOwnerName() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getGroupName() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean delete() {
// TODO Auto-generated method stub
return false;
}
};
return rfd;
}
@Override
public FileSystemView createFileSystemView(User user) throws FtpException {
public FileSystemView createFileSystemView(final User user) throws FtpException {
LOG.info("Creating new repository view for {}", user.getName());
// TODO Auto-generated method stub
return new FileSystemView() {
private final String username = user.getName();
private RepositoryFtpDirectory cwd;
private RepositoryFtpDirectory homeDir;
@Override
public boolean isRandomAccessible() throws FtpException {
// TODO Auto-generated method stub
......@@ -36,31 +150,32 @@ public class RepositoryFileSystemFactory implements FileSystemFactory {
@Override
public FtpFile getWorkingDirectory() throws FtpException {
// TODO Auto-generated method stub
return null;
LOG.debug("getWorkingDirectory for user={}", username);
return this.cwd;
}
@Override
public FtpFile getHomeDirectory() throws FtpException {
// TODO Auto-generated method stub
return null;
LOG.debug("getHomeDirectory for user={}", username);
return this.homeDir;
}
@Override
public FtpFile getFile(String file) throws FtpException {
LOG.debug("getFile file={} for user={}", file, username);
// TODO Auto-generated method stub
return null;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
LOG.info("Disposing repository view for user={}", username);
}
@Override
public boolean changeWorkingDirectory(String dir) throws FtpException {
// TODO Auto-generated method stub
LOG.debug("CWD dir={} for user={}", dir, username);
this.cwd.changeWorkingDirectory(dir);
return false;
}
};
......
/*
* Copyright 2017 Global Crop Diversity Trust, www.croptrust.org
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.filerepository.service.ftp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.ftpserver.ftplet.FtpFile;
/**
* {@link RepositoryFtpDirectory} wraps repository paths
*
* @author Matija Obreza
*
*/
public abstract class RepositoryFtpDirectory implements FtpFile {
private String parent;
private String name;
public RepositoryFtpDirectory() {
}
public RepositoryFtpDirectory(String path) {
this.parent = path;
}
@Override
public String getAbsolutePath() {
return this.parent == null ? this.name : this.parent.concat("/").concat(this.name);
}
@Override
public String getName() {
return this.name;
}
@Override
public boolean isHidden() {
return false;
}
@Override
public boolean isDirectory() {
return true;
}
@Override
public boolean isFile() {
return false;
}
@Override
public boolean doesExist() {
return true;
}
@Override
public boolean isReadable() {
return true;
}
@Override
public boolean isWritable() {
return true;
}
@Override
public boolean isRemovable() {
return true;
}
@Override
public abstract String getOwnerName();
@Override
public abstract String getGroupName();
@Override
public int getLinkCount() {
return 0;
}
@Override
public long getLastModified() {
return 0;
}
@Override
public boolean setLastModified(long time) {
return false;
}
@Override
public long getSize() {
return 0;
}
@Override
public Object getPhysicalFile() {
// TODO Auto-generated method stub
return null;
}
@Override
public abstract boolean mkdir();
@Override
public abstract boolean delete();
@Override
public abstract boolean move(FtpFile destination);
@Override
public abstract List<? extends FtpFile> listFiles();
@Override
public OutputStream createOutputStream(long offset) throws IOException {
throw new IOException("Cannot outputstream the Directory");
}
@Override
public InputStream createInputStream(long offset) throws IOException {
throw new IOException("Cannot inputstream the Directory");
}
}
/*
* Copyright 2017 Global Crop Diversity Trust, www.croptrust.org
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.filerepository.service.ftp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.ftpserver.ftplet.FtpFile;
import org.genesys.filerepository.model.RepositoryFile;
/**
* {@link RepositoryFtpFile} wraps {@link RepositoryFile}
*
* @author Matija Obreza
*
*/
public abstract class RepositoryFtpFile implements FtpFile {
private RepositoryFile repositoryFile;
public RepositoryFtpFile() {
}
public RepositoryFtpFile(RepositoryFile repositoryFile) {
this.repositoryFile = repositoryFile;
}
@Override
public String getAbsolutePath() {
return this.repositoryFile.getPath() + this.repositoryFile.getFilename();
}
@Override
public String getName() {
return this.repositoryFile.getFilename();
}
@Override
public boolean isHidden() {
return false;
}
@Override
public boolean isDirectory() {
return false;
}
@Override
public boolean isFile() {
return true;
}
@Override
public boolean doesExist() {
return true;
}
@Override
public boolean isReadable() {
return true;
}
@Override
public boolean isWritable() {
return true;
}
@Override
public boolean isRemovable() {
return true;
}
@Override
public abstract String getOwnerName();
@Override
public abstract String getGroupName();
@Override
public int getLinkCount() {
return 0;
}
@Override
public long getLastModified() {
return repositoryFile.getLastModifiedDate() != null ? repositoryFile.getLastModifiedDate().getTime() : 0;
}
@Override
public boolean setLastModified(long time) {
return false;
}
@Override
public long getSize() {
return repositoryFile.getSize();
}
@Override
public Object getPhysicalFile() {
// TODO Auto-generated method stub
return null;
}
@Override
public abstract boolean mkdir();
@Override
public abstract boolean delete();
@Override
public abstract boolean move(FtpFile destination);
@Override
public List<? extends FtpFile> listFiles() {
return null;
}
@Override
public abstract OutputStream createOutputStream(long offset) throws IOException;
@Override
public abstract InputStream createInputStream(long offset) throws IOException;
}
......@@ -34,6 +34,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
@Bean
public RepositoryFtpServer ftpServer() {
RepositoryFtpServer ftpServer = new RepositoryFtpServer();
......@@ -62,6 +63,7 @@ public class ApplicationConfig {
authorities.add(new ConcurrentLoginPermission(2, 0));
authorities.add(new WritePermission());
user.setAuthorities(authorities);
user.setHomeDirectory("/");
return user;
}
......