1 4 5 9 10 package org.openlaszlo.server; 11 12 import java.io.*; 13 import java.util.*; 14 15 import org.apache.log4j.Logger; 16 import org.jdom.*; 17 import org.apache.regexp.RE; 18 import org.apache.regexp.RESyntaxException; 19 20 26 public class Option implements Serializable { 27 28 private static Logger mLogger = Logger.getLogger(Option.class); 29 30 private ArrayList allowList = null; 33 private ArrayList deniesList = null; 34 35 private ArrayList allowSerializableList = null; 37 private ArrayList deniesSerializableList = null; 38 39 void init() { 40 if (allowList == null) 41 allowList = new ArrayList(); 42 if (deniesList == null) 43 deniesList = new ArrayList(); 44 if (allowSerializableList == null) 45 allowSerializableList = new ArrayList(); 46 if (deniesSerializableList == null) 47 deniesSerializableList = new ArrayList(); 48 } 49 50 Option() { 51 init(); 52 } 53 54 58 Option(Element el) { 59 this(); 60 addElement(el); 61 } 62 63 66 public void addElement(Element el) { 67 List list = el.getChildren(); 68 ListIterator iter = list.listIterator(); 69 70 Element a = el.getChild("allow", el.getNamespace()); 71 if (a != null) 72 addPatterns(allowList, allowSerializableList, a); 73 Element d = el.getChild("deny", el.getNamespace()); 74 if (d != null) 75 addPatterns(deniesList, deniesSerializableList, d); 76 } 77 78 82 private void addPatterns(ArrayList list, ArrayList serializableList, Element elt) { 83 ListIterator iter = 84 elt.getChildren("pattern", elt.getNamespace()).listIterator(); 85 86 while(iter.hasNext()) { 87 String p = ((Element)iter.next()).getTextNormalize(); 88 mLogger.debug(elt.getName() + ": " + p); 89 try { 90 RE re = new RE(p); 91 list.add(re); 92 serializableList.add(p); 93 } catch (RESyntaxException e) { 94 mLogger.error("ignoring bad regexp syntax: " + p); 95 continue; 96 } 97 } 98 } 99 100 106 boolean allows(String val, boolean allow) { 107 108 mLogger.debug("checking: " + val); 109 110 if (!allowList.isEmpty()) { 112 allow = false; 113 ListIterator iter = allowList.listIterator(); 114 while (iter.hasNext()) { 115 RE re = (RE)iter.next(); 116 if (re.match(val)) { 117 allow = true; 118 break; 119 } 120 } 121 } 122 123 if (allow) { 124 ListIterator iter = deniesList.listIterator(); 125 while (iter.hasNext()) { 126 RE re = (RE)iter.next(); 127 if (re.match(val)) { 128 allow = false; 129 break; 130 } 131 } 132 } 133 return allow; 134 } 135 136 137 140 private void writeObject(ObjectOutputStream out) 141 throws IOException { 142 143 ListIterator iter; 144 145 out.writeInt(allowSerializableList.size()); 147 iter = allowSerializableList.listIterator(); 148 while (iter.hasNext()) { 149 out.writeObject(iter.next()); 150 } 151 152 out.writeInt(deniesSerializableList.size()); 154 iter = deniesSerializableList.listIterator(); 155 while (iter.hasNext()) { 156 out.writeObject(iter.next()); 157 } 158 } 159 160 163 private void readObject(ObjectInputStream in) 164 throws IOException, ClassNotFoundException { 165 int size, i; 166 167 init(); 168 169 size = in.readInt(); 170 for (i=0; i < size; i++) { 171 String p = (String )in.readObject(); 172 try { 173 allowList.add(new RE(p)); 174 allowSerializableList.add(p); 175 } catch (RESyntaxException e) { 176 mLogger.error("ignoring bad regexp syntax: " + p); 177 continue; 178 } 179 } 180 181 size = in.readInt(); 182 for (i=0; i < size; i++) { 183 String p = (String )in.readObject(); 184 try { 185 deniesList.add(new RE(p)); 186 deniesSerializableList.add(p); 187 } catch (RESyntaxException e) { 188 mLogger.error("ignoring bad regexp syntax: " + p); 189 continue; 190 } 191 } 192 } 193 } 194 | Popular Tags |