KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > query > UserMatch


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 /*
15  * UserMatch.java
16  *
17  * Created on den 20 juli 2002, 23:20
18  */

19 package org.ejbca.util.query;
20
21 /**
22  * A class used by Query class to build a query for ejbca ra module. Inherits BasicMatch. Main
23  * function is getQueryString which returns a fragment of SQL statment.
24  *
25  * @author TomSelleck
26  * @version $Id: UserMatch.java,v 1.4.2.1 2007/05/04 09:10:26 anatom Exp $
27  *
28  * @see org.ejbca.util.query.BasicMatch
29  * @see org.ejbca.util.query.TimeMatch
30  * @see org.ejbca.util.query.LogMatch
31  */

32 public class UserMatch extends BasicMatch {
33     // Public Constants
34

35     public static final int MATCH_WITH_USERNAME = 0;
36     public static final int MATCH_WITH_EMAIL = 1;
37     public static final int MATCH_WITH_STATUS = 2; // Value must the number representation.
38
public static final int MATCH_WITH_ENDENTITYPROFILE = 3; // Matches the profile id not profilename.
39
public static final int MATCH_WITH_CERTIFICATEPROFILE = 4; // Matches the certificatetype id not name.
40
public static final int MATCH_WITH_CA = 5; // Matches the CA id not CA name.
41
public static final int MATCH_WITH_TOKEN = 6;
42     public static final int MATCH_WITH_DN = 7;
43     // Subject DN fields.
44
public static final int MATCH_WITH_UID = 100;
45     public static final int MATCH_WITH_COMMONNAME = 101;
46     public static final int MATCH_WITH_DNSERIALNUMBER = 102;
47     public static final int MATCH_WITH_GIVENNAME = 103;
48     public static final int MATCH_WITH_INITIALS = 104;
49     public static final int MATCH_WITH_SURNAME = 105;
50     public static final int MATCH_WITH_TITLE = 106;
51     public static final int MATCH_WITH_ORGANIZATIONUNIT = 107;
52     public static final int MATCH_WITH_ORGANIZATION = 108;
53     public static final int MATCH_WITH_LOCALE = 109;
54     public static final int MATCH_WITH_STATE = 110;
55     public static final int MATCH_WITH_DOMAINCOMPONENT = 111;
56     public static final int MATCH_WITH_COUNTRY = 112;
57     // Subject Altname Fields
58
public static final int MATCH_WITH_RFC822NAME = 200;
59     public static final int MATCH_WITH_DNSNAME = 201;
60     public static final int MATCH_WITH_IPADDRESS = 202;
61     public static final int MATCH_WITH_X400ADDRESS = 203;
62     public static final int MATCH_WITH_DIRECTORYNAME = 204;
63     public static final int MATCH_WITH_EDIPARTNAME = 205;
64     public static final int MATCH_WITH_URI = 206;
65     public static final int MATCH_WITH_REGISTEREDID = 207;
66     public static final int MATCH_WITH_UPN = 208;
67     public static final int MATCH_WITH_GUID = 209;
68
69
70
71     // Private Constants.
72
static final String JavaDoc[] MATCH_WITH_SQLNAMES = {"username", "subjectEmail", "status"
73                                                          , "endEntityProfileId", "certificateProfileId"
74                                                          , "cAId", "tokenType"};
75                                                          
76
77     // Represents the column names in ra userdata table.
78
private static final String JavaDoc MATCH_WITH_SUBJECTDN = "subjectDN";
79     private static final String JavaDoc[] MATCH_WITH_SUBJECTDN_NAMES = {
80         "UID=", "CN=", "SN=", "GIVENNAME=", "INITIALS=", "SURNAME=", "T=", "OU=", "O=", "L=", "ST=",
81         "DC", "C="
82     };
83     
84     private static final String JavaDoc MATCH_WITH_SUBJECTALTNAME = "subjectAltName";
85     private static final String JavaDoc[] MATCH_WITH_SUBJECTALTNAME_NAMES = {
86         "RFC822NAME=", "DNSNAME=", "IPADDRESS=", "X400ADDRESS=", "DIRECTORYNAME=",
87         "EDIPARTNAME=", "UNIFORMRESOURCEIDENTIFIER=", "REGISTEREDID=", "UPN=", "GUID="
88     };
89     // Public methods.
90

91     /**
92      * Creates a new instance of UserMatch.
93      *
94      * @param matchwith determines which field i userdata table to match with.
95      * @param matchtype determines how to match the field. SubjectDN fields can only be matched
96      * with 'begins with'.
97      * @param matchvalue the value to match with.
98      *
99      * @throws NumberFormatException if matchvalue constains illegal numbervalue when matching
100      * number field.
101      */

102     public UserMatch(int matchwith, int matchtype, String JavaDoc matchvalue)
103         throws NumberFormatException JavaDoc {
104         this.matchwith = matchwith;
105         this.matchtype = matchtype;
106         this.matchvalue = matchvalue;
107
108         if ((matchwith >= MATCH_WITH_STATUS) && (matchwith <= MATCH_WITH_CA)) {
109             new Integer JavaDoc(matchvalue);
110         }
111     }
112
113     /**
114      * Returns a SQL statement fragment from the given data.
115      *
116      * @return sql string
117      */

118     public String JavaDoc getQueryString() {
119         String JavaDoc returnval = "";
120
121         if (isSubjectDNMatch()) {
122             // Ignore MATCH_TYPE_EQUALS.
123
returnval = MATCH_WITH_SUBJECTDN + " LIKE '%" +
124             MATCH_WITH_SUBJECTDN_NAMES[matchwith - 100] + matchvalue + "%'";
125         }else{
126             if (isSubjectAltNameMatch()){
127                 returnval = MATCH_WITH_SUBJECTALTNAME + " LIKE '%" +
128                 MATCH_WITH_SUBJECTALTNAME_NAMES[matchwith - 200] + matchvalue + "%'";
129             }else {
130                 if(matchwith == MATCH_WITH_DN){
131                     if (matchtype == BasicMatch.MATCH_TYPE_EQUALS) {
132                         returnval = MATCH_WITH_SUBJECTDN + " = '" + matchvalue + "'";
133                     }
134
135                     if (matchtype == BasicMatch.MATCH_TYPE_BEGINSWITH) {
136                         returnval = MATCH_WITH_SUBJECTDN + " LIKE '" + matchvalue + "%'";
137                     }
138
139                     if (matchtype == BasicMatch.MATCH_TYPE_CONTAINS) {
140                         returnval = MATCH_WITH_SUBJECTDN + " LIKE '%" + matchvalue + "%'";
141                     }
142
143                 }else{
144                     if (matchtype == BasicMatch.MATCH_TYPE_EQUALS) {
145                         // Because some databases (read JavaDB/Derby) does not allow matching of integer with a string expression
146
// like "where status='10'" instead of "where status=10", we have to hav e some special handling here.
147
String JavaDoc stringChar = "'";
148                         if ((matchwith == MATCH_WITH_STATUS) || (matchwith == MATCH_WITH_CA) || (matchwith == MATCH_WITH_CERTIFICATEPROFILE) || (matchwith == MATCH_WITH_ENDENTITYPROFILE) || (matchwith == MATCH_WITH_TOKEN)) {
149                             stringChar = "";
150                         }
151                         returnval = MATCH_WITH_SQLNAMES[matchwith] + " = "+stringChar + matchvalue + stringChar;
152                     }
153
154                     if (matchtype == BasicMatch.MATCH_TYPE_BEGINSWITH) {
155                         returnval = MATCH_WITH_SQLNAMES[matchwith] + " LIKE '" + matchvalue + "%'";
156                     }
157                 }
158             }
159         }
160         return returnval;
161     }
162
163     // getQueryString
164

165     /**
166      * Checks if query data is ok.
167      *
168      * @return true if query is legal, false otherwise
169      */

170     public boolean isLegalQuery() {
171         if(matchvalue == null){
172             return false;
173         }
174         return !(matchvalue.trim().equals(""));
175     }
176
177     // Private Methods
178
private boolean isSubjectDNMatch() {
179         return this.matchwith >= 100 && this.matchwith < 200;
180     }
181     
182     // Private Methods
183
private boolean isSubjectAltNameMatch() {
184         return this.matchwith >= 200 && this.matchwith < 300;
185     }
186
187     // Private Fields.
188
private int matchwith;
189     private int matchtype;
190     private String JavaDoc matchvalue;
191 }
192
Popular Tags