1 19 20 package org.netbeans.modules.web.wizards; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.StringTokenizer ; 25 26 import org.openide.util.NbBundle; 27 28 import org.netbeans.modules.j2ee.dd.api.common.InitParam; 29 import org.netbeans.modules.j2ee.dd.api.web.Filter; 30 import org.netbeans.modules.j2ee.dd.api.web.FilterMapping; 31 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 32 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping; 33 34 37 41 class ServletData extends DeployData { 42 43 private String errorMessage = null; 44 private String name = null; 45 private String [] urlMappings = null; 47 private ArrayList filterMappings = null; 49 private String [][] initparams = null; 50 private boolean paramOK = true; 51 private FileType fileType = null; 52 private static final boolean debug = false; 53 private boolean addToDD=true; 54 55 ServletData(FileType fileType) { 56 this.fileType = fileType; 57 } 58 59 String getName() { 60 if(name == null) return ""; 61 return name; 62 } 63 64 void setName(String name) { 65 if(name != this.name) { 66 if(fileType == FileType.FILTER) 67 updateFilterMappings(getName(), name); 68 this.name = name; 69 } 70 } 71 72 String [] getServletNames() { 73 if(webApp == null) return new String [0]; 74 Servlet[] ss = webApp.getServlet(); 75 String [] names = new String [ss.length]; 76 for (int i=0; i<ss.length; i++) { 77 try { 78 names[i] = ss[i].getServletName(); 79 } 80 catch(Exception e) { 81 names[i] = ""; 82 } 83 } 84 return names; 85 } 86 87 java.util.List getUrlPatterns () { 88 if(webApp == null) { 89 if(debug) log("\tNo web app, return null"); return new ArrayList (); 91 } 92 ServletMapping[] maps = webApp.getServletMapping(); 93 java.util.List l = new ArrayList (); 94 for (int i=0;i<maps.length;i++) { 95 l.add(maps[i].getUrlPattern()); 96 } 97 return l; 98 } 99 100 ArrayList getFilterMappings() { 101 102 if(debug) log("::getFilterMappings()"); if(webApp == null) { 104 if(debug) log("\tNo web app, return null"); return new ArrayList (); 106 } 107 if(filterMappings != null) { 108 if(debug) log("\tFilter mappings already exist"); return filterMappings; 110 } 111 112 if(debug) log("\tCreating the filter mapping list"); FilterMapping[] fm = webApp.getFilterMapping(); 114 if(debug) { 115 log("\tOrder of mappings according to DD APIs"); for(int i=0; i<fm.length; ++i) 117 log("\tServlet name: " + fm[i].getFilterName()); } 119 120 filterMappings = new ArrayList (); 121 filterMappings.add(new FilterMappingData(getName())); 122 123 String string; 124 String [] d = null; 125 FilterMappingData fmd; 126 FilterMappingData.Dispatcher[] dispatchList; 127 128 for (int i=0; i<fm.length; i++) { 129 fmd = new FilterMappingData(); 130 fmd.setName(fm[i].getFilterName()); 131 132 string = fm[i].getUrlPattern(); 133 if(string == null || string.length() == 0) { 134 fmd.setType(FilterMappingData.Type.SERVLET); 135 fmd.setPattern(fm[i].getServletName()); 136 } 137 else { 138 fmd.setType(FilterMappingData.Type.URL); 139 fmd.setPattern(string); 140 } 141 142 try { 143 if(fm[i].sizeDispatcher() == 0) { 144 filterMappings.add(fmd); 145 continue; 146 } 147 } 148 catch(Exception ex) { 149 filterMappings.add(fmd); 151 continue; 152 } 153 154 try { 155 d = fm[i].getDispatcher(); 156 } 157 catch(Exception ex) { 158 if(debug) { 159 log(ex.toString()); 160 ex.printStackTrace(); 161 } 162 } 165 if(d == null) 166 167 dispatchList = new FilterMappingData.Dispatcher[0]; 168 else { 169 dispatchList = new FilterMappingData.Dispatcher[d.length]; 170 for(int j=0; j<d.length; ++j) { 171 dispatchList[j] = FilterMappingData.Dispatcher.findDispatcher(d[j]); 172 if(debug) log("\tDispatch: " + dispatchList[j]); } 174 } 175 fmd.setDispatcher(dispatchList); 176 filterMappings.add(fmd); 177 } 178 return filterMappings; 179 } 180 181 void setFilterMappings(ArrayList fmds) { 182 if(debug) log("::setFilterMappings()"); 183 filterMappings = fmds; 184 } 185 186 void updateFilterMappings(String oldName, String newName) { 187 if(debug) 188 log("::updateFilterMappings("+oldName+", " + newName + "),"); Iterator i = getFilterMappings().iterator(); 190 if(i == null) return; 192 FilterMappingData fmd; 193 while(i.hasNext()) { 194 fmd = (FilterMappingData)i.next(); 195 if(fmd.getName().equals(oldName)) fmd.setName(newName); 196 } 197 } 198 199 boolean isNameUnique() { 200 if(webApp == null) return true; 201 Servlet[] ss = webApp.getServlet(); 202 for (int i=0; i<ss.length; i++) { 203 if(name.equals(ss[i].getServletName())) { 204 return false; 205 } 206 } 207 208 Filter[] ff = webApp.getFilter(); 209 for (int i=0; i<ff.length; i++) { 210 if(name.equals(ff[i].getFilterName())) { 211 return false; 212 } 213 } 214 return true; 215 } 216 217 String [] getUrlMappings() { 218 if(urlMappings == null) return new String [0]; 219 return urlMappings; 220 } 221 222 String createDDServletName(String className) { 223 if (webApp==null) return null; 224 String result = className; 225 Servlet servlet = (Servlet) webApp.findBeanByName("Servlet","ServletName",result); while (servlet!=null) { 227 result = findNextId(result); 228 servlet = (Servlet) webApp.findBeanByName("Servlet","ServletName",result); } 230 setName(result); 231 return result; 232 } 233 234 void createDDServletMapping(String servletName) { 235 if (webApp==null) return; 236 String result = "/"+servletName; 237 ServletMapping mapping = (ServletMapping) webApp.findBeanByName("ServletMapping","UrlPattern",result); while (mapping!=null) { 239 result = findNextId(result); 240 mapping = (ServletMapping) webApp.findBeanByName("ServletMapping","UrlPattern",result); } 242 urlMappings = new String []{result}; 243 } 244 245 247 private String findNextId(String id) { 248 char ch = id.charAt(id.length()-1); 249 String result=null; 250 if (Character.isDigit(ch)) { 251 String lastDigit = id.substring(id.length()-1); 252 int num = new Integer (lastDigit).intValue()+1; 253 result=id.substring(0,id.length()-1)+new Integer (num).toString(); 254 } else { 255 return result=id+"_1"; } 257 return result; 258 } 259 260 261 String getUrlMappingsAsString() { 262 263 if(urlMappings == null || urlMappings.length == 0 ) return ""; StringBuffer buf = new StringBuffer (); 265 int index = 0; 266 while(index<urlMappings.length-1) { 267 buf.append(urlMappings[index]); 268 buf.append(", "); index++; 270 } 271 272 buf.append(urlMappings[index]); 273 return buf.toString(); 274 } 275 276 void parseUrlMappingString(String raw) { 277 278 urlMappings = null; 279 StringTokenizer st = new StringTokenizer (raw, ","); 280 ArrayList list = new ArrayList (); 281 String mapping; 282 String [] names = getServletNames(); 283 284 while(st.hasMoreTokens()) { 285 mapping = st.nextToken().trim(); 286 if(mapping.length() == 0) 287 continue; 288 list.add(mapping); 289 } 290 291 urlMappings = new String [list.size()]; 292 list.toArray(urlMappings); 293 } 294 295 String [][] getInitParams() { 296 if(initparams == null) return new String [0][2]; 297 return initparams; 298 } 299 300 void setInitParams(String [][] initparams, boolean paramOK) { 301 this.initparams = initparams; 302 this.paramOK = paramOK; 303 } 304 305 int getNumInitParams() { 306 if(initparams == null) return 0; 307 return initparams.length; 308 } 309 310 boolean isParamOK() { 311 return paramOK; 312 } 313 314 void setParamOK(boolean paramOK) { 315 this.paramOK = paramOK; 316 } 317 318 319 boolean isValid() { 320 if(debug) log("::isValid()"); errorMessage = new String (); 322 if(webApp == null) return true; 323 if (!isAddToDD()) return true; 324 325 if(getName().length() == 0) { 326 errorMessage = NbBundle.getMessage(ServletData.class, 327 "MSG_no_name"); 328 return false; 329 } 330 331 if(!isNameUnique()) { 332 errorMessage = NbBundle.getMessage(ServletData.class, 333 "MSG_name_unique"); 334 return false; 335 } 336 337 if(debug) log("\tname is fine"); 339 if(fileType == FileType.SERVLET) { 340 if(!checkMappingsForServlet()) return false; 341 if(!checkServletDuplicitMappings()) return false; 342 } 343 else if(fileType == FileType.FILTER) { 344 if(!checkMappingsForFilter()) return false; 345 } 346 347 if(!isParamOK()) { 348 errorMessage = NbBundle.getMessage(ServletData.class, 349 "MSG_invalid_param"); 350 return false; 351 } 352 353 if(debug) log("\tparams are fine"); return true; 355 } 356 357 358 boolean checkMappingsForServlet() { 359 360 errorMessage = new String (); 361 String [] mappings = getUrlMappings(); 362 if(mappings == null || mappings.length == 0) { 363 364 if(debug) log("\tNo URL mappings"); errorMessage = NbBundle.getMessage(ServletData.class, 366 "MSG_no_mapping"); 367 return false; 368 } 369 for (int i=0;i<mappings.length;i++) { 370 String errMessage = checkServletMappig(mappings[i]); 371 if (errMessage!=null) { 372 errorMessage = errMessage; 373 return false; 374 } 375 } 376 if(debug) log("\tmappings are fine"); return true; 378 } 379 380 boolean checkServletDuplicitMappings() { 381 errorMessage = new String (); 382 String [] newMappings = getUrlMappings(); 383 java.util.List urlPatterns = getUrlPatterns(); 384 for (int i=0;i<newMappings.length;i++) { 385 Iterator it = urlPatterns.iterator(); 386 while(it.hasNext()) { 387 String urlPattern = (String )it.next(); 388 if (newMappings[i].equals(urlPattern)) { 389 if(debug) log("\tDuplicit URL mappings"); errorMessage = NbBundle.getMessage(ServletData.class, 391 "MSG_url_pattern_unique"); 392 return false; 393 } 394 } 395 urlPatterns.add(newMappings[i]); 397 } 398 if(debug) log("\tmappings- duplicity - is fine"); return true; 400 } 401 402 boolean checkMappingsForFilter() { 403 errorMessage = new String (); 404 if(filterMappings == null || filterMappings.size() == 0) { 405 if(debug) log("\tNo mappings"); errorMessage = NbBundle.getMessage(ServletData.class, 407 "MSG_no_mapping"); 408 return false; 409 } 410 Iterator i = getFilterMappings().iterator(); 411 boolean found = false; 412 FilterMappingData fmd; 413 while(i.hasNext()) { 414 fmd = (FilterMappingData)(i.next()); 415 if(fmd.getName().equals(getName())) { 416 found = true; 417 break; 418 } 419 } 420 if(!found) { 421 errorMessage = NbBundle.getMessage(ServletData.class, 422 "MSG_no_mapping"); 423 return false; 424 } 425 return true; 426 } 427 428 void createDDEntries() { 429 if(debug) log("::createDDEntries()"); 430 if(webApp == null) return; 431 if(debug) log("\t...adding"); 432 433 if(fileType == FileType.SERVLET) { 434 boolean added = addServlet(); 435 if(added) { 436 addUrlMappings(); 437 if(debug) log("\t...writing changes"); 438 try { 439 writeChanges(); 440 } catch (java.io.IOException ex) { 441 if (debug) ex.printStackTrace(); 442 } 443 } 444 } 445 else if(fileType == FileType.FILTER) { 446 boolean added = addFilter(); 447 if(added) { 448 addFilterMappings(); 449 if(debug) log("\t...writing changes"); 450 try { 451 writeChanges(); 452 } catch (java.io.IOException ex) { 453 if (debug) ex.printStackTrace(); 454 } 455 } 456 } 457 } 458 459 460 private boolean addServlet() { 461 462 if(debug) log("::addServlet()"); if(webApp == null) return false; 464 Servlet s; 465 try { 466 s = (Servlet)webApp.createBean("Servlet"); if(debug) log("\tCreated servlet"); } 469 470 catch(ClassNotFoundException cnfe) { 471 if(debug) cnfe.printStackTrace(); 472 return false; 473 } 474 475 s.setServletName(name); 476 s.setServletClass(className); 477 478 int numInitParams = getInitParams().length; 479 480 if(debug) 481 log("\tnum params " + String.valueOf(numInitParams)); 483 484 for(int i=0; i<numInitParams; ++i) { 485 InitParam param; 486 try { 487 param = (InitParam)s.createBean("InitParam"); if(debug) log("\tCreated initparam"); } 490 491 catch(ClassNotFoundException cnfe) { 492 if(debug) cnfe.printStackTrace(); 493 continue; 494 } 495 496 param.setParamName(initparams[i][0]); 497 param.setParamValue(initparams[i][1]); 498 s.addInitParam(param); 499 } 500 501 if(debug) 502 log("\tnum params " + String.valueOf(s.sizeInitParam())); 504 webApp.addServlet(s); 505 return true; 506 } 507 508 509 private boolean addFilter() { 510 511 if(debug) log("::addFilter()"); if(webApp == null) return false; 513 Filter f; 514 try { 515 f = (Filter)webApp.createBean("Filter"); if(debug) log("\tCreated filter"); } 518 519 catch(ClassNotFoundException cnfe) { 520 if(debug) cnfe.printStackTrace(); 521 return false; 522 } 523 524 f.setFilterName(name); 525 f.setFilterClass(className); 526 527 int numInitParams = getInitParams().length; 528 529 if(debug) 530 log("\tnum params " + String.valueOf(numInitParams)); 532 533 for(int i=0; i<numInitParams; ++i) { 534 InitParam param; 535 try { 536 param = (InitParam)f.createBean("InitParam"); if(debug) log("\tCreated initparam"); } 539 540 catch(ClassNotFoundException cnfe) { 541 if(debug) cnfe.printStackTrace(); 542 continue; 543 } 544 545 param.setParamName(initparams[i][0]); 546 param.setParamValue(initparams[i][1]); 547 f.addInitParam(param); 548 } 549 550 if(debug) 551 log("\tnum params " + String.valueOf(f.sizeInitParam())); 553 webApp.addFilter(f); 554 return true; 555 } 556 557 558 private void addUrlMappings() { 559 560 if(webApp == null) return; 561 int numMappings = getUrlMappings().length; 562 for(int i=0; i<numMappings; ++i) { 563 ServletMapping m; 564 try { 565 m = (ServletMapping)webApp.createBean("ServletMapping"); } 567 catch(ClassNotFoundException cnfe) { 568 return; 569 } 570 m.setServletName(name); 571 m.setUrlPattern(urlMappings[i]); 572 webApp.addServletMapping(m); 573 } 574 } 575 576 private void addFilterMappings() { 577 578 if(debug) log("::addFilterMappings()"); 579 if(webApp == null) return; 580 581 int numFilterMappings = filterMappings.size(); 583 Iterator iterator = filterMappings.iterator(); 584 585 FilterMapping[] fm = new FilterMapping[numFilterMappings]; 586 587 FilterMappingData fmd; 588 for(int i=0; i<numFilterMappings; ++i) { 589 590 fmd = (FilterMappingData)(iterator.next()); 591 592 try { 593 fm[i] = (FilterMapping)webApp.createBean("FilterMapping"); if(debug) log("\tCreated filter mapping"); } 596 catch(ClassNotFoundException cnfe) { 597 return; 598 } 599 600 fm[i].setFilterName(fmd.getName()); 601 if(debug) log("\tFilter name: " + fmd.getName()); if(fmd.getType() == FilterMappingData.Type.URL) { 603 fm[i].setUrlPattern(fmd.getPattern()); 604 if(debug) log("URL pattern " + fmd.getPattern()); } 606 else { 607 fm[i].setServletName(fmd.getPattern()); 608 if(debug) log("Servlet pattern " + fmd.getPattern()); } 610 611 int length = fmd.getDispatcher().length; 612 if(length == 0) { 613 if(debug) log("\tNo dispatcher, continue"); continue; 615 } 616 617 String [] s = new String [length]; 618 FilterMappingData.Dispatcher[] d = fmd.getDispatcher(); 619 for(int j=0; j<length; ++j) { 620 if(debug) log("\tDispatcher: " + d[j].toString()); s[j] = d[j].toString(); 622 } 623 try { 624 fm[i].setDispatcher(s); 625 } 626 catch(Exception e) { 627 if(debug) log("\tFailed to set dispatcher"); } 630 if(debug) log(fm[i].toString()); 631 } 632 webApp.setFilterMapping(fm); 633 } 634 635 636 void setAddToDD(boolean addToDD){ 637 this.addToDD=addToDD; 638 } 639 640 boolean isAddToDD() { 641 return addToDD; 642 } 643 644 String getErrorMessage() { 645 return errorMessage; 646 } 647 648 void log(String s) { 649 System.out.println("ServletData" + s); 650 } 651 652 private String checkServletMappig(String uri) { 653 if (!uri.matches("[\\*/].*")) { return NbBundle.getMessage(ServletData.class,"MSG_WrongUriStart"); 655 } else if (uri.length()>1 && uri.endsWith("/")) { 656 return NbBundle.getMessage(ServletData.class,"MSG_WrongUriEnd"); 657 } else if (uri.matches(".*\\*.*\\*.*")) { return NbBundle.getMessage(ServletData.class,"MSG_TwoAsterisks"); 659 } else if (uri.matches("..*\\*..*")) { return NbBundle.getMessage(ServletData.class,"MSG_AsteriskInTheMiddle"); 661 } 662 return null; 663 } 664 } 665 666 | Popular Tags |