Skip to content

Commit

Permalink
enable encryption support for xls and xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Jun 22, 2018
1 parent 65416a0 commit a54025f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
abstract class HSSFUnmarshaller implements Unmarshaller {

private final DataFormatter dataFormatter;
private final PoijiOptions options;
protected final PoijiOptions options;
private final Casting casting;
private Map<String, Integer> titles;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ final class HSSFUnmarshallerFile extends HSSFUnmarshaller implements Unmarshalle
@Override
protected Workbook workbook() {
try {

if (options.getPassword() != null) {
return WorkbookFactory.create(poijiFile.file(), options.getPassword());
}

return WorkbookFactory.create(poijiFile.file());
} catch (InvalidFormatException | IOException e) {
throw new PoijiException("Problem occurred while creating HSSFWorkbook", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ final class HSSFUnmarshallerStream extends HSSFUnmarshaller implements Unmarshal
@Override
protected Workbook workbook() {
try {

if (options.getPassword() != null) {
return WorkbookFactory.create(poijiInputStream.stream(), options.getPassword());
}

return WorkbookFactory.create(poijiInputStream.stream());
} catch (InvalidFormatException | IOException e) {
throw new PoijiException("Problem occurred while creating HSSFWorkbook", e);
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import com.poiji.option.PoijiOptions;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.SAXHelper;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
Expand Down Expand Up @@ -56,8 +59,10 @@ <T> List<T> unmarshal0(Class<T> type, OPCPackage open) throws IOException, SAXEx
}

@SuppressWarnings("unchecked")
private <T> List<T> processSheet(StylesTable styles, ReadOnlySharedStringsTable readOnlySharedStringsTable,
Class<T> type, InputStream sheetInputStream) {
private <T> List<T> processSheet(StylesTable styles,
ReadOnlySharedStringsTable readOnlySharedStringsTable,
Class<T> type,
InputStream sheetInputStream) {

DataFormatter formatter = new DataFormatter();
InputSource sheetSource = new InputSource(sheetInputStream);
Expand All @@ -73,4 +78,20 @@ private <T> List<T> processSheet(StylesTable styles, ReadOnlySharedStringsTable
throw new PoijiException("Problem occurred while reading data", e);
}
}

<T> List<T> listOfEncryptedItems(Class<T> type, NPOIFSFileSystem fs) throws IOException {
InputStream stream = DocumentFactoryHelper.getDecryptedStream(fs, options.getPassword());

try (OPCPackage open = OPCPackage.open(stream)) {
return unmarshal0(type, open);

} catch (SAXException | IOException | OpenXML4JException e) {
IOUtils.closeQuietly(fs);
throw new PoijiException("Problem occurred while reading data", e);
}
}

abstract <T> List<T> returnFromExcelFile(Class<T> type);

abstract <T> List<T> returnFromEncryptedFile(Class<T> type);
}
23 changes: 23 additions & 0 deletions src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.poiji.option.PoijiOptions;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.xml.sax.SAXException;

import java.io.IOException;
Expand All @@ -25,6 +26,16 @@ final class XSSFUnmarshallerFile extends XSSFUnmarshaller {
@Override
@SuppressWarnings("unchecked")
public <T> List<T> unmarshal(Class<T> type) {

if (options.getPassword() != null) {
return returnFromEncryptedFile(type);
}

return returnFromExcelFile(type);
}

public <T> List<T> returnFromExcelFile(Class<T> type) {

try (OPCPackage open = OPCPackage.open(poijiFile.file())) {

return unmarshal0(type, open);
Expand All @@ -33,4 +44,16 @@ public <T> List<T> unmarshal(Class<T> type) {
throw new PoijiException("Problem occurred while reading data", e);
}
}

public <T> List<T> returnFromEncryptedFile(Class<T> type) {

try (NPOIFSFileSystem fs = new NPOIFSFileSystem(poijiFile.file())) {

return listOfEncryptedItems(type, fs);

} catch (IOException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
}

}
25 changes: 24 additions & 1 deletion src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.poiji.option.PoijiOptions;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.xml.sax.SAXException;

import java.io.IOException;
Expand All @@ -13,7 +14,7 @@
/**
* Created by hakan on 22/10/2017
*/
final class XSSFUnmarshallerStream extends XSSFUnmarshaller {
final class XSSFUnmarshallerStream extends XSSFUnmarshaller {

private final PoijiInputStream poijiInputStream;

Expand All @@ -25,6 +26,16 @@ final class XSSFUnmarshallerStream extends XSSFUnmarshaller {
@Override
@SuppressWarnings("unchecked")
public <T> List<T> unmarshal(Class<T> type) {

if (options.getPassword() != null) {
return returnFromEncryptedFile(type);
}

return returnFromExcelFile(type);
}

public <T> List<T> returnFromExcelFile(Class<T> type) {

try (OPCPackage open = OPCPackage.open(poijiInputStream.stream())) {

return unmarshal0(type, open);
Expand All @@ -33,4 +44,16 @@ public <T> List<T> unmarshal(Class<T> type) {
throw new PoijiException("Problem occurred while reading data", e);
}
}

public <T> List<T> returnFromEncryptedFile(Class<T> type) {

try (NPOIFSFileSystem fs = new NPOIFSFileSystem(poijiInputStream.stream())) {

return listOfEncryptedItems(type, fs);

} catch (IOException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
}

}
24 changes: 24 additions & 0 deletions src/main/java/com/poiji/option/PoijiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public final class PoijiOptions {
private String datePattern;
private boolean preferNullOverDefault;
private int sheetIndex;
private String password;

private PoijiOptions() {
super();
Expand All @@ -36,6 +37,15 @@ private PoijiOptions setSheetIndex(int sheetIndex) {
return this;
}

public String getPassword() {
return password;
}

private PoijiOptions setPassword(String password) {
this.password = password;
return this;
}

public int sheetIndex() {
return sheetIndex;
}
Expand Down Expand Up @@ -63,6 +73,7 @@ public static class PoijiOptionsBuilder {
private String datePattern = DEFAULT_DATE_PATTERN;
private boolean preferNullOverDefault = false;
private int sheetIndex;
private String password;

private PoijiOptionsBuilder() {
}
Expand All @@ -74,6 +85,7 @@ private PoijiOptionsBuilder(int skip) {
public PoijiOptions build() {
return new PoijiOptions()
.setSkip(skip)
.setPassword(password)
.setPreferNullOverDefault(preferNullOverDefault)
.setDatePattern(datePattern)
.setSheetIndex(sheetIndex);
Expand Down Expand Up @@ -137,5 +149,17 @@ public static PoijiOptionsBuilder settings(int skip) {
return new PoijiOptionsBuilder(skip);
}


/**
* set password for encrypted excel file, Default is null
*
* @param password
* @return this
*/
public PoijiOptionsBuilder password(String password) {
this.password = password;
return this;
}

}
}

0 comments on commit a54025f

Please sign in to comment.