Skip to content

Commit

Permalink
fix: Modify the reading mode of CSV file.
Browse files Browse the repository at this point in the history
  • Loading branch information
shy1st committed Apr 5, 2021
1 parent 5dc7d32 commit 8dc7cff
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 43 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Expand Up @@ -216,7 +216,11 @@
<artifactId>janino</artifactId>
<version>3.1.2</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
</dependencies>

</project>
37 changes: 37 additions & 0 deletions src/main/java/org/casbin/jcasbin/main/Enforcer.java
Expand Up @@ -14,6 +14,7 @@

package org.casbin.jcasbin.main;

import org.apache.commons.csv.CSVFormat;
import org.casbin.jcasbin.model.FunctionMap;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.Adapter;
Expand Down Expand Up @@ -44,6 +45,42 @@ public Enforcer(String modelPath, String policyFile) {
this(modelPath, new FileAdapter(policyFile));
}

/**
* Enforcer initializes an enforcer with a model file and a policy file.
*
* @param modelPath the path of the model file.
* @param policyFile the path of the policy file.
* @param codeMode the code mode, such as "GBK", "UTF-8" etc.
*/
public Enforcer(String modelPath, String policyFile, String codeMode) {
this(modelPath, new FileAdapter(policyFile, codeMode));
}

/**
* Enforcer initializes an enforcer with a model file and a policy file.
*
* @param modelPath the path of the model file.
* @param policyFile the path of the policy file.
* @param csvFormat specifies the format of a CSV file and parses input.
*/
public Enforcer(String modelPath, String policyFile, CSVFormat csvFormat) {
this(modelPath, new FileAdapter(policyFile, csvFormat));
}

/**
* Enforcer initializes an enforcer with a model file and a policy file.
*
* @param modelPath the path of the model file.
* @param policyFile the path of the policy file.
* @param csvFormat specifies the format of a CSV file and parses input.
* @param codeMode the code mode, such as "GBK", "UTF-8" etc.
*/
public Enforcer(String modelPath, String policyFile, CSVFormat csvFormat, String codeMode) {
this(modelPath, new FileAdapter(policyFile, csvFormat, codeMode));
}

/**
* Enforcer initializes an enforcer with a database adapter.
*
Expand Down
26 changes: 10 additions & 16 deletions src/main/java/org/casbin/jcasbin/persist/Helper.java
Expand Up @@ -14,30 +14,24 @@

package org.casbin.jcasbin.persist;

import static org.casbin.jcasbin.util.Util.splitCommaDelimited;

import org.apache.commons.csv.CSVRecord;
import org.casbin.jcasbin.model.Model;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

public class Helper {
public interface loadPolicyLineHandler<T, U> {
void accept(T t, U u);
}

public static void loadPolicyLine(String line, Model model) {
if (line.equals("")) {
return;
}

if (line.charAt(0) == '#') {
return;
}

String[] tokens = splitCommaDelimited(line);

String key = tokens[0];
public static void loadPolicyLine(CSVRecord csvRecord, Model model) {
String key = csvRecord.get(0).trim();
String sec = key.substring(0, 1);
model.model.get(sec).get(key).policy.add(Arrays.asList(Arrays.copyOfRange(tokens, 1, tokens.length)));
List<String> tokens = new ArrayList<>();
for (int i = 1; i < csvRecord.size(); i++) {
tokens.add(csvRecord.get(i).trim());
}
model.model.get(sec).get(key).policy.add(tokens);
}
}
123 changes: 112 additions & 11 deletions src/main/java/org/casbin/jcasbin/persist/file_adapter/FileAdapter.java
Expand Up @@ -14,6 +14,9 @@

package org.casbin.jcasbin.persist.file_adapter;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.IOUtils;
import org.casbin.jcasbin.exception.CasbinAdapterException;
import org.casbin.jcasbin.exception.CasbinPolicyFileNotFoundException;
Expand All @@ -35,7 +38,9 @@
public class FileAdapter implements Adapter {
private String filePath;
private boolean readOnly = false;
private ByteArrayInputStream byteArrayInputStream;
private List<CSVRecord> csvRecords;
private String codeMode = "GBK";
private CSVFormat csvFormat = CSVFormat.DEFAULT;

/**
* FileAdapter is the constructor for FileAdapter.
Expand All @@ -46,6 +51,41 @@ public FileAdapter(String filePath) {
this.filePath = filePath;
}

/**
* FileAdapter is the constructor for FileAdapter.
*
* @param codeMode the code mode, such as "GBK", "UTF-8" etc.
* @param filePath the path of the policy file.
*/
public FileAdapter(String filePath, String codeMode) {
this.filePath = filePath;
this.codeMode = codeMode;
}

