private Message moveResourceToCollection(
     final Resource source, final Resource destination, final boolean overwrite)
     throws IOException {
   if (overwrite) {
     destination.delete();
   }
   source.moveTo(destination);
   return overwrite ? successNoContent() : successCreated();
 }
  private Message propfind(Path path, Depth depth, ByteBuffer body) throws IOException {
    final Resource resource = resolveResource(path);

    final Iterable<QName> properties = getProperties(body);
    final List<RESPONSE> responses = propfind(properties, resource, depth);
    if (resource.exists()) {
      return successMultiStatus(multistatus(responses));
    } else {
      return clientErrorNotFound();
    }
  }
  private Message handleRequestWithIfMatch(Request request) throws IOException {
    final Path path = Path.fromString(request.uri().getPath());
    final Resource resource = resolveResource(path);

    final String ifMatch = getOnlyElement(request.headers().get(Header.IF_MATCH));

    if (ifMatch.equals("*") || !ifMatch.equals(resource.etag())) {
      return handleRequestWithoutIfMatch(request);
    } else {
      return Response.clientErrorPreconditionFailed();
    }
  }
  private Message handleRequestWithoutRange(Request request) {
    final Path path = Path.fromUri(request.uri());
    final Resource resource = resolveResource(path);

    try {
      return Response.build(SUCCESS_OK)
          .header(CONTENT_TYPE)
          .set(APPLICATION_OCTET_STREAM.toString())
          .setBody(resource.channel());
    } catch (IOException e) {
      return serverErrorInternal();
    }
  }
 private Response doGet(Environment env) {
   Path path = env.pathInfo();
   Resource resource = resolveResource(path);
   if (resource.exists()) {
     if (resource.isCollection()) {
       return clientErrorNotFound();
     } else {
       return getResource(request);
     }
   } else {
     return clientErrorNotFound();
   }
 }
  private Response doUnlock(Environment env) {
    final Resource resource = resolveResource(env.pathInfo());

    if (resource.exists()) {
      if (resource.isLocked()) {
        resource.unlock();
        return successNoContent();
      } else {
        return clientErrorLocked();
      }
    } else {
      return clientErrorNotFound();
    }
  }
  private Response doOptions(Environment env) {
    final Resource resource = resolveResource(env.pathInfo());

    Message response = Response.successNoContent();
    for (Method method : resource.supportedMethods()) {
      response.headers().put(ALLOW, method.name());
    }

    for (ComplianceClass value : resource.davOptions()) {
      response.headers().put(DAV, value.toString());
    }

    return response;
  }
  @PUT
  public Message put(@PathTranslated Path path, @Context Request request) throws IOException {
    final Resource resource = resolveResource(path);
    if (!resource.parent().exists()) {
      return clientErrorConflict();
    }

    if (resource.exists() && resource.isCollection()) {
      return clientErrorMethodNotAllowed();
    }

    ByteBuffer entity = request.buffer();
    resource.put(entity);
    return successCreated();
  }
  private Message handleRequestWithIfModifiedSince(final Request request) throws IOException {
    final Path path = Path.fromUri(request.uri());
    final Resource resource = resolveResource(path);

    try {
      String date = getOnlyElement(request.headers().get(IF_MODIFIED_SINCE));
      Date ifModifiedSince = new SimpleDateFormat().parse(date);
      if (!ifModifiedSince.after(new Date()) && !(resource.lastModified().after(new Date()))) {
        return redirectionNotModified();
      } else {
        return handleRequestWithoutIfModifiedSince(request);
      }
    } catch (ParseException e) {
      return handleRequestWithoutIfModifiedSince(request);
    }
  }
  @MKCOL
  public Message mkcol(@PathTranslated Path path, @Context Request request) {
    final Resource resource = resolveResource(path);

    if (request.hasBody()) {
      return clientErrorUnsupportedMediaType();
    } else {
      if (resource.exists()) {
        return clientErrorMethodNotAllowed();
      } else {
        if (resource.parent().exists()) {
          return (resource.mkcol() ? successCreated() : serverErrorInternal());
        } else {
          return clientErrorConflict();
        }
      }
    }
  }
  @LOCK
  public Message lock(@PathTranslated Path path, Depth depth) throws IOException {
    final Resource resource = resolveResource(path);

    if (resource.exists()) {
      final Lock lock = resource.lock(WRITE, EXCLUSIVE);

      final ACTIVE_LOCK activelock = activeLock(lock, depth, relativizeResource(resource));
      final Element lockDiscovery = lockDiscovery(activelock);
      final Element prop = prop(lockDiscovery);
      final Document doc = new Document(XML_DECLARATION, prop);
      return Response.build(SUCCESS_OK)
          .header(LOCK_TOKEN)
          .set("<" + lock.token() + ">")
          .setBody(Charset.defaultCharset().encode(XMLWriter.write(doc)));
    } else {
      return clientErrorNotFound();
    }
  }
  private final List<RESPONSE> propfind(
      final Iterable<QName> properties, final Resource resource, final Depth depth) {
    final List<RESPONSE> responses = new ArrayList<RESPONSE>();

    responses.add(
        response(href(relativizeResource(resource)), getProperties(resource, properties)));
    if (depth != ZERO) {
      for (final Resource child : resource.members()) {
        responses.addAll(propfind(properties, child, depth.decreaseDepth()));
      }
    }
    return responses;
  }
 private final List<PROPSTAT> getProperties(
     final Resource resource, final Iterable<QName> properties) {
   final List<PROPSTAT> propstats = new ArrayList<PROPSTAT>(2);
   final List<Element> foundProps = new ArrayList<Element>();
   final List<Element> notFoundProps = new ArrayList<Element>();
   for (final QName property : properties) {
     final Element prop = resource.getProperty(property);
     if (prop == null) {
       notFoundProps.add(new Element(property));
     } else {
       foundProps.add(prop);
     }
   }
   if (!foundProps.isEmpty()) {
     propstats.add(propertyStatus(prop(foundProps), Status.SUCCESS_OK));
   }
   if (!notFoundProps.isEmpty()) {
     propstats.add(propertyStatus(prop(notFoundProps), Status.CLIENT_ERROR_NOT_FOUND));
   }
   return propstats;
 }
  private Response doDelete(Environment env) {
    final Resource resource = resolveResource(env.pathInfo());

    //		if (fragment != null) {
    //			return clientErrorMethodNotAllowed();
    //		} else {
    if (resource.isLocked()) {
      return clientErrorLocked();
    }
    if (resource.exists()) {
      if (resource.isLocked()) {
        return clientErrorLocked();
      } else {
        if (resource.isCollection()) {
          return (resource.delete() ? successNoContent() : serverErrorInternal());
        } else {
          return (resource.delete() ? successNoContent() : serverErrorInternal());
        }
      }
    } else {
      return clientErrorNotFound();
    }
    //		}
  }
  @COPY
  public Message copy(
      @PathTranslated Path path, @Destination URI destinationUri, @Overwrite boolean overwrite)
      throws IOException {
    final Resource source = resolveResource(path);

    final Resource destination = resolveResource(Path.fromString(destinationUri.getPath()));

    if (destination.isLocked()) {
      return clientErrorLocked();
    }

    if (source.exists()) {
      if (source.isCollection()) { // source exists
        if (destination.exists()) { // source exists and is a collection
          if (destination.isCollection()) {
            return (overwrite
                ? copyCollectionToCollection(source, destination, overwrite)
                : clientErrorPreconditionFailed());
          } else {
            return (overwrite
                ? copyCollectionToResource(source, destination, overwrite)
                : clientErrorPreconditionFailed());
          }
        } else {
          return (destination.parent().exists()
              ? copyCollectionToCollection(source, destination, false)
              : clientErrorPreconditionFailed());
        }
      } else {
        if (destination.exists()) { // source exists
          if (destination.isCollection()) { // source exists,
            return (overwrite
                ? copyResourceToCollection(source, destination, overwrite)
                : clientErrorPreconditionFailed());
          } else {
            return (overwrite
                ? copyResourceToResource(source, destination, overwrite)
                : clientErrorPreconditionFailed());
          }
        } else {
          if (destination.parent().exists()) {
            return (overwrite
                ? clientErrorPreconditionFailed()
                : copyResourceToCollection(source, destination, overwrite));
          } else {
            return clientErrorConflict();
          }
        }
      }
    } else {
      return clientErrorNotFound();
    }
  }
 private Message moveCollectionToCollection(
     final Resource source, final Resource destination, final boolean overwrite)
     throws IOException {
   source.moveTo(destination);
   return successNoContent();
 }
 private Message copyResourceToResource(
     final Resource source, final Resource destination, final boolean overwrite)
     throws IOException {
   source.copyTo(destination);
   return successNoContent();
 }