Extract NAMESPACE response parsing to NamespaceResponse
This commit is contained in:
parent
0dbfa5ab9d
commit
bc5069bfac
5 changed files with 196 additions and 31 deletions
|
@ -511,37 +511,17 @@ class ImapConnection {
|
|||
private void handleNamespace() throws IOException, MessagingException {
|
||||
List<ImapResponse> responses = executeSimpleCommand(Commands.NAMESPACE);
|
||||
|
||||
for (ImapResponse response : responses) {
|
||||
if (equalsIgnoreCase(response.get(0), Responses.NAMESPACE)) {
|
||||
if (K9MailLib.isDebug()) {
|
||||
Log.d(LOG_TAG, "Got NAMESPACE response " + response + " on " + getLogId());
|
||||
}
|
||||
NamespaceResponse namespaceResponse = NamespaceResponse.parse(responses);
|
||||
if (namespaceResponse != null) {
|
||||
String prefix = namespaceResponse.getPrefix();
|
||||
String hierarchyDelimiter = namespaceResponse.getHierarchyDelimiter();
|
||||
|
||||
Object personalNamespaces = response.get(1);
|
||||
if (personalNamespaces instanceof ImapList) {
|
||||
if (K9MailLib.isDebug()) {
|
||||
Log.d(LOG_TAG, "Got personal namespaces: " + personalNamespaces);
|
||||
}
|
||||
settings.setPathPrefix(prefix);
|
||||
settings.setPathDelimiter(hierarchyDelimiter);
|
||||
settings.setCombinedPrefix(null);
|
||||
|
||||
ImapList bracketed = (ImapList) personalNamespaces;
|
||||
Object firstNamespace = bracketed.get(0);
|
||||
|
||||
if (firstNamespace != null && firstNamespace instanceof ImapList) {
|
||||
if (K9MailLib.isDebug()) {
|
||||
Log.d(LOG_TAG, "Got first personal namespaces: " + firstNamespace);
|
||||
}
|
||||
|
||||
bracketed = (ImapList) firstNamespace;
|
||||
settings.setPathPrefix(bracketed.getString(0));
|
||||
settings.setPathDelimiter(bracketed.getString(1));
|
||||
settings.setCombinedPrefix(null);
|
||||
|
||||
if (K9MailLib.isDebug()) {
|
||||
Log.d(LOG_TAG, "Got path '" + settings.getPathPrefix() + "' and separator '" +
|
||||
settings.getPathDelimiter() + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (K9MailLib.isDebug()) {
|
||||
Log.d(LOG_TAG, "Got path '" + prefix + "' and separator '" + hierarchyDelimiter + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@ public class ImapList extends ArrayList<Object> {
|
|||
return (ImapList)get(index);
|
||||
}
|
||||
|
||||
public boolean isList(int index) {
|
||||
return inRange(index) && get(index) instanceof ImapList;
|
||||
}
|
||||
|
||||
public Object getObject(int index) {
|
||||
return get(index);
|
||||
}
|
||||
|
@ -32,6 +36,10 @@ public class ImapList extends ArrayList<Object> {
|
|||
return (String)get(index);
|
||||
}
|
||||
|
||||
public boolean isString(int index) {
|
||||
return inRange(index) && get(index) instanceof String;
|
||||
}
|
||||
|
||||
public long getLong(int index) {
|
||||
return Long.parseLong(getString(index));
|
||||
}
|
||||
|
@ -104,6 +112,10 @@ public class ImapList extends ArrayList<Object> {
|
|||
throw new IllegalArgumentException("getKeyIndex() only works for keys that are in the collection.");
|
||||
}
|
||||
|
||||
private boolean inRange(int index) {
|
||||
return index >= 0 && index < size();
|
||||
}
|
||||
|
||||
private Date parseDate(String value) throws ParseException {
|
||||
//TODO: clean this up a bit
|
||||
try {
|
||||
|
|
|
@ -9,7 +9,6 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fsck.k9.mail.K9MailLib;
|
||||
|
@ -213,7 +212,7 @@ class ImapResponseParser {
|
|||
|
||||
String rest = readStringUntilEndOfLine();
|
||||
|
||||
if (!TextUtils.isEmpty(rest)) {
|
||||
if (rest != null && !rest.isEmpty()) {
|
||||
// The rest is free-form text.
|
||||
parent.add(rest);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package com.fsck.k9.mail.store.imap;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.fsck.k9.mail.store.imap.ImapResponseParser.equalsIgnoreCase;
|
||||
|
||||
|
||||
class NamespaceResponse {
|
||||
private String prefix;
|
||||
private String hierarchyDelimiter;
|
||||
|
||||
|
||||
private NamespaceResponse(String prefix, String hierarchyDelimiter) {
|
||||
this.prefix = prefix;
|
||||
this.hierarchyDelimiter = hierarchyDelimiter;
|
||||
}
|
||||
|
||||
public static NamespaceResponse parse(List<ImapResponse> responses) {
|
||||
for (ImapResponse response : responses) {
|
||||
NamespaceResponse prefix = parse(response);
|
||||
if (prefix != null) {
|
||||
return prefix;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static NamespaceResponse parse(ImapResponse response) {
|
||||
if (response.size() < 4 || !equalsIgnoreCase(response.get(0), Responses.NAMESPACE)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!response.isList(1)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ImapList personalNamespaces = response.getList(1);
|
||||
if (!personalNamespaces.isList(0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ImapList firstPersonalNamespace = personalNamespaces.getList(0);
|
||||
if (!firstPersonalNamespace.isString(0) || !firstPersonalNamespace.isString(1)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String prefix = firstPersonalNamespace.getString(0);
|
||||
String hierarchyDelimiter = firstPersonalNamespace.getString(1);
|
||||
|
||||
return new NamespaceResponse(prefix, hierarchyDelimiter);
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public String getHierarchyDelimiter() {
|
||||
return hierarchyDelimiter;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
package com.fsck.k9.mail.store.imap;
|
||||
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.fsck.k9.mail.filter.PeekableInputStream;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
|
||||
public class NamespaceResponseTest {
|
||||
|
||||
@Test
|
||||
public void parse_withProperNamespaceResponse() throws Exception {
|
||||
NamespaceResponse result = parse("* NAMESPACE ((\"\" \"/\")) NIL NIL");
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("", result.getPrefix());
|
||||
assertEquals("/", result.getHierarchyDelimiter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withoutNamespaceResponse_shouldReturnNull() throws Exception {
|
||||
NamespaceResponse result = parse("* OK Some text here");
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withTooShortNamespaceResponse_shouldReturnNull() throws Exception {
|
||||
NamespaceResponse result = parse("* NAMESPACE NIL NIL");
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withPersonalNamespacesNotPresent_shouldReturnNull() throws Exception {
|
||||
NamespaceResponse result = parse("* NAMESPACE NIL NIL NIL");
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withEmptyListForPersonalNamespaces_shouldReturnNull() throws Exception {
|
||||
NamespaceResponse result = parse("* NAMESPACE () NIL NIL");
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withEmptyListForFirstPersonalNamespace_shouldReturnNull() throws Exception {
|
||||
NamespaceResponse result = parse("* NAMESPACE (()) NIL NIL");
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withIncompleteFirstPersonalNamespace_shouldReturnNull() throws Exception {
|
||||
NamespaceResponse result = parse("* NAMESPACE ((\"\")) NIL NIL");
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withEmptyResponseList() throws Exception {
|
||||
NamespaceResponse result = NamespaceResponse.parse(Collections.<ImapResponse>emptyList());
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withSingleItemInResponseList() throws Exception {
|
||||
ImapResponse imapResponse = createImapResponse("* NAMESPACE ((\"\" \"/\")) NIL NIL");
|
||||
NamespaceResponse result = NamespaceResponse.parse(Collections.singletonList(imapResponse));
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("", result.getPrefix());
|
||||
assertEquals("/", result.getHierarchyDelimiter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withResponseList() throws Exception {
|
||||
ImapResponse imapResponseOne = createImapResponse("* OK");
|
||||
ImapResponse imapResponseTwo = createImapResponse("* NAMESPACE ((\"INBOX\" \".\")) NIL NIL");
|
||||
NamespaceResponse result = NamespaceResponse.parse(Arrays.asList(imapResponseOne, imapResponseTwo));
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("INBOX", result.getPrefix());
|
||||
assertEquals(".", result.getHierarchyDelimiter());
|
||||
}
|
||||
|
||||
|
||||
private ImapResponse createImapResponse(String response) throws IOException {
|
||||
String input = response + "\r\n";
|
||||
PeekableInputStream inputStream = new PeekableInputStream(new ByteArrayInputStream(input.getBytes()));
|
||||
ImapResponseParser parser = new ImapResponseParser(inputStream);
|
||||
|
||||
return parser.readResponse();
|
||||
}
|
||||
|
||||
private NamespaceResponse parse(String response) throws IOException {
|
||||
ImapResponse imapResponse = createImapResponse(response);
|
||||
|
||||
return NamespaceResponse.parse(imapResponse);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue