import java.io.IOException;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.openid4java.association.AssociationException;
import org.openid4java.consumer.ConsumerException;
import org.openid4java.consumer.ConsumerManager;
import org.openid4java.consumer.VerificationResult;
import org.openid4java.discovery.DiscoveryException;
import org.openid4java.discovery.DiscoveryInformation;
import org.openid4java.discovery.Identifier;
import org.openid4java.message.AuthRequest;
import org.openid4java.message.AuthSuccess;
import org.openid4java.message.MessageException;
import org.openid4java.message.ParameterList;
import org.openid4java.message.ax.AxMessage;
import org.openid4java.message.ax.FetchRequest;
import org.openid4java.message.ax.FetchResponse;public class SampleConsumer { // このobjectはuserをopenid serverに発送する、結果を受ける
public ConsumerManager manager; public SampleConsumer() throws ConsumerException {
// instantiate a ConsumerManager object
manager = new ConsumerManager();
}// 構造完了 // 取得userの入力情報 String userSuppliedString = http://moodoasis.myopenid.com/、 
// 請求を送信する
public String authRequest(String userSuppliedString,
HttpServletRequest httpReq, HttpServletResponse httpResp)
throws IOException, MessageException, ConsumerException {

 try {
// 取得ユーザのopenidサーバの場所、普通は一つだけ、念のため、Listを使った。
List discoveries = manager.discover(userSuppliedString);
// 加密方法を取得、openidサーバと一致にする
DiscoveryInformation discovered = manager.associate(discoveries);
// 加密方法を保存する、解密時使いますよう
httpReq.getSession().setAttribute("openid-disc", discovered);
// my website http://www.163.com
String returnToUrl = "http://www.163.com";
// 認証対象を作成して、その中は、加密方法と返回場所を含みました
AuthRequest authReq = manager.authenticate(discovered, returnToUrl);
// 何が欲しいですか、今設定する、i need the email of user
FetchRequest fetch = FetchRequest.createFetchRequest();
// emailの返回格式                     //分かりません
fetch.addAttribute("email","http://schema.openid.net/contact/email",true);
// 認証対象に追加する
authReq.addExtension(fetch);

// 発送請求
httpResp.sendRedirect(authReq.getDestinationUrl(true));

} catch (DiscoveryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}



// 認証戻りを処理
 public Identifier verifyResponse(HttpServletRequest httpReq) throws MessageException, DiscoveryException, AssociationException{
 // get 全部変数対 ?tid=023232556&&xx=xxxxxx 昨日QQで、見たもの、
 ParameterList response = new ParameterList(httpReq.getParameterMap());
 // 保存した解密方法を取得
 DiscoveryInformation discovered = (DiscoveryInformation)httpReq.getSession().getAttribute("openid-disc");
 // 全部のurl取得
 StringBuffer receivingURL = httpReq.getRequestURL();
 // 取得?の後ろもの ?tid=023232556&&xx=xxxxxx
 String queryString = httpReq.getQueryString();
 
 // 解密しましょう、それに、結果もらえた!!!
  VerificationResult verification = manager.verify(receivingURL.toString(), response, discovered);
  // 認証ID取得
  Identifier verified = verification.getVerifiedId();
  
  //もし成功したら、emailを所得する
  if (verified != null) {
AuthSuccess authSuccess = (AuthSuccess) verification
.getAuthResponse();
if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
FetchResponse fetchResp = (FetchResponse) authSuccess
.getExtension(AxMessage.OPENID_NS_AX);
List emails = fetchResp.getAttributeValues("email");

//emailを所得する
String email = (String) emails.get(0);
}
return verified; // success
}else{
 return null;//失敗
 }
 

 }



}