-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
file writing service implementation that checks the file before writing
- Loading branch information
Showing
6 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...n/java/edu/internet2/tier/shibboleth/admin/ui/service/FileCheckingFileWritingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.DigestInputStream; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Arrays; | ||
|
||
public class FileCheckingFileWritingService implements FileWritingService { | ||
private static final String DEFAULT_ALGORITHM = "MD5"; | ||
private final String algorithm; | ||
|
||
public FileCheckingFileWritingService() { | ||
this(DEFAULT_ALGORITHM); | ||
} | ||
|
||
public FileCheckingFileWritingService(String algorithm) { | ||
this.algorithm = algorithm; | ||
} | ||
|
||
@Override | ||
public void write(Path path, String content) throws IOException { | ||
if (Files.exists(path)) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance(this.algorithm); | ||
try ( | ||
InputStream is = Files.newInputStream(path); | ||
DigestInputStream dis = new DigestInputStream(is, md) | ||
) { | ||
byte[] buf = new byte[4096]; | ||
while (dis.read(buf) > -1){} | ||
} | ||
byte[] fileDigest = md.digest(); | ||
byte[] contentDigest = md.digest(content.getBytes()); | ||
if (Arrays.equals(fileDigest, contentDigest)) { | ||
return; | ||
} | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
writeContent(path, content.getBytes()); | ||
} | ||
|
||
void writeContent(Path path, byte[] bytes) throws IOException { | ||
Files.write(path, bytes); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/FileWritingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Service interface for writing files. Implementations may perform various tasks | ||
* before or after writing the file. | ||
*/ | ||
public interface FileWritingService { | ||
/** | ||
* write the file | ||
* | ||
* @param path target file Path | ||
* @param content content to write | ||
* @throws IOException | ||
*/ | ||
void write(Path path, String content) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...edu/internet2/tier/shibboleth/admin/ui/service/FileCheckingFileWritingServiceTests.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service | ||
|
||
import spock.lang.Specification | ||
|
||
import java.nio.file.Files | ||
import java.security.NoSuchAlgorithmException | ||
|
||
class FileCheckingFileWritingServiceTests extends Specification { | ||
def writer = Spy(FileCheckingFileWritingService) | ||
|
||
def file1 = Files.createTempFile('test1', '.txt') | ||
def file2 = Files.createTempFile('test2', '.txt') | ||
|
||
def "test bad algorithm"() { | ||
setup: | ||
def badWriter = new FileCheckingFileWritingService('badAlGoreRhythm') | ||
|
||
when: | ||
badWriter.write(Files.createTempFile('testbadalgorithm', '.txt'), 'bad') | ||
|
||
then: | ||
RuntimeException ex = thrown() | ||
assert ex.cause instanceof NoSuchAlgorithmException | ||
} | ||
|
||
def "test a single write"() { | ||
when: | ||
writer.write(file1, 'testme') | ||
|
||
then: | ||
1 * writer.writeContent(file1, 'testme'.bytes) | ||
} | ||
|
||
def "test writes with changed content"() { | ||
when: | ||
writer.write(file2, 'testme') | ||
writer.write(file2, 'anothertest') | ||
|
||
then: | ||
1 * writer.writeContent(file2, 'testme'.bytes) | ||
1 * writer.writeContent(file2, 'anothertest'.bytes) | ||
} | ||
|
||
def "test writes with unchanged content, should only write once"() { | ||
when: | ||
(1..5).each { | ||
writer.write(file1, 'testme') | ||
} | ||
|
||
then: | ||
1 * writer.writeContent(file1, 'testme'.bytes) | ||
} | ||
} |