/**
   * Executes the block main action
   *
   * @param dataSet a value of type 'DataSet'
   * @return the port to go to the next block
   */
  public Port after(UserInfoInterface userInfo, ProcessData procData) {
    Port outPort = portFalse;

    try {

      String sLogin = null;
      String sPass = null;

      sLogin = this.getAttribute(LOGIN_ATTR);
      sLogin = procData.transform(userInfo, sLogin);

      try {
        sPass = procData.transform(userInfo, this.getAttribute(PASS_ATTR));
      } catch (Exception ee) {
      }

      AuthProfile ap = BeanFactory.getAuthProfileBean();

      boolean bOk = ap.checkUser(sLogin, sPass);

      Logger.info(
          userInfo.getUtilizador(),
          this,
          "after",
          "["
              + procData.getFlowId()
              + ","
              + procData.getPid()
              + ","
              + procData.getSubPid()
              + "] "
              + "checked authentication for "
              + sLogin
              + ": "
              + bOk);

      if (bOk) {
        outPort = portTrue;
      } else {
        outPort = portFalse;
      }
    } catch (Exception e) {
      Logger.error(
          userInfo.getUtilizador(),
          this,
          "after",
          procData.getSignature(this.getId()) + "Exception caught: " + e.getMessage());
      e.printStackTrace();
    }

    return outPort;
  }
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    HttpSession session = request.getSession();
    AuthenticationResult result = new AuthenticationResult();
    String samlXMLB64Response = request.getParameter("SAMLResponse");
    // if (StringUtils.isBlank(samlXMLB64Response))
    //	samlXMLB64Response = request.getAttribute("SAMLResponse").toString();
    // samlXMLB64Response = org.apache.commons.lang.StringUtils.replaceChars(samlXMLB64Response, '
    // ', '+');
    // samlXMLB64Response =
    // "PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWxwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiIERlc3RpbmF0aW9uPSJodHRwczovL2JhcmNsYXlzLmNsLmluZm9zaXN0ZW1hLmNvbS9pRmxvdy9zYW1sMi9TU09TZXJ2aWNlIiBJRD0iaWQtcFEtdVlzdUdZTVktdEVyVzgtWlBvRlFYamtBLSIgSXNzdWVJbnN0YW50PSIyMDE0LTEwL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XVkaWVuY2VSZXN0cmljdGlvbj48c2FtbDpBdWRpZW5jZT5odHRwczovL2JhcmNsYXlzLmNsLmluZm9zaXN0ZW1hLmNvbS9pRmxvdzwvc2FtbDpBdWRpZW5jZT48L3NhbWw6QXVkaWVuY2VSZXN0cmljdGlvbj48L3NhbWw6Q29uZGl0aW9ucz48c2FtbDpBdXRoblN0YXRlbWVudCBBdXRobkluc3RhbnQ9IjIwMTQtMTAtMDNUMDk6MzE6MzVaIiBTZXNzaW9uSW5kZXg9ImlkLXlLYWJkN3FzZEd3QlQ0Vi1SLTViWDd0MklGby0iIFNlc3Npb25Ob3RPbk9yQWZ0ZXI9IjIwMTQtMTAtMDNUMDk6MzE6NDVaIj48c2FtbDpBdXRobkNvbnRleHQ+PHNhbWw6QXV0aG5Db250ZXh0Q2xhc3NSZWY+dXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOmFjOmNsYXNzZXM6S2VyYmVyb3M8L3NhbWw6QXV0aG5Db250ZXh0Q2xhc3NSZWY+PC9zYW1sOkF1dGhuQ29udGV4dD48L3NhbWw6QXV0aG5TdGF0ZW1lbnQ+PC9zYW1sOkFzc2VydGlvbj48L3NhbWxwOlJlc3BvbnNlPg==";
    samlXMLB64Response = StringEscapeUtils.unescapeHtml(samlXMLB64Response);
    result.nextUrl = "../main.jsp";
    UserInfoInterface ui = BeanFactory.getUserInfoFactory().newUserInfo();
    AuthProfile ap = BeanFactory.getAuthProfileBean();
    Boolean foundValidKey = false;

    try {
      Integer ePId = Integer.parseInt(Setup.getProperty("ENTITY_PROVIDERS_NUMBER"));
      String employeeid = null;
      for (int i = 1; i < ePId; i++) {
        String publicEPKey = Setup.getProperty("ENTITY_PROVIDER_PUBLIC_KEY_" + i);
        // user account specific settings. Import the certificate here
        AccountSettings accountSettings = new AccountSettings();
        accountSettings.setCertificate(publicEPKey);
        Response samlResponse = new Response(accountSettings);
        Logger.debug("System", this, "service", "Received Saml Response:" + samlXMLB64Response);
        samlResponse.loadXmlFromBase64(samlXMLB64Response);
        // samlResponse.loadXml(samlXMLB64Response);

        if (samlResponse.isValid()) {
          // the signature of the SAML Response is valid. The source is trusted
          foundValidKey = true;
          employeeid = samlResponse.getNameId();
          break;
        }
      }
      if (!foundValidKey) {
        // the signature of the SAML Response is not valid
        session.setAttribute(
            "login_error", ui.getMessages().getString("login.error.sso.signature"));
        return;
      }
      ////////////////////////////
      ui.loginSSO(employeeid);
      boolean isAuth = result.isAuth = ui.isLogged();

      if (isAuth) {

        /////////////////////////////
        //
        // Now set some session vars
        //
        /////////////////////////////

        // Application Data
        session.setAttribute("login", ui.getUtilizador());

        session.setAttribute(Const.USER_INFO, ui);
        UserSettings settings = ui.getUserSettings();
        OrganizationData orgData = ap.getOrganizationInfo(ui.getOrganization());
        session.setAttribute(Const.ORG_INFO, orgData);

        OrganizationTheme orgTheme = BeanFactory.getOrganizationThemeBean();
        if (orgTheme != null) {
          OrganizationThemeData themeData = orgTheme.getOrganizationTheme(ui);
          session.setAttribute("themedata", themeData);
        }

        if (settings.isDefault() && Const.USE_INDIVIDUAL_LOCALE && Const.ASK_LOCALE_AT_LOGIN) {
          result.nextUrl = "../setupUser";
        }

        // check license status
        //		      if(!licenseOk && isSystem) {
        //		        result.nextUrl = "Admin/licenseValidation.jsp";
        //		      }

        session.setAttribute("SessionHelperToken", new SimpleSessionHelper());

      } else {
        session.setAttribute("login_error", ui.getError());
      }
      PersistSession ps = new PersistSession();
      ps.getSession(ui, session);
    } catch (Exception e) {
      ui.loginSSO(null);
      session.setAttribute("login_error", ui.getError());
    } finally {
      response.sendRedirect(result.nextUrl + "?" + Utils.makeSycnhronizerToken());
    }
  }