package org.xbib.io.webdav.client.methods;

import org.xbib.io.webdav.api.DavConstants;
import org.xbib.io.webdav.common.CodedUrlHeader;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * Represents an HTTP UNLOCK request.
 *
 * @see <a href="http://webdav.org/specs/rfc4918.html#rfc.section.9.11">RFC 4918, Section 9.11</a>
 */
public class Unlock extends AbstractMethod {

    private final String lockToken;

    public Unlock(String lockToken) {
        this.lockToken = lockToken;
    }

    @Override
    public void accept(HttpRequest.Builder httpRequest) {
        super.accept(httpRequest);
        CodedUrlHeader lth = new CodedUrlHeader(DavConstants.HEADER_LOCK_TOKEN, lockToken);
        httpRequest.setHeader(lth.getHeaderName(), lth.getHeaderValue());
    }

    public String method() {
        return "UNLOCK";
    }

    @Override
    public boolean succeeded(HttpResponse<?> response) {
        return response.statusCode() == 200 || response.statusCode() == 204;
    }
}