Skip to content

Commit

Permalink
Use optString in CouchSessionImpl and guessing other places where it …
Browse files Browse the repository at this point in the history
…relies on null or is better than exception (#1191)
  • Loading branch information
alexgao1 authored Sep 26, 2023
1 parent 1e270a1 commit a6df67a
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ private JSONObject getChanges() throws Exception {
}

private void reportChanges(JSONObject changeObj) throws Exception {
String docId = changeObj.getString("id");
String docId = changeObj.optString("id", null);
boolean deleted = changeObj.optBoolean("deleted", false);
JSONArray changes = changeObj.getJSONArray("changes");
JSONObject change = changes.getJSONObject(0);
String rev = change.getString("rev");
String rev = change.optString("rev", null);

CouchDbChangeListener.Type type = CouchDbChangeListener.Type.DOC_UPDATED;
if( deleted ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public JSONObject uploadAttachment(
,long size
) throws Exception {

String docId = doc.getString("_id");
String docId = doc.optString("_id", null);
String path = URLEncoder.encode(docId,"UTF-8")
+ "/"
+ URLEncoder.encode(name,"UTF-8");
Expand Down Expand Up @@ -443,7 +443,7 @@ public String downloadAttachment(
,OutputStream os
) throws Exception {

String docId = doc.getString("_id");
String docId = doc.optString("_id", null);

return downloadAttachment(docId, name, os);
}
Expand Down Expand Up @@ -472,7 +472,7 @@ public String downloadAttachment(

@Override
public JSONObject deleteAttachment(JSONObject doc, String name) throws Exception {
String docId = doc.getString("_id");
String docId = doc.optString("_id", null);
String path = URLEncoder.encode(docId,"UTF-8")
+ "/"
+ URLEncoder.encode(name,"UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CouchAuthenticationContext getAuthenticationContext() throws Exception {
try {
JSONObject jsonUserCtx = response.getJSONObject("userCtx");
if( JSONSupport.containsKey(jsonUserCtx, "name") ) {
name = jsonUserCtx.getString("name");
name = jsonUserCtx.optString("name", null);
}
if( JSONSupport.containsKey(jsonUserCtx, "roles") ) {
JSONArray roleArray = jsonUserCtx.getJSONArray("roles");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static CouchAuthenticationContext authenticationContextFromDocument(JSONObject u
List<String> roles = new Vector<String>();
try {
if( JSONSupport.containsKey(userDoc, "name") ) {
name = userDoc.getString("name");
name = userDoc.optString("name", null);
}
if( JSONSupport.containsKey(userDoc, "roles") ) {
JSONArray roleArray = userDoc.getJSONArray("roles");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected String getStringAttribute(String key) throws Exception {

JSONObject json = getJson();
if( JSONSupport.containsKey(json, key) ) {
value = json.getString(key);
value = json.optString(key, null);
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected JSONObject getJson() throws Exception {

public String getDocId() throws Exception {
JSONObject doc = getJson();
return doc.getString(UploadConstants.KEY_DOC_ID);
return doc.optString(UploadConstants.KEY_DOC_ID, null);
}

public String getRevision() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public String getStringAttribute(String key) throws Exception {

JSONObject descriptionObj = getJson();
if( JSONSupport.containsKey(descriptionObj, key) ) {
value = descriptionObj.getString(key);
value = descriptionObj.optString(key, null);
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public String getStringAttribute(String key) throws Exception {

JSONObject descriptionObj = getJson();
if( JSONSupport.containsKey(descriptionObj, key) ) {
value = descriptionObj.getString(key);
value = descriptionObj.optString(key, null);
}

return value;
Expand Down

0 comments on commit a6df67a

Please sign in to comment.