-
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.
Merged in feature/SHIBUI-1253 (pull request #323)
[SHIBUI-1253] Approved-by: Dmitriy Kopylenko <dkopylenko@unicon.net>
- Loading branch information
Showing
7 changed files
with
227 additions
and
8 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
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
75 changes: 75 additions & 0 deletions
75
...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,75 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import org.springframework.core.io.WritableResource; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
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 (InputStream is = Files.newInputStream(path)) { | ||
if (checkContentMatches(is, content)) { | ||
return; | ||
} | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
writeContent(path, content); | ||
} | ||
|
||
@Override | ||
public void write(WritableResource resource, String content) throws IOException { | ||
if (resource.exists()) { | ||
try (InputStream is = resource.getInputStream()) { | ||
if (checkContentMatches(is, content)) { | ||
return; | ||
} | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
writeContent(resource, content); | ||
} | ||
|
||
private boolean checkContentMatches(InputStream inputStream, String content) throws NoSuchAlgorithmException, IOException { | ||
MessageDigest md = MessageDigest.getInstance(this.algorithm); | ||
try (DigestInputStream dis = new DigestInputStream(inputStream, md)) { | ||
byte[] buf = new byte[4096]; | ||
while (dis.read(buf) > -1) {} | ||
} | ||
byte[] fileDigest = md.digest(); | ||
byte[] contentDigest = md.digest(content.getBytes()); | ||
return Arrays.equals(fileDigest, contentDigest); | ||
} | ||
|
||
void writeContent(Path path, String content) throws IOException { | ||
Files.write(path, content.getBytes()); | ||
} | ||
|
||
void writeContent(WritableResource resource, String content) throws IOException { | ||
try (OutputStream os = resource.getOutputStream()) { | ||
os.write(content.getBytes()); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import org.springframework.core.io.WritableResource; | ||
|
||
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 content to a file | ||
* | ||
* @param path target file Path | ||
* @param content content to write | ||
* @throws IOException | ||
*/ | ||
void write(Path path, String content) throws IOException; | ||
|
||
/** | ||
* Write content to a writeable resource | ||
* | ||
* @param resource | ||
* @param content | ||
* @throws IOException | ||
*/ | ||
void write(WritableResource resource, 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
96 changes: 96 additions & 0 deletions
96
...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,96 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service | ||
|
||
import org.springframework.core.io.PathResource | ||
import org.springframework.core.io.WritableResource | ||
import spock.lang.Specification | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.security.NoSuchAlgorithmException | ||
|
||
class FileCheckingFileWritingServiceTests extends Specification { | ||
def writer = Spy(FileCheckingFileWritingService) | ||
|
||
Path file | ||
|
||
WritableResource resource | ||
|
||
def setup() { | ||
file = Files.createTempFile('test1', '.txt') | ||
resource = new PathResource(file) | ||
} | ||
|
||
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 to a Path'() { | ||
when: | ||
writer.write(file, 'testme') | ||
|
||
then: | ||
1 * writer.writeContent(file, 'testme') | ||
assert file.text == 'testme' | ||
} | ||
|
||
def 'test writes with changed content to a Path'() { | ||
when: | ||
writer.write(file, 'testme') | ||
writer.write(file, 'anothertest') | ||
|
||
then: | ||
1 * writer.writeContent(file, 'testme') | ||
1 * writer.writeContent(file, 'anothertest') | ||
assert file.text == 'anothertest' | ||
} | ||
|
||
def 'test writes with unchanged content, should only write once to a Path'() { | ||
when: | ||
(1..5).each { | ||
writer.write(file, 'testme2') | ||
} | ||
|
||
then: | ||
1 * writer.writeContent(file, 'testme2') | ||
assert file.text == 'testme2' | ||
} | ||
|
||
def 'test a single write to a WriteableResource'() { | ||
when: | ||
writer.write(resource, 'testme') | ||
|
||
then: | ||
1 * writer.writeContent(resource, 'testme') | ||
assert resource.getFile().text == 'testme' | ||
} | ||
|
||
def 'test write with changed content to a WritableResource'() { | ||
when: | ||
writer.write(resource, 'testme') | ||
writer.write(resource, 'anothertest') | ||
|
||
then: | ||
1 * writer.writeContent(resource, 'testme') | ||
1 * writer.writeContent(resource, 'anothertest') | ||
assert resource.getFile().text == 'anothertest' | ||
} | ||
|
||
def 'test writes with unchanged content, should only write once to a WriteableResource'() { | ||
when: | ||
(1..5).each { | ||
writer.write(resource, 'testme2') | ||
} | ||
|
||
then: | ||
1 * writer.writeContent(resource, 'testme2') | ||
assert resource.getFile().text == 'testme2' | ||
} | ||
} |