001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.http;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.net.MalformedURLException;
025import java.net.URL;
026import java.util.Enumeration;
027import java.util.Set;
028
029import javax.servlet.RequestDispatcher;
030import javax.servlet.Servlet;
031import javax.servlet.ServletContext;
032import javax.servlet.ServletException;
033
034import lucee.commons.io.log.log4j.Log4jUtil;
035import lucee.commons.io.res.Resource;
036import lucee.commons.io.res.util.ResourceUtil;
037import lucee.runtime.config.Config;
038import lucee.runtime.op.Caster;
039import lucee.runtime.type.KeyImpl;
040import lucee.runtime.type.Struct;
041import lucee.runtime.type.it.ItAsEnum;
042import lucee.runtime.util.EnumerationWrapper;
043
044import org.apache.log4j.Level;
045import org.apache.log4j.Logger;
046
047public class ServletContextDummy implements ServletContext {
048        private Struct attributes;
049        private Struct parameters;
050        private int majorVersion;
051        private int minorVersion;
052        private Config config;
053        private Logger log;
054        private Resource root;
055        
056        
057        public ServletContextDummy(Config config,Resource root,Struct attributes,Struct parameters,int majorVersion, int minorVersion) {
058                this.config=config;
059                this.root=root;
060                this.attributes=attributes;
061                this.parameters=parameters;
062                this.majorVersion=majorVersion;
063                this.minorVersion=minorVersion;
064                log=Log4jUtil.getConsoleLog(config, false,"servlet-context-dummy",Level.INFO);
065                
066        }
067
068        @Override
069        public Object getAttribute(String key) {
070                return attributes.get(key,null);
071        }
072
073        @Override
074        public Enumeration getAttributeNames() {
075                return ItAsEnum.toStringEnumeration(attributes.keyIterator());
076        }
077        
078        @Override
079        public String getInitParameter(String key) {
080                return Caster.toString(parameters.get(key,null),null);
081        }
082
083        @Override
084        public Enumeration getInitParameterNames() {
085                return new EnumerationWrapper(parameters.keyIterator());
086        }
087
088        @Override
089        public int getMajorVersion() {
090                return majorVersion;
091        }
092
093        @Override
094        public int getMinorVersion() {
095                return minorVersion;
096        }
097
098        @Override
099        public String getMimeType(String file) {
100                return ResourceUtil.getMimeType(config.getResource(file),null);
101        }
102
103        @Override
104        public String getRealPath(String relpath) {
105                return root.getRealResource(relpath).getAbsolutePath();
106        }
107
108        @Override
109        public URL getResource(String relpath) throws MalformedURLException {
110                Resource res = getRealResource(relpath);
111                if(res instanceof File)return ((File)res).toURL();
112                return new URL(res.getAbsolutePath());
113        }
114
115        @Override
116        public InputStream getResourceAsStream(String relpath) {
117                try {
118                        return getRealResource(relpath).getInputStream();
119                } catch (IOException e) {
120                        return null;
121                }
122        }
123
124        public Resource getRealResource(String relpath) {
125                return root.getRealResource(relpath);
126        }
127
128        public Set getResourcePaths(String relpath) {
129                // TODO Auto-generated method stub
130                return null;
131        }
132
133        public RequestDispatcher getRequestDispatcher(String path) {
134                // TODO Auto-generated method stub
135                return null;
136        }
137
138        public ServletContext getContext(String key) {
139                // TODO ?
140                return this;
141        }
142
143        public RequestDispatcher getNamedDispatcher(String name) {
144                // TODO Auto-generated method stub
145                return null;
146        }
147
148        @Override
149        public void log(String msg, Throwable t) {
150                if(t==null)log.log(Level.INFO,msg);
151                else log.log(Level.ERROR, msg,t);
152        }
153
154        @Override
155        public void log(Exception e, String msg) {
156                log(msg,e);
157        }
158
159        @Override
160        public void log(String msg) {
161                log(msg,null);
162        }
163
164        @Override
165        public void removeAttribute(String key) {
166                attributes.removeEL(KeyImpl.init(key));
167        }
168
169        @Override
170        public void setAttribute(String key, Object value) {
171                attributes.setEL(KeyImpl.init(key), value);
172        }
173        
174        
175
176
177        public String getServletContextName() {
178                // can return null
179                return null;
180        }
181
182        public String getServerInfo() {
183                // deprecated
184                return null;
185        }
186
187        public Servlet getServlet(String arg0) throws ServletException {
188                // deprecated
189                return null;
190        }
191
192        public Enumeration getServletNames() {
193                // deprecated
194                return null;
195        }
196
197        public Enumeration getServlets() {
198                // deprecated
199                return null;
200        }
201
202}