@Override
  protected int beforeHandle(Request request, Response response) {
    Cookie cookie = request.getCookies().getFirst("Credentials");

    if (cookie != null) {
      // Extract the challenge response from the cookie
      String[] credentials = cookie.getValue().split("=");

      if (credentials.length == 2) {
        String identifier = credentials[0];
        String secret = credentials[1];
        request.setChallengeResponse(
            new ChallengeResponse(ChallengeScheme.HTTP_COOKIE, identifier, secret));
      }
    } else if (Method.POST.equals(request.getMethod())
        && request.getResourceRef().getQueryAsForm().getFirst("login") != null) {
      // Intercepting a login form
      Form credentials = new Form(request.getEntity());
      String identifier = credentials.getFirstValue("identifier");
      String secret = credentials.getFirstValue("secret");
      request.setChallengeResponse(
          new ChallengeResponse(ChallengeScheme.HTTP_COOKIE, identifier, secret));

      // Continue call processing to return the target representation if
      // authentication is successful or a new login page
      request.setMethod(Method.GET);
    }

    return super.beforeHandle(request, response);
  }
 @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);
 }
Beispiel #3
0
 @Override
 public boolean supports(final Method method) {
   return Method.POST.equals(method) || Method.GET.equals(method) || super.supports(method);
 }
Beispiel #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");
    }
  }