001    /*
002     * Copyright © 2008, 2009 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU Lesser General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * Commercial use is permitted to the extent that the code/component(s)
011     * do NOT become part of another Open Source or Commercially developed
012     * licensed development library or toolkit without explicit permission.
013     *
014     * DirectJNgine is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public License
020     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
021     * 
022     * This software uses the ExtJs library (http://extjs.com), which is 
023     * distributed under the GPL v3 license (see http://extjs.com/license).
024     */
025    
026    package com.softwarementors.extjs.djn.servlet.ssm;
027    
028    import javax.servlet.ServletConfig;
029    import javax.servlet.ServletContext;
030    import javax.servlet.http.HttpServlet;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    import javax.servlet.http.HttpSession;
034    
035    import com.softwarementors.extjs.djn.StringUtils;
036    
037    import edu.umd.cs.findbugs.annotations.CheckForNull;
038    import edu.umd.cs.findbugs.annotations.NonNull;
039    
040    public class WebContext {
041      @NonNull private HttpServletRequest request;
042      @NonNull private HttpServletResponse response;
043      @NonNull private HttpServlet servlet;
044      private boolean open;
045    
046      /* package */ WebContext( WebContext context ) {
047        this( context.getServlet(), context.getRequest(), context.getResponse());
048      }  
049      
050      /* package */ WebContext( HttpServlet servlet, HttpServletRequest request, HttpServletResponse response ) {
051        assert servlet != null;
052        assert request != null;
053        assert response != null;
054    
055        this.servlet = servlet;
056        this.request = request;
057        this.response = response;
058        this.open = true;
059      }
060      
061      /* package */ void close() {
062        assert isOpen();
063    
064        /*
065        this.servlet = null;
066        this.request = null;
067        this.response = null;
068        */
069        this.open = false;
070      }
071      
072      private boolean isOpen() {
073        return this.open;
074      }
075      
076      public HttpServletRequest getRequest() {
077        assert isOpen();
078    
079        return this.request;
080      }
081      
082      public HttpServletResponse getResponse() {
083        assert isOpen();
084    
085        return this.response;
086      }
087      
088      public HttpSession getSession() {
089        assert isOpen();
090        
091        return this.request.getSession();
092      }
093      
094      public ServletContext getServletContext() {
095        assert isOpen();
096        
097        return this.servlet.getServletContext();
098      }
099      
100      public ServletConfig getServletConfig() {
101        assert isOpen();
102        
103        return this.servlet.getServletConfig();
104      }
105      
106      public HttpServlet getServlet() {
107        assert isOpen();
108        
109        return this.servlet;
110      }
111      
112      @CheckForNull public Object getSessionScopedObject( String actionName ) {
113        assert !StringUtils.isEmpty(actionName);
114    
115        HttpSession context = getSession();
116        String key = getSessionScopedActionName(actionName);
117        Object result = context.getAttribute(key);
118        return result;
119      }
120      
121      public static String getSessionScopedActionName( String actionName ) {
122        assert !StringUtils.isEmpty(actionName);
123        
124        String key = "DirectJNgine.SESSION." + actionName;
125        return key;    
126      }
127    
128      @CheckForNull public Object getApplicationScopedObject( String actionName ) {
129        assert !StringUtils.isEmpty(actionName);
130        
131        ServletContext context = getServletContext();
132        String key = getApplicationScopedActionName(actionName);
133        Object result = context.getAttribute(key);
134        return result;
135      }
136      
137      public static String getApplicationScopedActionName( String actionName ) {
138        assert !StringUtils.isEmpty(actionName);
139        
140        String key = "DirectJNgine.APPLICATION." + actionName;
141        return key; 
142      }
143    }