1 19 20 package com.sslexplorer.webforwards.forms; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.StringTokenizer ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.struts.Globals; 31 import org.apache.struts.action.ActionErrors; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessage; 34 35 import com.sslexplorer.boot.PropertyList; 36 import com.sslexplorer.input.MultiSelectSelectionModel; 37 import com.sslexplorer.policyframework.Resource; 38 import com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm; 39 import com.sslexplorer.security.User; 40 import com.sslexplorer.vfs.webdav.DAVUtilities; 41 import com.sslexplorer.webforwards.AbstractWebForward; 42 import com.sslexplorer.webforwards.ReplacementProxyWebForward; 43 import com.sslexplorer.webforwards.ReverseProxyWebForward; 44 import com.sslexplorer.webforwards.WebForward; 45 import com.sslexplorer.webforwards.WebForwardTypes; 46 47 52 public class WebForwardForm extends AbstractFavoriteResourceForm { 53 public static final String ATTR_NO_AUTHENTICATION = "none"; 54 public static final String ATTR_FORM_BASED_AUTHENTICATION = "form"; 55 public static final String ATTR_HTTP_BASED_AUTHENTICATION = "http"; 56 private String selectedTab = "details"; 57 private int type; 58 private String destinationURL; 59 private String category; 60 61 private PropertyList restrictToHosts; 63 private String encoding; 64 65 private String authenticationType; 66 67 private String authenticationUsername; 69 private String authenticationPassword; 70 private String preferredAuthenticationScheme; 71 72 private String formType; 74 private String formParameters; 75 76 private String paths; 78 private boolean activeDNS; 79 private String hostHeader; 80 private Map customHeaders; 81 82 88 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 89 ActionErrors errs = super.validate(mapping, request); 90 if (isCommiting()) { 91 if (getResourceName().equalsIgnoreCase("default") 92 && (!getEditing() || (getEditing() && !getResource().getResourceName().equalsIgnoreCase("default")))) { 93 errs.add(Globals.ERROR_KEY, new ActionMessage("error.createNetworkPlace.cantUseNameDefault")); 94 setResourceName(""); 95 } 96 try { 97 if (this.getDestinationURL().indexOf("${") == -1){ 98 new URL (this.getDestinationURL()); 100 } 101 } catch (MalformedURLException e) { 102 errs.add(Globals.ERROR_KEY, new ActionMessage("webForwardWizard.webForwardSpecificDetails.error.malformedURLException")); 103 } 104 105 if(getCategory().trim().equals("")) { 106 errs.add(Globals.ERROR_KEY, new ActionMessage("editWebForward.error.noCategory")); 107 } 108 109 if (type == WebForward.TYPE_PATH_BASED_REVERSE_PROXY) { 110 if (!this.activeDNS && this.hostHeader.equals("")) { 111 if (this.paths != null && this.paths.length() == 0) { 112 errs.add(Globals.ERROR_KEY, 113 new ActionMessage("webForwardWizard.webForwardSpecificDetails.error.needs.path")); 114 } 115 } 116 if (this.paths != null && this.paths.length() > 0) { 117 String paths = ""; 118 StringTokenizer t = new StringTokenizer (this.paths, "\n\r"); 119 while (t.hasMoreTokens()) { 120 String path = t.nextToken(); 121 path = path.trim(); 122 if (!path.startsWith("/")) 123 path = "/" + path; 124 if (path.endsWith("/")) 125 path = DAVUtilities.stripTrailingSlash(path); 126 if (!path.startsWith("/")) { 127 errs.add(Globals.ERROR_KEY, new ActionMessage( 128 "webForwardWizard.webForwardSpecificDetails.error.invalidPath", path)); 129 } else { 130 paths += path + "\n"; 131 } 132 } 133 if(errs.size()==0) 134 this.paths = paths; 135 } 136 } else if (type == WebForward.TYPE_HOST_BASED_REVERSE_PROXY) { 137 if (this.activeDNS && !this.hostHeader.equals("")) { 138 errs.add(Globals.ERROR_KEY, new ActionMessage("webForwardWizard.webForwardSpecificDetails.error.hostBased.bothSelected")); 139 } 140 if (!this.activeDNS && this.hostHeader.equals("")) { 141 errs.add(Globals.ERROR_KEY, new ActionMessage("webForwardWizard.webForwardSpecificDetails.error.hostBased.nonSelected")); 142 } 143 } 144 } 145 return errs; 146 } 147 148 153 public int getTabCount() { 154 if (type == WebForward.TYPE_TUNNELED_SITE){ 155 return 3; 156 } 157 else{ 158 return 4; 159 } 160 } 161 162 167 public String getTabName(int idx) { 168 if (type == WebForward.TYPE_TUNNELED_SITE){ 169 switch (idx) { 170 case 0: 171 return "details"; 172 case 1: 173 return "attributes"; 174 default: 175 return "policies"; 176 } 177 } 178 else{ 179 switch (idx) { 180 case 0: 181 return "details"; 182 case 1: 183 return "attributes"; 184 case 2: 185 return "authentication"; 186 default: 187 return "policies"; 188 } 189 190 } 191 } 192 193 201 public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel, 202 PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception { 203 super.initialise(user, resource, editing, policyModel, selectedPolicies, owner, assignOnly); 204 WebForward webForward = (WebForward) resource; 205 206 this.type = webForward.getType(); 207 this.destinationURL = webForward.getDestinationURL(); 208 this.category = webForward.getCategory(); 209 210 if (this.type == WebForward.TYPE_REPLACEMENT_PROXY) { 211 ReplacementProxyWebForward spwf = (ReplacementProxyWebForward) webForward; 212 this.restrictToHosts = spwf.getRestrictToHosts(); 213 this.encoding = spwf.getEncoding(); 214 this.authenticationUsername = spwf.getAuthenticationUsername(); 215 this.authenticationPassword = spwf.getAuthenticationPassword(); 216 this.preferredAuthenticationScheme = spwf.getPreferredAuthenticationScheme(); 217 this.formParameters = spwf.getFormParameters(); 218 this.formType = spwf.getFormType(); 219 } else if (this.type == WebForward.TYPE_PATH_BASED_REVERSE_PROXY || this.type == WebForward.TYPE_HOST_BASED_REVERSE_PROXY) { 220 ReverseProxyWebForward rpwf = (ReverseProxyWebForward) webForward; 221 this.paths = rpwf.getPaths(); 222 this.activeDNS = rpwf.getActiveDNS(); 223 this.customHeaders = rpwf.getCustomHeaders(); 224 this.authenticationUsername = rpwf.getAuthenticationUsername(); 225 this.authenticationPassword = rpwf.getAuthenticationPassword(); 226 this.preferredAuthenticationScheme = rpwf.getPreferredAuthenticationScheme(); 227 this.hostHeader = rpwf.getHostHeader(); 228 this.formParameters = rpwf.getFormParameters(); 229 this.formType = rpwf.getFormType(); 230 this.encoding = rpwf.getCharset(); 231 } 232 } 233 234 239 public String getSelectedTab() { 240 return selectedTab; 241 } 242 243 248 public void setSelectedTab(String selectedTab) { 249 this.selectedTab = selectedTab; 250 } 251 252 257 public void applyToResource() throws Exception { 258 ((AbstractWebForward)getResource()).setCategory(getCategory()); 259 ((AbstractWebForward)getResource()).setDestinationURL(getDestinationURL()); 260 if (getType() == WebForward.TYPE_TUNNELED_SITE) { 261 } else if (getType() == WebForward.TYPE_REPLACEMENT_PROXY) { 262 ((ReplacementProxyWebForward)resource).setAuthenticationUsername(getAuthenticationUsername()); 263 ((ReplacementProxyWebForward)resource).setAuthenticationPassword(getAuthenticationPassword()); 264 ((ReplacementProxyWebForward)getResource()).setPreferredAuthenticationScheme(getPreferredAuthenticationScheme()); 265 ((ReplacementProxyWebForward)getResource()).setEncoding(getEncoding()); 266 ((ReplacementProxyWebForward)getResource()).setRestrictToHosts(getRestrictToHostsList()); 267 ((ReplacementProxyWebForward)getResource()).setFormType(getFormType()); 268 ((ReplacementProxyWebForward)getResource()).setFormParameters(getFormParameters()); 269 } else if (getType() == WebForward.TYPE_PATH_BASED_REVERSE_PROXY || getType() == WebForward.TYPE_HOST_BASED_REVERSE_PROXY) { 270 ((ReverseProxyWebForward)resource).setAuthenticationUsername(getAuthenticationUsername()); 271 ((ReverseProxyWebForward)resource).setAuthenticationPassword(getAuthenticationPassword()); 272 ((ReverseProxyWebForward)resource).setPreferredAuthenticationScheme(getPreferredAuthenticationScheme()); 273 ((ReverseProxyWebForward)resource).setPaths(getPaths()); 274 ((ReverseProxyWebForward)resource).setHostHeader(getHostHeader()); 275 ((ReverseProxyWebForward)resource).setActiveDNS(isActiveDNS()); 276 ((ReverseProxyWebForward)resource).setFormType(getFormType()); 277 ((ReverseProxyWebForward)resource).setFormParameters(getFormParameters()); 278 ((ReverseProxyWebForward)resource).setCharset(getEncoding()); 279 } 280 } 281 282 285 public String getCategory() { 286 return category; 287 } 288 289 292 public void setCategory(String category) { 293 this.category = category; 294 } 295 296 299 public String getDestinationURL() { 300 return destinationURL; 301 } 302 303 306 public void setDestinationURL(String destinationURL) { 307 this.destinationURL = destinationURL; 308 } 309 310 public boolean isPathBased() { 311 return WebForward.TYPE_PATH_BASED_REVERSE_PROXY == type; 312 } 313 314 public boolean isHostBased() { 315 return WebForward.TYPE_HOST_BASED_REVERSE_PROXY == type; 316 } 317 318 321 public int getType() { 322 return type; 323 } 324 325 328 public void setType(int type) { 329 this.type = type; 330 } 331 332 public boolean isActiveDNS() { 333 return activeDNS; 334 } 335 336 public void setActiveDNS(boolean activeDNS) { 337 this.activeDNS = activeDNS; 338 } 339 340 public String getAuthenticationType() { 341 if (authenticationType == null) { 342 if (!isEmpty(authenticationUsername) || !isEmpty(authenticationPassword)) { 343 return ATTR_HTTP_BASED_AUTHENTICATION; 344 } else if (!isEmpty(formParameters)) { 345 return ATTR_FORM_BASED_AUTHENTICATION; 346 } else { 347 return ATTR_NO_AUTHENTICATION; 348 } 349 } 350 return authenticationType; 351 } 352 353 public void setAuthenticationType(String authenticationType) { 354 this.authenticationType = authenticationType; 355 } 356 357 public String getAuthenticationPassword() { 358 return authenticationPassword; 359 } 360 361 public void setAuthenticationPassword(String authenticationPassword) { 362 this.authenticationPassword = authenticationPassword; 363 } 364 365 public String getAuthenticationUsername() { 366 return authenticationUsername; 367 } 368 369 public void setAuthenticationUsername(String authenticationUsername) { 370 this.authenticationUsername = authenticationUsername; 371 } 372 373 public Map getCustomHeaders() { 374 return customHeaders; 375 } 376 377 public void setCustomHeaders(Map customHeaders) { 378 this.customHeaders = customHeaders; 379 } 380 381 public String getEncoding() { 382 return encoding; 383 } 384 385 public void setEncoding(String encoding) { 386 this.encoding = encoding; 387 } 388 389 public String getPaths() { 390 return paths; 391 } 392 393 public void setPaths(String paths) { 394 this.paths = paths; 395 } 396 397 public String getPreferredAuthenticationScheme() { 398 return preferredAuthenticationScheme; 399 } 400 401 public void setPreferredAuthenticationScheme(String preferredAuthenticationScheme) { 402 this.preferredAuthenticationScheme = preferredAuthenticationScheme; 403 } 404 405 public String getRestrictToHosts() { 406 return restrictToHosts.getAsTextFieldText(); 407 } 408 409 public PropertyList getRestrictToHostsList() { 410 return restrictToHosts; 411 } 412 413 public void setRestrictToHosts(String restrictToHosts) { 414 this.restrictToHosts.setAsTextFieldText(restrictToHosts); 415 } 416 417 public List getPreferredAuthenticationSchemeList() { 418 return WebForwardTypes.PREFERED_SCHEMES; 419 } 420 421 426 public String getTabTitle(int i) { 427 return null; 428 } 429 430 public String getHostHeader() { 431 return hostHeader; 432 } 433 434 public void setHostHeader(String hostHeader) { 435 this.hostHeader = hostHeader; 436 } 437 438 public void reset(ActionMapping mapping, HttpServletRequest request) { 439 super.reset(mapping, request); 440 this.activeDNS = false; 441 } 442 443 public String getFormType() { 444 return formType; 445 } 446 447 public void setFormType(String formType) { 448 this.formType = formType; 449 } 450 451 public String getFormParameters() { 452 return formParameters; 453 } 454 455 public void setFormParameters(String formParameters) { 456 this.formParameters = formParameters; 457 } 458 459 public List getFormTypeList() { 460 return WebForwardTypes.FORM_SUBMIT_TYPES; 461 } 462 463 public List getEncodeingTypeList() { 464 return WebForwardTypes.ENCODING_TYPES; 465 } 466 467 470 public String getTabBundle(int idx) { 471 return null; 472 } 473 } | Popular Tags |