import javax.xml.namespace.NamespaceContext;
import org.argeo.api.acr.ContentNotFoundException;
-import org.argeo.util.http.HttpHeader;
-import org.argeo.util.http.HttpMethod;
-import org.argeo.util.http.HttpResponseStatus;
-import org.argeo.util.http.HttpServerUtils;
+import org.argeo.cms.http.HttpHeader;
+import org.argeo.cms.http.HttpMethod;
+import org.argeo.cms.http.HttpServerUtils;
+import org.argeo.cms.http.HttpStatus;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
/**
* Centralise patterns which are not ACR specific. Not really meant as a
- * framework for building WebDav servers, but rather to make uppe-level of
+ * framework for building WebDav servers, but rather to make upper-level of
* ACR-specific code more readable and maintainable.
*/
public abstract class DavHttpHandler implements HttpHandler {
- private NamespaceContext namespaceContext;
@Override
public void handle(HttpExchange exchange) throws IOException {
handleGET(exchange, subPath);
} else if (HttpMethod.OPTIONS.name().equals(method)) {
handleOPTIONS(exchange, subPath);
- exchange.sendResponseHeaders(HttpResponseStatus.NO_CONTENT.getCode(), -1);
+ exchange.sendResponseHeaders(HttpStatus.NO_CONTENT.getCode(), -1);
} else if (HttpMethod.PROPFIND.name().equals(method)) {
DavDepth depth = DavDepth.fromHttpExchange(exchange);
if (depth == null) {
try (InputStream in = exchange.getRequestBody()) {
davPropfind = DavPropfind.load(depth, in);
}
- MultiStatusWriter multiStatusWriter = new MultiStatusWriter();
+ MultiStatusWriter multiStatusWriter = new MultiStatusWriter(exchange.getProtocol());
CompletableFuture<Void> published = handlePROPFIND(exchange, subPath, davPropfind, multiStatusWriter);
- exchange.sendResponseHeaders(HttpResponseStatus.MULTI_STATUS.getCode(), 0l);
+ exchange.sendResponseHeaders(HttpStatus.MULTI_STATUS.getCode(), 0l);
+ NamespaceContext namespaceContext = getNamespaceContext(exchange, subPath);
try (OutputStream out = exchange.getResponseBody()) {
multiStatusWriter.process(namespaceContext, out, published.minimalCompletionStage(),
davPropfind.isPropname());
throw new IllegalArgumentException("Unsupported method " + method);
}
} catch (ContentNotFoundException e) {
- exchange.sendResponseHeaders(HttpResponseStatus.NOT_FOUND.getCode(), -1);
+ exchange.sendResponseHeaders(HttpStatus.NOT_FOUND.getCode(), -1);
}
// TODO return a structured error message
catch (UnsupportedOperationException e) {
- exchange.sendResponseHeaders(HttpResponseStatus.NOT_IMPLEMENTED.getCode(), -1);
+ e.printStackTrace();
+ exchange.sendResponseHeaders(HttpStatus.NOT_IMPLEMENTED.getCode(), -1);
} catch (Exception e) {
- exchange.sendResponseHeaders(HttpResponseStatus.INTERNAL_SERVER_ERROR.getCode(), -1);
+ exchange.sendResponseHeaders(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), -1);
}
}
+ protected abstract NamespaceContext getNamespaceContext(HttpExchange httpExchange, String path);
+
protected abstract CompletableFuture<Void> handlePROPFIND(HttpExchange exchange, String path,
DavPropfind davPropfind, Consumer<DavResponse> consumer) throws IOException;
exchange.getResponseHeaders().add(HttpHeader.ALLOW.getHeaderName(), methods.toString());
}
- public void setNamespaceContext(NamespaceContext namespaceContext) {
- this.namespaceContext = namespaceContext;
- }
-
}