001 package railo.runtime.tag; 002 003 import railo.commons.io.res.Resource; 004 import railo.commons.io.res.util.ResourceUtil; 005 import railo.commons.lang.StringUtil; 006 import railo.runtime.exp.ApplicationException; 007 import railo.runtime.exp.PageException; 008 import railo.runtime.ext.tag.TagImpl; 009 import railo.runtime.op.Caster; 010 import railo.runtime.search.SearchCollection; 011 import railo.runtime.search.SearchEngine; 012 import railo.runtime.search.SearchException; 013 014 /** 015 * Allows you to create and administer Collections. 016 **/ 017 public final class Collection extends TagImpl { 018 019 /** Specifies the action to perform. */ 020 private String action="list"; 021 022 /** */ 023 private Resource path; 024 025 /** Specifies a collection name or an alias if action = "map" */ 026 private String collection; 027 028 /** Name of the output variable (action=list) */ 029 private String name; 030 031 /** language of the collection (operators,stopwords) */ 032 private String language="english"; 033 034 //private boolean categories=false; 035 036 037 @Override 038 public void release() { 039 super.release(); 040 action="list"; 041 path=null; 042 collection=null; 043 name=null; 044 language="english"; 045 //categories=false; 046 } 047 048 049 /** 050 * @param categories the categories to set 051 * @throws ApplicationException 052 */ 053 public void setCategories(boolean categories) { 054 // Railo always support categories 055 //this.categories = categories; 056 } 057 058 /** set the value action 059 * Specifies the action to perform. 060 * @param action value to set 061 **/ 062 public void setAction(String action) { 063 if(action==null) return; 064 this.action=action.toLowerCase().trim(); 065 } 066 067 068 public void setEngine(String engine) { 069 // This setter only exists for compatibility reasions to other CFML engines, the attribute is completely ignored by Railo. 070 } 071 072 /** set the value path 073 * 074 * @param path value to set 075 * @throws PageException 076 **/ 077 public void setPath(String strPath) throws PageException { 078 if(strPath==null) return; 079 this.path=ResourceUtil.toResourceNotExisting(pageContext,strPath.trim() ); 080 //this.path=new File(path.toLowerCase().trim()); 081 082 pageContext.getConfig().getSecurityManager().checkFileLocation(this.path); 083 084 if(!this.path.exists()) { 085 Resource parent=this.path.getParentResource(); 086 if(parent!=null && parent.exists())this.path.mkdirs(); 087 else { 088 throw new ApplicationException("attribute path of the tag collection must be a existing directory"); 089 } 090 } 091 else if(!this.path.isDirectory()) 092 throw new ApplicationException("attribute path of the tag collection must be a existing directory"); 093 } 094 095 /** set the value collection 096 * Specifies a collection name or an alias if action = "map" 097 * @param collection value to set 098 **/ 099 public void setCollection(String collection) { 100 if(collection==null) return; 101 this.collection=collection.toLowerCase().trim(); 102 } 103 104 /** set the value name 105 * 106 * @param name value to set 107 **/ 108 public void setName(String name) { 109 if(name==null) return; 110 this.name=name.toLowerCase().trim(); 111 } 112 113 /** set the value language 114 * 115 * @param language value to set 116 **/ 117 public void setLanguage(String language) { 118 if(language==null) return; 119 this.language=validateLanguage(language); 120 } 121 122 public static String validateLanguage(String language) { 123 if(StringUtil.isEmpty(language,true)) 124 return "english"; 125 language=language.toLowerCase().trim(); 126 if("standard".equals(language)) 127 return "english"; 128 return language; 129 } 130 131 132 @Override 133 public int doStartTag() throws PageException { 134 //SerialNumber sn = pageContext.getConfig().getSerialNumber(); 135 //if(sn.getVersion()==SerialNumber.VERSION_COMMUNITY) 136 // throw new SecurityException("no access to this functionality with the "+sn.getStringVersion()+" version of railo"); 137 138 try { 139 if(action.equals("create")) doCreate(); 140 else if(action.equals("repair")) doRepair(); 141 else if(action.equals("delete")) doDelete(); 142 else if(action.equals("optimize")) doOptimize(); 143 else if(action.equals("list")) doList(); 144 else if(action.equals("map")) doMap(); 145 else if(action.equals("categorylist"))doCategoryList(); 146 147 else throw new ApplicationException("Invalid value [" + action + "] for attribute action.", "allowed values are [create,repair,map,delete,optimize,list ]"); 148 } catch (SearchException e) { 149 throw Caster.toPageException(e); 150 } 151 return SKIP_BODY; 152 } 153 154 /** 155 * @throws SearchException 156 * @throws PageException 157 * 158 */ 159 private void doMap() throws SearchException, PageException { 160 required("collection",action,"collection",collection); 161 required("collection",action,"path",path); 162 getCollection().map(path); 163 } 164 165 /** 166 * Creates a query in the PageContext containing all available Collections of the current searchStorage 167 * @throws ApplicationException 168 * @throws PageException 169 * 170 */ 171 private void doList() throws ApplicationException, PageException { 172 required("collection",action,"name",name); 173 //if(StringUtil.isEmpty(name))throw new ApplicationException("for action list attribute name is required"); 174 pageContext.setVariable(name,getSearchEngine().getCollectionsAsQuery()); 175 } 176 177 178 private void doCategoryList() throws PageException, SearchException { 179 // check attributes 180 required("collection",action,"collection",collection); 181 required("collection",action,"name",name); 182 pageContext.setVariable(name,getCollection().getCategoryInfo()); 183 } 184 185 /** 186 * Optimizes the Collection 187 * @throws SearchException 188 * @throws PageException 189 * 190 */ 191 private void doOptimize() throws SearchException, PageException { 192 required("collection",action,"collection",collection); 193 getCollection().optimize(); 194 } 195 196 /** 197 * Deletes a Collection 198 * @throws SearchException 199 * @throws PageException 200 * 201 */ 202 private void doDelete() throws SearchException, PageException { 203 required("collection",action,"collection",collection); 204 getCollection().delete(); 205 } 206 207 /** 208 * 209 * @throws SearchException 210 * @throws PageException 211 * 212 */ 213 private void doRepair() throws SearchException, PageException { 214 required("collection",action,"collection",collection); 215 getCollection().repair(); 216 } 217 218 /** 219 * Creates a new collection 220 * @throws SearchException 221 * @throws PageException 222 * 223 */ 224 private void doCreate() throws SearchException, PageException { 225 required("collection",action,"collection",collection); 226 required("collection",action,"path",path); 227 getSearchEngine().createCollection(collection,path,language,SearchEngine.DENY_OVERWRITE); 228 } 229 230 /** 231 * Returns the Searchstorage defined in the Environment 232 * @return searchStorage 233 */ 234 private SearchEngine getSearchEngine() { 235 return pageContext.getConfig().getSearchEngine(); 236 } 237 238 /** 239 * the collection matching the collection name 240 * @return collection 241 * @throws SearchException 242 */ 243 private SearchCollection getCollection() throws SearchException { 244 return getSearchEngine().getCollectionByName(collection); 245 } 246 247 @Override 248 public int doEndTag() { 249 return EVAL_PAGE; 250 } 251 }