Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4a04d4
commit cb297a0
Showing
8 changed files
with
175 additions
and
200 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...unity/security/src/main/java/org/neo4j/server/security/auth/FileRepositorySerializer.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,92 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.server.security.auth; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.neo4j.server.security.auth.exception.FormatException; | ||
import org.neo4j.string.UTF8; | ||
|
||
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; | ||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | ||
|
||
public abstract class FileRepositorySerializer<S> | ||
{ | ||
public void saveRecordsToFile(Path recordsFile, Collection<S> records) throws IOException | ||
{ | ||
Path directory = recordsFile.getParent(); | ||
if ( !Files.exists( directory ) ) | ||
{ | ||
Files.createDirectories( directory ); | ||
} | ||
|
||
Path tempFile = Files.createTempFile( directory, recordsFile.getFileName().toString() + "-", ".tmp" ); | ||
try | ||
{ | ||
Files.write( tempFile, serialize( records ) ); | ||
Files.move( tempFile, recordsFile, ATOMIC_MOVE, REPLACE_EXISTING ); | ||
} | ||
catch ( Throwable e ) | ||
{ | ||
Files.delete( tempFile ); | ||
throw e; | ||
} | ||
} | ||
|
||
public List<S> loadRecordsFromFile(Path recordsFile) throws IOException, FormatException | ||
{ | ||
byte[] fileBytes = Files.readAllBytes( recordsFile ); | ||
return deserializeRecords( fileBytes ); | ||
} | ||
|
||
public byte[] serialize(Collection<S> records) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
for ( S record : records ) | ||
{ | ||
sb.append( serialize( record ) ).append( "\n" ); | ||
} | ||
return UTF8.encode( sb.toString() ); | ||
} | ||
|
||
public List<S> deserializeRecords( byte[] bytes ) throws FormatException | ||
{ | ||
List<S> out = new ArrayList<>( ); | ||
int lineNumber = 1; | ||
for ( String line : UTF8.decode( bytes ).split( "\n" ) ) | ||
{ | ||
if ( line.trim().length() > 0 ) | ||
{ | ||
out.add( deserializeRecord( line, lineNumber ) ); | ||
} | ||
lineNumber++; | ||
} | ||
return out; | ||
} | ||
|
||
protected abstract String serialize( S record ); | ||
|
||
protected abstract S deserializeRecord( String line, int lineNumber ) throws FormatException; | ||
} |
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
28 changes: 28 additions & 0 deletions
28
...nity/security/src/main/java/org/neo4j/server/security/auth/exception/FormatException.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,28 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.server.security.auth.exception; | ||
|
||
public class FormatException extends Exception | ||
{ | ||
public FormatException( String message ) | ||
{ | ||
super( message ); | ||
} | ||
} |
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
Oops, something went wrong.