/**
   * Reads configured roles.
   *
   * @param request HTTP request.
   * @param response HTTP response.
   * @param context request context
   * @throws IdentityException if a system error occurs
   */
  private void executeReadConfigureRoles(
      HttpServletRequest request, HttpServletResponse response, RequestContext context)
      throws Exception {
    String mimeType = "application/json";
    String rolesJson = " { \"configuredRoles\" : [";
    Roles roles = buildSelectableRoles(context);
    ArrayList<String> sortedKeys = new ArrayList<String>(roles.keySet());
    Collections.sort(sortedKeys);
    boolean firstRole = true;
    for (int i = 0; i < sortedKeys.size(); i++) {
      Role role = roles.get(sortedKeys.get(i));
      String roleDn = Val.chkStr(role.getDistinguishedName());
      String roleKey = Val.chkStr(role.getKey());
      String roleName = msgBroker.retrieveMessage(Val.chkStr(role.getResKey()));
      if (!role.isManage()) continue;
      if (!firstRole) {
        rolesJson += ",";
      } else {
        firstRole = false;
      }
      rolesJson +=
          " { \"roleName\" : \""
              + Val.escapeStrForJson(roleName)
              + "\" , \"roleDn\" : \""
              + Val.escapeStrForJson(roleDn)
              + "\" , \"roleKey\" : \""
              + Val.escapeStrForJson(roleKey)
              + "\" }";
    }
    rolesJson += " ] } ";

    writeCharacterResponse(response, rolesJson, "UTF-8", mimeType + ";charset=UTF-8");
  }
  /**
   * Serializes list of ldap users matching filter.
   *
   * @param context the current request context
   * @param filter the user search filter for ldap
   * @return the list of users as json
   * @throws IdentityException if a system error occurs preventing the action
   * @throws NamingException if an LDAP naming exception occurs
   * @throws SQLException
   */
  protected String serializeUsersAsJson(
      RequestContext context, String filter, String attributeName, boolean isMemberSearch)
      throws IdentityException, NamingException, SQLException {
    Users users = new Users();
    int totalMatches = 0;
    if (!isMemberSearch) {
      HashMap<String, Object> resultsMap = buildUsersList(context, filter, null);
      users = (Users) resultsMap.get("topUserMatches");
      totalMatches = (Integer) resultsMap.get("totalMatches");
    } else if (isMemberSearch && attributeName != null) {
      Roles configuredRoles = context.getIdentityConfiguration().getConfiguredRoles();
      Role role = configuredRoles.get(attributeName);
      String sDn = role.getDistinguishedName();
      IdentityAdapter idAdapter = context.newIdentityAdapter();
      users = idAdapter.readGroupMembers(sDn);
      totalMatches = users.size();
      users.sort();
    } else {
      IdentityAdapter idAdapter = context.newIdentityAdapter();
      Users members = idAdapter.readGroupMembers(filter);
      for (User u : members.values()) {
        users.add(u);
      }
      users.sort();
      totalMatches = users.size();
    }

    String usersJson =
        "{ \"totalUsers\" : \""
            + totalMatches
            + "\" ,\"topUsers\" : \""
            + users.size()
            + "\" , \"users\": [";
    boolean firstUser = true;
    for (User user : users.values()) {
      String userName = user.getName();
      String dn = user.getKey();
      if (!firstUser) {
        usersJson += ",";
      } else {
        firstUser = false;
      }
      usersJson +=
          " { \"dn\" : \""
              + dn
              + "\" , \"userName\" : \""
              + Val.escapeStrForJson(userName)
              + "\" }";
    }
    usersJson += " ] }";
    return usersJson;
  }
