Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 157d280

Browse files
committed
Merge pull request #39 from couchbaselabs/feature/issue_924_viewname_special_chars
Fixed Java Core 924: viewname special chars
2 parents fe63bd0 + 5218bea commit 157d280

2 files changed

Lines changed: 54 additions & 22 deletions

File tree

src/main/java/com/couchbase/lite/store/ForestDBStore.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,24 +1047,20 @@ public Status run() {
10471047
}
10481048

10491049
@Override
1050-
public ViewStore getViewStorage(String name, boolean create) {
1051-
try {
1052-
return new ForestDBViewStore(this, name, create);
1053-
} catch (CouchbaseLiteException e) {
1054-
if(e.getCBLStatus().getCode() != Status.NOT_FOUND)
1055-
Log.e(TAG, "Error in getViewStorage()", e);
1056-
return null;
1057-
}
1050+
public ViewStore getViewStorage(String name, boolean create) throws CouchbaseLiteException {
1051+
return new ForestDBViewStore(this, name, create);
10581052
}
10591053

10601054
@Override
10611055
public List<String> getAllViewNames() {
10621056
List<String> result = new ArrayList<String>();
10631057
String[] fileNames = new File(directory).list();
1064-
for(String filename : fileNames){
1065-
String viewName = ForestDBViewStore.fileNameToViewName(filename);
1066-
if(viewName != null)
1067-
result.add(viewName);
1058+
for (String filename : fileNames) {
1059+
try {
1060+
result.add(ForestDBViewStore.fileNameToViewName(filename));
1061+
} catch (CouchbaseLiteException e) {
1062+
Log.w(TAG, "Error in fileNameToViewName(): filename=" + filename, e);
1063+
}
10681064
}
10691065
return result;
10701066
}
@@ -1311,8 +1307,12 @@ public Action actionToChangeEncryptionKey(final SymmetricKey newKey) {
13111307
// Re-key the views!
13121308
List<String> viewNames = getAllViewNames();
13131309
for (String viewName : viewNames) {
1314-
ForestDBViewStore viewStorage = (ForestDBViewStore) getViewStorage(viewName, true);
1315-
action.add(viewStorage.getActionToChangeEncryptionKey());
1310+
try {
1311+
ForestDBViewStore viewStorage = (ForestDBViewStore) getViewStorage(viewName, true);
1312+
action.add(viewStorage.getActionToChangeEncryptionKey());
1313+
} catch (CouchbaseLiteException ex) {
1314+
Log.w(TAG, "Error in getViewStorage() viewName=" + viewName, ex);
1315+
}
13161316
}
13171317

13181318
// Re-key the database:

src/main/java/com/couchbase/lite/store/ForestDBViewStore.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939
import com.couchbase.lite.support.action.ActionException;
4040
import com.couchbase.lite.support.security.SymmetricKey;
4141
import com.couchbase.lite.util.Log;
42-
import com.fasterxml.jackson.core.JsonProcessingException;
4342

4443
import java.io.File;
4544
import java.io.IOException;
45+
import java.io.UnsupportedEncodingException;
4646
import java.math.BigInteger;
47+
import java.net.URLDecoder;
48+
import java.net.URLEncoder;
4749
import java.util.ArrayList;
4850
import java.util.HashMap;
4951
import java.util.List;
@@ -699,23 +701,53 @@ public void execute() throws ActionException {
699701
// Internal (Protected/Private) Static Methods
700702
///////////////////////////////////////////////////////////////////////////
701703

702-
protected static String fileNameToViewName(String fileName) {
704+
protected static String fileNameToViewName(String fileName) throws CouchbaseLiteException {
703705
if (!fileName.endsWith(kViewIndexPathExtension))
704-
return null;
706+
throw new CouchbaseLiteException(Status.BAD_PARAM);
705707
if (fileName.startsWith("."))
706-
return null;
708+
throw new CouchbaseLiteException(Status.BAD_PARAM);
709+
707710
String viewName = fileName.substring(0, fileName.indexOf("."));
708-
viewName = viewName.replaceAll(":", "/");
711+
viewName = isWindows() ? unescapeViewNameWindows(viewName) : viewName.replaceAll(":", "/");
709712
return viewName;
710713
}
711714

712-
private static String viewNameToFileName(String viewName) {
715+
private static String viewNameToFileName(String viewName) throws CouchbaseLiteException {
713716
if (viewName.startsWith(".") || viewName.indexOf(":") > 0)
714-
return null;
715-
viewName = viewName.replaceAll("/", ":");
717+
throw new CouchbaseLiteException(Status.BAD_PARAM);
718+
719+
viewName = isWindows() ? escapeViewNameWindows(viewName) : viewName.replaceAll("/", ":");
720+
716721
return viewName + "." + kViewIndexPathExtension;
717722
}
718723

724+
private static String escapeViewNameWindows(String viewName)throws CouchbaseLiteException {
725+
try {
726+
viewName = URLEncoder.encode(viewName, "UTF-8");
727+
} catch (UnsupportedEncodingException e) {
728+
Log.w(TAG, "Error to url decode: " + viewName, e);
729+
throw new CouchbaseLiteException(e, Status.BAD_ENCODING);
730+
}
731+
viewName = viewName.replaceAll("\\*", "%2A");
732+
return viewName;
733+
}
734+
735+
private static String unescapeViewNameWindows(String viewName)throws CouchbaseLiteException {
736+
viewName = viewName.replaceAll("%2A", "*");
737+
try {
738+
viewName = URLDecoder.decode(viewName, "UTF-8");
739+
} catch (UnsupportedEncodingException e) {
740+
Log.w(TAG, "Error to url decode: " + viewName, e);
741+
throw new CouchbaseLiteException(e, Status.BAD_ENCODING);
742+
}
743+
return viewName;
744+
}
745+
private static String OS = System.getProperty("os.name").toLowerCase();
746+
747+
private static boolean isWindows(){
748+
return (OS.indexOf("win") >= 0);
749+
}
750+
719751
/**
720752
* Are key1 and key2 grouped together at this groupLevel?
721753
*/

0 commit comments

Comments
 (0)