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.cli.servlet; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.net.MalformedURLException; 026import java.net.URL; 027import java.util.Enumeration; 028import java.util.Map; 029import java.util.Set; 030 031import javax.servlet.RequestDispatcher; 032import javax.servlet.Servlet; 033import javax.servlet.ServletContext; 034import javax.servlet.ServletException; 035 036import lucee.cli.util.EnumerationWrapper; 037 038public class ServletContextImpl implements ServletContext { 039 private Map<String,Object> attributes; 040 private Map<String, String> parameters; 041 private int majorVersion; 042 private int minorVersion; 043 private File root; 044 045 046 public ServletContextImpl(File root,Map<String,Object> attributes,Map<String, String> parameters,int majorVersion, int minorVersion) { 047 this.root=root; 048 this.attributes=attributes; 049 this.parameters=parameters; 050 this.majorVersion=majorVersion; 051 this.minorVersion=minorVersion; 052 } 053 054 /** 055 * @see javax.servlet.ServletContext#getAttribute(java.lang.String) 056 */ 057 public Object getAttribute(String key) { 058 return attributes.get(key); 059 } 060 061 /** 062 * @see javax.servlet.ServletContext#getAttributeNames() 063 */ 064 public Enumeration getAttributeNames() { 065 return new EnumerationWrapper(attributes); 066 } 067 068 /** 069 * @see javax.servlet.ServletContext#getInitParameter(java.lang.String) 070 */ 071 public String getInitParameter(String key) { 072 return parameters.get(key); 073 } 074 075 /** 076 * @see javax.servlet.ServletContext#getInitParameterNames() 077 */ 078 public Enumeration getInitParameterNames() { 079 return new EnumerationWrapper(parameters); 080 } 081 082 /** 083 * @see javax.servlet.ServletContext#getMajorVersion() 084 */ 085 public int getMajorVersion() { 086 return majorVersion; 087 } 088 089 /** 090 * @see javax.servlet.ServletContext#getMinorVersion() 091 */ 092 public int getMinorVersion() { 093 return minorVersion; 094 } 095 096 /** 097 * @see javax.servlet.ServletContext#getMimeType(java.lang.String) 098 */ 099 public String getMimeType(String file) { 100 throw notSupported("getMimeType(String file)"); 101 // TODO 102 //return ResourceUtil.getMimeType(config.getResource(file),null); 103 } 104 105 @Override 106 public String getRealPath(String relpath) { 107 return getRealFile(relpath).getAbsolutePath(); 108 } 109 110 /** 111 * @see javax.servlet.ServletContext#getResource(java.lang.String) 112 */ 113 public URL getResource(String relpath) throws MalformedURLException { 114 File file = getRealFile(relpath); 115 return file.toURL(); 116 } 117 118 /** 119 * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String) 120 */ 121 public InputStream getResourceAsStream(String relpath) { 122 try { 123 return new FileInputStream(getRealFile(relpath)); 124 } catch (IOException e) { 125 return null; 126 } 127 } 128 129 public File getRealFile(String relpath) { 130 return new File(root,relpath); 131 } 132 133 public File getRoot() { 134 return root; 135 } 136 137 public Set getResourcePaths(String relpath) { 138 throw notSupported("getResourcePaths(String relpath)"); 139 } 140 141 public RequestDispatcher getRequestDispatcher(String path) { 142 throw notSupported("getNamedDispatcher(String name)"); 143 } 144 145 public ServletContext getContext(String key) { 146 // TODO ? 147 return this; 148 } 149 150 public RequestDispatcher getNamedDispatcher(String name) { 151 // TODO Auto-generated method stub 152 throw notSupported("getNamedDispatcher(String name)"); 153 } 154 155 /** 156 * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable) 157 */ 158 public void log(String msg, Throwable t) {// TODO better 159 if(t==null)System.out.println(msg); 160 else System.out.println(msg+":\n"+t.getMessage()); 161 162 163 //if(t==null)log.log(Log.LEVEL_INFO, "ServletContext", msg); 164 //else log.log(Log.LEVEL_ERROR, "ServletContext", msg+":\n"+ExceptionUtil.getStacktrace(t,false)); 165 } 166 167 /** 168 * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String) 169 */ 170 public void log(Exception e, String msg) { 171 log(msg,e); 172 } 173 174 /** 175 * @see javax.servlet.ServletContext#log(java.lang.String) 176 */ 177 public void log(String msg) { 178 log(msg,null); 179 } 180 181 /** 182 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String) 183 */ 184 public void removeAttribute(String key) { 185 attributes.remove(key); 186 } 187 188 /** 189 * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object) 190 */ 191 public void setAttribute(String key, Object value) { 192 attributes.put(key, value); 193 } 194 195 196 197 198 public String getServletContextName() { 199 // can return null 200 return null; 201 } 202 203 public String getServerInfo() { 204 // deprecated 205 throw notSupported("getServlet()"); 206 } 207 208 public Servlet getServlet(String arg0) throws ServletException { 209 // deprecated 210 throw notSupported("getServlet()"); 211 } 212 213 public Enumeration getServletNames() { 214 // deprecated 215 throw notSupported("getServlet()"); 216 } 217 218 public Enumeration getServlets() { 219 // deprecated 220 throw notSupported("getServlet()"); 221 } 222 223 private RuntimeException notSupported(String method) { 224 throw new RuntimeException(new ServletException("method "+method+" not supported")); 225 } 226 227}