Example #1
0
 @Override
 public void setup(OperatorContext context) {
   url = prepareURL();
   client = new HttpClient();
   method = new GetMethod(url);
   DefaultHttpParams.getDefaultParams()
       .setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
 }
Example #2
0
 public static void main(String[] args) throws Exception {
   HttpClient client = new HttpClient();
   client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF8");
   // 原先采用自己合并COOKIE,但是出现问题,用此方法OK
   DefaultHttpParams.getDefaultParams()
       .setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
   client
       .getParams()
       .setParameter(
           HttpMethodParams.USER_AGENT,
           "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");
   client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
   String ajaxLoginUrl = Login(client, preLogin(client));
   String uniqueid = ajaxLogin(client, ajaxLoginUrl);
   getHomePage(client, homePageUrl + uniqueid);
 }
  /**
   * get grouper privileges lite web service with REST
   *
   * @param wsSampleRestType is the type of rest (xml, xhtml, etc)
   */
  @SuppressWarnings("deprecation")
  public static void getGrouperPrivilegesLite(WsSampleRestType wsSampleRestType) {

    try {

      HttpClient httpClient = new HttpClient();

      DefaultHttpParams.getDefaultParams()
          .setParameter(
              HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));

      // URL e.g. http://localhost:8093/grouper-ws/servicesRest/v1_3_000/...
      // NOTE: aStem:aGroup urlencoded substitutes %3A for a colon
      PostMethod method =
          new PostMethod(
              RestClientSettings.URL + "/" + RestClientSettings.VERSION + "/grouperPrivileges");

      httpClient.getParams().setAuthenticationPreemptive(true);
      Credentials defaultcreds =
          new UsernamePasswordCredentials(RestClientSettings.USER, RestClientSettings.PASS);

      // no keep alive so response if easier to indent for tests
      method.setRequestHeader("Connection", "close");

      // e.g. localhost and 8093
      httpClient
          .getState()
          .setCredentials(
              new AuthScope(RestClientSettings.HOST, RestClientSettings.PORT), defaultcreds);

      // Make the body of the request, in this case with beans and marshaling, but you can make
      // your request document in whatever language or way you want
      WsRestGetGrouperPrivilegesLiteRequest wsRestGetGrouperPrivilegesLiteRequest =
          new WsRestGetGrouperPrivilegesLiteRequest();

      // set the act as id
      wsRestGetGrouperPrivilegesLiteRequest.setActAsSubjectId("GrouperSystem");

      wsRestGetGrouperPrivilegesLiteRequest.setSubjectId("test.subject.0");
      wsRestGetGrouperPrivilegesLiteRequest.setGroupName("aStem:aGroup");

      wsRestGetGrouperPrivilegesLiteRequest.setPrivilegeType("access");
      wsRestGetGrouperPrivilegesLiteRequest.setPrivilegeName("admin");

      // get the xml / json / xhtml / paramString
      String requestDocument =
          wsSampleRestType
              .getWsLiteRequestContentType()
              .writeString(wsRestGetGrouperPrivilegesLiteRequest);

      // make sure right content type is in request (e.g. application/xhtml+xml
      String contentType = wsSampleRestType.getWsLiteRequestContentType().getContentType();

      method.setRequestEntity(new StringRequestEntity(requestDocument, contentType, "UTF-8"));

      httpClient.executeMethod(method);

      // make sure a request came back
      Header successHeader = method.getResponseHeader("X-Grouper-success");
      String successString = successHeader == null ? null : successHeader.getValue();
      if (StringUtils.isBlank(successString)) {
        throw new RuntimeException("Web service did not even respond!");
      }
      boolean success = "T".equals(successString);
      String resultCode = method.getResponseHeader("X-Grouper-resultCode").getValue();

      String response = RestClientSettings.responseBodyAsString(method);

      // convert to object (from xhtml, xml, json, etc)
      WsGetGrouperPrivilegesLiteResult wsGetGrouperPrivilegesLiteResult =
          (WsGetGrouperPrivilegesLiteResult)
              wsSampleRestType.getWsLiteResponseContentType().parseString(response);

      String resultMessage =
          wsGetGrouperPrivilegesLiteResult.getResultMetadata().getResultMessage();

      // see if request worked or not
      if (!success) {
        throw new RuntimeException(
            "Bad response from web service: resultCode: " + resultCode + ", " + resultMessage);
      }

      System.out.println(
          "Server version: "
              + wsGetGrouperPrivilegesLiteResult.getResponseMetadata().getServerVersion()
              + ", result code: "
              + resultCode
              + ", result message: "
              + resultMessage);

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }