esoe 1 year ago
parent
commit
7cf5bccf15
  1. 3
      README.md
  2. 32
      src/main/java/ru/molokoin/storage/api/RestStorageService.java
  3. 2
      src/main/java/ru/molokoin/storage/entities/ContentEntity.java
  4. 50
      src/main/java/ru/molokoin/storage/services/Storage.java
  5. 12
      src/main/java/ru/molokoin/storage/services/StorageFace.java
  6. 9
      src/main/resources/META-INF/persistance.xml
  7. 5
      src/main/scripts/prepare.sql
  8. 9
      target/classes/META-INF/persistance.xml
  9. BIN
      target/classes/ru/molokoin/storage/api/RestStorageService.class
  10. BIN
      target/classes/ru/molokoin/storage/entities/ContentEntity.class
  11. BIN
      target/classes/ru/molokoin/storage/services/RepositoryFace.class
  12. BIN
      target/classes/ru/molokoin/storage/services/Storage.class
  13. BIN
      target/classes/ru/molokoin/storage/services/StorageFace.class

3
README.md

@ -4,4 +4,5 @@ @@ -4,4 +4,5 @@
## API
- getFile
- postFile
- deleteFile
- deleteFile
- listFiles

32
src/main/java/ru/molokoin/storage/api/RestStorageService.java

@ -1,19 +1,24 @@ @@ -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 { @@ -27,10 +32,29 @@ public class RestStorageService {
*/
@GET
@Produces(MediaType.APPLICATION_XML)
public Collection<ContentEntity> getFiles(){
public Collection<ContentEntity> getInfo(){
System.out.println("Передача данных обо всех файлах ...");
Collection<ContentEntity> cce = repository.getInfo();
Collection<ContentEntity> 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();
// }
}

2
src/main/java/ru/molokoin/storage/entities/ContentEntity.java

@ -17,7 +17,7 @@ import jakarta.xml.bind.annotation.XmlRootElement; @@ -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)

50
src/main/java/ru/molokoin/storage/services/Storage.java

@ -0,0 +1,50 @@ @@ -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<ContentEntity> getInfo() {
List<ContentEntity> 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'");
}
}

12
src/main/java/ru/molokoin/storage/services/RepositoryFace.java → src/main/java/ru/molokoin/storage/services/StorageFace.java

@ -3,21 +3,25 @@ package ru.molokoin.storage.services; @@ -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<ContentEntity> getInfo();
/**
* Получение байткода из файловой системы по идентификатору файла
*/
byte[] getContentById();
//byte[] getContentById();
/**
* Сохранение байткода в файловой системе
@ -28,5 +32,7 @@ public interface RepositoryFace { @@ -28,5 +32,7 @@ public interface RepositoryFace {
* Удаление файла из файловой системы по id
*/
void delete(Long id);
Response findContentById(Integer id);
}

9
src/main/resources/META-INF/persistance.xml

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemalocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
<persistence-unit name="Storage" transaction-type="JTA">
<description>Подключение к базе molokoin.ru:3306/home</description>
<jta-data-source>java:/home</jta-data-source>
<class>ru.molokoin.storage.entities.ContentEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

5
src/main/scripts/prepare.sql

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
create table home.Storage (
id INT PRIMARY KEY AUTO_INCREMENT,
filename varchar(50) not null,
location varchar(1000) not null
);

9
target/classes/META-INF/persistance.xml

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemalocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
<persistence-unit name="Storage" transaction-type="JTA">
<description>Подключение к базе molokoin.ru:3306/home</description>
<jta-data-source>java:/home</jta-data-source>
<class>ru.molokoin.storage.entities.ContentEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

BIN
target/classes/ru/molokoin/storage/api/RestStorageService.class

Binary file not shown.

BIN
target/classes/ru/molokoin/storage/entities/ContentEntity.class

Binary file not shown.

BIN
target/classes/ru/molokoin/storage/services/RepositoryFace.class

Binary file not shown.

BIN
target/classes/ru/molokoin/storage/services/Storage.class

Binary file not shown.

BIN
target/classes/ru/molokoin/storage/services/StorageFace.class

Binary file not shown.
Loading…
Cancel
Save