Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
some fixes that get radicale *closer* to working with Flock.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhodey committed Sep 23, 2014
1 parent ccbc7f2 commit 8a358a2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void removeComponent(String path) throws DavException, IOException {
try {

client.execute(deleteMethod);
if (!deleteMethod.succeeded())
if (!deleteMethod.succeeded() && deleteMethod.getStatusCode() != DavServletResponse.SC_OK)
throw new DavException(deleteMethod.getStatusCode(), deleteMethod.getStatusText());

} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

import com.google.common.base.Optional;

import org.apache.commons.httpclient.Header;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavServletResponse;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.Status;
import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import org.apache.jackrabbit.webdav.property.DavProperty;
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
Expand Down Expand Up @@ -103,7 +105,42 @@ public List<String> getDavOptions() throws DavException, IOException {
return davOptions.get();
}

public abstract Optional<String> getCurrentUserPrincipal() throws IOException, DavException;
protected abstract String getWellKnownUri();

protected Optional<String> getCurrentUserPrincipalFromMultiStatus(MultiStatus multiStatus) {
for (MultiStatusResponse msResponse : multiStatus.getResponses()) {
DavPropertySet foundProperties = msResponse.getProperties(DavServletResponse.SC_OK);
DavProperty homeSetProperty = foundProperties.get(WebDavConstants.PROPERTY_NAME_CURRENT_USER_PRINCIPAL);

for (Status status : msResponse.getStatus()) {
if (status.getStatusCode() == DavServletResponse.SC_OK) {

if (homeSetProperty != null && homeSetProperty.getValue() instanceof ArrayList) {
for (Object child : (ArrayList<?>) homeSetProperty.getValue()) {
if (child instanceof Element) {
String currentUserPrincipalUri = ((Element) child).getTextContent();
if (!(currentUserPrincipalUri.endsWith("/")))
currentUserPrincipalUri = currentUserPrincipalUri.concat("/");

return Optional.of(currentUserPrincipalUri);
}
}
}

// Owncloud :(
else if (homeSetProperty != null && homeSetProperty.getValue() instanceof Element) {
String currentUserPrincipalUri = ((Element) homeSetProperty.getValue()).getTextContent();
if (!(currentUserPrincipalUri.endsWith("/")))
currentUserPrincipalUri = currentUserPrincipalUri.concat("/");

return Optional.of(currentUserPrincipalUri);
}
}
}
}

return Optional.absent();
}

protected Optional<String> getCurrentUserPrincipal(String propFindUri)
throws IOException, DavException
Expand All @@ -118,40 +155,41 @@ protected Optional<String> getCurrentUserPrincipal(String propFindUri)
try {

davClient.execute(propFindMethod);
return getCurrentUserPrincipalFromMultiStatus(propFindMethod.getResponseBodyAsMultiStatus());

MultiStatus multiStatus = propFindMethod.getResponseBodyAsMultiStatus();
MultiStatusResponse[] msResponses = multiStatus.getResponses();
} finally {
propFindMethod.releaseConnection();
}
}

