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.KeyImpl; 024 import railo.runtime.type.Struct; 025 import railo.runtime.type.it.ItAsEnum; 026 import railo.runtime.util.EnumerationWrapper; 027 028 public class ServletContextDummy implements ServletContext { 029 private Struct attributes; 030 private Struct parameters; 031 private int majorVersion; 032 private int minorVersion; 033 private Config config; 034 private LogConsole log; 035 private Resource root; 036 037 038 public ServletContextDummy(Config config,Resource root,Struct attributes,Struct parameters,int majorVersion, int minorVersion) { 039 this.config=config; 040 this.root=root; 041 this.attributes=attributes; 042 this.parameters=parameters; 043 this.majorVersion=majorVersion; 044 this.minorVersion=minorVersion; 045 log=new LogConsole(Log.LEVEL_INFO,config.getOutWriter()); 046 047 } 048 049 @Override 050 public Object getAttribute(String key) { 051 return attributes.get(key,null); 052 } 053 054 @Override 055 public Enumeration getAttributeNames() { 056 return ItAsEnum.toStringEnumeration(attributes.keyIterator()); 057 } 058 059 @Override 060 public String getInitParameter(String key) { 061 return Caster.toString(parameters.get(key,null),null); 062 } 063 064 @Override 065 public Enumeration getInitParameterNames() { 066 return new EnumerationWrapper(parameters.keyIterator()); 067 } 068 069 @Override 070 public int getMajorVersion() { 071 return majorVersion; 072 } 073 074 @Override 075 public int getMinorVersion() { 076 return minorVersion; 077 } 078 079 @Override 080 public String getMimeType(String file) { 081 return ResourceUtil.getMimeType(config.getResource(file),null); 082 } 083 084 @Override 085 public String getRealPath(String realpath) { 086 return root.getRealResource(realpath).getAbsolutePath(); 087 } 088 089 @Override 090 public URL getResource(String realpath) throws MalformedURLException { 091 Resource res = getRealResource(realpath); 092 if(res instanceof File)return ((File)res).toURL(); 093 return new URL(res.getAbsolutePath()); 094 } 095 096 @Override 097 public InputStream getResourceAsStream(String realpath) { 098 try { 099 return getRealResource(realpath).getInputStream(); 100 } catch (IOException e) { 101 return null; 102 } 103 } 104 105 public Resource getRealResource(String realpath) { 106 return root.getRealResource(realpath); 107 } 108 109 public Set getResourcePaths(String realpath) { 110 // TODO Auto-generated method stub 111 return null; 112 } 113 114 public RequestDispatcher getRequestDispatcher(String path) { 115 // TODO Auto-generated method stub 116 return null; 117 } 118 119 public ServletContext getContext(String key) { 120 // TODO ? 121 return this; 122 } 123 124 public RequestDispatcher getNamedDispatcher(String name) { 125 // TODO Auto-generated method stub 126 return null; 127 } 128 129 @Override 130 public void log(String msg, Throwable t) { 131 if(t==null)log.log(Log.LEVEL_INFO, "ServletContext", msg); 132 else log.log(Log.LEVEL_ERROR, "ServletContext", msg+":\n"+ExceptionUtil.getStacktrace(t,false)); 133 } 134 135 @Override 136 public void log(Exception e, String msg) { 137 log(msg,e); 138 } 139 140 @Override 141 public void log(String msg) { 142 log(msg,null); 143 } 144 145 @Override 146 public void removeAttribute(String key) { 147 attributes.removeEL(KeyImpl.init(key)); 148 } 149 150 @Override 151 public void setAttribute(String key, Object value) { 152 attributes.setEL(KeyImpl.init(key), value); 153 } 154 155 156 157 158 public String getServletContextName() { 159 // can return null 160 return null; 161 } 162 163 public String getServerInfo() { 164 // deprecated 165 return null; 166 } 167 168 public Servlet getServlet(String arg0) throws ServletException { 169 // deprecated 170 return null; 171 } 172 173 public Enumeration getServletNames() { 174 // deprecated 175 return null; 176 } 177 178 public Enumeration getServlets() { 179 // deprecated 180 return null; 181 } 182 183 }