Skip to content

Commit

Permalink
Added method save(InputStream,String) to DataService
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Mar 24, 2024
1 parent 7f27d45 commit 162ca0b
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
4 changes: 4 additions & 0 deletions data-service-base/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- method save(InputStream,String) to DataService

## [0.3.0 - 2024-02-01]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,19 @@ public interface DataService {
* @throws IOException if any I/O issue arises
*/
String save( InputStream data ) throws IOException;

/**
* <p>Save data and assign an ID to it.</p>
*
* <p>Default implementation of this method will invoke <code>this.save( data )</code></p>
*
* @param data the data to be saved
* @param resourceName resource name to be saved
* @return the ID assigned to saved data
* @throws IOException if any I/O issue arises
*/
default String save( InputStream data, String resourceName ) throws IOException {
return this.save( data );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,25 @@ public static String loadBase64( DataService service, String id ) throws IOExcep
}
return base64;
}



/**
* <p>Save a resource as base64 encoded string.</p>
*
* @param service the data service
* @param data the resource to save
* @param resourceName the resource name to be saved
* @return the ID of the saved resource
* @throws IOException if any I/O issue arises
*/
public static String saveBytes( DataService service, byte[] data, String resourceName ) throws IOException {
String id = null;
try ( InputStream is = new ByteArrayInputStream( data ) ) {
id = service.save(is, resourceName);
}
return id;
}

/**
* <p>Save a resource as base64 encoded string.</p>
*
Expand Down Expand Up @@ -79,6 +97,19 @@ public static String saveBytes( DataService service, byte[] data ) throws IOExce
public static String saveBase64( DataService service, String base64 ) throws IOException {
return saveBytes(service, Base64.getDecoder().decode( base64 ) );
}

/**
* <p>Save a resource as base64 encoded string.</p>
*
* @param service the data service
* @param base64 the resource to save as base64 encoded string
* @param resourceName the resource name to be saved
* @return the ID of the saved resource
* @throws IOException if any I/O issue arises
*/
public static String saveBase64( DataService service, String base64, String resourceName ) throws IOException {
return saveBytes(service, Base64.getDecoder().decode( base64 ), resourceName);
}

/**
* <p>generate a new ID in the format of ${yyyyMMddHHmmss}_${UUID}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public String save(InputStream data) throws IOException {
return this.unwrap().save(data);
}

@Override
public String save(InputStream data, String resourceName) throws IOException {
return this.unwrap().save(data, resourceName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ public InputStream load(String id) throws IOException {

@Override
public String save(InputStream data) throws IOException {
String id = DataServiceIO.generateId();
File file = this.toFile(id);
return this.save( data, null );
}

@Override
public String save(InputStream data, String resourceName) throws IOException {
String id = resourceName == null ? DataServiceIO.generateId() : DataServiceIO.generateId()+"_"+resourceName ;
File file = this.toFile( id );
try ( FileOutputStream fos = new FileOutputStream(file) ) {
StreamIO.pipeStream( data , fos, StreamIO.MODE_CLOSE_BOTH );
}
log.info( "save - file:{} -> id:{}", file.getCanonicalPath(), id );
return id;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test.org.fugerit.java.dsb;

import org.fugerit.java.dsb.DataService;
import org.fugerit.java.dsb.DataServiceIO;
import org.junit.Assert;
import org.junit.Test;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.InputStream;

public class TestDataService {

@Test
public void testBase() throws IOException {
String testId = DataServiceIO.generateId();
DataService ds = new DataService() {
@Override
public InputStream load(String id) throws IOException {
return null;
}
@Override
public String save(InputStream data) throws IOException {
return testId;
}

};
Assert.assertEquals( testId, ds.save( null, null ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public void testFileDataService() throws IOException {
String base64 = DataServiceIO.loadBase64( wrapper , id );
Assert.assertEquals( TEST_DATA_BASE64 , base64 );
}
try ( InputStream data = this.newData() ) {
String id = DataServiceIO.saveBase64( wrapper , TEST_DATA_BASE64, "res_name.txt" );
String base64 = DataServiceIO.loadBase64( wrapper , id );
Assert.assertEquals( TEST_DATA_BASE64 , base64 );
}
wrapper.wrap( service );
Assert.assertThrows( NullPointerException.class , () -> new DataServiceWrapper( null ) );
}
Expand Down

0 comments on commit 162ca0b

Please sign in to comment.