KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tunnels > forms > TunnelForm


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.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 import org.apache.struts.util.LabelValueBean;
35
36 import com.sslexplorer.boot.HostService;
37 import com.sslexplorer.boot.PropertyList;
38 import com.sslexplorer.boot.Util;
39 import com.sslexplorer.input.MultiSelectSelectionModel;
40 import com.sslexplorer.input.validators.HostnameOrIPAddressWithReplacementsValidator;
41 import com.sslexplorer.input.validators.IPAddressValidator;
42 import com.sslexplorer.policyframework.Resource;
43 import com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm;
44 import com.sslexplorer.security.User;
45 import com.sslexplorer.tunnels.TransportType;
46 import com.sslexplorer.tunnels.Tunnel;
47
48 /**
49  * Implementation of {@link AbstractFavoriteResourceForm} suitable for editing
50  * an <i>SSL Tunnel</i>.
51  *
52  * @author James Robinson <a HREF="mailto: james@3sp.com">&lt;james@3sp.com&gt;</a>
53  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
54  */

55 public class TunnelForm extends AbstractFavoriteResourceForm {
56     static Log log = LogFactory.getLog(TunnelForm.class);
57
58     // Private instance variables
59

60     private String JavaDoc selectedTab = "details";
61
62     private String JavaDoc sourceInterface;
63     private String JavaDoc sourcePort;
64     private String JavaDoc destinationHost;
65     private String JavaDoc destinationPort;
66     private int tunnelType;
67     private String JavaDoc transport;
68     private boolean autoStart;
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#validate(org.apache.struts.action.ActionMapping,
74      * javax.servlet.http.HttpServletRequest)
75      */

76     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
77         ActionErrors errs = super.validate(mapping, request);
78         if (isCommiting()) {
79
80
81             if (!Util.isNullOrTrimmedBlank(sourceInterface)) {
82                 /**
83                  * For remote tunnels, the listening interface must be a valid
84                  * IP address of a network interface on this server
85                  */

86                 if(getTunnelType() == TransportType.REMOTE_TUNNEL_ID) {
87                     if(!sourceInterface.trim().equals("0.0.0.0") &&
88                         !sourceInterface.trim().equals("127.0.0.2"))
89                     try {
90                         InetAddress JavaDoc addr = InetAddress.getByName(sourceInterface);
91                         NetworkInterface JavaDoc nif = NetworkInterface.getByInetAddress(addr);
92                         if(nif == null) {
93                             throw new Exception JavaDoc();
94                         }
95                     }
96                     catch(Exception JavaDoc e) {
97                         errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidRemoteSourceInterface"));
98                     }
99                 }
100                 else {
101                     /**
102                      * For local tunnels, we do not know what will be a valid IP
103                      * address until the client is running so all we can do
104                      * is validate that it looks like an IP address
105                      */

106                     if(!IPAddressValidator.isIpAddressExpressionValid(sourceInterface)) {
107                         errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidLocaleSourceInterface"));
108                     }
109                 }
110             }
111                         
112             try {
113                 int port = Integer.valueOf(sourcePort).intValue();
114                 if(port < 1 || port > 65535) {
115                     throw new IllegalArgumentException JavaDoc();
116                 }
117             } catch (Exception JavaDoc e) {
118                 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.sourcePortNotInteger"));
119             }
120             
121             try {
122                 int port = Integer.valueOf(destinationPort).intValue();
123                 if(port < 1 || port > 65535) {
124                     throw new IllegalArgumentException JavaDoc();
125                 }
126                 Integer.valueOf(destinationPort).intValue();
127             } catch (Exception JavaDoc e) {
128                 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.destinationPortNotInteger"));
129             }
130
131             if (Util.isNullOrTrimmedBlank(destinationHost)) {
132                 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.noDestinationHost"));
133             }
134             else{
135                 if(!HostnameOrIPAddressWithReplacementsValidator.isValidAsHostOrIp(destinationHost)) {
136                     errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidHost"));
137                 }
138             }
139             
140             if (getResourceName().equalsIgnoreCase("default")
141                             && (!getEditing() || (getEditing() && !getResource().getResourceName().equalsIgnoreCase("default")))) {
142                 errs.add(Globals.ERROR_KEY, new ActionMessage("error.createNetworkPlace.cantUseNameDefault"));
143                 setResourceName("");
144             }
145         }
146         return errs;
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see com.sslexplorer.tabs.TabModel#getTabCount()
153      */

