KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.struts.util.LabelValueBean;
27
28 import com.sslexplorer.boot.Util;
29 import com.sslexplorer.core.CoreUtil;
30 import com.sslexplorer.navigation.AbstractFavoriteItem;
31 import com.sslexplorer.policyframework.LaunchSession;
32 import com.sslexplorer.security.LogonControllerFactory;
33 import com.sslexplorer.security.SessionInfo;
34 import com.sslexplorer.tunnels.TransportType;
35 import com.sslexplorer.tunnels.Tunnel;
36
37 /**
38  * Implementation of an {@link com.sslexplorer.navigation.AbstractFavoriteItem}
39  * that wraps {@link com.sslexplorer.tunnels.Tunnel} resources for display.
40  *
41  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
43  */

44 public class TunnelItem extends AbstractFavoriteItem {
45
46     private LaunchSession launchSession;
47
48     /**
49      * Constructor
50      *
51      * @param tunnel tunnel
52      * @param policies policies item is attached to
53      * @param launchSession the launch session if open or <code>null</code> if
54      * not
55      */

56     public TunnelItem(Tunnel tunnel, List JavaDoc policies, LaunchSession launchSession) {
57         super(tunnel, policies);
58         this.launchSession = launchSession;
59     }
60
61     /**
62      * Get the transport type for this resource.
63      *
64      * @return transport type
65      * @see Tunnel#getTransport()
66      */

67     public String JavaDoc getTransport() {
68         return ((Tunnel) this.getResource()).getTransport();
69     }
70
71     /**
72      * Get the source port for this tunnel
73      *
74      * @return source port
75      * @see Tunnel#getSourcePort()
76      */

77     public String JavaDoc getSourcePort() {
78         return String.valueOf(((Tunnel) this.getResource()).getSourcePort());
79     }
80
81     /**
82      * Get the destination host for this tunnel
83      *
84      * @return destination host
85      * @see Tunnel#getDestination()
86      */

87     public String JavaDoc getDestinationHost() {
88         return ((Tunnel) this.getResource()).getDestination().getHost();
89     }
90
91     /**
92      * Get the type of tunnel.
93      *
94      * @return type of tunnel
95      * @see Tunnel#getType()
96      */

97     public String JavaDoc getTunnelType() {
98         return ((LabelValueBean) TransportType.TYPES.get(((Tunnel) this.getResource()).getType())).getLabel();
99     }
100
101     /**
102      * Get the destination port for this tunnel
103      *
104      * @return destination port
105      * @see Tunnel#getDestination()
106      */

107     public String JavaDoc getDestinationPort() {
108         return String.valueOf(((Tunnel) this.getResource()).getDestination().getPort());
109     }
110
111     /**
112      * Get the source interface
113      *
114      * @return source interface
115      */

116     public String JavaDoc getSourceInterface() {
117         return ((Tunnel) this.getResource()).getSourceInterface();
118     }
119
120     /**
121      * Get if this tunnel should autostart.
122      *
123      * @return autostart
124      * @see Tunnel#isAutoStart()
125      */

126     public String JavaDoc getAutoStart() {
127         return ((Tunnel) this.getResource()).isAutoStart() ? "Yes" : "No";
128     }
129
130     /**
131      * Get if this tunnel is currently open and active.
132      *
133      * @return tunnel open
134      */

135     public String JavaDoc getOpen() {
136         return launchSession != null ? "true" : "false";
137     }
138
139     /**
140      * Get the tunnel object this item wraps. Provided more for convenience as
141      * {@link com.sslexplorer.policyframework.ResourceItem#getResource} would
142      * normally be used.
143      *
144      * @return tunnel
145      */

146     public Tunnel getTunnel() {
147         return ((Tunnel) this.getResource());
148     }
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see com.sslexplorer.navigation.FavoriteItem#getOnClick()
154      */

155     public String JavaDoc getOnClick(int policy, HttpServletRequest JavaDoc request) {
156         return "return true";
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see com.sslexplorer.navigation.FavoriteItem#getLink()
163      */

164     public String JavaDoc getLink(int policy, String JavaDoc referer, HttpServletRequest JavaDoc request) {
165         if (new Boolean JavaDoc(getOpen()).booleanValue()) {
166             return getCloseLink(referer, request);
167         } else {
168             return getOpenLink(policy, referer, request);
169         }
170     }
171
172     /**
173      * Get the link used to close the tunnel.
174      *
175      * @param referer referer
176      * @return close link
177      */

178     public String JavaDoc getCloseLink(String JavaDoc referer, HttpServletRequest JavaDoc request) {
179         String JavaDoc encodedReferer = Util.urlEncode(referer == null ? "" : referer);
180         SessionInfo info = LogonControllerFactory.getInstance().getSessionInfo(request);
181         if(null != info) {
182             if(info.getNavigationContext() == SessionInfo.MANAGEMENT_CONSOLE_CONTEXT) {
183                 return "/showTunnels.do?actionTarget=stop&" + LaunchSession.LAUNCH_ID + "=" + launchSession.getId()
184                     + (encodedReferer.equals("") ? "" : "&returnTo=" + encodedReferer);
185             } else if (info.getNavigationContext() == SessionInfo.USER_CONSOLE_CONTEXT) {
186                 return "/showUserTunnels.do?actionTarget=stop&" + LaunchSession.LAUNCH_ID + "=" + launchSession.getId()
187                 + (encodedReferer.equals("") ? "" : "&returnTo=" + encodedReferer);
188             }
189         }
190         return "";
191     }
192
193     /**
194      * Get the link to use to open this tunnel
195      *
196      * @param policy policy
197      * @param referer referer
198      * @param request request
199      * @return open link
200      */

201     public String JavaDoc getOpenLink(int policy, String JavaDoc referer, HttpServletRequest JavaDoc request) {
202         return "launchTunnel.do?policy=" + policy + "&resourceId=" + getResource().getResourceId() + "&returnTo="
203                         + Util.urlEncode(Util.isNullOrTrimmedBlank(referer) ? CoreUtil.getRealRequestURI(request) : referer);
204
205     }
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see com.sslexplorer.navigation.FavoriteItem#getName()
211      */

212     public String JavaDoc getFavoriteName() {
213         return getSourcePort() + ":" + getDestinationHost() + ":" + getDestinationPort();
214     }
215
216     /*
217      * (non-Javadoc)
218      *
219      * @see com.sslexplorer.navigation.FavoriteItem#getTarget()
220      */

221     public String JavaDoc getTarget() {
222         return "_self";
223     }
224
225     /*
226      * (non-Javadoc)
227      *
228      * @see com.sslexplorer.navigation.AbstractFavoriteItem#getFavoriteSubType()
229      */

230     public String JavaDoc getFavoriteSubType() {
231         return getTunnel().getType() == TransportType.LOCAL_TUNNEL_ID ? "Local" : "Remote";
232     }
233
234     /*
235      * (non-Javadoc)
236      *
237      * @see com.sslexplorer.navigation.AbstractFavoriteItem#getSmallIconPath(javax.servlet.http.HttpServletRequest)
238      */

239     public String JavaDoc getSmallIconPath(HttpServletRequest JavaDoc request) {
240         return CoreUtil.getThemePath(request.getSession()) + "/images/actions/runTunnel.gif";
241     }
242
243     /*
244      * (non-Javadoc)
245      *
246      * @see com.sslexplorer.navigation.AbstractFavoriteItem#getLargeIconPath(javax.servlet.http.HttpServletRequest)
247      */

248     public String JavaDoc getLargeIconPath(HttpServletRequest JavaDoc request) {
249         return CoreUtil.getThemePath(request.getSession()) + "/images/actions/runTunnelLarge.gif";
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see com.sslexplorer.policyframework.ResourceItem#getLargeIconAdditionalIcon(javax.servlet.http.HttpServletRequest)
256      */

257     public String JavaDoc getLargeIconAdditionalIcon(HttpServletRequest JavaDoc request) {
258         return CoreUtil.getThemePath(request.getSession())
259                 + "/images/actions/" + (launchSession != null ? "switchOn.gif" : "switchOff.gif")/* : ""*/;
260     }
261 }
262
Popular Tags