示例#1
0
 void removeLockHolder(String name) {
   if (children != null) {
     Iterator<CommonResource> it = children.iterator();
     while (it.hasNext()) {
       Resource r = it.next();
       if (r instanceof LockNullResource && r.getName().equals(name)) {
         it.remove();
       }
     }
   }
 }
 @Override
 public void respondMethodNotAllowed(Resource res, Response response, Request request) {
   log.debug(
       "method not allowed. handler: "
           + this.getClass().getName()
           + " resource: "
           + res.getClass().getName());
   response.setStatus(Response.Status.SC_METHOD_NOT_ALLOWED);
   contentGenerator.generate(res, request, response, Status.SC_METHOD_NOT_ALLOWED);
 }
示例#3
0
  @Override
  public void process(HttpManager manager, Request request, Response response)
      throws NotAuthorizedException, BadRequestException {
    if (!handlerHelper.checkExpects(responseHandler, request, response)) {
      return;
    }

    String host = request.getHostHeader();
    String url = HttpManager.decodeUrl(request.getAbsolutePath());

    // Find a resource if it exists
    Resource r = manager.getResourceFactory().getResource(host, url);
    if (r != null) {
      log.debug("locking existing resource: " + r.getName());
      processExistingResource(manager, request, response, r);
    } else {
      log.debug("lock target doesnt exist, attempting lock null..");
      processNonExistingResource(manager, request, response, host, url);
    }
  }
