001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package org.ejbca.util.query;
013:
014: /**
015: * A class used by Query class to build a query for ejbca ra module. Inherits BasicMatch. Main
016: * function is getQueryString which returns a fragment of SQL statment.
017: *
018: * @author TomSelleck
019: * @version $Id: UserMatch.java,v 1.5 2007/05/04 09:06:39 anatom Exp $
020: *
021: * @see org.ejbca.util.query.BasicMatch
022: * @see org.ejbca.util.query.TimeMatch
023: * @see org.ejbca.util.query.LogMatch
024: */
025: public class UserMatch extends BasicMatch {
026: // Public Constants
027:
028: public static final int MATCH_WITH_USERNAME = 0;
029: public static final int MATCH_WITH_EMAIL = 1;
030: public static final int MATCH_WITH_STATUS = 2; // Value must the number representation.
031: public static final int MATCH_WITH_ENDENTITYPROFILE = 3; // Matches the profile id not profilename.
032: public static final int MATCH_WITH_CERTIFICATEPROFILE = 4; // Matches the certificatetype id not name.
033: public static final int MATCH_WITH_CA = 5; // Matches the CA id not CA name.
034: public static final int MATCH_WITH_TOKEN = 6;
035: public static final int MATCH_WITH_DN = 7;
036: // Subject DN fields.
037: public static final int MATCH_WITH_UID = 100;
038: public static final int MATCH_WITH_COMMONNAME = 101;
039: public static final int MATCH_WITH_DNSERIALNUMBER = 102;
040: public static final int MATCH_WITH_GIVENNAME = 103;
041: public static final int MATCH_WITH_INITIALS = 104;
042: public static final int MATCH_WITH_SURNAME = 105;
043: public static final int MATCH_WITH_TITLE = 106;
044: public static final int MATCH_WITH_ORGANIZATIONUNIT = 107;
045: public static final int MATCH_WITH_ORGANIZATION = 108;
046: public static final int MATCH_WITH_LOCALE = 109;
047: public static final int MATCH_WITH_STATE = 110;
048: public static final int MATCH_WITH_DOMAINCOMPONENT = 111;
049: public static final int MATCH_WITH_COUNTRY = 112;
050: // Subject Altname Fields
051: public static final int MATCH_WITH_RFC822NAME = 200;
052: public static final int MATCH_WITH_DNSNAME = 201;
053: public static final int MATCH_WITH_IPADDRESS = 202;
054: public static final int MATCH_WITH_X400ADDRESS = 203;
055: public static final int MATCH_WITH_DIRECTORYNAME = 204;
056: public static final int MATCH_WITH_EDIPARTNAME = 205;
057: public static final int MATCH_WITH_URI = 206;
058: public static final int MATCH_WITH_REGISTEREDID = 207;
059: public static final int MATCH_WITH_UPN = 208;
060: public static final int MATCH_WITH_GUID = 209;
061:
062: // Private Constants.
063: static final String[] MATCH_WITH_SQLNAMES = { "username",
064: "subjectEmail", "status", "endEntityProfileId",
065: "certificateProfileId", "cAId", "tokenType" };
066:
067: // Represents the column names in ra userdata table.
068: private static final String MATCH_WITH_SUBJECTDN = "subjectDN";
069: private static final String[] MATCH_WITH_SUBJECTDN_NAMES = {
070: "UID=", "CN=", "SN=", "GIVENNAME=", "INITIALS=",
071: "SURNAME=", "T=", "OU=", "O=", "L=", "ST=", "DC", "C=" };
072:
073: private static final String MATCH_WITH_SUBJECTALTNAME = "subjectAltName";
074: private static final String[] MATCH_WITH_SUBJECTALTNAME_NAMES = {
075: "RFC822NAME=", "DNSNAME=", "IPADDRESS=", "X400ADDRESS=",
076: "DIRECTORYNAME=", "EDIPARTNAME=",
077: "UNIFORMRESOURCEIDENTIFIER=", "REGISTEREDID=", "UPN=",
078: "GUID=" };
079:
080: // Public methods.
081:
082: /**
083: * Creates a new instance of UserMatch.
084: *
085: * @param matchwith determines which field i userdata table to match with.
086: * @param matchtype determines how to match the field. SubjectDN fields can only be matched
087: * with 'begins with'.
088: * @param matchvalue the value to match with.
089: *
090: * @throws NumberFormatException if matchvalue constains illegal numbervalue when matching
091: * number field.
092: */
093: public UserMatch(int matchwith, int matchtype, String matchvalue)
094: throws NumberFormatException {
095: this .matchwith = matchwith;
096: this .matchtype = matchtype;
097: this .matchvalue = matchvalue;
098:
099: if ((matchwith >= MATCH_WITH_STATUS)
100: && (matchwith <= MATCH_WITH_CA)) {
101: new Integer(matchvalue);
102: }
103: }
104:
105: /**
106: * Returns a SQL statement fragment from the given data.
107: *
108: * @return sql string
109: */
110: public String getQueryString() {
111: String returnval = "";
112:
113: if (isSubjectDNMatch()) {
114: // Ignore MATCH_TYPE_EQUALS.
115: returnval = MATCH_WITH_SUBJECTDN + " LIKE '%"
116: + MATCH_WITH_SUBJECTDN_NAMES[matchwith - 100]
117: + matchvalue + "%'";
118: } else {
119: if (isSubjectAltNameMatch()) {
120: returnval = MATCH_WITH_SUBJECTALTNAME
121: + " LIKE '%"
122: + MATCH_WITH_SUBJECTALTNAME_NAMES[matchwith - 200]
123: + matchvalue + "%'";
124: } else {
125: if (matchwith == MATCH_WITH_DN) {
126: if (matchtype == BasicMatch.MATCH_TYPE_EQUALS) {
127: returnval = MATCH_WITH_SUBJECTDN + " = '"
128: + matchvalue + "'";
129: }
130:
131: if (matchtype == BasicMatch.MATCH_TYPE_BEGINSWITH) {
132: returnval = MATCH_WITH_SUBJECTDN + " LIKE '"
133: + matchvalue + "%'";
134: }
135:
136: if (matchtype == BasicMatch.MATCH_TYPE_CONTAINS) {
137: returnval = MATCH_WITH_SUBJECTDN + " LIKE '%"
138: + matchvalue + "%'";
139: }
140:
141: } else {
142: if (matchtype == BasicMatch.MATCH_TYPE_EQUALS) {
143: // Because some databases (read JavaDB/Derby) does not allow matching of integer with a string expression
144: // like "where status='10'" instead of "where status=10", we have to hav e some special handling here.
145: String stringChar = "'";
146: if ((matchwith == MATCH_WITH_STATUS)
147: || (matchwith == MATCH_WITH_CA)
148: || (matchwith == MATCH_WITH_CERTIFICATEPROFILE)
149: || (matchwith == MATCH_WITH_ENDENTITYPROFILE)
150: || (matchwith == MATCH_WITH_TOKEN)) {
151: stringChar = "";
152: }
153: returnval = MATCH_WITH_SQLNAMES[matchwith]
154: + " = " + stringChar + matchvalue
155: + stringChar;
156: }
157:
158: if (matchtype == BasicMatch.MATCH_TYPE_BEGINSWITH) {
159: returnval = MATCH_WITH_SQLNAMES[matchwith]
160: + " LIKE '" + matchvalue + "%'";
161: }
162: }
163: }
164: }
165: return returnval;
166: }
167:
168: // getQueryString
169:
170: /**
171: * Checks if query data is ok.
172: *
173: * @return true if query is legal, false otherwise
174: */
175: public boolean isLegalQuery() {
176: if (matchvalue == null) {
177: return false;
178: }
179: return !(matchvalue.trim().equals(""));
180: }
181:
182: // Private Methods
183: private boolean isSubjectDNMatch() {
184: return this .matchwith >= 100 && this .matchwith < 200;
185: }
186:
187: // Private Methods
188: private boolean isSubjectAltNameMatch() {
189: return this .matchwith >= 200 && this .matchwith < 300;
190: }
191:
192: // Private Fields.
193: private int matchwith;
194: private int matchtype;
195: private String matchvalue;
196: }
|