KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > webforwards > webforwardwizard > forms > WebForwardSpecificDetailsForm


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.webforwardwizard.forms;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.struts.Globals;
34 import org.apache.struts.action.ActionErrors;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37
38 import com.sslexplorer.boot.PropertyList;
39 import com.sslexplorer.core.BundleActionMessage;
40 import com.sslexplorer.core.forms.AbstractResourceDetailsWizardForm;
41 import com.sslexplorer.vfs.webdav.DAVUtilities;
42 import com.sslexplorer.webforwards.WebForward;
43 import com.sslexplorer.webforwards.WebForwardPlugin;
44 import com.sslexplorer.webforwards.WebForwardTypes;
45 import com.sslexplorer.wizard.AbstractWizardSequence;
46
47 /**
48  * <p>
49  * The form for all other attributes associated with the TunneledSite resource.
50  *
51  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
52  *
53  */

54 public class WebForwardSpecificDetailsForm extends AbstractResourceDetailsWizardForm {
55
56     final static Log log = LogFactory.getLog(WebForwardSpecificDetailsForm.class);
57
58     private int type = -1;
59     // for all web forwards
60
public final static String JavaDoc ATTR_DESTINATION_URL = "destinationURL";
61     public final static String JavaDoc ATTR_CATEGORY = "category";
62
63     private String JavaDoc destinationURL;
64     private String JavaDoc category;
65
66     // Replacement proxy attributes
67
public final static String JavaDoc ATTR_RESTRICT_TO_HOSTS = "restrictToHosts";
68     public final static String JavaDoc ATTR_ENCODEING = "encoding";
69
70     private String JavaDoc encoding;
71     private PropertyList restrictToHosts;
72
73     // reverse proxy attribute
74
public final static String JavaDoc ATTR_PATHS = "paths";
75     public final static String JavaDoc ATTR_ACTIVE_DNS = "activeDNS";
76     public final static String JavaDoc ATTR_CUSTOM_HEADERS = "customHeaders";
77
78     public static final String JavaDoc ATTR_HOST_HEADER = "hostHeader";
79
80     private String JavaDoc hostHeader;
81     private String JavaDoc paths;
82     private boolean activeDNS;
83     private Map JavaDoc customHeaders;
84
85     /**
86      * Construtor
87      */

88     public WebForwardSpecificDetailsForm() {
89         super(true, true, "/WEB-INF/jsp/content/webforward/webforwardwizard/webForwardSpecificDetails.jspf", "destinationURL", true, false,
90                         "webForwardSpecificDetails", "webForwards", "webForwardWizard.webForwardSpecificDetails", 3, WebForwardPlugin.WEBFORWARD_RESOURCE_TYPE);
91     }
92
93     /*
94      * (non-Javadoc)
95      *
96      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence,
97      * javax.servlet.http.HttpServletRequest)
98      */

99     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
100         super.init(sequence, request);
101         this.restrictToHosts = (PropertyList)sequence.getAttribute(ATTR_RESTRICT_TO_HOSTS, new PropertyList());
102         this.destinationURL = (String JavaDoc) sequence.getAttribute(ATTR_DESTINATION_URL, "");
103         this.category = (String JavaDoc) sequence.getAttribute(ATTR_CATEGORY, "General");
104         type = ((Integer JavaDoc) sequence.getAttribute(WebForwardTypeSelectionForm.ATTR_TYPE, new Integer JavaDoc(0))).intValue();
105         this.encoding = (String JavaDoc) sequence.getAttribute(ATTR_ENCODEING, WebForwardTypes.DEFAULT_ENCODING);
106
107         this.paths = (String JavaDoc) sequence.getAttribute(ATTR_PATHS, "");
108         this.hostHeader = (String JavaDoc) sequence.getAttribute(ATTR_HOST_HEADER, "");
109         this.activeDNS = ((Boolean JavaDoc) sequence.getAttribute(ATTR_ACTIVE_DNS, Boolean.FALSE)).booleanValue();
110         this.customHeaders = (Map JavaDoc) sequence.getAttribute(ATTR_CUSTOM_HEADERS, new HashMap JavaDoc());
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#apply(com.sslexplorer.wizard.AbstractWizardSequence)
117      */

118     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
119         super.apply(sequence);
120         sequence.putAttribute(ATTR_DESTINATION_URL, this.destinationURL);
121         sequence.putAttribute(ATTR_CATEGORY, this.category);
122         sequence.putAttribute(ATTR_ENCODEING, this.encoding);
123         sequence.putAttribute(ATTR_PATHS, this.paths);
124         sequence.putAttribute(ATTR_ACTIVE_DNS, new Boolean JavaDoc(this.activeDNS));
125         sequence.putAttribute(ATTR_HOST_HEADER, activeDNS ? "" : this.hostHeader);
126         sequence.putAttribute(ATTR_CUSTOM_HEADERS, this.customHeaders);
127         sequence.putAttribute(ATTR_RESTRICT_TO_HOSTS, restrictToHosts);
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
134      * javax.servlet.http.HttpServletRequest)
135      */

