001: /******************************************************************************
002: * Option.java
003: * ****************************************************************************/package org.openlaszlo.server;
004:
005: import java.io.*;
006: import java.util.*;
007:
008: import org.apache.log4j.Logger;
009: import org.jdom.*;
010: import org.apache.regexp.RE;
011: import org.apache.regexp.RESyntaxException;
012:
013: /**
014: * An Option contains a set of deny and allow patterns.
015: *
016: * @author Eric Bloch
017: * @version 1.0
018: */
019: public class Option implements Serializable {
020:
021: private static Logger mLogger = Logger.getLogger(Option.class);
022:
023: // No need to sync; access is read only after construction. These lists hold
024: // regexp objects.
025: private ArrayList allowList = null;
026: private ArrayList deniesList = null;
027:
028: // These lists hold the pattern for the regexp objects.
029: private ArrayList allowSerializableList = null;
030: private ArrayList deniesSerializableList = null;
031:
032: void init() {
033: if (allowList == null)
034: allowList = new ArrayList();
035: if (deniesList == null)
036: deniesList = new ArrayList();
037: if (allowSerializableList == null)
038: allowSerializableList = new ArrayList();
039: if (deniesSerializableList == null)
040: deniesSerializableList = new ArrayList();
041: }
042:
043: Option() {
044: init();
045: }
046:
047: /**
048: * Constructs a new option
049: * @param elt
050: */
051: Option(Element el) {
052: this ();
053: addElement(el);
054: }
055:
056: /**
057: * Add new patterns to option
058: */
059: public void addElement(Element el) {
060: List list = el.getChildren();
061: ListIterator iter = list.listIterator();
062:
063: Element a = el.getChild("allow", el.getNamespace());
064: if (a != null)
065: addPatterns(allowList, allowSerializableList, a);
066: Element d = el.getChild("deny", el.getNamespace());
067: if (d != null)
068: addPatterns(deniesList, deniesSerializableList, d);
069: }
070:
071: /**
072: * @param l list to add REs to
073: * @param element that has pattern element children
074: */
075: private void addPatterns(ArrayList list,
076: ArrayList serializableList, Element elt) {
077: ListIterator iter = elt.getChildren("pattern",
078: elt.getNamespace()).listIterator();
079:
080: while (iter.hasNext()) {
081: String p = ((Element) iter.next()).getTextNormalize();
082: mLogger.debug(elt.getName() + ": " + p);
083: try {
084: RE re = new RE(p);
085: list.add(re);
086: serializableList.add(p);
087: } catch (RESyntaxException e) {
088: mLogger.error(
089: /* (non-Javadoc)
090: * @i18n.test
091: * @org-mes="ignoring bad regexp syntax: " + p[0]
092: */
093: org.openlaszlo.i18n.LaszloMessages.getMessage(
094: Option.class.getName(), "051019-99",
095: new Object[] { p }));
096: continue;
097: }
098: }
099: }
100:
101: /**
102: * @param val value to check against
103: * @param allow if true, an undefined option means that it is allowed, else
104: * it is denied.
105: * @return true if this option allows the given value
106: */
107: boolean allows(String val, boolean allow) {
108:
109: mLogger.debug(
110: /* (non-Javadoc)
111: * @i18n.test
112: * @org-mes="checking: " + p[0]
113: */
114: org.openlaszlo.i18n.LaszloMessages.getMessage(Option.class
115: .getName(), "051019-120", new Object[] { val }));
116:
117: // If we don't specify what's allowed, allow all.
118: if (!allowList.isEmpty()) {
119: allow = false;
120: ListIterator iter = allowList.listIterator();
121: while (iter.hasNext()) {
122: RE re = (RE) iter.next();
123: if (re.match(val)) {
124: allow = true;
125: break;
126: }
127: }
128: }
129:
130: if (allow) {
131: ListIterator iter = deniesList.listIterator();
132: int i = 0;
133: while (iter.hasNext()) {
134: RE re = (RE) iter.next();
135: if (re.match(val)) {
136: allow = false;
137: break;
138: }
139: }
140: }
141: return allow;
142: }
143:
144: /**
145: * Handle object serialization.
146: */
147: private void writeObject(ObjectOutputStream out) throws IOException {
148:
149: ListIterator iter;
150:
151: // write out allow
152: out.writeInt(allowSerializableList.size());
153: iter = allowSerializableList.listIterator();
154: while (iter.hasNext()) {
155: out.writeObject(iter.next());
156: }
157:
158: // write out denies
159: out.writeInt(deniesSerializableList.size());
160: iter = deniesSerializableList.listIterator();
161: while (iter.hasNext()) {
162: out.writeObject(iter.next());
163: }
164: }
165:
166: /**
167: * Handle object deserialization.
168: */
169: private void readObject(ObjectInputStream in) throws IOException,
170: ClassNotFoundException {
171: int size, i;
172:
173: init();
174:
175: size = in.readInt();
176: for (i = 0; i < size; i++) {
177: String p = (String) in.readObject();
178: try {
179: allowList.add(new RE(p));
180: allowSerializableList.add(p);
181: } catch (RESyntaxException e) {
182: mLogger.error(
183: /* (non-Javadoc)
184: * @i18n.test
185: * @org-mes="ignoring bad regexp syntax: " + p[0]
186: */
187: org.openlaszlo.i18n.LaszloMessages.getMessage(
188: Option.class.getName(), "051019-99",
189: new Object[] { p }));
190: continue;
191: }
192: }
193:
194: size = in.readInt();
195: for (i = 0; i < size; i++) {
196: String p = (String) in.readObject();
197: try {
198: deniesList.add(new RE(p));
199: deniesSerializableList.add(p);
200: } catch (RESyntaxException e) {
201: mLogger.error(
202: /* (non-Javadoc)
203: * @i18n.test
204: * @org-mes="ignoring bad regexp syntax: " + p[0]
205: */
206: org.openlaszlo.i18n.LaszloMessages.getMessage(
207: Option.class.getName(), "051019-99",
208: new Object[] { p }));
209: continue;
210: }
211: }
212: }
213: }
|