private String getRealm(ParameterList requestp) {
    final String realm = requestp.getParameterValue("openid.realm");
    final String returnTo = requestp.getParameterValue("openid.return_to");

    if (realm == null && returnTo != null)
      try {
        return new URL(returnTo).getHost();
      } catch (MalformedURLException e) {
        // Fall back
        return returnTo;
      }

    return realm;
  }
  public HttpResponse doEndpoint(StaplerRequest request) throws IOException {
    final ParameterList requestp = new ParameterList(request.getParameterMap());
    final String mode = requestp.getParameterValue("openid.mode");
    final String realm = getRealm(requestp);

    if ("associate".equals(mode)) {
      // --- process an association extend ---
      return new MessageResponse(manager.associationResponse(requestp));
    } else if ("checkid_setup".equals(mode) || "checkid_immediate".equals(mode)) {

      // No need to redirect to a page with an HTML form
      // Skip the authentication step

      String identity = getUserIdentity();
      Message rsp = manager.authResponse(requestp, identity, identity, true, false);
      try {
        respondToExtensions(requestp, rsp);
      } catch (MessageException ex) {
        throw new OperationFailure(ex.getMessage());
      }

      // Need to sign after because SReg extension parameters are signed by openid4java
      if (rsp instanceof AuthSuccess) {
        try {
          manager.sign((AuthSuccess) rsp);
        } catch (ServerException e) {
          throw new OperationFailure(e.getMessage());
        } catch (AssociationException e) {
          throw new OperationFailure(e.getMessage());
        }
      }

      return new HttpRedirect(rsp.getDestinationUrl(true));
    } else if ("check_authentication".equals(mode)) {
      return new MessageResponse(manager.verify(requestp));
    } else {
      throw new OperationFailure("Unknown extend: " + mode);
    }
  }