001: /**
002: * $Id: Rewriter.java,v 1.12 2006/10/05 07:26:49 vp154433 Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.rewriter.admin.mbeans;
014:
015: import com.iplanet.sso.SSOToken;
016: import com.sun.identity.sm.ServiceConfig;
017: import com.sun.identity.sm.ServiceConfigManager;
018:
019: import java.util.*;
020: import java.util.logging.Logger;
021: import java.util.logging.Level;
022: import com.sun.portal.log.common.PortalLogger;
023:
024: import com.sun.portal.admin.server.mbeans.PSResource;
025: import com.sun.portal.admin.server.AdminServerUtil;
026: import com.sun.portal.admin.common.PSMBeanException;
027:
028: import com.sun.portal.rewriter.rom.RuleSetManager;
029: import com.sun.portal.rewriter.rom.InvalidXMLException;
030: import com.sun.portal.rewriter.services.idsame.IDSAMEDataService;
031: import com.sun.portal.rewriter.util.Constants;
032: import org.xml.sax.SAXParseException;
033: import org.xml.sax.SAXException;
034:
035: /**
036: *
037: * @author Francis Sujai
038: */
039: public class Rewriter extends PSResource implements RewriterMBean {
040:
041: private static final String SERVICE_NAME = "SunPortalRewriterService";
042: private static final String SERVICE_SCHEMA_NAME = "SunPortalRewriterGlobal";
043: private static final String ATTRIBUTE_NAME = "sunPortalRewriterRuleset";
044: private static final String SUBCONFIG_ID = "SunPortalRewriterRuleSets";
045:
046: // holds the config node SERVICE_SCHEMA_NAME
047: private ServiceConfig config;
048: private ServiceConfigManager configManager;
049:
050: public RuleSetManager rulesetManager;
051:
052: private static Logger _logger = PortalLogger
053: .getLogger(Rewriter.class);
054:
055: public Rewriter() {
056: config = null;
057: }
058:
059: public Rewriter(SSOToken aSSOToken) {
060: try {
061: config = getConfig(aSSOToken);
062: } catch (PSMBeanException e) {
063: //Internal Error
064: }
065: }
066:
067: private ServiceConfig getConfig(SSOToken token)
068: throws PSMBeanException {
069: try {
070: rulesetManager = new RuleSetManager(new IDSAMEDataService(
071: token));
072: configManager = new ServiceConfigManager(SERVICE_NAME,
073: token);
074: ServiceConfig globalConfig = configManager
075: .getGlobalConfig(null);
076: return globalConfig.getSubConfig(SERVICE_SCHEMA_NAME);
077: } catch (Exception e) {
078: _logger.log(Level.INFO, "PSRW_CSPR_ADM_MBEANS000", e);
079: throw new PSMBeanException(
080: "psadmin.error.rewriter.unknownerror");
081: }
082: }
083:
084: public Set listRules() throws PSMBeanException {
085: config = getConfig(AdminServerUtil.getSSOToken());
086: try {
087: Set rules = rulesetManager.getRuleSetNames();
088: final String[] listOfRules = (String[]) (rules
089: .toArray(Constants.EMPTY_STRING_ARRAY));
090: if (listOfRules.length == 0) {
091: throw new PSMBeanException(
092: "psadmin.error.rewriter.norulesetsfound");
093: }
094: return rules;
095: //return Collections.unmodifiableSet( config.getSubConfigNames() );
096: } catch (Exception e) {
097: _logger.log(Level.INFO, "PSRW_CSPR_ADM_MBEANS000", e);
098: throw new PSMBeanException(
099: "psadmin.error.rewriter.unknownerror");
100: }
101: }
102:
103: public String displayRule(String ruleSetID) throws PSMBeanException {
104: config = getConfig(AdminServerUtil.getSSOToken());
105: try {
106: final String xmlString = rulesetManager.retrieve(ruleSetID);
107:
108: Set rules = rulesetManager.getRuleSetNames();
109: if (!rules.contains(ruleSetID))
110: throw new PSMBeanException(
111: "psadmin.error.rewriter.doesntexists");
112:
113: if (xmlString == null) {
114: throw new PSMBeanException(
115: "psadmin.error.rewriter.rulesetxmlmissing");
116: }
117:
118: return xmlString;
119:
120: // ServiceConfig subConfig = config.getSubConfig( ruleSetID );
121: // if ( null == subConfig ) {
122: // return null;
123: // }
124: //
125: // Map attributes = subConfig.getAttributes();
126: // Set values = (Set) attributes.get( ATTRIBUTE_NAME );
127: // Iterator itr = values.iterator();
128: // return (String) itr.next();
129: } catch (PSMBeanException e) {
130: _logger.log(Level.INFO, "PSRW_CSPR_ADM_MBEANS000", e);
131: throw new PSMBeanException(e.getErrorKey());
132: }
133:
134: }
135:
136: public Boolean storeRule(String aXMLRuleSet)
137: throws PSMBeanException {
138:
139: config = getConfig(AdminServerUtil.getSSOToken());
140: // HashSet set = new HashSet( 1 );
141: // Map map = new HashMap( 1 );
142: // set.add( aXMLRuleSet );
143: // map.put( ATTRIBUTE_NAME, set );
144: // config.addSubConfig( rule.toLowerCase(), SUBCONFIG_ID, 0, map);
145: // return Boolean.TRUE;
146:
147: if (aXMLRuleSet.trim().length() == 0) {
148: throw new PSMBeanException(
149: "psadmin.error.rewriter.xmlcontentinvalid");
150: } else {
151: try {
152: String matchID = rulesetManager
153: .matchesWithID(aXMLRuleSet);
154: if (matchID == null) {
155: rulesetManager.store(aXMLRuleSet);
156: return Boolean.TRUE;
157: } else {
158: _logger.log(Level.SEVERE,
159: "PSRW_CSPR_ADM_MBEANS001", matchID);
160: throw new PSMBeanException(
161: "psadmin.error.rewriter.alreadyexists");
162: }
163: } catch (InvalidXMLException e) {
164: _logger.log(Level.INFO, "PSRW_CSPR_ADM_MBEANS000", e);
165: processException(e);
166: //throw new PSMBeanException("psadmin.error.rewriter.xmlcontentinvalid");
167: }
168: }//if
169: return Boolean.TRUE;
170: }
171:
172: public Boolean removeRule(String ruleSetID) throws PSMBeanException {
173: config = getConfig(AdminServerUtil.getSSOToken());
174: String result = null;
175: //config.removeSubConfig( ruleSetID );
176: Set rules = rulesetManager.getRuleSetNames();
177: if (!rules.contains(ruleSetID))
178: throw new PSMBeanException(
179: "psadmin.error.rewriter.doesntexists");
180:
181: result = rulesetManager.delete(ruleSetID);
182: if (result != null) {
183: return Boolean.TRUE;
184: }
185: throw new PSMBeanException(
186: "psadmin.error.rewriter.rulesetxmlmissing");
187: }
188:
189: public Boolean setRule(String aXMLRuleSet) throws PSMBeanException {
190:
191: config = getConfig(AdminServerUtil.getSSOToken());
192: try {
193: String matchID = rulesetManager.matchesWithID(aXMLRuleSet);
194: if (matchID != null) {
195: rulesetManager.store(aXMLRuleSet);
196: return Boolean.TRUE;
197: } else {
198: throw new PSMBeanException(
199: "psadmin.error.rewriter.doesntexists");
200: /*config.removeSubConfig( matchID );
201: HashSet set = new HashSet( 1 );
202: Map map = new HashMap( 1 );
203: set.add( aXMLRuleSet );
204: map.put( ATTRIBUTE_NAME, set );
205: config.addSubConfig( matchID.toLowerCase(), SUBCONFIG_ID, 0, map );
206: return Boolean.TRUE;*/
207: }
208: } catch (PSMBeanException e) {
209: _logger.log(Level.INFO, "PSRW_CSPR_ADM_MBEANS000", e);
210: throw new PSMBeanException(e.getErrorKey());
211: } catch (InvalidXMLException e) {
212: _logger.log(Level.INFO, "PSRW_CSPR_ADM_MBEANS000", e);
213: //throw new PSMBeanException("psadmin.error.rewriter.xmlcontentinvalid");
214: processException(e);
215: } catch (Exception e) {
216: _logger.log(Level.INFO, "PSRW_CSPR_ADM_MBEANS000", e);
217: throw new PSMBeanException(
218: "psadmin.error.rewriter.unknownerror");
219: }
220: return Boolean.TRUE;
221: }
222:
223: protected void processException(InvalidXMLException ixe)
224: throws PSMBeanException {
225: int code = ixe.getLocaleID();
226: Throwable t = ixe.getCause();
227: switch (code) {
228: case InvalidXMLException.SAX_EXCEPTION:
229: if ((t != null) && (t instanceof SAXParseException)) {
230: SAXParseException spe = (org.xml.sax.SAXParseException) t;
231: Object[] params = {
232: new Integer(spe.getLineNumber()).toString(),
233: new Integer(spe.getColumnNumber()).toString(),
234: spe.getMessage(),
235:
236: };
237: throw new PSMBeanException(
238: "psadmin.error.rewriter.saxparseerr.msg",
239: params);
240: } else if ((t != null) && (t instanceof SAXException)) {
241: SAXException se = (org.xml.sax.SAXException) t;
242: Object[] params = { se.getMessage() };
243: throw new PSMBeanException(
244: "psadmin.error.rewriter.saxerr.msg", params);
245: } else {
246: throw new PSMBeanException(
247: "psadmin.error.rewriter.generr.msg");
248: }
249: case InvalidXMLException.KEY_DOES_NOT_EXIST:
250: throw new PSMBeanException(
251: "psadmin.error.rewriter.keyerr.msg");
252: case InvalidXMLException.REPEATED_TAG:
253: throw new PSMBeanException(
254: "psadmin.error.rewriter.reptagerr.msg");
255: case InvalidXMLException.INVALID_ID:
256: Object[] params = { ixe.getErrorInfo() };
257: throw new PSMBeanException(
258: "psadmin.error.rewriter.invalididerr.msg", params);
259: default:
260: throw new PSMBeanException(
261: "psadmin.error.rewriter.generr.msg");
262: }
263: }
264:
265: // public static void main( String[] args ) {
266: // Rewriter rwadmin = new Rewriter(AdminServerUtil.getSSOToken());
267: // System.out.println("Rule 0 is"+ rwadmin.getRule("","","wml_ruleset"));
268: // System.out.println("Rule 1 is"+ rwadmin.getRule("","","default_ruleset"));
269: // }
270:
271: }
|