Exemplo n.º 1
0
  /**
   * Handles a call by first verifying the optional request conditions and continue the processing
   * if possible. Note that in order to evaluate those conditions, {@link #getInfo()} or {@link
   * #getInfo(Variant)} methods might be invoked.
   *
   * @return The response entity.
   * @throws ResourceException
   */
  protected Representation doConditionalHandle() throws ResourceException {
    Representation result = null;

    if (getConditions().hasSome()) {
      RepresentationInfo resultInfo = null;

      if (existing) {
        if (isNegotiated()) {
          resultInfo = doGetInfo(getPreferredVariant(getVariants(Method.GET)));
        } else {
          resultInfo = doGetInfo();
        }

        if (resultInfo == null) {
          if ((getStatus() == null)
              || (getStatus().isSuccess() && !Status.SUCCESS_NO_CONTENT.equals(getStatus()))) {
            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
          } else {
            // Keep the current status as the developer might prefer
            // a special status like 'method not authorized'.
          }
        } else {
          Status status = getConditions().getStatus(getMethod(), resultInfo);

          if (status != null) {
            setStatus(status);
          }
        }
      } else {
        Status status = getConditions().getStatus(getMethod(), resultInfo);

        if (status != null) {
          setStatus(status);
        }
      }

      if ((Method.GET.equals(getMethod()) || Method.HEAD.equals(getMethod()))
          && resultInfo instanceof Representation) {
        result = (Representation) resultInfo;
      } else if ((getStatus() != null) && getStatus().isSuccess()) {
        // Conditions were passed successfully, continue the normal
        // processing.
        if (isNegotiated()) {
          // Reset the list of variants, as the method differs.
          getVariants().clear();
          result = doNegotiatedHandle();
        } else {
          result = doHandle();
        }
      }
    } else {
      if (isNegotiated()) {
        result = doNegotiatedHandle();
      } else {
        result = doHandle();
      }
    }

    return result;
  }
Exemplo n.º 2
0
  /**
   * Handles a call for a local entity. By default, only GET and HEAD methods are implemented.
   *
   * @param request The request to handle.
   * @param response The response to update.
   * @param decodedPath The URL decoded entity path.
   */
  @Override
  protected void handleLocal(Request request, Response response, String decodedPath) {
    int spi = decodedPath.indexOf("!/");
    String fileUri;
    String entryName;
    if (spi != -1) {
      fileUri = decodedPath.substring(0, spi);
      entryName = decodedPath.substring(spi + 2);
    } else {
      fileUri = decodedPath;
      entryName = "";
    }

    LocalReference fileRef = new LocalReference(fileUri);
    if (Protocol.FILE.equals(fileRef.getSchemeProtocol())) {
      final File file = fileRef.getFile();
      if (Method.GET.equals(request.getMethod()) || Method.HEAD.equals(request.getMethod())) {
        handleGet(request, response, file, entryName, getMetadataService());
      } else if (Method.PUT.equals(request.getMethod())) {
        handlePut(request, response, file, entryName);
      } else {
        response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
        response.getAllowedMethods().add(Method.GET);
        response.getAllowedMethods().add(Method.HEAD);
        response.getAllowedMethods().add(Method.PUT);
      }
    } else {
      response.setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED, "Only works on local files.");
    }
  }