136     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
137         if (getResourceName() != null && isCommiting()) {
138             ActionErrors errs = super.validate(mapping, request);
139             AbstractWizardSequence seq = getWizardSequence(request);
140             
141             try {
142                 if (this.getDestinationURL().indexOf("${") == -1){
143                     // only chek the format if there is no hash on the front.
144
new URL JavaDoc(this.getDestinationURL());
145                 }
146             } catch (MalformedURLException JavaDoc e) {
147                 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
148                                 .getCurrentPageForm().getResourcePrefix()
149                                 + ".error.malformedURLException"));
150             }
151             
152
153             if(getCategory().trim().equals("")) {
154                 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
155                                 .getCurrentPageForm().getResourcePrefix()
156                                 + ".error.noCategory"));
157             }
158             
159             if (type == WebForward.TYPE_PATH_BASED_REVERSE_PROXY){
160                 if (this.paths != null && this.paths.length() == 0){
161                     errs.add(Globals.ERROR_KEY, new ActionMessage("webForwardWizard.webForwardSpecificDetails.error.needs.path"));
162                 }
163                 String JavaDoc validatedPaths = "";
164                 
165                 if (this.paths != null && this.paths.length() > 0){
166                     StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(this.paths, "\n\r");
167                     while (t.hasMoreTokens()) {
168                         String JavaDoc path = t.nextToken();
169                         path = path.trim();
170                         if(!path.startsWith("/"))
171                             path = "/" + path;
172                         if(path.endsWith("/"))
173                             path = DAVUtilities.stripTrailingSlash(path);
174
175                         validatedPaths += path + "\n";
176                     }
177                     if(errs.size() == 0)
178                         this.paths = validatedPaths;
179                 }
180             } else if (type == WebForward.TYPE_HOST_BASED_REVERSE_PROXY){
181                 if (this.activeDNS && !this.hostHeader.equals("")){
182                     errs.add(Globals.ERROR_KEY, new ActionMessage("webForwardWizard.webForwardSpecificDetails.error.hostBased.bothSelected"));
183                 }
184                 if (!this.activeDNS && this.hostHeader.equals("")){
185                     errs.add(Globals.ERROR_KEY, new ActionMessage("webForwardWizard.webForwardSpecificDetails.error.hostBased.nonSelected"));
186                 }
187             }
188             
189             return errs;
190         }
191         return null;
192     }
193
194     /**
195      * @return The Web Forward Category.
196      */

197     public String JavaDoc getCategory() {
198         return category;
199     }
200
201     /**
202      * @param category The Web Forward Category.
203      */

204     public void setCategory(String JavaDoc category) {
205         this.category = category;
206     }
207
208     /**
209      * @return The destination URL.
210      */

211     public String JavaDoc getDestinationURL() {
212         return destinationURL;
213     }
214
215     /**
216      * @param destinationURL The destination URL.
217      */

218     public void setDestinationURL(String JavaDoc destinationURL) {
219         this.destinationURL = destinationURL;
220     }
221
222     public boolean isPathBased() {
223         return WebForward.TYPE_PATH_BASED_REVERSE_PROXY == type;
224     }
225     
226     public boolean isHostBased() {
227         return WebForward.TYPE_HOST_BASED_REVERSE_PROXY == type;
228     }
229     
230     public PropertyList getRestrictToHostsList() {
231         return restrictToHosts;
232     }
233
234     public String JavaDoc getRestrictToHosts() {
235         return restrictToHosts.getAsTextFieldText();
236     }
237
238     public void setRestrictToHosts(String JavaDoc restrictToHosts) {
239         this.restrictToHosts.setAsTextFieldText(restrictToHosts);
240     }
241
242     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
243         super.reset(mapping, request);
244         this.activeDNS = false;
245     }
246
247     public boolean isActiveDNS() {
248         return activeDNS;
249     }
250
251     public void setActiveDNS(boolean activeDNS) {
252         this.activeDNS = activeDNS;
253     }
254
255     public Map JavaDoc getCustomHeaders() {
256         return customHeaders;
257     }
258
259     public void setCustomHeaders(Map JavaDoc customHeaders) {
260         this.customHeaders = customHeaders;
261     }
262
263     public String JavaDoc getPaths() {
264         return paths;
265     }
266
267     public void setPaths(String JavaDoc paths) {
268         this.paths = paths;
269     }
270     public String JavaDoc getEncoding() {
271         return encoding;
272     }
273
274     public void setEncoding(String JavaDoc encoding) {
275         this.encoding = encoding;
276     }
277
278     public String JavaDoc getHostHeader() {
279         return hostHeader;
280     }
281
282     public void setHostHeader(String JavaDoc hostHeader) {
283         this.hostHeader = hostHeader;
284     }
285
286     public List JavaDoc getEncodeingTypeList() {
287         return WebForwardTypes.ENCODING_TYPES;
288     }
289 }
Popular Tags