for (MultiStatusResponse msResponse : msResponses) {
DavPropertySet foundProperties = msResponse.getProperties(DavServletResponse.SC_OK);
DavProperty homeSetProperty = foundProperties.get(WebDavConstants.PROPERTY_NAME_CURRENT_USER_PRINCIPAL);
public Optional<String> getCurrentUserPrincipal() throws DavException, IOException {
if (currentUserPrincipal.isPresent())
return currentUserPrincipal;

for (Status status : msResponse.getStatus()) {
if (status.getStatusCode() == DavServletResponse.SC_OK) {
DavPropertyNameSet props = new DavPropertyNameSet();
props.add(WebDavConstants.PROPERTY_NAME_CURRENT_USER_PRINCIPAL);

if (homeSetProperty != null && homeSetProperty.getValue() instanceof ArrayList) {
for (Object child : (ArrayList<?>) homeSetProperty.getValue()) {
if (child instanceof Element) {
String currentUserPrincipalUri = ((Element) child).getTextContent();
if (!(currentUserPrincipalUri.endsWith("/")))
currentUserPrincipalUri = currentUserPrincipalUri.concat("/");
String propFindUri = getHostHREF().concat(getWellKnownUri());
PropFindMethod propFindMethod = new PropFindMethod(propFindUri,
props,
PropFindMethod.DEPTH_0);

return Optional.of(currentUserPrincipalUri);
}
}
}
try {

// Owncloud :(
else if (homeSetProperty != null && homeSetProperty.getValue() instanceof Element) {
String currentUserPrincipalUri = ((Element) homeSetProperty.getValue()).getTextContent();
if (!(currentUserPrincipalUri.endsWith("/")))
currentUserPrincipalUri = currentUserPrincipalUri.concat("/");
getClient().execute(propFindMethod);
return getCurrentUserPrincipalFromMultiStatus(propFindMethod.getResponseBodyAsMultiStatus());

return Optional.of(currentUserPrincipalUri);
}
}
} catch (DavException e) {

if (e.getErrorCode() == DavServletResponse.SC_MOVED_PERMANENTLY) {
Header locationHeader = propFindMethod.getResponseHeader("location"); // TODO: find constant for this...
if (locationHeader.getValue() != null) {
currentUserPrincipal = getCurrentUserPrincipal(locationHeader.getValue());
return currentUserPrincipal;
}
}
else
throw e;

} finally {
propFindMethod.releaseConnection();
Expand All @@ -160,6 +198,22 @@ else if (homeSetProperty != null && homeSetProperty.getValue() instanceof Elemen
return Optional.absent();
}

@Override
public void removeCollection(String path) throws DavException, IOException {
DeleteMethod deleteMethod = new DeleteMethod(getHostHREF().concat(path));

try {

getClient().execute(deleteMethod);

if (!deleteMethod.succeeded() && deleteMethod.getStatusCode() != DavServletResponse.SC_OK)
throw new DavException(deleteMethod.getStatusCode(), deleteMethod.getStatusText());

} finally {
deleteMethod.releaseConnection();
}
}

@Override
public void closeHttpConnection() {
davClient.closeHttpConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.Status;
import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import org.apache.jackrabbit.webdav.property.DavProperty;
Expand Down Expand Up @@ -79,39 +78,8 @@ public CalDavStore(DavClient client,
}

@Override
public Optional<String> getCurrentUserPrincipal() throws DavException, IOException {
if (currentUserPrincipal.isPresent())
return currentUserPrincipal;

DavPropertyNameSet props = new DavPropertyNameSet();
props.add(WebDavConstants.PROPERTY_NAME_CURRENT_USER_PRINCIPAL);

String propFindUri = getHostHREF().concat("/.well-known/caldav");
PropFindMethod propFindMethod = new PropFindMethod(propFindUri,
props,
PropFindMethod.DEPTH_0);

try {

getClient().execute(propFindMethod);
propFindMethod.getResponseBodyAsMultiStatus();

} catch (DavException e) {

if (e.getErrorCode() == DavServletResponse.SC_MOVED_PERMANENTLY) {
Header locationHeader = propFindMethod.getResponseHeader("location"); // TODO: find constant for this...
if (locationHeader.getValue() != null) {
currentUserPrincipal = super.getCurrentUserPrincipal(locationHeader.getValue());
return currentUserPrincipal;
}
}
throw e;

} finally {
propFindMethod.releaseConnection();
}

return Optional.absent();
protected String getWellKnownUri() {
return "/.well-known/caldav";
}

public Optional<String> getCalendarHomeSet()
Expand Down Expand Up @@ -249,7 +217,8 @@ public static List<CalDavCollection> getCollectionsFromMultiStatusResponses(CalD
if (child instanceof Element) {
String localName = child.getLocalName();
if (localName != null) {
isCalendarCollection = localName.equals(CalDavConstants.RESOURCE_TYPE_CALENDAR) ||
isCalendarCollection = isCalendarCollection ||
localName.equals(CalDavConstants.RESOURCE_TYPE_CALENDAR) ||
localName.equals(CalDavConstants.RESOURCE_TYPE_CALENDAR_PROXY_READ) ||
localName.equals(CalDavConstants.RESOURCE_TYPE_CALENDAR_PROXY_WRITE);
}
Expand Down Expand Up @@ -283,17 +252,17 @@ public Optional<CalDavCollection> getCollection(String path) throws DavException

getClient().execute(propFindMethod);

MultiStatus multiStatus = propFindMethod.getResponseBodyAsMultiStatus();
MultiStatusResponse[] responses = multiStatus.getResponses();

MultiStatus multiStatus = propFindMethod.getResponseBodyAsMultiStatus();
MultiStatusResponse[] responses = multiStatus.getResponses();
List<CalDavCollection> returnedCollections = getCollectionsFromMultiStatusResponses(this, responses);

if (returnedCollections.size() == 0)
Optional.absent();
return Optional.absent();

return Optional.of(returnedCollections.get(0));

} catch (DavException e) {

if (e.getErrorCode() == DavServletResponse.SC_NOT_FOUND)
return Optional.absent();

Expand All @@ -316,8 +285,8 @@ public List<CalDavCollection> getCollections()
DavPropertyNameSet calendarProps = hack.getPropertyNamesForFetch();

PropFindMethod method = new PropFindMethod(getHostHREF().concat(calHomeSetUri.get()),
calendarProps,
PropFindMethod.DEPTH_1);
calendarProps,
PropFindMethod.DEPTH_1);

try {

Expand All @@ -332,21 +301,4 @@ public List<CalDavCollection> getCollections()
method.releaseConnection();
}
}

@Override
public void removeCollection(String path) throws DavException, IOException {
DeleteMethod deleteMethod = new DeleteMethod(getHostHREF().concat(path));

try {

getClient().execute(deleteMethod);

if (!deleteMethod.succeeded())
throw new DavException(deleteMethod.getStatusCode(), deleteMethod.getStatusText());

} finally {
deleteMethod.releaseConnection();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,40 +79,8 @@ public CardDavStore(DavClient client,
}

@Override
public Optional<String> getCurrentUserPrincipal() throws DavException, IOException {
if (currentUserPrincipal.isPresent())
return currentUserPrincipal;

DavPropertyNameSet props = new DavPropertyNameSet();
props.add(WebDavConstants.PROPERTY_NAME_CURRENT_USER_PRINCIPAL);

String propFindUri = getHostHREF().concat("/.well-known/carddav");
PropFindMethod propFindMethod = new PropFindMethod(propFindUri,
props,
PropFindMethod.DEPTH_0);

try {

getClient().execute(propFindMethod);
propFindMethod.getResponseBodyAsMultiStatus();

} catch (DavException e) {

if (e.getErrorCode() == DavServletResponse.SC_MOVED_PERMANENTLY) {
Header locationHeader = propFindMethod.getResponseHeader("location"); // TODO: find constant for this...
if (locationHeader.getValue() != null) {
currentUserPrincipal = super.getCurrentUserPrincipal(locationHeader.getValue());
return currentUserPrincipal;
}
}
else
throw e;

} finally {
propFindMethod.releaseConnection();
}

return Optional.absent();
protected String getWellKnownUri() {
return "/.well-known/carddav";
}

public Optional<String> getAddressbookHomeSet()
Expand Down Expand Up @@ -249,7 +217,8 @@ public static List<CardDavCollection> getCollectionsFromMultiStatusResponses(Car
if (child instanceof Element) {
String localName = child.getLocalName();
if (localName != null)
isAddressbookCollection = localName.equals(CardDavConstants.RESOURCE_TYPE_ADDRESSBOOK);
isAddressbookCollection = isAddressbookCollection ||
localName.equals(CardDavConstants.RESOURCE_TYPE_ADDRESSBOOK);
}
}
}
Expand Down Expand Up @@ -285,7 +254,7 @@ public Optional<CardDavCollection> getCollection(String path) throws DavExceptio
List<CardDavCollection> returnedCollections = getCollectionsFromMultiStatusResponses(this, responses);

if (returnedCollections.size() == 0)
Optional.absent();
return Optional.absent();

return Optional.of(returnedCollections.get(0));

Expand Down Expand Up @@ -330,20 +299,4 @@ public List<CardDavCollection> getCollections()
method.releaseConnection();
}
}

@Override
public void removeCollection(String path) throws DavException, IOException {
DeleteMethod deleteMethod = new DeleteMethod(getHostHREF().concat(path));

try {

getClient().execute(deleteMethod);

if (!deleteMethod.succeeded())
throw new DavException(deleteMethod.getStatusCode(), deleteMethod.getStatusText());

} finally {
deleteMethod.releaseConnection();
}
}
}

0 comments on commit 8a358a2

Please sign in to comment.