Package org.butor.sso.validator

Source Code of org.butor.sso.validator.HttpSSOValidator

/*
* Copyright (C) butor.com. All rights reserved.
*
* This software is published under the terms of the GNU Library General
* Public License (GNU LGPL), a copy of which has been included with this
* distribution in the LICENSE.txt file.
*/

package org.butor.sso.validator;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.butor.json.JsonHelper;
import org.butor.sso.DefaultSSOInfo;
import org.butor.sso.SSOConstants;
import org.butor.sso.SSOException;
import org.butor.sso.SSOInfo;
import org.butor.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
*TODO
*
* @author asawan
* @date 24-Jan-09
*/
public class HttpSSOValidator implements ISSOValidator {
  protected Logger _logger = LoggerFactory.getLogger(this.getClass());
  protected String ssoCheckUrl = null;
  private String signInUrl = null;
  private String sessionTimeoutUrl = null;
  private String sessionTimeoutStreamingUrl = null;

  private JsonHelper jsh = new JsonHelper();

  // public Properties login(String id_, String pwd_) throws IOException {
  // PostMethod method = null;
  // try {
  // method = buildPost(new NameValuePair[] {
  // new NameValuePair(Constants.SSO_ARG_ACTION, Constants.SSO_ACTION_LOGIN),
  // new NameValuePair(Constants.SSO_ID, id_),
  // new NameValuePair(Constants.SSO_PWD, pwd_)
  // });
  // sendRequest(method);
  // return parseReply(method);
  // }
  // finally {
  // if (method != null) {
  // method.releaseConnection();
  // }
  // }
  // }
  public SSOInfo validate(String ssoId_) throws SSOException {
    if (StringUtil.isEmpty(ssoCheckUrl)) {
      throw new SSOException("Got null/empty ssoUrl");
    }
    if (StringUtil.isEmpty(signInUrl)) {
      throw new SSOException("Got null/empty signInUrl");
    }

    PostMethod method = null;
    try {
      method = buildPost(new NameValuePair[] { new NameValuePair(SSOConstants.SSO_SSO_ID, ssoId_) });
      method.addRequestHeader("Cookie", SSOConstants.SSO_SSO_ID +"=" +ssoId_);
      sendRequest(method);
      return parseReply(method);
    } catch (IOException e) {
      throw new SSOException(e);
    } finally {
      if (method != null) {
        method.releaseConnection();
      }
    }
  }

  // public Properties logout(String ssoId_) throws IOException {
  // PostMethod method = null;
  // try {
  // method = buildPost(new NameValuePair[] {
  // new NameValuePair(Constants.SSO_ARG_ACTION, Constants.SSO_ACTION_LOGOUT),
  // new NameValuePair(Constants.SSO_SSO_ID, ssoId_)
  // });
  // sendRequest(method);
  // return parseReply(method);
  // }
  // finally {
  // if (method != null) {
  // method.releaseConnection();
  // }
  // }
  // }

  public PostMethod buildPost(NameValuePair[] args_) {
    PostMethod method = new PostMethod(ssoCheckUrl);
    method.getParams().setCookiePolicy(CookiePolicy.DEFAULT);
    method.setRequestBody(args_);
    return method;
  }

  /**
   * Sends an import request to the wiki.
   *
   * @param m
   *            The post method to send.
   * @throws Exception
   *             In case the transmission fails.
   */
  private void sendRequest(PostMethod m) throws IOException {
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

    int status = client.executeMethod(m);

    if (status != HttpStatus.SC_OK) {
      throw new IOException("Post failed with HTTP status : (" + status + ") " + HttpStatus.getStatusText(status));
    }
  }

  private SSOInfo parseReply(PostMethod m) throws IOException {
    String pl = m.getResponseBodyAsString();
    SSOInfo sos = jsh.deserialize(pl, DefaultSSOInfo.class);
    return sos;
  }

  @Override
  public String getSignInUrl() {
    return signInUrl;
  }

  public void setSignInUrl(String signInUrl) {
    this.signInUrl = signInUrl;
  }

  public String getSsoCheckUrl() {
    return ssoCheckUrl;
  }

  public void setSsoCheckUrl(String ssoCheckUrl) {
    this.ssoCheckUrl = ssoCheckUrl;
  }

  @Override
  public String getSessionTimeoutUrl() {
    return sessionTimeoutUrl;
  }

  @Override
  public String getSessionTimeoutStreamingUrl() {
    return sessionTimeoutStreamingUrl;
  }

  public void setSessionTimeoutUrl(String sessionTimeoutUrl) {
    this.sessionTimeoutUrl = sessionTimeoutUrl;
  }

  public void setSessionTimeoutStreamingUrl(String sessionTimeoutStreamingUrl) {
    this.sessionTimeoutStreamingUrl = sessionTimeoutStreamingUrl;
  }
}
TOP

Related Classes of org.butor.sso.validator.HttpSSOValidator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.