KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tunnels > TunnelResourceType


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;
21
22 import java.util.Set JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.struts.util.LabelValueBean;
29
30 import com.sslexplorer.agent.DefaultAgentManager;
31 import com.sslexplorer.core.CoreEvent;
32 import com.sslexplorer.core.CoreServlet;
33 import com.sslexplorer.navigation.FavoriteResourceType;
34 import com.sslexplorer.navigation.WrappedFavoriteItem;
35 import com.sslexplorer.policyframework.DefaultResourceType;
36 import com.sslexplorer.policyframework.LaunchSession;
37 import com.sslexplorer.policyframework.LaunchSessionFactory;
38 import com.sslexplorer.policyframework.PolicyConstants;
39 import com.sslexplorer.policyframework.Resource;
40 import com.sslexplorer.policyframework.ResourceChangeEvent;
41 import com.sslexplorer.policyframework.ResourceDeleteEvent;
42 import com.sslexplorer.security.LogonControllerFactory;
43 import com.sslexplorer.security.SessionInfo;
44 import com.sslexplorer.tunnels.forms.TunnelItem;
45
46 /**
47  * Implementation of a {@link com.sslexplorer.policyframework.ResourceType} for
48  * <i>SSL Tunnel</i> resources.
49  *
50  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
51  */

52 public class TunnelResourceType extends DefaultResourceType implements FavoriteResourceType {
53
54     final static Log log = LogFactory.getLog(TunnelResourceType.class);
55
56     /**
57      * Constructor
58      */

59     public TunnelResourceType() {
60         super(TunnelPlugin.SSL_TUNNEL_RESOURCE_TYPE_ID, "policyframework", PolicyConstants.DELEGATION_CLASS);
61     }
62
63     /*
64      * (non-Javadoc)
65      *
66      * @see com.sslexplorer.navigation.FavoriteResourceType#createWrappedFavoriteItem(int,
67      * javax.servlet.http.HttpServletRequest, java.lang.String)
68      */

69     public WrappedFavoriteItem createWrappedFavoriteItem(int resourceId, HttpServletRequest JavaDoc request, String JavaDoc type) throws Exception JavaDoc {
70         Tunnel tunnel = TunnelDatabaseFactory.getInstance().getTunnel(resourceId);
71         if (tunnel == null) {
72             return null;
73         }
74
75         if (DefaultAgentManager.getInstance().hasActiveAgent(request)) {
76             Set JavaDoc activeTunnels = ((TunnelingService) DefaultAgentManager.getInstance().getService(TunnelingService.class))
77                             .getActiveTunnels(LogonControllerFactory.getInstance().getSessionInfo(request));
78             SessionInfo sessionInfo = LogonControllerFactory.getInstance().getSessionInfo(request);
79             LaunchSession launchSession = activeTunnels.contains(Integer.valueOf(tunnel.getResourceId())) ? LaunchSessionFactory
80                             .getInstance().getFirstLaunchSessionForResource(sessionInfo, tunnel) : null;
81             return new WrappedFavoriteItem(new TunnelItem(tunnel, null, launchSession), type);
82         } else
83             return new WrappedFavoriteItem(new TunnelItem(tunnel, null, null), type);
84
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see com.sslexplorer.navigation.FavoriteResourceType#getResourceById(int)
91      */

92     public Resource getResourceById(int resourceId) throws Exception JavaDoc {
93         return TunnelDatabaseFactory.getInstance().getTunnel(resourceId);
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see com.sslexplorer.policyframework.DefaultResourceType#getResourceByName(java.lang.String,
100      * com.sslexplorer.security.SessionInfo)
101      */

102     public Resource getResourceByName(String JavaDoc resourceName, SessionInfo session) throws Exception JavaDoc {
103         return TunnelDatabaseFactory.getInstance().getTunnel(resourceName, session.getUser().getRealm().getRealmID());
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see com.sslexplorer.boot.policyframework.ResourceType#removeResource(int,
110      * com.sslexplorer.security.SessionInfo)
111      */

112     public Resource removeResource(int resourceId, SessionInfo session) throws Exception JavaDoc {
113         Tunnel resource = null;
114         try {
115             resource = TunnelDatabaseFactory.getInstance().removeTunnel(resourceId);
116             CoreServlet.getServlet().fireCoreEvent(
117                             addTunnelAttributes(new ResourceDeleteEvent(this, TunnelsEventConstants.REMOVE_TUNNEL, resource, session,
118                                             CoreEvent.STATE_SUCCESSFUL), resource));
119             return resource;
120         } catch (Exception JavaDoc e) {
121             CoreServlet.getServlet().fireCoreEvent(
122                             addTunnelAttributes(new ResourceDeleteEvent(this, TunnelsEventConstants.REMOVE_TUNNEL, null, session,
123                                             CoreEvent.STATE_UNSUCCESSFUL), resource));
124             throw e;
125         }
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see com.sslexplorer.boot.policyframework.ResourceType#updateResource(com.sslexplorer.boot.policyframework.Resource,
132      * com.sslexplorer.security.SessionInfo)
133      */

134     public void updateResource(Resource resource, SessionInfo session) throws Exception JavaDoc {
135         Tunnel t = null;
136         try {
137             t = (Tunnel) resource;
138             TunnelDatabaseFactory.getInstance().updateTunnel(t.getResourceId(), t.getResourceName(), t.getResourceDescription(),
139                             t.getType(), t.isAutoStart(), t.getTransport(), t.getUsername(), t.getSourcePort(), t.getDestination(),
140                             t.getSourceInterface());
141             CoreServlet.getServlet().fireCoreEvent(
142                             addTunnelAttributes(new ResourceChangeEvent(this, TunnelsEventConstants.UPDATE_TUNNEL, resource, session,
143                                             CoreEvent.STATE_SUCCESSFUL), t));
144         } catch (Exception JavaDoc e) {
145             CoreServlet.getServlet().fireCoreEvent(
146                             addTunnelAttributes(new ResourceChangeEvent(this, TunnelsEventConstants.UPDATE_TUNNEL, null, session,
147                                             CoreEvent.STATE_UNSUCCESSFUL), t));
148             throw e;
149         }
150     }
151
152     CoreEvent addTunnelAttributes(CoreEvent evt, Tunnel tunnel) {
153         evt.addAttribute(TunnelsEventConstants.EVENT_ATTR_TUNNEL_SOURCE_INTERFACE, tunnel.getSourceInterface());
154         evt.addAttribute(TunnelsEventConstants.EVENT_ATTR_TUNNEL_SOURCE_PORT, String.valueOf(tunnel.getSourcePort()));
155         evt.addAttribute(TunnelsEventConstants.EVENT_ATTR_TUNNEL_DESTINATION, String.valueOf(tunnel.getDestination()));
156         evt.addAttribute(TunnelsEventConstants.EVENT_ATTR_TUNNEL_TRANSPORT, String.valueOf(tunnel.getTransport()));
157         evt.addAttribute(TunnelsEventConstants.EVENT_ATTR_TUNNEL_TYPE,
158                         ((LabelValueBean) TransportType.TYPES.get(tunnel.getType())).getLabel());
159         return evt;
160     }
161
162 }
Popular Tags