KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tunnels > wizards > forms > TunnelDetailsForm


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.tunnels.wizards.forms;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.NetworkInterface JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
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.Util;
36 import com.sslexplorer.core.BundleActionMessage;
37 import com.sslexplorer.core.forms.AbstractResourceDetailsWizardForm;
38 import com.sslexplorer.input.validators.HostnameOrIPAddressWithReplacementsValidator;
39 import com.sslexplorer.input.validators.IPAddressValidator;
40 import com.sslexplorer.tunnels.TransportType;
41 import com.sslexplorer.tunnels.TunnelPlugin;
42 import com.sslexplorer.wizard.AbstractWizardSequence;
43
44 public class TunnelDetailsForm extends AbstractResourceDetailsWizardForm {
45
46     public final static String JavaDoc ATTR_SOURCE_PORT = "sourcePort";
47     public final static String JavaDoc ATTR_DESTINATION_HOST = "destinationHost";
48     public final static String JavaDoc ATTR_DESTINATION_PORT = "destinationPort";
49     public final static String JavaDoc ATTR_TYPE = "tunnelType";
50     public final static String JavaDoc ATTR_TRANSPORT = "transport";
51     public final static String JavaDoc ATTR_AUTO_START = "autoStart";
52     public final static String JavaDoc ATTR_SOURCE_INTERFACE = "sourceInterface";
53
54     private String JavaDoc sourcePort;
55     private String JavaDoc destinationHost;
56     private String JavaDoc destinationPort;
57     private int tunnelType;
58     private String JavaDoc transport;
59     private boolean autoStart;
60     private String JavaDoc sourceInterface;
61
62     final static Log log = LogFactory.getLog(TunnelDetailsForm.class);
63
64     public TunnelDetailsForm() {
65         super(true, true, "/WEB-INF/jsp/content/tunnels/tunnelWizard/tunnelDetails.jspf", "resourceName", true, false,
66                         "tunnelDetails", "tunnels", "tunnelWizard.tunnelDetails", 2, TunnelPlugin.SSL_TUNNEL_RESOURCE_TYPE);
67     }
68
69     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
70         super.init(sequence, request);
71         sourcePort = ((Integer JavaDoc) sequence.getAttribute(ATTR_SOURCE_PORT, new Integer JavaDoc(0))).toString();
72         destinationHost = (String JavaDoc) sequence.getAttribute(ATTR_DESTINATION_HOST, "");
73         destinationPort = ((Integer JavaDoc) sequence.getAttribute(ATTR_DESTINATION_PORT, new Integer JavaDoc(0))).toString();
74         tunnelType = ((Integer JavaDoc) sequence.getAttribute(ATTR_TYPE, new Integer JavaDoc(TransportType.LOCAL_TUNNEL_ID))).intValue();
75         transport = (String JavaDoc) sequence.getAttribute(ATTR_TRANSPORT, String.valueOf(TransportType.TCP_TUNNEL));
76         autoStart = ((Boolean JavaDoc) sequence.getAttribute(ATTR_AUTO_START, Boolean.FALSE)).booleanValue();
77         sourceInterface = ((String JavaDoc) sequence.getAttribute(ATTR_SOURCE_INTERFACE, "127.0.0.1"));
78     }
79
80     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
81         super.apply(sequence);
82         sequence.putAttribute(ATTR_SOURCE_PORT, new Integer JavaDoc(sourcePort));
83         sequence.putAttribute(ATTR_DESTINATION_HOST, destinationHost);
84         sequence.putAttribute(ATTR_DESTINATION_PORT, new Integer JavaDoc(destinationPort));
85         sequence.putAttribute(ATTR_TYPE, new Integer JavaDoc(tunnelType));
86         sequence.putAttribute(ATTR_TRANSPORT, transport);
87         sequence.putAttribute(ATTR_AUTO_START, new Boolean JavaDoc(autoStart));
88         sequence.putAttribute(ATTR_SOURCE_INTERFACE, sourceInterface);
89     }
90
91     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
92         if (getResourceName() != null && isCommiting()) {
93             ActionErrors errs = super.validate(mapping, request);
94             AbstractWizardSequence seq = getWizardSequence(request);
95
96
97             if (!Util.isNullOrTrimmedBlank(sourceInterface)) {
98                 /**
99                  * For remote tunnels, the listening interface must be a valid
100                  * IP address of a network interface on this server
101                  */

102                 if(getTunnelType() == TransportType.REMOTE_TUNNEL_ID) {
103                     if(!sourceInterface.trim().equals("0.0.0.0") &&
104                         !sourceInterface.trim().equals("127.0.0.2"))
105                     try {
106                         InetAddress JavaDoc addr = InetAddress.getByName(sourceInterface);
107                         NetworkInterface JavaDoc nif = NetworkInterface.getByInetAddress(addr);
108                         if(nif == null) {
109                             throw new Exception JavaDoc();
110                         }
111                     }
112                     catch(Exception JavaDoc e) {
113                         errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidRemoteSourceInterface"));
114                     }
115                 }
116                 else {
117                     /**
118                      * For local tunnels, we do not know what will be a valid IP
119                      * address until the client is running so all we can do
120                      * is validate that it looks like an IP address
121                      */

122                     if(!IPAddressValidator.isIpAddressExpressionValid(sourceInterface)) {
123                         errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidLocaleSourceInterface"));
124                     }
125                 }
126             }
127             
128             try {
129                 int port = Integer.valueOf(sourcePort).intValue();
130                 if(port < 1 || port > 65535) {
131                     throw new IllegalArgumentException JavaDoc();
132                 }
133             } catch (Exception JavaDoc e) {
134                 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
135                                 .getCurrentPageForm().getResourcePrefix()
136                                 + ".error.sourcePortNotInteger"));
137             }
138
139             try {
140                 int port = Integer.valueOf(destinationPort).intValue();
141                 if(port < 1 || port > 65535) {
142                     throw new IllegalArgumentException JavaDoc();
143                 }
144             } catch (Exception JavaDoc e) {
145                 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
146                                 .getCurrentPageForm().getResourcePrefix()
147                                 + ".error.destinationPortNotInteger"));
148             }
149
150             if (destinationHost == null || destinationHost.equals("")) {
151                 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
152                                 .getCurrentPageForm().getResourcePrefix()
153                                 + ".error.noDestinationHost"));
154             }
155             else{
156                 if(!HostnameOrIPAddressWithReplacementsValidator.isValidAsHostOrIp(destinationHost)) {
157                     errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidHost"));
158                 }
159             }
160
161             if (transport.equals(TransportType.UDP_TUNNEL) && tunnelType == TransportType.REMOTE_TUNNEL_ID) {
162                 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq
163                                 .getCurrentPageForm().getResourcePrefix()
164                                 + ".error.remote.udp"));
165             }
166
167             return errs;
168         }
169         return null;
170     }
171
172     public String JavaDoc getSourcePort() {
173         return sourcePort;
174     }
175
176     public void setSourcePort(String JavaDoc sourcePort) {
177         this.sourcePort = sourcePort;
178     }
179
180     public void setSourceInterface(String JavaDoc sourceInterface) {
181         this.sourceInterface = sourceInterface;
182     }
183
184     public String JavaDoc getSourceInterface() {
185         return sourceInterface;
186     }
187
188     public boolean isAutoStart() {
189         return autoStart;
190     }
191
192     public void setAutoStart(boolean autoStart) {
193         this.autoStart = autoStart;
194     }
195
196     public String JavaDoc getDestinationHost() {
197         return destinationHost;
198     }
199
200     public void setDestinationHost(String JavaDoc destinationHost) {
201         this.destinationHost = destinationHost;
202     }
203
204     public String JavaDoc getDestinationPort() {
205         return destinationPort;
206     }
207
208     public void setDestinationPort(String JavaDoc destinationPort) {
209         this.destinationPort = destinationPort;
210     }
211
212     public int getTunnelType() {
213         return tunnelType;
214     }
215
216     public void setTunnelType(int tunnelType) {
217         this.tunnelType = tunnelType;
218     }
219
220     public void setTransport(String JavaDoc transport) {
221         this.transport = transport;
222     }
223
224     public String JavaDoc getTransport() {
225         return transport;
226     }
227
228     public List JavaDoc getTunnelTypeList() {
229         return TransportType.TYPES;
230     }
231
232     public List JavaDoc getTransportList() {
233         return TransportType.TRANSPORTS;
234     }
235 }
Popular Tags