Esempio n. 1
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.");
    }
  }
 @Override
 protected TaskCreator getTaskCreator(Form form, Method method, boolean async, Reference reference)
     throws Exception {
   if (Method.PUT.equals(method)
       && (getRequest().getResourceRef().getQueryAsForm().getFirstValue("code") == null)) {
     throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid code");
   }
   return super.getTaskCreator(form, method, async, reference);
 }
 @Override
 protected ReadRegistration createUpdateQuery(
     Method method, Context context, Request request, Response response) throws ResourceException {
   Object key = request.getAttributes().get(UserDBResource.resourceKey);
   if (Method.POST.equals(method)) {
     if (key == null) return null; // post allowed only on /user level, not on /user/id
   } else if (Method.PUT.equals(method)) {
     return createQuery(context, request, response);
   }
   throw new ResourceException(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
 }
Esempio n. 4
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");
    }
  }