154     public int getTabCount() {
155         return 3;
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see com.sslexplorer.tabs.TabModel#getTabName(int)
162      */

163     public String JavaDoc getTabName(int idx) {
164         switch (idx) {
165             case 0:
166                 return "details";
167             case 1:
168                 return "other";
169             default:
170                 return "policies";
171         }
172     }
173
174     /*
175      * (non-Javadoc)
176      *
177      * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
178      */

179     public String JavaDoc getTabTitle(int idx) {
180
181         // Get from resources
182
return null;
183     }
184
185     /*
186      * (non-Javadoc)
187      *
188      * @see com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm#initialise(com.sslexplorer.security.User,
189      * com.sslexplorer.policyframework.Resource, boolean,
190      * com.sslexplorer.input.MultiSelectSelectionModel,
191      * com.sslexplorer.boot.PropertyList, com.sslexplorer.security.User,
192      * boolean)
193      */

194     public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel,
195                     PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception JavaDoc {
196         super.initialise(user, resource, editing, policyModel, selectedPolicies, owner, assignOnly);
197         Tunnel tunnel = (Tunnel) resource;
198         sourcePort = String.valueOf(tunnel.getSourcePort());
199         destinationHost = tunnel.getDestination().getHost();
200         destinationPort = String.valueOf(tunnel.getDestination().getPort());
201         tunnelType = tunnel.getType();
202         transport = tunnel.getTransport();
203         autoStart = tunnel.isAutoStart();
204         sourceInterface = tunnel.getSourceInterface();
205     }
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
211      */

212     public String JavaDoc getSelectedTab() {
213         return selectedTab;
214     }
215
216     /*
217      * (non-Javadoc)
218      *
219      * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
220      */

221     public void setSelectedTab(String JavaDoc selectedTab) {
222         this.selectedTab = selectedTab;
223     }
224
225     /*
226      * (non-Javadoc)
227      *
228      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#applyToResource()
229      */

230     public void applyToResource() throws Exception JavaDoc {
231         Tunnel tunnel = (Tunnel) getResource();
232         tunnel.setType(getTunnelType());
233         tunnel.setAutoStart(isAutoStart());
234         tunnel.setTransport(getTransport());
235         tunnel.setSourcePort(Integer.parseInt(getSourcePort()));
236         tunnel.setDestination(new HostService(getDestinationHost(), Integer.parseInt(getDestinationPort())));
237         tunnel.setSourceInterface(getSourceInterface());
238     }
239
240     /**
241      * Get the source port
242      *
243      * @return source port
244      */

245     public String JavaDoc getSourcePort() {
246         return sourcePort;
247     }
248
249     /**
250      * Set the source port
251      *
252      * @param sourcePort source port
253      */

254     public void setSourcePort(String JavaDoc sourcePort) {
255         this.sourcePort = sourcePort;
256     }
257
258     /**
259      * Get the source interface
260      *
261      * @return source interface
262      */

263     public String JavaDoc getSourceInterface() {
264         return sourceInterface;
265     }
266
267     /**
268      * Set the source interface
269      *
270      * @param sourceInterface source interface
271      */

272     public void setSourceInterface(String JavaDoc sourceInterface) {
273         this.sourceInterface = sourceInterface;
274     }
275
276     /**
277      * Get whether the tunnel should auto-start
278      *
279      * @return auto start
280      */

281     public boolean isAutoStart() {
282         return autoStart;
283     }
284
285     /**
286      * Set whether the tunnel shhould auto-start
287      *
288      * @param autoStart auto start
289      */

290     public void setAutoStart(boolean autoStart) {
291         this.autoStart = autoStart;
292     }
293
294     /**
295      * Get the destination hostname or IP address
296      *
297      * @return destination hostname or IP address
298      */

299     public String JavaDoc getDestinationHost() {
300         return destinationHost;
301     }
302
303     /**
304      * Set the destination hostname or IP address
305      *
306      * @param destinationHost destination hostname or IP address
307      */

308     public void setDestinationHost(String JavaDoc destinationHost) {
309         this.destinationHost = destinationHost;
310     }
311
312     /**
313      * Get the destination port
314      *
315      * @return destination port
316      */

317     public String JavaDoc getDestinationPort() {
318         return destinationPort;
319     }
320
321     /**
322      * Set the destination port.
323      *
324      * @param destinationPort destination port
325      */

326     public void setDestinationPort(String JavaDoc destinationPort) {
327         this.destinationPort = destinationPort;
328     }
329
330     /**
331      * Get the tunnel type. This may be one of
332      * {@link TransportType#REMOTE_TUNNEL_ID} or
333      * {@link TransportType#LOCAL_TUNNEL_ID}.
334      *
335      * @return tunnel type
336      */

337     public int getTunnelType() {
338         return tunnelType;
339     }
340
341     /**
342      * Set the tunnel type. This may be one of
343      * {@link TransportType#REMOTE_TUNNEL_ID} or
344      * {@link TransportType#LOCAL_TUNNEL_ID}.
345      *
346      * @param tunnelType tunnel type
347      */

348     public void setTunnelType(int tunnelType) {
349         this.tunnelType = tunnelType;
350     }
351
352     /**
353      * Set the tunnel trasport. This may be one of
354      * {@link TransportType#TCP_TUNNEL} or {@link TransportType#UDP_TUNNEL}.
355      *
356      * @param transport transport type
357      */

358     public void setTransport(String JavaDoc transport) {
359         this.transport = transport;
360     }
361
362     /**
363      * Get the tunnel trasport. This may be one of
364      * {@link TransportType#TCP_TUNNEL} or {@link TransportType#UDP_TUNNEL}.
365      *
366      * @return transport type
367      */

368     public String JavaDoc getTransport() {
369         return transport;
370     }
371
372     /**
373      * Get a list of available tunnel types as {@link LabelValueBean} objects.
374      *
375      * @return tunnel types
376      */

377     public List JavaDoc getTunnelTypeList() {
378         return TransportType.TYPES;
379     }
380
381     /**
382      * Get a list of available tunnel transports as {@link LabelValueBean}
383      * objects.
384      *
385      * @return tunnel types
386      */

387     public List JavaDoc getTransportList() {
388         return TransportType.TRANSPORTS;
389     }
390
391     /**
392      * Get the tunnel type as a string
393      *
394      * @return tunnel type string
395      */

396     public String JavaDoc getTunnelTypeString() {
397         return ((LabelValueBean) getTunnelTypeList().get(getTunnelType())).getLabel();
398     }
399
400     /*
401      * (non-Javadoc)
402      *
403      * @see com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm#reset(org.apache.struts.action.ActionMapping,
404      * javax.servlet.http.HttpServletRequest)
405      */

406     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
407         super.reset(mapping, request);
408         this.sourceInterface = "127.0.0.1";
409         this.autoStart = false;
410     }
411
412     /*
413      * (non-Javadoc)
414      *
415      * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
416      */

417     public String JavaDoc getTabBundle(int idx) {
418         return null;
419     }
420 }
Popular Tags