/**
* FileAdapter is the constructor for FileAdapter.
*
* @param csvFormat specifies the format of a CSV file and parses input.
* @param filePath the path of the policy file.
*/
public FileAdapter(String filePath, CSVFormat csvFormat) {
this.filePath = filePath;
this.csvFormat = csvFormat;
}

/**
* FileAdapter is the constructor for FileAdapter.
*
* @param filePath the path of the policy file.
* @param csvFormat specifies the format of a CSV file and parses input.
* @param codeMode the code mode, such as "GBK", "UTF-8" etc.
*/
public FileAdapter(String filePath, CSVFormat csvFormat, String codeMode) {
this.filePath = filePath;
this.csvFormat = csvFormat;
this.codeMode = codeMode;
}

/**
* FileAdapter is the constructor for FileAdapter.
*
Expand All @@ -54,7 +94,61 @@ public FileAdapter(String filePath) {
public FileAdapter(InputStream inputStream) {
readOnly = true;
try {
byteArrayInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
Reader reader = new BufferedReader(new InputStreamReader(inputStream, codeMode));
CSVParser csvParser = csvFormat.parse(reader);
csvRecords = csvParser.getRecords();
} catch (IOException e) {
e.printStackTrace();
throw new CasbinAdapterException("File adapter init error");
}
}

/**
* FileAdapter is the constructor for FileAdapter.
*
* @param codeMode the code mode, such as "GBK", "UTF-8" etc.
* @param inputStream the policy file.inputStream
*/
public FileAdapter(InputStream inputStream, String codeMode) {
readOnly = true;
try {
Reader reader = new BufferedReader(new InputStreamReader(inputStream, codeMode));
CSVParser csvParser = csvFormat.parse(reader);
csvRecords = csvParser.getRecords();
} catch (IOException e) {
e.printStackTrace();
throw new CasbinAdapterException("File adapter init error");
}
}

/**
* FileAdapter is the constructor for FileAdapter.
*
* @param inputStream the policy file.inputStream
*/
public FileAdapter(InputStream inputStream, CSVFormat csvFormat) {
readOnly = true;
try {
Reader reader = new BufferedReader(new InputStreamReader(inputStream, codeMode));
CSVParser csvParser = csvFormat.parse(reader);
csvRecords = csvParser.getRecords();
} catch (IOException e) {
e.printStackTrace();
throw new CasbinAdapterException("File adapter init error");
}
}

