AdminHelper.java
/*
** Module : AdminHelper.java
** Abstract : Provides admin routines
**
** Copyright (c) 2017, Golden Code Development Corporation.
** ALL RIGHTS RESERVED. Use is subject to license terms.
**
** Golden Code Development Corporation
** CONFIDENTIAL
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 SBI 20170320 Added wrapIntoRegExpPattern, moved the shared code out of CertificateUtils
** to be used on the client side.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.admin;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import com.goldencode.p2j.admin.client.application.home.accounts.CertificateKeys;
/**
* Admin helper methods.
*/
public class AdminHelper
{
/**
* Returns a regular expression pattern for the given search string that is "case insensitive".
*
* @param substring
* The given search string.
*
* @return The case insensitive pattern.
*/
public static String wrapIntoCaseInsensitivePattern(String substring)
{
StringBuilder sb = new StringBuilder(".*");
for (int i = 0; i < substring.length(); i++)
{
char c = substring.charAt(i);
sb.append("[");
if (Character.isUpperCase(c))
{
sb.append(Character.toLowerCase(c));
sb.append(c);
}
else
{
sb.append(c);
sb.append(Character.toUpperCase(c));
}
sb.append("]");
}
sb.append(".*");
return sb.toString();
}
/**
* Extract properties from the give Distinctive Name data.
* <p>
* The properties are separated by commas; the value is separated from the
* key by '=' sign. Also, if value contains commas or '=' signs, it will
* be quoted.
* <p>
* In the returned map, all keys will be converted to lowercase.
*
* @param DN
* The DN data to be parsed.
*
* @return a map containing (key, value) pairs for all entries in this DN.
*/
public static Map<String, String> parseDistinctiveName(String DN)
{
boolean prop = true;
boolean value = false;
boolean quotes = false;
String sprop = "";
String sval = "";
Map<String, String> info = new LinkedHashMap<String, String>();
for (int i = 0; i < DN.length(); i++)
{
char c = DN.charAt(i);
// property ends, value starts - switch from property to value
if (c == '=' && !quotes)
{
prop = false;
value = true;
continue;
}
// value may be in quotes - track quote opening
if (c == '"' && value)
{
quotes = !quotes;
continue;
}
// check if value ends - a ',' ends value only if there are
// no open quotes
if (c == ',' && !quotes)
{
prop = true;
value = false;
// save this info
info.put(sprop.toLowerCase().trim(), sval.trim());
// a new prop=value pair is starting
sprop = "";
sval = "";
continue;
}
// build property
if (prop)
{
sprop = sprop + c;
}
// build value
if (value)
{
sval = sval + c;
}
}
return info;
}
/**
* From the given Distinctive Name data, extract the value for specified
* property.
*
* @param DN
* The DN data which holds the property.
* @param prop
* The property which value should be extracted
*
* @return the value for this property
*/
public static String getCertificateDetail(String DN, String prop)
{
prop = prop.toLowerCase().trim();
Map<String, String> details = AdminHelper.parseDistinctiveName(DN);
String val = details == null ? null : details.get(prop);
return val == null ? " " : val;
}
/**
* Builds {@link TaggedName} objects from the specified source. Also, it
* adds a title row with the specified text.
*
* @param source
* a map for which the its <code>prop</code> entry holds a
* @param title
* the title to be added
*
* @return a list of rows.
*/
@SuppressWarnings("unchecked")
public static ArrayList<TaggedName> buildCertificateInfo(Map source,
String title)
{
ArrayList<TaggedName> res = new ArrayList<TaggedName>();
res.add(new TaggedName(title, ""));
res.addAll(buildCertificateTreeNodes(source));
// add an empty row
res.add(new TaggedName("", ""));
return res;
}
/**
* Builds the array of certificate properties given by their tagged names.
*
* @param source
* The certificate properties map
*
* @return The array of certificate properties given by their tagged names.
*/
public static ArrayList<TaggedName> buildCertificateTreeNodes(Map source)
{
ArrayList<TaggedName> res = new ArrayList<TaggedName>();
Iterator itr = source.keySet().iterator();
while (itr.hasNext())
{
String key = (String) itr.next();
String val = (String) source.get(key);
String lbl = getPropertyLabel(key);
res.add(new TaggedName(lbl, val));
}
return res;
}
/**
* Given a certificate property, it returns its readable form.
*
* @param prop
* The given certificate property name
*
* @return The certificate property presentation value.
*/
public static String getPropertyLabel(String prop)
{
prop = prop.toUpperCase();
CertificateKeys certKey = CertificateKeys.getKey(prop);
return certKey == CertificateKeys.UNKNOWN ? prop : certKey.getPresentationValue();
}
}