001    package railo.runtime.net.http;
002    
003    import java.io.File;
004    import java.io.IOException;
005    import java.io.InputStream;
006    import java.net.MalformedURLException;
007    import java.net.URL;
008    import java.util.Enumeration;
009    import java.util.Set;
010    
011    import javax.servlet.RequestDispatcher;
012    import javax.servlet.Servlet;
013    import javax.servlet.ServletContext;
014    import javax.servlet.ServletException;
015    
016    import railo.commons.io.log.Log;
017    import railo.commons.io.log.LogConsole;
018    import railo.commons.io.res.Resource;
019    import railo.commons.io.res.util.ResourceUtil;
020    import railo.commons.lang.ExceptionUtil;
021    import railo.runtime.config.Config;
022    import railo.runtime.op.Caster;
023    import railo.runtime.type.Struct;
024    import railo.runtime.util.EnumerationWrapper;
025    
026    public class ServletContextDummy implements ServletContext {
027            private Struct attributes;
028            private Struct parameters;
029            private int majorVersion;
030            private int minorVersion;
031            private Config config;
032            private LogConsole log;
033            private Resource root;
034            
035            
036            public ServletContextDummy(Config config,Resource root,Struct attributes,Struct parameters,int majorVersion, int minorVersion) {
037                    this.config=config;
038                    this.root=root;
039                    this.attributes=attributes;
040                    this.parameters=parameters;
041                    this.majorVersion=majorVersion;
042                    this.minorVersion=minorVersion;
043                    log=new LogConsole(Log.LEVEL_INFO,config.getOutWriter());
044                    
045            }
046    
047            /**
048             * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
049             */
050            public Object getAttribute(String key) {
051                    return attributes.get(key,null);
052            }
053    
054            /**
055             * @see javax.servlet.ServletContext#getAttributeNames()
056             */
057            public Enumeration getAttributeNames() {
058                    return new EnumerationWrapper(attributes.keyIterator());
059            }
060            
061            /**
062             * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
063             */
064            public String getInitParameter(String key) {
065                    return Caster.toString(parameters.get(key,null),null);
066            }
067    
068            /**
069             * @see javax.servlet.ServletContext#getInitParameterNames()
070             */
071            public Enumeration getInitParameterNames() {
072                    return new EnumerationWrapper(parameters.keyIterator());
073            }
074    
075            /**
076             * @see javax.servlet.ServletContext#getMajorVersion()
077             */
078            public int getMajorVersion() {
079                    return majorVersion;
080            }
081    
082            /**
083             * @see javax.servlet.ServletContext#getMinorVersion()
084             */
085            public int getMinorVersion() {
086                    return minorVersion;
087            }
088    
089            /**
090             * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
091             */
092            public String getMimeType(String file) {
093                    return ResourceUtil.getMymeType(config.getResource(file),null);
094            }
095    
096            /**
097             * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
098             */
099            public String getRealPath(String realpath) {
100                    return root.getRealResource(realpath).getAbsolutePath();
101            }
102    
103            /**
104             * @see javax.servlet.ServletContext#getResource(java.lang.String)
105             */
106            public URL getResource(String realpath) throws MalformedURLException {
107                    Resource res = getRealResource(realpath);
108                    if(res instanceof File)return ((File)res).toURL();
109                    return new URL(res.getAbsolutePath());
110            }
111    
112            /**
113             * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
114             */
115            public InputStream getResourceAsStream(String realpath) {
116                    try {
117                            return getRealResource(realpath).getInputStream();
118                    } catch (IOException e) {
119                            return null;
120                    }
121            }
122    
123            public Resource getRealResource(String realpath) {
124                    return root.getRealResource(realpath);
125            }
126    
127            public Set getResourcePaths(String realpath) {
128                    // TODO Auto-generated method stub
129                    return null;
130            }
131    
132            public RequestDispatcher getRequestDispatcher(String path) {
133                    // TODO Auto-generated method stub
134                    return null;
135            }
136    
137            public ServletContext getContext(String key) {
138                    // TODO ?
139                    return this;
140            }
141    
142            public RequestDispatcher getNamedDispatcher(String name) {
143                    // TODO Auto-generated method stub
144                    return null;
145            }
146    
147            /**
148             * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
149             */
150            public void log(String msg, Throwable t) {
151                    if(t==null)log.log(Log.LEVEL_INFO, "ServletContext", msg);
152                    else log.log(Log.LEVEL_ERROR, "ServletContext", msg+":\n"+ExceptionUtil.getStacktrace(t,false));
153            }
154    
155            /**
156             * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
157             */
158            public void log(Exception e, String msg) {
159                    log(msg,e);
160            }
161    
162            /**
163             * @see javax.servlet.ServletContext#log(java.lang.String)
164             */
165            public void log(String msg) {
166                    log(msg,null);
167            }
168    
169            /**
170             * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
171             */
172            public void removeAttribute(String key) {
173                    attributes.remove(key);
174            }
175    
176            /**
177             * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
178             */
179            public void setAttribute(String key, Object value) {
180                    attributes.setEL(key, value);
181            }
182            
183            
184    
185    
186            public String getServletContextName() {
187                    // can return null
188                    return null;
189            }
190    
191            public String getServerInfo() {
192                    // deprecated
193                    return null;
194            }
195    
196            public Servlet getServlet(String arg0) throws ServletException {
197                    // deprecated
198                    return null;
199            }
200    
201            public Enumeration getServletNames() {
202                    // deprecated
203                    return null;
204            }
205    
206            public Enumeration getServlets() {
207                    // deprecated
208                    return null;
209            }
210    
211    }