/**
* FileAdapter is the constructor for FileAdapter.
*
* @param inputStream the policy file.inputStream
*/
public FileAdapter(InputStream inputStream, CSVFormat csvFormat, String codeMode) {
readOnly = true;
try {
Reader reader = new BufferedReader(new InputStreamReader(inputStream, codeMode));
CSVParser csvParser = csvFormat.parse(reader);
csvRecords = csvParser.getRecords();
} catch (IOException e) {
e.printStackTrace();
throw new CasbinAdapterException("File adapter init error");
Expand All @@ -67,14 +161,17 @@ public FileAdapter(InputStream inputStream) {
@Override
public void loadPolicy(Model model) {
if (filePath != null && !"".equals(filePath)) {
try (FileInputStream fis = new FileInputStream(filePath)) {
loadPolicyData(model, Helper::loadPolicyLine, fis);
try {
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), codeMode));
CSVParser csvParser = CSVFormat.DEFAULT.parse(reader);
List<CSVRecord> csvRecords = csvParser.getRecords();
loadPolicyData(model, Helper::loadPolicyLine, csvRecords);
} catch (IOException e) {
throw new CasbinAdapterException("Load policy file error", e.getCause());
}
}
if (byteArrayInputStream != null) {
loadPolicyData(model, Helper::loadPolicyLine, byteArrayInputStream);
if (csvRecords != null) {
loadPolicyData(model, Helper::loadPolicyLine, csvRecords);
}
}

Expand All @@ -83,7 +180,7 @@ public void loadPolicy(Model model) {
*/
@Override
public void savePolicy(Model model) {
if (byteArrayInputStream != null && readOnly) {
if (csvRecords != null && readOnly) {
throw new CasbinAdapterException("Policy file can not write, because use inputStream is readOnly");
}
if (filePath == null || "".equals(filePath) || !new File(filePath).exists()) {
Expand All @@ -106,11 +203,15 @@ private List<String> getModelPolicy(Model model, String ptype) {
return policy;
}

private void loadPolicyData(Model model, Helper.loadPolicyLineHandler<String, Model> handler, InputStream inputStream) {
private void loadPolicyData(Model model, Helper.loadPolicyLineHandler<CSVRecord, Model> handler, List<CSVRecord> csvRecords) {
try {
List<String> lines = IOUtils.readLines(inputStream, Charset.forName("UTF-8"));
lines.forEach(x -> handler.accept(x, model));
} catch (IOException e) {
for (CSVRecord csvRecord:csvRecords) {
if (csvRecord.size() <= 1) {
continue;
}
handler.accept(csvRecord, model);
}
} catch (Exception e) {
e.printStackTrace();
throw new CasbinAdapterException("Policy load error");
}
Expand Down
Expand Up @@ -14,16 +14,19 @@

package org.casbin.jcasbin.persist.file_adapter;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.IOUtils;
import org.casbin.jcasbin.exception.CasbinAdapterException;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.Adapter;
import org.casbin.jcasbin.persist.Helper;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;

/**
* FilteredAdapter is the filtered file adapter for Casbin.
Expand All @@ -37,12 +40,33 @@ public class FilteredAdapter implements org.casbin.jcasbin.persist.FilteredAdapt
private Adapter adapter;
private boolean isFiltered = true;
private String filepath;
private String codeMode = "GBK";
private CSVFormat csvFormat = CSVFormat.DEFAULT;

public FilteredAdapter(String filepath) {
adapter = new FileAdapter(filepath);
this.filepath = filepath;
}

public FilteredAdapter(String filepath, String codeMode) {
adapter = new FileAdapter(filepath);
this.filepath = filepath;
this.codeMode = codeMode;
}

public FilteredAdapter(String filepath, CSVFormat csvFormat) {
adapter = new FileAdapter(filepath);
this.filepath = filepath;
this.csvFormat = csvFormat;
}

public FilteredAdapter(String filepath, CSVFormat csvFormat, String codeMode) {
adapter = new FileAdapter(filepath);
this.filepath = filepath;
this.csvFormat = csvFormat;
this.codeMode = codeMode;
}

/**
* loadFilteredPolicy loads only policy rules that match the filter.
* @param model the model.
Expand Down Expand Up @@ -73,15 +97,24 @@ public void loadFilteredPolicy(Model model, Object filter) throws CasbinAdapterE
/**
* loadFilteredPolicyFile loads only policy rules that match the filter from file.
*/
private void loadFilteredPolicyFile(Model model, Filter filter, Helper.loadPolicyLineHandler<String, Model> handler) throws CasbinAdapterException {
try (FileInputStream fis = new FileInputStream(filepath)) {
List<String> lines = IOUtils.readLines(fis, Charset.forName("UTF-8"));
for (String line : lines) {
line = line.trim();
if (filterLine(line, filter)) {
private void loadFilteredPolicyFile(Model model, Filter filter, Helper.loadPolicyLineHandler<CSVRecord, Model> handler) throws CasbinAdapterException {
Reader reader;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filepath), codeMode));
CSVParser csvParser = csvFormat.parse(reader);
List<CSVRecord> csvRecords = csvParser.getRecords();
for (CSVRecord csvRecord:csvRecords) {
if (csvRecord.size() <= 1) {
continue;
}
String[] p = new String[csvRecord.size()];
for (int i = 0; i < p.length; i++) {
p[i] = csvRecord.get(i).trim();
}
if (filterLine(p, filter)) {
continue;
}
handler.accept(line, model);
handler.accept(csvRecord, model);
}
} catch (IOException e) {
throw new CasbinAdapterException("Load policy file error", e.getCause());
Expand All @@ -91,11 +124,10 @@ private void loadFilteredPolicyFile(Model model, Filter filter, Helper.loadPolic
/**
* match the line.
*/
private boolean filterLine(String line, Filter filter) {
private boolean filterLine(String[] p, Filter filter) {
if (filter == null) {
return false;
}
String[] p = line.split(",");
if (p.length == 0) {
return true;
}
Expand Down

0 comments on commit 8dc7cff

Please sign in to comment.