diff --git a/README.md b/README.md index 8607f7d..cf5f364 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ ## API - getFile - postFile -- deleteFile \ No newline at end of file +- deleteFile +- listFiles \ No newline at end of file diff --git a/src/main/java/ru/molokoin/storage/api/RestStorageService.java b/src/main/java/ru/molokoin/storage/api/RestStorageService.java index 94a65a5..27ceb40 100644 --- a/src/main/java/ru/molokoin/storage/api/RestStorageService.java +++ b/src/main/java/ru/molokoin/storage/api/RestStorageService.java @@ -1,19 +1,24 @@ package ru.molokoin.storage.api; +import java.io.File; import java.util.Collection; import jakarta.ejb.EJB; +import jakarta.servlet.http.HttpServletResponse; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; import ru.molokoin.storage.entities.ContentEntity; -import ru.molokoin.storage.services.RepositoryFace; +import ru.molokoin.storage.services.StorageFace; @Path("content") public class RestStorageService { @EJB - private RepositoryFace repository; + private StorageFace storage; /** * Получение сведений о всех файлах в хранилище. @@ -27,10 +32,29 @@ public class RestStorageService { */ @GET @Produces(MediaType.APPLICATION_XML) - public Collection getFiles(){ + public Collection getInfo(){ System.out.println("Передача данных обо всех файлах ..."); - Collection cce = repository.getInfo(); + Collection cce = storage.getInfo(); return cce; } + // @GET + // @Path("{id}") + // @Produces(MediaType.APPLICATION_OCTET_STREAM) + // //@Produces(MediaType.MULTIPART_FORM_DATA) + // public Response getContent(@PathParam("id") Integer id, @Context HttpServletResponse response){ + // System.out.println("Передача байткода по id файла ..."); + + // return storage.findContentById(id); + // } + + // @GET + // @Produces(MediaType.APPLICATION_OCTET_STREAM) + // public Response getFile() { + // File file = ... // Initialize this to the File path you want to serve. + // return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM) + // .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional + // .build(); + // } + } diff --git a/src/main/java/ru/molokoin/storage/entities/ContentEntity.java b/src/main/java/ru/molokoin/storage/entities/ContentEntity.java index a78f4ec..e5a318d 100644 --- a/src/main/java/ru/molokoin/storage/entities/ContentEntity.java +++ b/src/main/java/ru/molokoin/storage/entities/ContentEntity.java @@ -17,7 +17,7 @@ import jakarta.xml.bind.annotation.XmlRootElement; */ @Entity @XmlRootElement(name = "storage") -@Table(name = "Storage", schema = "j200", catalog = "")//поправить схему +@Table(name = "Storage", schema = "home", catalog = "")//поправить схему public class ContentEntity implements Serializable{ @Id //уникальный идентификатор строки @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/ru/molokoin/storage/services/Storage.java b/src/main/java/ru/molokoin/storage/services/Storage.java new file mode 100644 index 0000000..2b63bf8 --- /dev/null +++ b/src/main/java/ru/molokoin/storage/services/Storage.java @@ -0,0 +1,50 @@ +package ru.molokoin.storage.services; + +import java.util.ArrayList; +import java.util.List; + +import jakarta.ejb.Singleton; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.ws.rs.core.Response; +import ru.molokoin.storage.entities.ContentEntity; + +@Singleton +public class Storage implements StorageFace{ + @PersistenceContext (unitName="Storage") + private EntityManager em; + + @Override + public List getInfo() { + List list = new ArrayList<>(); + System.out.println("getInfo()"); + //Обращаемся к базе и запрашиваем сведения + + + return list; + } + + @Override + public void save(String filename, byte[] content) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'save'"); + } + + @Override + public void delete(Long id) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'delete'"); + } + + public static void main(String[] args) { + new Storage().getInfo(); + } + + @Override + public Response findContentById(Integer id) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'findContentById'"); + } + + +} \ No newline at end of file diff --git a/src/main/java/ru/molokoin/storage/services/RepositoryFace.java b/src/main/java/ru/molokoin/storage/services/StorageFace.java similarity index 63% rename from src/main/java/ru/molokoin/storage/services/RepositoryFace.java rename to src/main/java/ru/molokoin/storage/services/StorageFace.java index a445502..4f471e4 100644 --- a/src/main/java/ru/molokoin/storage/services/RepositoryFace.java +++ b/src/main/java/ru/molokoin/storage/services/StorageFace.java @@ -3,21 +3,25 @@ package ru.molokoin.storage.services; import java.util.List; import jakarta.ejb.Local; +import jakarta.ws.rs.core.Response; import ru.molokoin.storage.entities.ContentEntity; /** - * Интерфейс работы с хранилищем файлов на жестком диске + * Интерфейс работы с хранилищем + * - файлов на жестком диске + * - сведениями о файлах в базе данных */ @Local -public interface RepositoryFace { +public interface StorageFace { /** * Коллекция сведений о файлах, доступных в хранилице + * полученная из базы данных */ List getInfo(); /** * Получение байткода из файловой системы по идентификатору файла */ - byte[] getContentById(); + //byte[] getContentById(); /** * Сохранение байткода в файловой системе @@ -28,5 +32,7 @@ public interface RepositoryFace { * Удаление файла из файловой системы по id */ void delete(Long id); + + Response findContentById(Integer id); } diff --git a/src/main/resources/META-INF/persistance.xml b/src/main/resources/META-INF/persistance.xml new file mode 100644 index 0000000..28f7f16 --- /dev/null +++ b/src/main/resources/META-INF/persistance.xml @@ -0,0 +1,9 @@ + + + + Подключение к базе molokoin.ru:3306/home + java:/home + ru.molokoin.storage.entities.ContentEntity + true + + \ No newline at end of file diff --git a/src/main/scripts/prepare.sql b/src/main/scripts/prepare.sql new file mode 100644 index 0000000..75dadc6 --- /dev/null +++ b/src/main/scripts/prepare.sql @@ -0,0 +1,5 @@ +create table home.Storage ( + id INT PRIMARY KEY AUTO_INCREMENT, + filename varchar(50) not null, + location varchar(1000) not null +); \ No newline at end of file diff --git a/target/classes/META-INF/persistance.xml b/target/classes/META-INF/persistance.xml new file mode 100644 index 0000000..28f7f16 --- /dev/null +++ b/target/classes/META-INF/persistance.xml @@ -0,0 +1,9 @@ + + + + Подключение к базе molokoin.ru:3306/home + java:/home + ru.molokoin.storage.entities.ContentEntity + true + + \ No newline at end of file diff --git a/target/classes/ru/molokoin/storage/api/RestStorageService.class b/target/classes/ru/molokoin/storage/api/RestStorageService.class index e08d316..9c5de45 100644 Binary files a/target/classes/ru/molokoin/storage/api/RestStorageService.class and b/target/classes/ru/molokoin/storage/api/RestStorageService.class differ diff --git a/target/classes/ru/molokoin/storage/entities/ContentEntity.class b/target/classes/ru/molokoin/storage/entities/ContentEntity.class index 9c8b754..68779bb 100644 Binary files a/target/classes/ru/molokoin/storage/entities/ContentEntity.class and b/target/classes/ru/molokoin/storage/entities/ContentEntity.class differ diff --git a/target/classes/ru/molokoin/storage/services/RepositoryFace.class b/target/classes/ru/molokoin/storage/services/RepositoryFace.class deleted file mode 100644 index 68baff5..0000000 Binary files a/target/classes/ru/molokoin/storage/services/RepositoryFace.class and /dev/null differ diff --git a/target/classes/ru/molokoin/storage/services/Storage.class b/target/classes/ru/molokoin/storage/services/Storage.class new file mode 100644 index 0000000..a4d275a Binary files /dev/null and b/target/classes/ru/molokoin/storage/services/Storage.class differ diff --git a/target/classes/ru/molokoin/storage/services/StorageFace.class b/target/classes/ru/molokoin/storage/services/StorageFace.class new file mode 100644 index 0000000..f3f2038 Binary files /dev/null and b/target/classes/ru/molokoin/storage/services/StorageFace.class differ