KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > webforwards > forms > WebForwardForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.webforwards.forms;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
48  * <p>
49  * Form for providing the attributes to be edited and validated.
50  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
51  */

52 public class WebForwardForm extends AbstractFavoriteResourceForm {
53     public static final String JavaDoc ATTR_NO_AUTHENTICATION = "none";
54     public static final String JavaDoc ATTR_FORM_BASED_AUTHENTICATION = "form";
55     public static final String JavaDoc ATTR_HTTP_BASED_AUTHENTICATION = "http";
56     private String JavaDoc selectedTab = "details";
57     private int type;
58     private String JavaDoc destinationURL;
59     private String JavaDoc category;
60
61     // Replacement proxy attributes
62
private PropertyList restrictToHosts;
63     private String JavaDoc encoding;
64
65     private String JavaDoc authenticationType;
66     
67     // Authenticating web forward
68
private String JavaDoc authenticationUsername;
69     private String JavaDoc authenticationPassword;
70     private String JavaDoc preferredAuthenticationScheme;
71
72     // Form based authentication
73
private String JavaDoc formType;
74     private String JavaDoc formParameters;
75
76     // reverse proxy attribute
77
private String JavaDoc paths;
78     private boolean activeDNS;
79     private String JavaDoc hostHeader;
80     private Map JavaDoc customHeaders;
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
86      * javax.servlet.http.HttpServletRequest)
87      */

88     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc 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                     // only chek the format if there is no hash on the front as this indicates no validation.
99
new URL JavaDoc(this.getDestinationURL());
100                 }
101             } catch (MalformedURLException JavaDoc 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 JavaDoc paths = "";
118                     StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(this.paths, "\n\r");
119                     while (t.hasMoreTokens()) {
120                         String JavaDoc 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     /*
149      * (non-Javadoc)
150      *
151      * @see com.sslexplorer.tabs.TabModel#getTabCount()
152      */

153     public int getTabCount() {
154         if (type == WebForward.TYPE_TUNNELED_SITE){
155             return 3;
156         }
157         else{
158             return 4;
159         }
160     }
161
162     /*
163      * (non-Javadoc)
164      *
165      * @see com.sslexplorer.tabs.TabModel#getTabName(int)
166      */

167     public String JavaDoc 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     /*
194      * (non-Javadoc)
195      *
196      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#initialise(com.sslexplorer.security.User,
197      * com.sslexplorer.boot.policyframework.Resource, boolean,
198      * com.sslexplorer.boot.MultiSelectSelectionModel,
199      * com.sslexplorer.boot.PropertyList, com.sslexplorer.security.User)
200      */

201     public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel,
202                     PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception JavaDoc {
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     /*
235      * (non-Javadoc)
236      *
237      * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
238      */

239     public String JavaDoc getSelectedTab() {
240         return selectedTab;
241     }
242
243     /*
244      * (non-Javadoc)
245      *
246      * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
247      */

248     public void setSelectedTab(String JavaDoc selectedTab) {
249         this.selectedTab = selectedTab;
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#applyToResource()
256      */

257     public void applyToResource() throws Exception JavaDoc {
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     /**
283      * @return The Web Forward Category.
284      */

285     public String JavaDoc getCategory() {
286         return category;
287     }
288
289     /**
290      * @param category The Web Forward Category.
291      */

292     public void setCategory(String JavaDoc category) {
293         this.category = category;
294     }
295
296     /**
297      * @return The destination URL.
298      */

299     public String JavaDoc getDestinationURL() {
300         return destinationURL;
301     }
302
303     /**
304      * @param destinationURL The destination URL.
305      */

306     public void setDestinationURL(String JavaDoc 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     /**
319      * @return The type of web forward.
320      */

321     public int getType() {
322         return type;
323     }
324
325     /**
326      * @param type The type of web forward.
327      */

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 JavaDoc 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 JavaDoc authenticationType) {
354         this.authenticationType = authenticationType;
355     }
356     
357     public String JavaDoc getAuthenticationPassword() {
358         return authenticationPassword;
359     }
360
361     public void setAuthenticationPassword(String JavaDoc authenticationPassword) {
362         this.authenticationPassword = authenticationPassword;
363     }
364
365     public String JavaDoc getAuthenticationUsername() {
366         return authenticationUsername;
367     }
368
369     public void setAuthenticationUsername(String JavaDoc authenticationUsername) {
370         this.authenticationUsername = authenticationUsername;
371     }
372
373     public Map JavaDoc getCustomHeaders() {
374         return customHeaders;
375     }
376
377     public void setCustomHeaders(Map JavaDoc customHeaders) {
378         this.customHeaders = customHeaders;
379     }
380
381     public String JavaDoc getEncoding() {
382         return encoding;
383     }
384
385     public void setEncoding(String JavaDoc encoding) {
386         this.encoding = encoding;
387     }
388
389     public String JavaDoc getPaths() {
390         return paths;
391     }
392
393     public void setPaths(String JavaDoc paths) {
394         this.paths = paths;
395     }
396
397     public String JavaDoc getPreferredAuthenticationScheme() {
398         return preferredAuthenticationScheme;
399     }
400
401     public void setPreferredAuthenticationScheme(String JavaDoc preferredAuthenticationScheme) {
402         this.preferredAuthenticationScheme = preferredAuthenticationScheme;
403     }
404
405     public String JavaDoc getRestrictToHosts() {
406         return restrictToHosts.getAsTextFieldText();
407     }
408
409     public PropertyList getRestrictToHostsList() {
410         return restrictToHosts;
411     }
412
413     public void setRestrictToHosts(String JavaDoc restrictToHosts) {
414         this.restrictToHosts.setAsTextFieldText(restrictToHosts);
415     }
416
417     public List JavaDoc getPreferredAuthenticationSchemeList() {
418         return WebForwardTypes.PREFERED_SCHEMES;
419     }
420
421     /*
422      * (non-Javadoc)
423      *
424      * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
425      */

426     public String JavaDoc getTabTitle(int i) {
427         return null;
428     }
429
430     public String JavaDoc getHostHeader() {
431         return hostHeader;
432     }
433
434     public void setHostHeader(String JavaDoc hostHeader) {
435         this.hostHeader = hostHeader;
436     }
437
438     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
439         super.reset(mapping, request);
440         this.activeDNS = false;
441     }
442
443     public String JavaDoc getFormType() {
444         return formType;
445     }
446
447     public void setFormType(String JavaDoc formType) {
448         this.formType = formType;
449     }
450
451     public String JavaDoc getFormParameters() {
452         return formParameters;
453     }
454
455     public void setFormParameters(String JavaDoc formParameters) {
456         this.formParameters = formParameters;
457     }
458
459     public List JavaDoc getFormTypeList() {
460         return WebForwardTypes.FORM_SUBMIT_TYPES;
461     }
462     
463     public List JavaDoc getEncodeingTypeList() {
464         return WebForwardTypes.ENCODING_TYPES;
465     }
466
467     /* (non-Javadoc)
468      * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
469      */

470     public String JavaDoc getTabBundle(int idx) {
471         return null;
472     }
473 }
Popular Tags