Exemple #1
0
 /**
  * @param userId
  * @return true if successful sets user context, false otherwise
  */
 public boolean setUser(String userId) {
   if (userId != null && httpRequestChecker != null) {
     UserInfo userInfo = userMap.get(userId);
     if (userInfo == null) {
       String userEmail = getUserProperty(userId, "email");
       if (userEmail == null) {
         log.warn("user " + userId + " email not found in config");
         return false;
       }
       String password = getUserProperty(userId, "password");
       if (password == null) {
         log.warn("user " + userId + " password not found in config");
         return false;
       }
       userInfo = new UserInfo(userEmail, password);
       userMap.put(userId, userInfo);
     }
     try {
       // ? currentUser = null; // null or keep last value ??
       httpRequestChecker.setUser(this, userId, userInfo.email, userInfo.password);
       if (userInfo.email.equals(httpRequestChecker.getCurrentUser(this))) {
         currentUser = userId;
         return true;
       }
     } catch (IllegalStateException e) {
       log.warn("failed to set user", e);
       return false;
     } catch (IllegalArgumentException e) {
       log.warn("failed to set user", e);
       return false;
     }
   }
   return false;
 }
Exemple #2
0
  /**
   * Load configuration
   *
   * @param config
   * @throws IllegalArgumentException if any required configuration element is invalid or missing
   * @throws IllegalStateException if authentication fails
   * @exception NumberFormatException if any required string property does not contain a parsable
   *     integer.
   */
  public void load(XMLConfiguration config) {
    this.config = config;
    final String url = config.getString("baseURL");
    // see http://commons.apache.org/configuration/userguide/howto_xml.html
    if (StringUtils.isBlank(url)) {
      // TODO: if any tests don't require baseURL then may this optional and have tests check and
      // mark status = SKIPPED
      throw new IllegalArgumentException("baseURL property must be defined");
    } else {
      try {
        baseURL = new URI(url);
        if (baseURL.getQuery() != null) {
          log.error("6.1.1 baseURL MUST NOT contain a query component, baseURL=" + baseURL);
        }
        baseUrlString = baseURL.toASCIIString();
        final String baseRawString = baseURL.toString();
        if (!baseUrlString.equals(baseRawString)) {
          log.warn(
              "baseURL appears to have non-ASCII characters and comparisons using URL may have problems");
          log.debug("baseURL ASCII String=" + baseUrlString);
          log.debug("baseURL raw String=" + baseRawString);
        }
        if (!baseUrlString.endsWith("/")) baseUrlString += '/'; // end the baseURL with slash
      } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
      }
    }

    // setup HTTP proxy if required
    String proxyHost = config.getString("proxy.host");
    String proxyPort = config.getString("proxy.port");
    if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort)) {
      proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort), "http");
    }

    // load optional HttpRequestChecker for HTTP request handling
    final String httpRequestCheckerClass = config.getString("HttpRequestChecker");
    if (StringUtils.isNotBlank(httpRequestCheckerClass)) {
      try {
        Class httpClass = Class.forName(httpRequestCheckerClass);
        httpRequestChecker = (HttpRequestChecker) httpClass.newInstance();
        httpRequestChecker.setup(this);
        if (httpRequestChecker.getCurrentUser(this) != null) currentUser = DEFAULT_USER;
      } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException(e);
      } catch (InstantiationException e) {
        throw new IllegalArgumentException(e);
      } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e);
      }
    }
  }
Exemple #3
0
 /**
  * Wrap <tt>HttpClient.execute()</tt> to pre/post-test HTTP requests for any server specific
  * implementation handling such as authentication.
  *
  * @param client the HttpClient, must never be null
  * @param request the request to execute, must never be null
  * @return the response to the request.
  * @throws IOException in case of a problem or the connection was aborted
  * @throws ClientProtocolException in case of an http protocol error
  */
 public HttpResponse executeRequest(HttpClient client, HttpRequestBase request)
     throws IOException {
   if (httpRequestChecker != null) {
     return httpRequestChecker.executeRequest(this, client, request);
   } else {
     return client.execute(request);
   }
 }