001: package com.sun.portal.search.admin.mbeans.tasks;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.util.logging.*;
006: import java.net.*;
007: import java.lang.*;
008: import java.text.*;
009:
010: import java.security.AccessController;
011: import com.iplanet.am.util.AdminUtils;
012: import com.sun.identity.security.EncryptAction;
013:
014: import com.sun.portal.admin.server.AdminServerUtil;
015: import com.sun.portal.admin.server.mbeans.PSResource;
016: import com.sun.portal.admin.common.context.PortalDomainContext;
017: import com.sun.portal.admin.common.context.PSConfigContext;
018: import com.sun.portal.admin.common.PSMBeanException;
019:
020: import com.iplanet.sso.*;
021:
022: import com.sun.portal.search.util.*;
023: import com.sun.portal.search.soif.*;
024: import com.sun.portal.search.rdm.*;
025: import com.sun.portal.search.rdmserver.*;
026: import com.sun.portal.search.db.*;
027: import com.sun.portal.search.rdmgr.*;
028:
029: import com.sun.portal.util.Platform;
030:
031: import com.sun.portal.search.admin.mbeans.*;
032:
033: public class ResourceDescription {
034:
035: private final static int MAX_RESOURCE_DESCRIPTIONS = 99999;
036:
037: private final static String ALL = "all";
038: private final static String UNCATEGORIZED = "uncategorized";
039: private final static String CATEGORIZED = "categorized";
040: private final static String BY_CATEGORY = "by_category";
041: private final static String BY_URL = "by_url";
042: private final static String BY_QUERY = "by_query";
043:
044: private String searchServerRoot = null;
045: private String searchServerURL = null;
046: private String documentSecurityLevel = null;
047: private static Logger logger = null;
048:
049: public ResourceDescription(String searchServerRoot,
050: String searchServerURL, String documentSecurityLevel,
051: Logger logger) {
052: this .searchServerRoot = searchServerRoot;
053: this .searchServerURL = searchServerURL;
054: this .documentSecurityLevel = documentSecurityLevel;
055: this .logger = logger;
056: }
057:
058: public ArrayList getAll(String searchType, String query,
059: List viewAttributes, int startPage, int hitsPerPage,
060: String database) throws PSMBeanException {
061:
062: if (viewAttributes.size() == 0) {
063: try {
064: String fileName = searchServerRoot + File.separator
065: + "config" + File.separator + "schema.rdm";
066: SOIFInputStream is = new SOIFInputStream(fileName);
067: RDMSchema schema = new RDMSchema(is.readSOIF());
068: for (int index = 0; index <= schema.getMaxIndex(); index++) {
069: String attribute = schema.getSOIFAttribute(index);
070: if (attribute != null) {
071: viewAttributes.add(attribute);
072: }
073: }
074: } catch (Exception e) {
075: viewAttributes.add("url");
076: }
077: }
078:
079: if (startPage == 0) {
080: startPage = 1;
081: }
082:
083: if (hitsPerPage == 0) {
084: hitsPerPage = MAX_RESOURCE_DESCRIPTIONS;
085: }
086:
087: return search(searchType, query, viewAttributes, startPage,
088: hitsPerPage, database);
089: }
090:
091: public void create(byte[] fileData, String database)
092: throws PSMBeanException {
093: try {
094: String fileName = searchServerRoot + File.separator + "tmp"
095: + File.separator + "createrd."
096: + Long.toString(System.currentTimeMillis());
097:
098: FileOutputStream fos = new FileOutputStream(fileName);
099: fos.write(fileData);
100: fos.close();
101:
102: String command = Platform.getCommand("run-cs-cli");
103: String option = (!database.equals("")) ? " -y " + database
104: : "";
105: String call = searchServerRoot + File.separator + command
106: + " rdmgr" + option + " " + fileName;
107: Runtime rt = Runtime.getRuntime();
108: Process p = rt.exec(call);
109: p.waitFor();
110: int value = p.exitValue();
111: if (value != 0) {
112: String message = "Create resource description failed with "
113: + value;
114: Object tokens[] = { message };
115: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
116: throw new PSMBeanException("PSALI_CSPACCSH0001");
117: }
118: } catch (Exception e) {
119: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
120: throw new PSMBeanException("PSALI_CSPACCSH0001", e
121: .toString(), e);
122: }
123: }
124:
125: public void create(Map attributes, String database)
126: throws PSMBeanException {
127: try {
128: SOIF soif = new SOIF();
129: soif.setSchemaName("DOCUMENT");
130:
131: Iterator i = attributes.keySet().iterator();
132: while (i.hasNext()) {
133: String attribute = (String) i.next();
134: ArrayList values = (ArrayList) attributes
135: .get(attribute);
136: int valueSize = values.size();
137:
138: if (attribute.equals("url")) {
139: if (valueSize == 0) {
140: String message = "No value for attribute: "
141: + attribute;
142: Object tokens[] = { message };
143: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002",
144: tokens);
145: throw new PSMBeanException("PSALI_CSPACCSH0001");
146: } else {
147: String value = (String) values.get(0);
148: if (value.equals("")) {
149: String message = "No value for attribute: "
150: + attribute;
151: Object tokens[] = { message };
152: logger.log(Level.SEVERE,
153: "PSSH_CSPSAMB0002", tokens);
154: throw new PSMBeanException(
155: "PSALI_CSPACCSH0001");
156: } else {
157: soif.setURL(value);
158: }
159: }
160: } else if (valueSize > 0) {
161: for (int index = 0; index < valueSize; index++) {
162: String value = (String) values.get(index);
163: soif.insert(attribute, value, index);
164: }
165: }
166: }
167:
168: String fileName = searchServerRoot + File.separator + "tmp"
169: + File.separator + "createrd."
170: + Long.toString(System.currentTimeMillis());
171: SOIFOutputStream so = new SOIFOutputStream(fileName);
172: so.write(soif);
173: so.close();
174:
175: String command = Platform.getCommand("run-cs-cli");
176: String option = (!database.equals("")) ? " -y " + database
177: : "";
178: String call = searchServerRoot + File.separator + command
179: + " rdmgr" + option + " " + fileName;
180: Runtime rt = Runtime.getRuntime();
181: Process p = rt.exec(call);
182: p.waitFor();
183: int value = p.exitValue();
184: if (value != 0) {
185: String message = "Create resource description failed with "
186: + value;
187: Object tokens[] = { message };
188: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
189: throw new PSMBeanException("PSALI_CSPACCSH0001");
190: }
191: } catch (Exception e) {
192: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
193: throw new PSMBeanException("PSALI_CSPACCSH0001", e
194: .toString(), e);
195: }
196: }
197:
198: public void delete(List urls, String database)
199: throws PSMBeanException {
200: try {
201: String fileName = searchServerRoot + File.separator + "tmp"
202: + File.separator + "deleterd."
203: + Long.toString(System.currentTimeMillis());
204: SOIFOutputStream so = new SOIFOutputStream(fileName);
205: for (int index = 0; index < urls.size(); index++) {
206: SOIF soif = new SOIF("DOCUMENT", (String) urls
207: .get(index));
208: so.write(soif);
209: }
210: so.close();
211:
212: String command = Platform.getCommand("run-cs-cli");
213: String option = (!database.equals("")) ? " -y " + database
214: : "";
215: String call = searchServerRoot + File.separator + command
216: + " rdmgr -d -q" + option + " " + fileName;
217: Runtime rt = Runtime.getRuntime();
218: Process p = rt.exec(call);
219: p.waitFor();
220: int value = p.exitValue();
221: if (value != 0) {
222: String message = "Delete resource description failed with "
223: + value;
224: Object tokens[] = { message };
225: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
226: throw new PSMBeanException("PSALI_CSPACCSH0001");
227: }
228: } catch (Exception e) {
229: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
230: throw new PSMBeanException("PSALI_CSPACCSH0001", e
231: .toString(), e);
232: }
233: }
234:
235: public void edit(String fileData, String database)
236: throws PSMBeanException {
237: try {
238: String fileName = searchServerRoot + File.separator + "tmp"
239: + File.separator + "editrd."
240: + Long.toString(System.currentTimeMillis());
241:
242: FileWriter fw = new FileWriter(fileName);
243: fw.write(fileData);
244: fw.close();
245:
246: String command = Platform.getCommand("run-cs-cli");
247: String option = (!database.equals("")) ? " -y " + database
248: : "";
249: String call = searchServerRoot + File.separator + command
250: + " rdmgr -m -P" + option + " " + fileName;
251:
252: Runtime rt = Runtime.getRuntime();
253: Process p = rt.exec(call);
254: p.waitFor();
255: int value = p.exitValue();
256: if (value != 0) {
257: String message = "Edit resource description failed with "
258: + value;
259: Object tokens[] = { message };
260: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
261: throw new PSMBeanException("PSALI_CSPACCSH0001");
262: }
263: } catch (Exception e) {
264: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
265: throw new PSMBeanException("PSALI_CSPACCSH0001", e
266: .toString(), e);
267: }
268: }
269:
270: public void edit(List urls, Map attributes, String database)
271: throws PSMBeanException {
272: try {
273: if (urls.size() == 0) {
274: ArrayList list = new ArrayList();
275: list.add("url");
276: ArrayList data = search(ALL, "", list, 1,
277: MAX_RESOURCE_DESCRIPTIONS, database);
278: ArrayList al = (ArrayList) data.get(3);
279: for (int index = 0; index < al.size(); index++) {
280: HashMap hm = (HashMap) al.get(index);
281: ArrayList values = (ArrayList) hm.get("url");
282: urls.add((String) values.get(0));
283: }
284: }
285:
286: String fileName = searchServerRoot + File.separator
287: + "config" + File.separator + "schema.rdm";
288: SOIFInputStream is = new SOIFInputStream(fileName);
289: RDMSchema schema = new RDMSchema(is.readSOIF());
290:
291: fileName = searchServerRoot + File.separator + "tmp"
292: + File.separator + "editrd."
293: + Long.toString(System.currentTimeMillis());
294: SOIFOutputStream so = new SOIFOutputStream(fileName);
295:
296: for (int urlIndex = 0; urlIndex < urls.size(); urlIndex++) {
297: String url = (String) urls.get(urlIndex);
298:
299: SOIF soif = new SOIF("DOCUMENT", url);
300:
301: Iterator i = attributes.keySet().iterator();
302: while (i.hasNext()) {
303: String attribute = (String) i.next();
304: String isEditable = schema.getValue(attribute,
305: RDM.A_RDM_EDIT_ATTR);
306: if (isEditable != null && isEditable.equals("1")) {
307: ArrayList values = (ArrayList) attributes
308: .get(attribute);
309: int valueSize = values.size();
310: if (valueSize == 0) {
311: soif.insert(attribute, "");
312: } else {
313: for (int valueIndex = 0; valueIndex < valueSize; valueIndex++) {
314: String value = (String) values
315: .get(valueIndex);
316: soif.insert(attribute, value,
317: valueIndex);
318: }
319: }
320: } else {
321: String message = "Attribute is not editable: "
322: + url + " - " + attribute;
323: Object tokens[] = { message };
324: logger.log(Level.INFO, "PSSH_CSPSAMB0001",
325: tokens);
326: }
327: }
328:
329: so.write(soif);
330: }
331:
332: so.close();
333:
334: String command = Platform.getCommand("run-cs-cli");
335: String option = (!database.equals("")) ? " -y " + database
336: : "";
337: String call = searchServerRoot + File.separator + command
338: + " rdmgr -m -P" + option + " " + fileName;
339: Runtime rt = Runtime.getRuntime();
340: Process p = rt.exec(call);
341: p.waitFor();
342: int value = p.exitValue();
343: if (value != 0) {
344: String message = "Edit resource description failed with "
345: + value;
346: Object tokens[] = { message };
347: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
348: throw new PSMBeanException("PSALI_CSPACCSH0001");
349: }
350: } catch (Exception e) {
351: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
352: throw new PSMBeanException("PSALI_CSPACCSH0001", e
353: .toString(), e);
354: }
355: }
356:
357: private ArrayList search(String searchType, String query,
358: List viewAttributes, int startPage, int hitsPerPage,
359: String database) throws PSMBeanException {
360: String queryLanguage = "";
361: String scope = "";
362: if (searchType.equals(ALL)) {
363: queryLanguage = "search";
364: scope = "*";
365: } else if (searchType.equals(UNCATEGORIZED)) {
366: queryLanguage = "search";
367: scope = "<not> (CLASSIFICATION!=\"\")";
368: } else if (searchType.equals(CATEGORIZED)) {
369: queryLanguage = "search";
370: scope = "(Classification!=\"\")";
371: } else if (searchType.equals(BY_CATEGORY)) {
372: queryLanguage = "search";
373: scope = "(Classification=\"" + query + "\")";
374: } else if (searchType.equals(BY_URL)) {
375: queryLanguage = "url";
376: scope = query;
377: } else if (searchType.equals(BY_QUERY)) {
378: queryLanguage = "search";
379: scope = query;
380: } else {
381: String message = "Unknown search type - " + searchType;
382: Object tokens[] = { message };
383: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
384: throw new PSMBeanException("PSALI_CSPACCSH0001");
385: }
386:
387: RDMHeader rdmh = null;
388: try {
389: rdmh = new RDMHeader("rd-request", null, queryLanguage,
390: null);
391: } catch (Exception e) {
392: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
393: throw new PSMBeanException("PSALI_CSPACCSH0001", e
394: .toString(), e);
395: }
396: if (documentSecurityLevel.equals("ON")) {
397: String user = AdminUtils.getAdminDN();
398: String password = (String) AccessController
399: .doPrivileged(new EncryptAction(new String(
400: AdminUtils.getAdminPassword())));
401: rdmh.setUser(user);
402: if (password != null) {
403: rdmh.setPassword(password);
404: }
405: }
406:
407: RDMQuery rdmq = new RDMQuery(scope);
408:
409: StringBuffer sb = new StringBuffer();
410: for (int index = 0; index < viewAttributes.size(); index++) {
411: if (sb.length() > 0) {
412: sb.append(",");
413: }
414: sb.append((String) viewAttributes.get(index));
415: }
416: rdmq.setViewAttr(sb.toString());
417:
418: int startHit = (startPage - 1) * hitsPerPage + 1;
419: String viewHits = startHit + ".."
420: + (startHit + hitsPerPage - 1);
421: rdmq.setViewHits(viewHits);
422:
423: if (!database.equals("")) {
424: rdmq.setDatabase(database);
425: }
426:
427: SOIFInputStream is = null;
428: try {
429: SOIFBuffer soifb = new SOIFBuffer();
430: SOIFOutputStream soifos = new SOIFOutputStream(soifb);
431: soifos.write(rdmh.getSOIF());
432: soifos.write(rdmq.getSOIF());
433:
434: URLConnection urlc = new URL(searchServerURL + "/search")
435: .openConnection();
436: urlc.setAllowUserInteraction(true);
437: urlc.setDoOutput(true);
438: urlc.getOutputStream().write(soifb.toByteArray());
439:
440: is = new SOIFInputStream(urlc.getInputStream());
441: } catch (Exception e) {
442: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
443: throw new PSMBeanException("PSALI_CSPACCSH0001", e
444: .toString(), e);
445: }
446:
447: ArrayList data = new ArrayList();
448:
449: try {
450: RDMHeader rdmHeader = new RDMHeader(is);
451: String ri = rdmHeader.getResponseInterpret();
452: int results = getKeyedNumber(ri, "results");
453: int totalHits = (searchType.equals(BY_URL) && results == 1) ? 1
454: : getKeyedNumber(ri, "hits");
455: int totalPages = totalHits / hitsPerPage;
456: if (totalPages * hitsPerPage < totalHits) {
457: totalPages += 1;
458: }
459:
460: data.add(0, new Integer(totalHits));
461: data.add(1, new Integer(startPage));
462: data.add(2, new Integer(totalPages));
463: } catch (Exception e) {
464: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
465: throw new PSMBeanException("PSALI_CSPACCSH0001", e
466: .toString(), e);
467: }
468:
469: ArrayList values = new ArrayList();
470: try {
471: while (!is.isEOS()) {
472: SOIF soif = is.readSOIF();
473: if (soif != null
474: && soif.getSchemaName().equals("DOCUMENT")) {
475: HashMap hm = new HashMap();
476: for (int index1 = 0; index1 < viewAttributes.size(); index1++) {
477: String attribute = (String) viewAttributes
478: .get(index1);
479: ArrayList al = new ArrayList();
480: if (attribute.equalsIgnoreCase("url")) {
481: al.add(soif.getURL());
482: } else {
483: String[] strings = soif
484: .getStringValues(attribute);
485: if (strings != null) {
486: for (int index2 = 0; index2 < strings.length; index2++) {
487: al.add(strings[index2]);
488: }
489: }
490: }
491: hm.put(attribute, al);
492: }
493: values.add(hm);
494: }
495: }
496: } catch (Exception e) {
497: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
498: throw new PSMBeanException("PSALI_CSPACCSH0001", e
499: .toString(), e);
500: }
501: data.add(3, values);
502:
503: return data;
504: }
505:
506: private int getKeyedNumber(String str, String key) {
507: int endIndex = str.indexOf(" " + key);
508: if (endIndex == -1) {
509: return 0;
510: } else {
511: int startIndex = str.lastIndexOf(' ', endIndex - 1);
512: String value = str.substring(startIndex + 1, endIndex);
513: return new Integer(value).intValue();
514: }
515: }
516:
517: }
|