1 37 package net.thauvin.google.taglibs; 38 39 import net.thauvin.google.GoogleSearchBean; 40 import net.thauvin.google.TagUtility; 41 42 import javax.servlet.jsp.JspException ; 43 import javax.servlet.jsp.PageContext ; 44 45 53 public class Search extends QuerySupport 54 { 55 private GoogleSearchBean bean = null; 56 private boolean cache = GoogleSearchBean.DEFAULT_CACHE; 57 private boolean filter = GoogleSearchBean.DEFAULT_FILTER; 58 private String lr = GoogleSearchBean.DEFAULT_LR; 59 private int maxResults = GoogleSearchBean.DEFAULT_MAX_RESULTS; 60 private String restrict = GoogleSearchBean.DEFAULT_RESTRICT; 61 private boolean safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH; 62 private String site = GoogleSearchBean.DEFAULT_SITE; 63 private int start = GoogleSearchBean.DEFAULT_START; 64 private String type = GoogleSearchBean.DEFAULT_TYPE; 65 66 71 public final void setCache(String cache) 72 { 73 this.cache = Boolean.valueOf(cache).booleanValue(); 74 } 75 76 81 public final void setFilter(String filter) 82 { 83 this.filter = Boolean.valueOf(filter).booleanValue(); 84 } 85 86 91 public final void setLr(String lr) 92 { 93 this.lr = lr; 94 } 95 96 101 public final void setMaxResults(String maxResults) 102 { 103 try 104 { 105 this.maxResults = Integer.valueOf(maxResults).intValue(); 106 } 107 catch (NumberFormatException e) 108 { 109 ; } 111 } 112 113 118 public final void setRestrict(String restrict) 119 { 120 this.restrict = restrict; 121 } 122 123 128 public final void setSafeSearch(String safeSearch) 129 { 130 this.safeSearch = Boolean.valueOf(safeSearch).booleanValue(); 131 } 132 133 138 public final void setSite(String site) 139 { 140 this.site = site; 141 } 142 143 148 public final void setStart(String start) 149 { 150 try 151 { 152 this.start = Integer.valueOf(start).intValue(); 153 } 154 catch (NumberFormatException e) 155 { 156 ; } 158 } 159 160 166 public final void setType(String type) 167 { 168 this.type = type; 169 } 170 171 176 private final boolean getCache() 177 { 178 return getBoolParam(TagUtility.CACHE_PARAM, cache); 179 } 180 181 186 private final boolean getFilter() 187 { 188 return getBoolParam(TagUtility.FILTER_PARAM, filter); 189 } 190 191 196 private final String getLr() 197 { 198 return getStringParam(TagUtility.LR_PARAM, lr); 199 } 200 201 206 private final int getMaxResults() 207 { 208 return getIntParam(TagUtility.MAX_RESULTS_PARAM, maxResults); 209 } 210 211 216 private final String getRestrict() 217 { 218 return getStringParam(TagUtility.RESTRICT_PARAM, restrict); 219 } 220 221 226 private final boolean getSafeSearch() 227 { 228 return getBoolParam(TagUtility.SAFE_SEARCH_PARAM, safeSearch); 229 } 230 231 236 private final String getSite() 237 { 238 String site = getStringParam(TagUtility.SITE_PARAM, this.site); 239 240 if (site.length() > 0) 241 { 242 return ("site:" + site + ' '); 243 } 244 245 return ""; 246 } 247 248 253 private final int getStart() 254 { 255 return getIntParam(TagUtility.START_PARAM, start); 256 } 257 258 263 private final String getType() 264 { 265 String type = getStringParam(TagUtility.TYPE_PARAM, this.type); 266 267 if (type.length() > 0) 268 { 269 return (" filetype:" + type); 270 } 271 272 return ""; 273 } 274 275 282 private boolean getBoolParam(String paramName, boolean defaultValue) 283 { 284 String param = 285 TagUtility.getParameter(pageContext.getRequest(), paramName); 286 287 if (TagUtility.isValidString(param, true)) 288 { 289 return Boolean.valueOf(param).booleanValue(); 290 } 291 292 return defaultValue; 293 } 294 295 302 private int getIntParam(String paramName, int defaultValue) 303 { 304 String param = 305 TagUtility.getParameter(pageContext.getRequest(), paramName); 306 307 if (TagUtility.isValidString(param, true)) 308 { 309 try 310 { 311 return Integer.valueOf(param).intValue(); 312 } 313 catch (NumberFormatException e) 314 { 315 ; } 317 } 318 319 return defaultValue; 320 } 321 322 329 private String getStringParam(String paramName, String defaultValue) 330 { 331 String param = 332 TagUtility.getParameter(pageContext.getRequest(), paramName); 333 334 if (TagUtility.isValidString(param, true)) 335 { 336 return param; 337 } 338 339 return defaultValue; 340 } 341 342 348 public int doEndTag() 349 throws JspException 350 { 351 final String query = getQuery(); 352 353 if (TagUtility.isValidString(query, true)) 354 { 355 try 356 { 357 bean.setProxyServer(pageContext.getServletContext() 358 .getInitParameter(TagUtility.GOOGLE_PROXY_HOST), 359 pageContext.getServletContext() 360 .getInitParameter(TagUtility.GOOGLE_PROXY_PORT), 361 pageContext.getServletContext() 362 .getInitParameter(TagUtility.GOOGLE_PROXY_USERNAME), 363 pageContext.getServletContext() 364 .getInitParameter(TagUtility.GOOGLE_PROXY_PASSWORD)); 365 366 bean.setKeywords(getQuery()); 367 368 bean.getGoogleSearch(getKey(), getSite() + getQuery() + getType(), 369 getStart(), getMaxResults(), getFilter(), 370 getRestrict(), getSafeSearch(), getLr()); 371 } 372 catch (Exception e) 373 { 374 throw TagUtility.outputError("search", e); 375 } 376 } 377 else if (!getCache()) 378 { 379 bean.reset(); 380 } 381 382 reset(); 384 385 return EVAL_PAGE; 386 } 387 388 394 public int doStartTag() 395 throws JspException 396 { 397 bean = TagUtility.getGoogleSearchBean(pageContext); 399 400 if (bean == null) 402 { 403 try 404 { 405 bean = new GoogleSearchBean(); 406 407 pageContext.setAttribute(TagUtility.GOOGLE_SEARCH_BEAN, bean, 409 PageContext.SESSION_SCOPE); 410 } 411 catch (Exception e) 412 { 413 throw new JspException ("An unknown error ocurred while creating the Google search bean."); 414 } 415 } 416 417 return EVAL_BODY_TAG; 418 } 419 420 423 public void release() 424 { 425 super.release(); 426 427 start = GoogleSearchBean.DEFAULT_START; 429 maxResults = GoogleSearchBean.DEFAULT_MAX_RESULTS; 430 filter = GoogleSearchBean.DEFAULT_FILTER; 431 safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH; 432 restrict = GoogleSearchBean.DEFAULT_RESTRICT; 433 lr = GoogleSearchBean.DEFAULT_LR; 434 site = GoogleSearchBean.DEFAULT_SITE; 435 cache = GoogleSearchBean.DEFAULT_CACHE; 436 type = GoogleSearchBean.DEFAULT_TYPE; 437 438 bean = null; 440 441 reset(); 443 } 444 445 448 protected void reset() 449 { 450 super.reset(); 451 } 452 } 453 | Popular Tags |