Example #1
0
  /**
   * parameter와 queryString 를 가져온다.
   *
   * @param method
   * @return
   */
  private String getHttpInfoDumy(HttpMethod method) {
    NameValuePair[] params = null;
    String methodType = "GET";
    String reqBody = null;
    if (method instanceof PostMethod) {
      params = ((PostMethod) method).getParameters();
      methodType = "POST";
      StringRequestEntity sre = (StringRequestEntity) ((PostMethod) method).getRequestEntity();
      reqBody = sre.getContent();
    }

    StringBuffer sb = new StringBuffer();

    try {
      sb.append("#### getHttpInfoDumy ####");
      sb.append("\n## " + methodType + " [" + method.getURI() + "], hscd[" + this.hashCode() + "]");
    } catch (URIException e) {
      sb.append("\n## getParamsQueryStr- URIException " + e.getMessage() + "]");
      return sb.toString();
    }

    if (method.getQueryString() != null && method.getQueryString().length() > 0)
      sb.append("\n" + "## queryString[" + method.getQueryString() + "]");

    if (params != null) {
      for (int i = 0; i < params.length; i++) {
        NameValuePair param = params[i];
        sb.append(
            "\n"
                + "## POST body param["
                + i
                + "], name["
                + param.getName()
                + "], value["
                + param.getValue()
                + "]");
      }
    }

    if (reqBody != null) {
      sb.append("\n" + "## POST body String [" + reqBody + "]");
    }

    sb.append("\n##########");

    return sb.toString();
  }
Example #2
0
 // ------------- log methods ----------------//
 private void logHttpGetRequest(HttpMethod method) {
   try {
     if (logger.isDebugEnabled()) {
       logger.debug("/n/n============= HTTP Request Start =============");
       logger.debug("HTTP Get Request URL ==>/n" + method.getURI().toString());
       logger.debug("HTTP Get Request Headers ==>/n" + getHttpRequestHeader(method));
       logger.debug("HTTP Get Request Cookies ==>/n" + getHttpCookie());
       logger.debug("HTTP Get Request QueryString ==>/n" + method.getQueryString());
       logger.debug("============= HTTP Request End =============/n/n");
     }
   } catch (URIException e) {
     logger.error(e);
   }
 }
