forked from lookfirst/sardine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic lock handling. Should be extended to include If header in P…
…UT, POST, PROPPATCH, MOVE, DELETE, or MKCOL on the locked resource.
- Loading branch information
dkocher@sudo.ch
committed
May 30, 2011
1 parent
40fa969
commit c3acbdd
Showing
6 changed files
with
362 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/com/googlecode/sardine/impl/handler/LockResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2009-2011 Jon Stevens et al. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.googlecode.sardine.impl.handler; | ||
|
||
import com.googlecode.sardine.impl.SardineException; | ||
import com.googlecode.sardine.model.Prop; | ||
import com.googlecode.sardine.util.SardineUtil; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.StatusLine; | ||
|
||
import javax.xml.bind.JAXBException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* @version $Id$ | ||
*/ | ||
public final class LockResponseHandler extends ValidatingResponseHandler<String> | ||
{ | ||
public String handleResponse(HttpResponse response) throws IOException | ||
{ | ||
super.validateResponse(response); | ||
|
||
// Process the response from the server. | ||
HttpEntity entity = response.getEntity(); | ||
if (entity == null) | ||
{ | ||
StatusLine statusLine = response.getStatusLine(); | ||
throw new SardineException("No entity found in response", statusLine.getStatusCode(), | ||
statusLine.getReasonPhrase()); | ||
} | ||
return this.getToken(entity.getContent()); | ||
} | ||
|
||
/** | ||
* Helper method for getting the Multistatus response processor. | ||
* | ||
* @param stream The input to read the status | ||
* @return Multistatus element parsed from the stream | ||
* @throws java.io.IOException When there is a JAXB error | ||
*/ | ||
protected String getToken(InputStream stream) | ||
throws IOException | ||
{ | ||
try | ||
{ | ||
final Prop prop = (Prop) SardineUtil.createUnmarshaller().unmarshal(stream); | ||
return prop.getLockdiscovery().getActivelock().iterator().next().getLocktoken().getHref().iterator().next(); | ||
} | ||
catch (JAXBException e) | ||
{ | ||
IOException failure = new IOException(e.getMessage()); | ||
// Backward compatibility | ||
failure.initCause(e); | ||
throw failure; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2009-2011 Jon Stevens et al. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.googlecode.sardine.impl.methods; | ||
|
||
import org.apache.http.HttpHeaders; | ||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* @version $Id:$ | ||
*/ | ||
public class HttpLock extends HttpEntityEnclosingRequestBase | ||
{ | ||
public static final String METHOD_NAME = "LOCK"; | ||
|
||
public HttpLock(String url) | ||
{ | ||
this(URI.create(url)); | ||
} | ||
|
||
public HttpLock(URI url) | ||
{ | ||
this.setURI(url); | ||
this.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml"); | ||
} | ||
|
||
@Override | ||
public String getMethod() | ||
{ | ||
return METHOD_NAME; | ||
} | ||
|
||
/** | ||
* The Depth header may be used with the <code>LOCK</code> method. Values other than <code>0</code> or <code>infinity</code> must not | ||
* be used with the Depth header on a <code>LOCK</code> method. All resources that support the <code>LOCK</code> | ||
* method must support the Depth header. | ||
* <p/> | ||
* If no Depth header is submitted on a <code>LOCK</code> request then the request must act as if | ||
* a <code>Depth:infinity</code> had been submitted. | ||
* | ||
* @param depth <code>"0"</code> or <code>"infinity"</code>. | ||
*/ | ||
public void setDepth(String depth) | ||
{ | ||
this.setHeader("Depth", depth); | ||
} | ||
|
||
/** | ||
* Clients may include Timeout headers in their LOCK requests. However, the server is not required to honor | ||
* or even consider these requests. | ||
*/ | ||
public void setTimeout(int seconds) | ||
{ | ||
this.setHeader("Timeout", "Second-" + seconds); | ||
} | ||
|
||
/** | ||
* Desires an infinite length lock. | ||
*/ | ||
public void setInfinite() | ||
{ | ||
this.setHeader("Timeout", "Infinite"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2009-2011 Jon Stevens et al. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.googlecode.sardine.impl.methods; | ||
|
||
import org.apache.http.HttpHeaders; | ||
import org.apache.http.client.methods.HttpRequestBase; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* @version $Id:$ | ||
*/ | ||
public class HttpUnlock extends HttpRequestBase | ||
{ | ||
public static final String METHOD_NAME = "UNLOCK"; | ||
|
||
/** | ||
* @param url | ||
* @param token The Lock-Token request header is used with the UNLOCK method to identify the lock to be removed. | ||
* The lock token in the Lock-Token request header must identify a lock that contains the resource | ||
* identified by Request-URI as a member. | ||
*/ | ||
public HttpUnlock(String url, String token) | ||
{ | ||
this(URI.create(url), token); | ||
} | ||
|
||
/** | ||
* @param url | ||
* @param token The Lock-Token request header is used with the UNLOCK method to identify the lock to be removed. | ||
* The lock token in the Lock-Token request header must identify a lock that contains the resource | ||
* identified by Request-URI as a member. | ||
*/ | ||
public HttpUnlock(URI url, String token) | ||
{ | ||
this.setURI(url); | ||
this.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml"); | ||
this.setHeader("Lock-Token", "<" + token + ">"); | ||
} | ||
|
||
@Override | ||
public String getMethod() | ||
{ | ||
return METHOD_NAME; | ||
} | ||
} |
Oops, something went wrong.