001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.admin.model;
006:
007: import java.util.Set;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.ArrayList;
011: import java.util.HashMap;
012: import java.util.Arrays;
013: import java.util.Collections;
014: import java.util.StringTokenizer;
015: import java.util.ResourceBundle;
016: import java.text.MessageFormat;
017:
018: import javax.servlet.http.HttpServletRequest;
019:
020: import org.xml.sax.SAXParseException;
021: import org.xml.sax.SAXException;
022:
023: import com.iplanet.sso.SSOException;
024: import com.iplanet.sso.SSOTokenManager;
025: import com.iplanet.am.console.base.model.AMModelBase;
026:
027: import com.sun.portal.rewriter.util.Resource;
028: import com.sun.portal.rewriter.rom.RuleSetManager;
029: import com.sun.portal.rewriter.rom.InvalidXMLException;
030: import com.sun.portal.rewriter.services.DataServiceException;
031: import com.sun.portal.rewriter.services.DataService;
032: import com.sun.portal.rewriter.services.idsame.IDSAMEDataService;
033:
034: /**
035: * Model implemenation for the Rewriter admin UI
036: * This model implemented is instantiated by all the Rewriter admin viewbeans
037: */
038: public class RewriterModelImpl extends AMModelBase implements
039: RewriterModel {
040:
041: private static final String TOKEN_DELIMITER = "\u0C20";
042:
043: private final RuleSetManager rulesetManager;
044:
045: /**
046: * Constructor for the Rewriter Model Implementation
047: * @param req Current HTTP Request
048: */
049: public RewriterModelImpl(HttpServletRequest req) {
050: super (req, "psrwadminmsg");
051: resBundle = ResourceBundle.getBundle("psrwadminmsg",
052: getUserLocale());
053: DataService lDataService = null;
054: try {
055: lDataService = new IDSAMEDataService(SSOTokenManager
056: .getInstance().createSSOToken(req));
057: } catch (Exception e) {
058: throw new DataServiceException(e.getMessage(), e);
059: }
060: rulesetManager = new RuleSetManager(lDataService);
061: }
062:
063: /**
064: * Creates a String from the List of values that was given
065: * @param values List of values to create a delimitied string
066: * @return A dilimited String
067: */
068: protected String getDelimitedValue(List values) {
069: StringBuffer sb = new StringBuffer();
070: if (values != null && values.size() > 0) {
071: for (int i = 0; i < values.size(); i++) {
072: sb.append((String) values.get(i));
073: if (i < (values.size() - 1)) {
074: sb.append(TOKEN_DELIMITER);
075: }
076: }
077: }
078: return sb.toString();
079: }
080:
081: /**
082: * Gets a list of values from the dilimited String
083: * @param value String to get the values from
084: * @return List of values retrived from the String
085: */
086: protected List getUnDelimitedValue(String value) {
087: StringTokenizer st = new StringTokenizer(value, TOKEN_DELIMITER);
088: List result = new ArrayList();
089: while (st.hasMoreTokens()) {
090: String token = st.nextToken();
091: result.add(token);
092: }
093: return result;
094: }
095:
096: /**
097: * Stores values in the session
098: * @param name Key of the value to be stored in the session
099: * @param values List of values to be stored
100: */
101: public void storeToSession(String name, List values) {
102: String delimitedValue = null;
103: if ((name != null) && (!name.equals("")) && (values != null)) {
104: delimitedValue = getDelimitedValue(values);
105: }
106:
107: try {
108: ssoToken.setProperty(name, delimitedValue);
109: } catch (SSOException ssoe) {
110: debug.error("RewriterModelImpl.storeToSession() ", ssoe);
111: }
112: }
113:
114: /**
115: * Gets values stored in the session
116: * @param name Key of the values to be retrived from the session
117: * @return a List of values from the session
118: */
119: public List getFromSession(String name) {
120: String delimitedValue = null;
121: try {
122: delimitedValue = ssoToken.getProperty(name);
123: } catch (SSOException ssoe) {
124: debug.error("RewriterModelImpl.getFromSession() ", ssoe);
125: }
126: return getUnDelimitedValue(delimitedValue);
127: }
128:
129: /**
130: * Gets the Ruleset XML from the XML template
131: * @return ruleset XML Template as String
132: */
133: public String getRulesetXMLTemplate() {
134: String template = Resource
135: .read("/resources/RuleSetTemplate.xml");
136: if (template == null) {
137: template = "";
138: debug.error("RewriterModelImpl.getRulesetXMLTemplate()::"
139: + "Template ruleset XML is null or not found");
140: }
141: return template;
142: }
143:
144: /**
145: * Gets the Ruleset XML for a given ruleset ID
146: * @param ruleSetName Ruleset name as String
147: * @return RuleSet XML as String
148: */
149: public String getRulesetXML(String ruleSetName) {
150: String ruleSetXML = "";
151: try {
152: ruleSetXML = rulesetManager.retrieve(ruleSetName);
153: } catch (Exception e) {
154: debug.error("RewriterModelImpl.getRulesetXML()::"
155: + "Failed to retrive XML for ruleset: "
156: + ruleSetName, e);
157: }
158: return ruleSetXML;
159: }
160:
161: /**
162: * Called by the CreateRuleViewbean to check if the current ruleset is
163: * overwriting an existing rule.
164: * @param xml New ruleset XML as String
165: * @throws InvalidXMLException when the XML input is not valid RuleSet XML
166: * @return true if a ruleSet exists with the same ID as new ruleset that
167: * is created
168: */
169: public boolean isCreateOverwrite(String xml) throws Exception {
170: boolean isOverwrite = false;
171: try {
172: isOverwrite = ((rulesetManager.matchesWithID(xml)) != null);
173: } catch (DataServiceException dse) {
174: debug.error("RewriterModelImpl.isCreateOverwrite()", dse);
175: } catch (InvalidXMLException ixe) {
176: if (debug.warningEnabled()) {
177: debug.warning("Failed to create a new ruleset:", ixe);
178: }
179: throw new Exception(processException(ixe));
180: }
181:
182: return isOverwrite;
183: }
184:
185: /**
186: * Saves the modified XML
187: * @param xml XML data to save
188: * @param isEdit value is set to true if the save operation is done after an
189: * edit operation
190: * @throws InvalidXMLException when the XML data is not a valid Ruleset XML
191: */
192: public void saveRulesetXML(String xml, boolean isEdit)
193: throws Exception {
194:
195: try {
196: rulesetManager.store(xml);
197: if (isEdit) {
198: logger.doLog(getLocalizedString("ruleset.modified"));
199: if (debug.messageEnabled()) {
200: debug.message("Ruleset modified");
201: }
202: } else {
203: logger.doLog(getLocalizedString("ruleset.created"));
204: if (debug.messageEnabled()) {
205: debug.message("Ruleset new created");
206: }
207: }
208: } catch (InvalidXMLException ixe) {
209: if (debug.warningEnabled()) {
210: debug.warning("Failed to save ruleset:", ixe);
211: }
212: throw new Exception(processException(ixe));
213: }
214: }
215:
216: /**
217: * Deletes the Rulesets
218: * @param rules a List of Ruleset IDs
219: * @throws Exception When the delete operation fails
220: */
221: public Map deleteRules(List rules) {
222:
223: Map failedRules = Collections.EMPTY_MAP;
224: if ((rules != null) && (rules.size() > 0)) {
225: String rule;
226:
227: for (int i = 0; i < rules.size(); i++) {
228: rule = (String) rules.get(i);
229: try {
230: rulesetManager.delete(rule);
231: logger.doLog(getLocalizedString("ruleset.deleted")
232: + rule);
233: } catch (Exception e) {
234: if (failedRules.equals(Collections.EMPTY_MAP)) {
235: failedRules = new HashMap();
236: }
237: failedRules.put(rule, e);
238: debug.error("RewriterModelImpl.deleteRules()", e);
239: }
240: }
241: }
242: return failedRules;
243: }
244:
245: /**
246: * This method is called by the EditRuleSet viewbean to check if another
247: * ruleset exists with the same ID as in the edited xml ruleset.
248: * @param ruleOrigName Original name of the ruleset that is being edited
249: * @param editedXML Modified XML as String
250: * @throws InvalidXMLException when the input XML data is not a
251: * valid RuleSet XML
252: * @return true if another ruleset exists with the same ID
253: */
254: public boolean isEditOverwrite(String ruleOrigName, String editedXML)
255: throws Exception {
256: boolean isOverwrite = false;
257: try {
258: String newRuleSetName = rulesetManager
259: .matchesWithID(editedXML);
260: // During the edit operation the user can modify the ID(which is the
261: // display name) of the ruleset XML.
262: // If there is a rule existing with the same ID as the one the
263: // current ruleset is being renamed to. Then by saving this XML we
264: // are overwriting another ruleset that has the same ID which is
265: // not acceptable.
266: if (newRuleSetName != null) {
267: if (!newRuleSetName.equals(ruleOrigName)) {
268: // if ruleset ID in modified XML matches with ID of another
269: // ruleset existing in the backend then we are setting
270: // overwriter flag to true.
271: isOverwrite = true;
272: }
273: } else {
274: // If newRuleSetName is null - Then the user has renamed the
275: // current ruleset. So we delete the original ruleset and create
276: // a new ruleset with the modifications.
277: rulesetManager.delete(ruleOrigName);
278: }
279:
280: } catch (DataServiceException dse) {
281: debug.error("RewriterModelImpl.isOverwrite():", dse);
282: } catch (InvalidXMLException ixe) {
283: if (debug.warningEnabled()) {
284: debug.warning("Edit ruleset failed:", ixe);
285: }
286: throw new Exception(processException(ixe));
287: }
288:
289: return isOverwrite;
290: }
291:
292: /**
293: * Gets the Rulesets available
294: * @return Ruleset names as a List
295: */
296: public List getRuleSetNames() {
297: List rulesetList = Collections.EMPTY_LIST;
298: try {
299: Set ruleset = rulesetManager.getRuleSetNames();
300: Object[] rulesArray = ruleset.toArray();
301: Arrays.sort(rulesArray);
302: rulesetList = Arrays.asList(rulesArray);
303: } catch (Exception e) {
304: debug.error("RewriterModelImpl.getRuleSetNames(): ", e);
305: }
306:
307: return rulesetList;
308: }
309:
310: /**
311: * Handles the InvalidXMLException and returns a localized error msg.
312: * @param ixe the InvalidXMLException thrown by the rewriter API.
313: */
314: protected String processException(InvalidXMLException ixe) {
315: int code = ixe.getLocaleID();
316: Throwable t = ixe.getCause();
317: String pattern = null;
318: String msg = "";
319: switch (code) {
320: case InvalidXMLException.SAX_EXCEPTION:
321: if ((t != null) && (t instanceof SAXParseException)) {
322: SAXParseException spe = (org.xml.sax.SAXParseException) t;
323: pattern = getLocalizedString("saverulemsgbox.saxparseerr.msg");
324: if ((pattern != null) && (pattern != "")) {
325: MessageFormat mf = new MessageFormat(pattern);
326: mf.setLocale(getUserLocale());
327: Object[] args = new Object[] {
328: new Integer(spe.getLineNumber()),
329: new Integer(spe.getColumnNumber()),
330: spe.getMessage() };
331: msg = mf.format(args);
332: }
333: } else if ((t != null) && (t instanceof SAXException)) {
334: SAXException se = (org.xml.sax.SAXException) t;
335: pattern = getLocalizedString("saverulemsgbox.saxerr.msg");
336: if ((pattern != null) && (pattern != "")) {
337: MessageFormat mf = new MessageFormat(pattern);
338: mf.setLocale(getUserLocale());
339: Object[] arg = new Object[] { se.getMessage() };
340: msg = mf.format(arg);
341: }
342: } else {
343: msg = getLocalizedString("saverulemsgbox.generr.msg");
344: }
345: break;
346:
347: case InvalidXMLException.KEY_DOES_NOT_EXIST:
348: msg = getLocalizedString("saverulemsgbox.keyerr.msg");
349: break;
350: case InvalidXMLException.REPEATED_TAG:
351: msg = getLocalizedString("saverulemsgbox.reptagerr.msg");
352: break;
353: case InvalidXMLException.INVALID_ID:
354: msg = ixe.getErrorInfo()
355: + " "
356: + getLocalizedString("saverulemsgbox.invalididerr.msg");
357: break;
358: default:
359: msg = getLocalizedString("saverulemsgbox.generr.msg");
360: }
361:
362: return msg;
363: }
364:
365: /**
366: * Public wrapper for protected getHelpURL method in AMModelBase.
367: */
368: public String getHelpUrl(String docName) {
369: return getHelpURL(docName);
370: }
371:
372: }
|