Example #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;
  }
Example #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.");
    }
  }
Example #3
0
  /**
   * Constructor.
   *
   * @param helper The parent HTTP client helper.
   * @param method The method name.
   * @param requestUri The request URI.
   * @param hasEntity Indicates if the call will have an entity to send to the server.
   * @throws IOException
   */
  public HttpMethodCall(
      HttpClientHelper helper, final String method, String requestUri, boolean hasEntity)
      throws IOException {
    super(helper, method, requestUri);
    this.clientHelper = helper;

    if (requestUri.startsWith("http")) {
      if (method.equalsIgnoreCase(Method.GET.getName())) {
        this.httpMethod = new GetMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.POST.getName())) {
        this.httpMethod = new PostMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.PUT.getName())) {
        this.httpMethod = new PutMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.HEAD.getName())) {
        this.httpMethod = new HeadMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.DELETE.getName())) {
        this.httpMethod = new DeleteMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.CONNECT.getName())) {
        final HostConfiguration host = new HostConfiguration();
        host.setHost(new URI(requestUri, false));
        this.httpMethod = new ConnectMethod(host);
      } else if (method.equalsIgnoreCase(Method.OPTIONS.getName())) {
        this.httpMethod = new OptionsMethod(requestUri);
      } else if (method.equalsIgnoreCase(Method.TRACE.getName())) {
        this.httpMethod = new TraceMethod(requestUri);
      } else {
        this.httpMethod =
            new EntityEnclosingMethod(requestUri) {
              @Override
              public String getName() {
                return method;
              }
            };
      }

      this.httpMethod.setFollowRedirects(this.clientHelper.isFollowRedirects());
      this.httpMethod.setDoAuthentication(false);

      if (this.clientHelper.getRetryHandler() != null) {
        try {
          this.httpMethod
              .getParams()
              .setParameter(
                  HttpMethodParams.RETRY_HANDLER,
                  Engine.loadClass(this.clientHelper.getRetryHandler()).newInstance());
        } catch (Exception e) {
          this.clientHelper
              .getLogger()
              .log(
                  Level.WARNING,
                  "An error occurred during the instantiation of the retry handler.",
                  e);
        }
      }

      this.responseHeadersAdded = false;
      setConfidential(
          this.httpMethod.getURI().getScheme().equalsIgnoreCase(Protocol.HTTPS.getSchemeName()));
    } else {
      throw new IllegalArgumentException("Only HTTP or HTTPS resource URIs are allowed here");
    }
  }