Example #3
0
 public int executeMethod(
     HostConfiguration hostconfig, final HttpMethod httpMethod, final HttpState state)
     throws IOException, HttpException {
   httpMethod.setQueryString(appendParams(httpMethod.getQueryString()));
   return super.executeMethod(hostconfig, httpMethod, state);
 }
  protected byte[] doPreemptiveAuthentication(
      HttpClient client, HttpMethod method, RenderRequest request, RenderResponse response) {
    byte[] result = super.doPreemptiveAuthentication(client, method, request, response);
    if (result != null) {
      // already handled
      return result;
    }

    // System.out.println("SSOWebContentPortlet.doPreemptiveAuthentication...");

    PortletPreferences prefs = request.getPreferences();
    String type = getSingleSignOnAuthType(prefs);

    if (type.equalsIgnoreCase(SSO_TYPE_BASIC_PREEMPTIVE)) {
      // Preemptive, basic authentication
      String userName = (String) request.getAttribute(SSO_REQUEST_ATTRIBUTE_USERNAME);
      if (userName == null) userName = "";
      String password = (String) request.getAttribute(SSO_REQUEST_ATTRIBUTE_PASSWORD);
      if (password == null) password = "";

      // System.out.println("...performing preemptive basic authentication with userName:
      // "+userName+", and password: "******"");
        if (formAction == null || formAction.length() == 0) {
          log.warn(
              "sso.type specified as 'form', but no: "
                  + SSO_TYPE_FORM_ACTION_URL
                  + ", action was specified - unable to preemptively authenticate by form.");
          return null;
        }
        String userNameField = prefs.getValue(SSO_TYPE_FORM_USERNAME_FIELD, "");
        if (userNameField == null || userNameField.length() == 0) {
          log.warn(
              "sso.type specified as 'form', but no: "
                  + SSO_TYPE_FORM_USERNAME_FIELD
                  + ", username field was specified - unable to preemptively authenticate by form.");
          return null;
        }
        String passwordField = prefs.getValue(SSO_TYPE_FORM_PASSWORD_FIELD, "password");
        if (passwordField == null || passwordField.length() == 0) {
          log.warn(
              "sso.type specified as 'form', but no: "
                  + SSO_TYPE_FORM_PASSWORD_FIELD
                  + ", password field was specified - unable to preemptively authenticate by form.");
          return null;
        }

        String userName = (String) request.getAttribute(SSO_REQUEST_ATTRIBUTE_USERNAME);
        if (userName == null) userName = "";
        String password = (String) request.getAttribute(SSO_REQUEST_ATTRIBUTE_PASSWORD);
        if (password == null) password = "";

        // get submit method
        int i = type.indexOf('.');
        boolean isPost =
            i > 0
                ? type.substring(i + 1).equalsIgnoreCase("post")
                : true; // default to post, since it is a form

        // get parameter map
        HashMap formParams = new HashMap();
        formParams.put(userNameField, new String[] {userName});
        formParams.put(passwordField, new String[] {password});
        String formArgs = prefs.getValue(SSO_TYPE_FORM_ACTION_ARGS, "");
        if (formArgs != null && formArgs.length() > 0) {
          StringTokenizer iter = new StringTokenizer(formArgs, ";");
          while (iter.hasMoreTokens()) {
            String pair = iter.nextToken();
            i = pair.indexOf('=');
            if (i > 0) formParams.put(pair.substring(0, i), new String[] {pair.substring(i + 1)});
          }
        }

        // resuse client - in case new cookies get set - but create a new method (for the
        // formAction)
        String formMethod = (isPost) ? FORM_POST_METHOD : FORM_GET_METHOD;
        method =
            getHttpMethod(
                client,
                getURLSource(formAction, formParams, request, response),
                formParams,
                formMethod,
                request);
        // System.out.println("...posting credentials");
        result = doHttpWebContent(client, method, 0, request, response);
        // System.out.println("Result of attempted authorization: "+success);
        PortletMessaging.publish(request, FORM_AUTH_STATE, Boolean.valueOf(result != null));
        return result;
      } catch (Exception ex) {
        // bad
        log.error("Form-based authentication failed", ex);
      }
    } else if (type.equalsIgnoreCase(SSO_TYPE_URL) || type.equalsIgnoreCase(SSO_TYPE_URL_BASE64)) {
      // set user name and password parameters in the HttpMethod
      String userNameParam = prefs.getValue(SSO_TYPE_URL_USERNAME_PARAM, "");
      if (userNameParam == null || userNameParam.length() == 0) {
        log.warn(
            "sso.type specified as 'url', but no: "
                + SSO_TYPE_URL_USERNAME_PARAM
                + ", username parameter was specified - unable to preemptively authenticate by URL.");
        return null;
      }
      String passwordParam = prefs.getValue(SSO_TYPE_URL_PASSWORD_PARAM, "");
      if (passwordParam == null || passwordParam.length() == 0) {
        log.warn(
            "sso.type specified as 'url', but no: "
                + SSO_TYPE_URL_PASSWORD_PARAM
                + ", password parameter was specified - unable to preemptively authenticate by URL.");
        return null;
      }
      String userName = (String) request.getAttribute(SSO_REQUEST_ATTRIBUTE_USERNAME);
      if (userName == null) userName = "";
      String password = (String) request.getAttribute(SSO_REQUEST_ATTRIBUTE_PASSWORD);
      if (password == null) password = "";
      if (type.equalsIgnoreCase(SSO_TYPE_URL_BASE64)) {
        Base64 encoder = new Base64();
        userName = new String(encoder.encode(userName.getBytes()));
        password = new String(encoder.encode(password.getBytes()));
      }

      // GET and POST accept args differently
      if (method instanceof PostMethod) {
        // add POST data
        PostMethod postMethod = (PostMethod) method;
        postMethod.addParameter(userNameParam, userName);
        postMethod.addParameter(passwordParam, password);
      } else {
        // augment GET query string
        NameValuePair[] authPairs =
            new NameValuePair[] {
              new NameValuePair(userNameParam, userName), new NameValuePair(passwordParam, password)
            };
        String existingQuery = method.getQueryString();
        method.setQueryString(authPairs);
        if (existingQuery != null && existingQuery.length() > 0) {
          // augment existing query with new auth query
          existingQuery = existingQuery + '&' + method.getQueryString();
          method.setQueryString(existingQuery);
        }
      }

      return result;
    }
    // else System.out.println("...sso.type: "+type+", no pre-emptive authentication");

    // not handled
    return null;
  }