public void process(OpenIdTestService s, AuthRequest authReq, Message rep)
            throws MessageException {
          if (authReq.hasExtension(SRegMessage.OPENID_NS_SREG)) {
            MessageExtension ext = authReq.getExtension(SRegMessage.OPENID_NS_SREG);
            if (ext instanceof SRegRequest) {
              SRegRequest sregReq = (SRegRequest) ext;
              SRegResponse sregRes = SRegResponse.createFetchResponse();

              List<String> attrs = (List<String>) sregReq.getAttributes();
              if (attrs.contains("fullname") && s.props.containsKey(IdProperty.fullName)) {
                sregRes.addAttribute("fullname", s.props.get(IdProperty.fullName));
              }

              if (attrs.contains("nickname") && s.props.containsKey(IdProperty.nick)) {
                sregRes.addAttribute("nickname", s.props.get(IdProperty.nick));
              }

              if (attrs.contains("email") && s.props.containsKey(IdProperty.email)) {
                sregRes.addAttribute("email", s.props.get(IdProperty.email));
              }

              rep.addExtension(sregRes);
            }
          }
        }
        public void process(OpenIdTestService s, AuthRequest authReq, Message rep)
            throws MessageException {
          if (authReq.hasExtension(TeamExtensionFactory.URI)) {
            MessageExtension ext = authReq.getExtension(TeamExtensionFactory.URI);
            if (ext instanceof TeamExtensionRequest) {
              TeamExtensionRequest teamReq = (TeamExtensionRequest) ext;

              rep.addExtension(new ServiceTeamExtensionResponse(s.teams));
            }
          }
        }
        public void process(OpenIdTestService s, AuthRequest authReq, Message rep)
            throws MessageException {
          if (authReq.hasExtension(AxMessage.OPENID_NS_AX)) {
            MessageExtension ext = authReq.getExtension(AxMessage.OPENID_NS_AX);
            if (ext instanceof FetchRequest) {
              FetchRequest fetchReq = (FetchRequest) ext;
              FetchResponse fr = FetchResponse.createFetchResponse();

              for (Map.Entry<String, String> e :
                  ((Map<String, String>) fetchReq.getAttributes()).entrySet()) {
                if ((e.getValue().equals("http://axschema.org/contact/email"))
                    && s.props.containsKey(IdProperty.email)) {
                  if (s.props.get(IdProperty.email) != null) {
                    fr.addAttribute(e.getKey(), e.getValue(), s.props.get(IdProperty.email));
                  }
                } else if ((e.getValue().equals("http://schema.openid.net/contact/email"))
                    && s.props.containsKey(IdProperty.email2)) {
                  if (s.props.get(IdProperty.email2) != null) {
                    fr.addAttribute(e.getKey(), e.getValue(), s.props.get(IdProperty.email2));
                  }
                } else if ((e.getValue().equals("http://openid.net/schema/contact/email"))
                    && s.props.containsKey(IdProperty.email3)) {
                  if (s.props.get(IdProperty.email3) != null) {
                    fr.addAttribute(e.getKey(), e.getValue(), s.props.get(IdProperty.email3));
                  }
                }

                if (e.getValue().equals("http://axschema.org/namePerson/first")
                    && s.props.containsKey(IdProperty.firstName))
                  fr.addAttribute(e.getKey(), e.getValue(), s.props.get(IdProperty.firstName));

                if (e.getValue().equals("http://axschema.org/namePerson/last")
                    && s.props.containsKey(IdProperty.lastName))
                  fr.addAttribute(e.getKey(), e.getValue(), s.props.get(IdProperty.lastName));
              }

              rep.addExtension(fr);
            }
          }
        }
  @SuppressWarnings("unchecked")
  private void storeRequestedAttributesToSession(IWContext iwc, AuthRequest authReq) {
    if (authReq.hasExtension(AxMessage.OPENID_NS_AX)) {
      MessageExtension ext;
      try {
        ext = authReq.getExtension(AxMessage.OPENID_NS_AX);
        if (ext instanceof FetchRequest) {
          FetchRequest fetchReq = (FetchRequest) ext;
          OpenIDServerBean serverBean = ELUtil.getInstance().getBean("openIDServerBean");
          Map<String, String> allAttributes = (Map<String, String>) fetchReq.getAttributes();
          Map<String, String> required = (Map<String, String>) fetchReq.getAttributes(true);

          List<AuthorizedAttribute> allAttributesList = new ArrayList<AuthorizedAttribute>();
          List<AuthorizedAttribute> requiredAttributesList = new ArrayList<AuthorizedAttribute>();
          List<AuthorizedAttribute> optionalAttributesList = new ArrayList<AuthorizedAttribute>();

          String realm = serverBean.getRealm();

          for (String alias : allAttributes.keySet()) {
            ExchangeAttribute attribute =
                getDAO().getExchangeAttribute(alias, allAttributes.get(alias));
            if (attribute != null) {
              User user = iwc.getCurrentUser();
              AuthorizedAttribute aattr =
                  getDAO().getAuthorizedAttributes(user.getUniqueId(), realm, attribute);
              if (aattr == null) {
                aattr = new AuthorizedAttribute();
                aattr.setExchangeAttribute(attribute);
                aattr.setRealm(realm);
                aattr.setUserUUID(user.getUniqueId());
                aattr.setIsAllowed(true);
              }
              allAttributesList.add(aattr);
              if (required.containsKey(alias) && attribute.getType().equals(required.get(alias))) {
                requiredAttributesList.add(aattr);
              } else {
                optionalAttributesList.add(aattr);
              }
            } else {
              LOGGER.warning(
                  "Requesting unknown exchange attribute: "
                      + alias
                      + " : "
                      + allAttributes.get(alias));
              // throw new UnsupportedOperationException("Requesting unknown exchange attribute.");
            }
          }

          serverBean.setRequestedAttributes(allAttributesList);
          serverBean.setRequiredAttributes(requiredAttributesList);
          serverBean.setOptionalAttributes(optionalAttributesList);
          return;
        } else /*if (ext instanceof StoreRequest)*/ {
          // TODO implement?
          throw new UnsupportedOperationException("TODO");
        }
      } catch (MessageException e) {
        e.printStackTrace();
      }
    }
    if (authReq.hasExtension(SRegMessage.OPENID_NS_SREG11)) {
      try {
        MessageExtension ext = authReq.getExtension(SRegMessage.OPENID_NS_SREG11);

        if (ext instanceof SRegRequest) {
          SRegRequest sregReq = (SRegRequest) ext;
          List<String> all = sregReq.getAttributes();
          List<String> required = sregReq.getAttributes(true);
          List<String> optional = sregReq.getAttributes(false);

          List<AuthorizedAttribute> allAttributesList = new ArrayList<AuthorizedAttribute>();
          List<AuthorizedAttribute> requiredAttributesList = new ArrayList<AuthorizedAttribute>();
          List<AuthorizedAttribute> optionalAttributesList = new ArrayList<AuthorizedAttribute>();

          OpenIDServerBean serverBean = ELUtil.getInstance().getBean("openIDServerBean");
          String realm = serverBean.getRealm();
          User user = iwc.getCurrentUser();

          for (String alias : all) {
            ExchangeAttribute attribute = getDAO().getExchangeAttribute(alias);
            if (attribute != null) {
              AuthorizedAttribute aattr =
                  getDAO().getAuthorizedAttributes(user.getUniqueId(), realm, attribute);
              if (aattr == null) {
                aattr = new AuthorizedAttribute();
                aattr.setExchangeAttribute(attribute);
                aattr.setRealm(realm);
                aattr.setUserUUID(user.getUniqueId());
                aattr.setIsAllowed(true);
              }
              allAttributesList.add(aattr);
              if (required.contains(alias)) {
                requiredAttributesList.add(aattr);
              }
              if (optional.contains(alias)) {
                optionalAttributesList.add(aattr);
              }
            }
          }

          serverBean.setRequestedAttributes(allAttributesList);
          serverBean.setRequiredAttributes(requiredAttributesList);
          serverBean.setOptionalAttributes(optionalAttributesList);
          return;
        }
      } catch (MessageException e) {
        e.printStackTrace();
      }
    }
  }
  protected String[] prepareResponse(
      OpenIDServerBean serverBean, Message response, IWContext iwc, User user, AuthRequest authReq)
      throws MessageException {
    List<String> extensionsToSign = new ArrayList<String>();
    if (authReq.hasExtension(AxMessage.OPENID_NS_AX)) {
      try {
        MessageExtension ext = authReq.getExtension(AxMessage.OPENID_NS_AX);
        if (ext instanceof FetchRequest) {
          FetchRequest fetchReq = (FetchRequest) ext;
          Map<String, String> requestedAttributes = fetchReq.getAttributes();
          Map userDataExt = new HashMap();

          FetchResponse fetchResp = FetchResponse.createFetchResponse(fetchReq, userDataExt);
          List<AuthorizedAttribute> s = serverBean.getRequestedAttributes();
          Set<String> keys = requestedAttributes.keySet();
          Collection<String> types = requestedAttributes.values();
          for (AuthorizedAttribute a : s) {
            ExchangeAttribute attr = a.getExchangeAttribute();
            String alias = attr.getName();
            String type = attr.getType();
            if (keys.contains(alias) || types.contains(type)) {
              String value = getAttributeValue(iwc, user, alias, type);
              if (value == null) {
                value = "";
              }
              fetchResp.addAttribute(alias, type, value);
            } else {
              // FetchRequest not asking for this attribute
              throw new UnsupportedOperationException(
                  "Processed and requested attributes do not match.");
            }
          }

          response.addExtension(fetchResp);
          extensionsToSign.add(AxMessage.OPENID_NS_AX);
        } else /*if (ext instanceof StoreRequest)*/ {
          throw new UnsupportedOperationException("TODO");
        }
      } catch (MessageException me) {
        System.err.println(me.getMessage());
      }
    }
    if (authReq.hasExtension(SRegMessage.OPENID_NS_SREG11)) {
      MessageExtension ext = authReq.getExtension(SRegMessage.OPENID_NS_SREG11);

      if (ext instanceof SRegRequest) {
        SRegRequest sregReq = (SRegRequest) ext;
        List<String> required = sregReq.getAttributes(true);
        List<String> optional = sregReq.getAttributes(false);

        Map userData = new HashMap();
        for (String alias : required) {
          String value = getAttributeValue(iwc, user, alias, null);
          if (alias.length() > 0 && value != null) {
            userData.put(alias, value);
          } else if (alias.length() > 0) {
            throw new UnsupportedOperationException("Required attribute not supported: " + alias);
          }
        }
        for (String alias : optional) {
          String value = getAttributeValue(iwc, user, alias, null);
          if (alias.length() > 0 && value != null) {
            userData.put(alias, value);
          }
        }

        SRegResponse sregResp = SRegResponse.createSRegResponse(sregReq, userData);
        response.addExtension(sregResp);
        extensionsToSign.add(SRegMessage.OPENID_NS_SREG11);
      } else if (ext instanceof SRegResponse) {
        response.addExtension(ext);
      }
    }

    return extensionsToSign.toArray(new String[extensionsToSign.size()]);
  }