예제 #3
0
 /**
  * Prints argument.
  *
  * @param argName argument name
  * @param argVal argument value
  * @param more flag to indicate if there will be more arguments
  */
 protected void printArg2(String argName, String argVal, boolean more) {
   argName = Val.chkStr(argName);
   argVal = Val.chkStr(argVal);
   if (argName.length() > 0) {
     println(
         "\""
             + Val.escapeStrForJson(argName)
             + "\""
             + sp()
             + ":"
             + sp()
             + (argVal)
             + (more ? "," : ""));
   }
 }
  /**
   * Serializes user information from ldap to json string.
   *
   * @param context request context
   * @param user the user to be serialized
   * @return the user profile information serialized as json string.
   * @throws IdentityException if a system error occurs preventing the action
   * @throws NamingException if an LDAP naming exception occurs
   */
  protected String serializeUserAsJson(RequestContext context, User user)
      throws IdentityException, NamingException {
    String usersJson = "{ \"attributes\": [";
    UserAttributeMap attributes = user.getProfile();
    boolean first = true;
    List<String> sortedKeys = new ArrayList<String>(attributes.keySet());
    // Collections.sort(sortedKeys); TODO to sort or not ?
    for (int i = 0; i < sortedKeys.size(); i++) {
      UserAttribute attr = attributes.get(sortedKeys.get(i));
      String key =
          Val.chkStr(msgBroker.retrieveMessage("catalog.identity.profile.label." + attr.getKey()));
      String value = "";
      value = Val.chkStr(attr.getValue());
      if (attr.getKey().equalsIgnoreCase("password")) continue;
      if (!first) {
        usersJson += ",";
      } else {
        first = false;
      }
      usersJson +=
          " { \"key\" : \""
              + Val.escapeStrForJson(key)
              + "\" , \"value\" : \""
              + Val.escapeStrForJson(value)
              + "\" }";
    }
    usersJson += " ] , ";

    usersJson += " \"userDn\" : \"" + user.getDistinguishedName() + " \" , ";

    String groupsJson = " \"groups\" : [";
    Groups groups = user.getGroups();
    groups.sort();
    boolean firstGroup = true;
    for (Group group : groups.values()) {
      String gkey = Val.chkStr(group.getKey());
      String name = Val.chkStr(group.getName());
      String dn = Val.chkStr(group.getDistinguishedName());
      if (!firstGroup) {
        groupsJson += ",";
      } else {
        firstGroup = false;
      }
      groupsJson +=
          " { \"key\" : \""
              + Val.escapeStrForJson(gkey)
              + "\" , \"name\" : \""
              + Val.escapeStrForJson(name)
              + "\" , \"dn\" : \""
              + Val.escapeStrForJson(dn)
              + "\" }";
    }
    groupsJson += " ] , ";

    String rolesJson = " \"selectableRoles\" : [";
    Roles roles = buildSelectableRoles(context);
    sortedKeys = new ArrayList<String>(roles.keySet());
    Collections.sort(sortedKeys);
    boolean firstRole = true;
    for (int i = 0; i < sortedKeys.size(); i++) {
      Role role = roles.get(sortedKeys.get(i));
      String roleDn = Val.chkStr(role.getDistinguishedName());
      String roleKey = Val.chkStr(role.getKey());
      String roleName = msgBroker.retrieveMessage(Val.chkStr(role.getResKey()));
      if (!role.isManage()) continue;
      boolean hasRole = false;
      for (Group group : groups.values()) {
        String groupDn = Val.chkStr(group.getDistinguishedName());
        if (roleDn.equals(groupDn)) {
          hasRole = true;
          break;
        }
      }
      if (!firstRole) {
        rolesJson += ",";
      } else {
        firstRole = false;
      }
      rolesJson +=
          " { \"roleName\" : \""
              + Val.escapeStrForJson(roleName)
              + "\" , \"roleDn\" : \""
              + Val.escapeStrForJson(roleDn)
              + "\" , \"roleKey\" : \""
              + Val.escapeStrForJson(roleKey)
              + "\" , \"hasRole\" : \""
              + hasRole
              + "\" }";
    }
    rolesJson += " ] } ";
    String json = usersJson + groupsJson + rolesJson;
    return json;
  }
예제 #5
0
  /**
   * Cleans value from lucene index
   *
   * @param value value to clean
   * @return cleaned value
   */
  private String cleanValue(String value, String type, String dcatFieldName, DcatField dcatField) {
    String delimiter = ", ";
    String dateFormat = "";
    int maxChars = -1;
    if (dcatField != null) {
      String df = dcatField.getDateFormat();
      if (df.length() > 0) {
        dateFormat = df;
      }
      int mc = dcatField.getMaxChars();
      if (mc > -1) {
        maxChars = mc;
      }
    }
    if (value == null) {
      return "";
    }
    if (value == "null") {
      return "";
    }
    if (type.equalsIgnoreCase("date") || dcatFieldName.equalsIgnoreCase("spatial")) {
      value = value.replaceAll("\"", "");
    }
    if (value.startsWith("[")) {
      value = value.replace("[", "");
      if (type.equalsIgnoreCase("string")) {
        value = value.replaceAll("\",\"", ",");
      }
    }
    if (value.endsWith("]")) {
      value = value.replace("]", "");
    }

    // only one webService url
    if (dcatFieldName.equalsIgnoreCase("webService") && value != null && value.length() > 0) {
      String[] parts = value.split(",http");
      if (parts != null && parts.length > 0) {
        value = parts[0];
        if (!value.startsWith("\"")) {
          value = "\"" + Val.escapeStrForJson(value);
        }
        if (!value.endsWith("\"")) {
          value += "\"";
        }
      }
    }

    if (value != null && value.length() > 0) {
      if (type.equalsIgnoreCase("date")) {
        int firstIdx = value.indexOf(",");
        if (firstIdx > -1) {
          value = value.substring(0, firstIdx);
        }
        value = parseDateTime(value);
        if (dateFormat.length() > 0) {
          SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
          Date dt = null;
          try {
            dt = sdf.parse(value);
          } catch (ParseException e) {
          }
          value = sdf.format(dt);
        }
      } else if (type.equalsIgnoreCase("array")) {
        value = value.replace("\"", "").replace("\"", "");
        String[] parts = value.split(",");
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        boolean hasValue = false;
        HashMap<String, String> repKeyword = new HashMap<String, String>();
        for (String part : parts) {
          String partTrimUpper = part.trim().toUpperCase();
          if (!part.startsWith("\"") && !part.endsWith("\"")) {
            if ((!dcatFieldName.equalsIgnoreCase("keyword"))
                || (!repKeyword.containsKey(partTrimUpper))) {
              repKeyword.put(partTrimUpper, partTrimUpper);
              if (hasValue) {
                sb.append(delimiter);
              }

              sb.append("\"").append(Val.escapeStrForJson(part.trim())).append("\"");
              hasValue = true;
            }
          }
        }
        sb.append("]");
        value = sb.toString();
      }

      if (type.equalsIgnoreCase("string")) {
        if (maxChars > -1 && value.length() > maxChars) {
          if (value.startsWith("\"") && value.endsWith("\"")) {
            value = "\"" + Val.escapeStrForJson(value.substring(1, maxChars)) + "\"";
          }
        }
      }
    }
    return value;
  }