示例#4
0
  @Override
  public Resource child(String childName) throws NotAuthorizedException, BadRequestException {
    if (children == null) {
      // attempt to locate singly, ie without loading entire list of children
      // first check if it has already been loaded singly
      if (singlyLoadedChildItems != null && singlyLoadedChildItems.hasChild(childName)) {
        return singlyLoadedChildItems.get(childName);
      }
      // try to load singly using ChildOf annotation, if present
      // childTriValue can be null, the child source object, or a special value indicating no search
      Object childTriValue = annoFactory.childOfAnnotationHandler.execute(this, childName);
      if (childTriValue == null) {
        // return null; // definitely not found

        // well, actually. ChildOf can just apply to a certain sort of child, so if its not found
        // that doesnt mean that there might not be some othe child
        // so we can't assume in any circumstance that a null means not found. Must always fall
        // through to ChildrenOf
      } else if (childTriValue.equals(ChildOfAnnotationHandler.NOT_ATTEMPTED)) {
        // there is no ChildOf method, so fall through to iterating over all children
      } else {
        // got one!
        AnnoResource r = (AnnoResource) childTriValue;
        if (singlyLoadedChildItems == null) {
          singlyLoadedChildItems = new ResourceList();
        }
        singlyLoadedChildItems.add(r);
        return r;
      }
    }

    for (Resource r : getChildren()) {
      if (r.getName().equals(childName)) {
        return r;
      }
    }
    return null;
  }
  @Override
  public void respondContent(
      Resource resource, Response response, Request request, Map<String, String> params)
      throws NotAuthorizedException, BadRequestException, NotFoundException {
    log.debug("respondContent: " + resource.getClass());
    Auth auth = request.getAuthorization();
    setRespondContentCommonHeaders(response, resource, auth);
    if (resource instanceof GetableResource) {
      GetableResource gr = (GetableResource) resource;
      String acc = request.getAcceptHeader();
      String ct = gr.getContentType(acc);
      if (ct != null) {
        ct = pickBestContentType(ct);
        response.setContentTypeHeader(ct);
      }
      cacheControlHelper.setCacheControl(gr, response, request.getAuthorization());

      Long contentLength = gr.getContentLength();
      Boolean doBuffering = null;
      if (resource instanceof BufferingControlResource) {
        BufferingControlResource bcr = (BufferingControlResource) resource;
        doBuffering = bcr.isBufferingRequired();
      }
      if (doBuffering == null) {
        if (buffering == null || buffering == BUFFERING.whenNeeded) {
          doBuffering =
              (contentLength
                  == null); // if no content length then we buffer content to find content length
        } else {
          doBuffering =
              (buffering
                  == BUFFERING
                      .always); // if not null or whenNeeded then buffering is explicitly enabled or
                                // disabled
        }
      }
      if (!doBuffering) {
        log.trace("sending content with known content length: " + contentLength);
        if (contentLength != null) {
          response.setContentLengthHeader(contentLength);
        }
        response.setEntity(new GetableResourceEntity(gr, params, ct));
      } else {
        BufferingGetableResourceEntity e =
            new BufferingGetableResourceEntity(gr, params, ct, contentLength, getMaxMemorySize());
        response.setEntity(e);
      }
    }
  }
 /**
  * The authentication result is written to a request attribute called "loginResult".
  *
  * <p>Its value is "true" if login succeeded and "false" if not. Note that a successful login does
  * not ensure that that authorisation will succeed.
  *
  * <p>If rendering a login page based on authentication and authorisation you should also look at
  * the "authReason" attribute set by the LoginResponseHandler which gives the reason for an
  * authorisation failure
  *
  * @param resource
  * @param request
  * @return
  */
 @Override
 public Object authenticate(Resource resource, Request request) {
   String userName = request.getParams().get(userNameParam);
   String pwd = request.getParams().get(passwordParam);
   Object o = resource.authenticate(userName, pwd);
   // set a request attribute that can be used when rendering
   if (o == null) {
     log.trace("Form authentication failed");
     request.getAttributes().put("loginResult", Boolean.FALSE);
   } else {
     log.trace("Form authentication succeeded");
     request.getAttributes().put("loginResult", Boolean.TRUE);
   }
   return o;
 }
 /**
  * The modified date response header is used by the client for content caching. It seems obvious
  * that if we have a modified date on the resource we should set it. BUT, because of the
  * interaction with max-age we should always set it to the current date if we have max-age The
  * problem, is that if we find that a condition GET has an expired mod-date (based on maxAge) then
  * we want to respond with content (even if our mod-date hasnt changed. But if we use the actual
  * mod-date in that case, then the browser will continue to use the old mod-date, so will forever
  * more respond with content. So we send a mod-date of now to ensure that future requests will be
  * given a 304 not modified.*
  *
  * @param response
  * @param resource
  * @param auth
  */
 public static void setModifiedDate(Response response, Resource resource, Auth auth) {
   Date modDate = resource.getModifiedDate();
   if (modDate != null) {
     // HACH - see if this helps IE
     response.setLastModifiedHeader(modDate);
     //            if (resource instanceof GetableResource) {
     //                GetableResource gr = (GetableResource) resource;
     //                Long maxAge = gr.getMaxAgeSeconds(auth);
     //                if (maxAge != null && maxAge > 0) {
     //                    log.trace("setModifiedDate: has a modified date and a positive maxAge,
     // so adjust modDate");
     //                    long tm = System.currentTimeMillis() - 60000; // modified 1 minute ago
     //                    modDate = new Date(tm); // have max-age, so use current date
     //                }
     //            }
     //            response.setLastModifiedHeader(modDate);
   }
 }
  @Override
  public List<QName> getResourceTypes(Resource r) {
    if (log.isTraceEnabled()) {
      log.trace("getResourceTypes:" + r.getClass().getCanonicalName());
    }
    QName qn, qn2;
    List<QName> list = wrapped.getResourceTypes(r);

    if (r instanceof AddressBookResource) {
      log.trace("getResourceTypes: is a AddressBookResource");
      qn = new QName(CardDavProtocol.CARDDAV_NS, "addressbook");
      qn2 = new QName(CardDavProtocol.CARDDAV_NS, "directory");
      if (list == null) {
        list = new ArrayList<QName>();
      }
      if (r instanceof AddressBookDirectoryResource) {
        list.add(qn2);
      }
      list.add(qn);
    }
    return list;
  }