00001 /******************************************************************************* 00002 00003 @file ServletResponse.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, April 2004 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.servlet.ServletResponse; 00040 00041 private import mango.io.Uri, 00042 mango.io.Buffer, 00043 mango.io.Exception, 00044 mango.io.FileConduit; 00045 00046 private import mango.io.model.IBuffer; 00047 00048 private import mango.servlet.ServletContext; 00049 00050 private import mango.servlet.model.IServletResponse; 00051 00052 private import mango.http.HttpWriter; 00053 00054 private import mango.http.server.HttpParams, 00055 mango.http.server.HttpCookies, 00056 mango.http.server.HttpHeaders, 00057 mango.http.server.HttpResponse; 00058 00059 private import mango.http.server.model.IProviderBridge; 00060 00061 /****************************************************************************** 00062 00063 ******************************************************************************/ 00064 00065 class ServletResponse : HttpResponse, IServletResponse 00066 { 00067 /********************************************************************** 00068 00069 **********************************************************************/ 00070 00071 this (IProviderBridge bridge) 00072 { 00073 // initialize the HttpRequest 00074 super (bridge); 00075 } 00076 00077 /********************************************************************** 00078 00079 **********************************************************************/ 00080 00081 void reset () 00082 { 00083 // reset HttpRequest 00084 super.reset(); 00085 } 00086 00087 /********************************************************************** 00088 00089 **********************************************************************/ 00090 00091 HttpMutableParams getParameters () 00092 { 00093 return super.getOutputParams(); 00094 } 00095 00096 /********************************************************************** 00097 00098 **********************************************************************/ 00099 00100 HttpMutableCookies getCookies () 00101 { 00102 return super.getOutputCookies(); 00103 } 00104 00105 /********************************************************************** 00106 00107 **********************************************************************/ 00108 00109 HttpMutableHeaders getHeaders () 00110 { 00111 return super.getOutputHeaders(); 00112 } 00113 00114 /*********************************************************************** 00115 00116 ***********************************************************************/ 00117 00118 HttpWriter getWriter() 00119 { 00120 return super.getWriter(); 00121 } 00122 00123 /*********************************************************************** 00124 00125 ***********************************************************************/ 00126 00127 void setContentLength (int len) 00128 { 00129 getHeader().addInt (HttpHeader.ContentLength, len); 00130 } 00131 00132 00133 /*********************************************************************** 00134 00135 ***********************************************************************/ 00136 00137 void setContentType (char[] type) 00138 { 00139 super.setContentType (type); 00140 } 00141 00142 /*********************************************************************** 00143 00144 ***********************************************************************/ 00145 00146 void flushBuffer() 00147 { 00148 super.flush (getWriter ()); 00149 } 00150 00151 /*********************************************************************** 00152 00153 The argument 'status' should be "inout" instead so as to 00154 enforce pass-by-reference semantics. However, one cannot 00155 do that with a const struct. D apparently still requires 00156 further development in this area. 00157 00158 ***********************************************************************/ 00159 00160 void sendError (inout HttpStatus status, char[] msg) 00161 { 00162 super.sendError (status, msg); 00163 } 00164 00165 00166 /*********************************************************************** 00167 00168 The argument 'status' should be "inout" instead so as to 00169 enforce pass-by-reference semantics. However, one cannot 00170 do that with a const struct. D apparently still requires 00171 further development in this area. 00172 00173 ***********************************************************************/ 00174 00175 void sendError (inout HttpStatus status) 00176 { 00177 super.sendError (status); 00178 } 00179 00180 00181 /*********************************************************************** 00182 00183 ***********************************************************************/ 00184 00185 void sendRedirect(char[] location) 00186 { 00187 super.sendRedirect (location); 00188 } 00189 00190 /*********************************************************************** 00191 00192 The argument 'status' should be "inout" instead so as to 00193 enforce pass-by-reference semantics. However, one cannot 00194 do that with a const struct. D apparently still requires 00195 further development in this area. 00196 00197 ***********************************************************************/ 00198 00199 void setStatus (inout HttpStatus status) 00200 { 00201 super.setStatus (status); 00202 } 00203 00204 /*********************************************************************** 00205 00206 ***********************************************************************/ 00207 00208 bool copyFile (ServletContext context, char[] path) 00209 { 00210 FileConduit conduit; 00211 00212 try { 00213 // does the file exist? 00214 conduit = context.getResourceAsFile (path); 00215 00216 // set expected output size 00217 setContentLength (cast(int) conduit.length()); 00218 00219 // set content-type if not already set 00220 if (super.getContentType() is null) 00221 { 00222 char[] mime = context.getMimeType (conduit.getPath.getExtension()); 00223 if (mime is null) 00224 mime = "text/plain"; 00225 00226 super.setContentType (mime); 00227 } 00228 00229 // copy file to output conduit 00230 getWriter.getBuffer.getConduit.copy (conduit); 00231 return true; 00232 00233 } catch (IOException x) 00234 { 00235 sendError (HttpResponses.NotFound); 00236 } 00237 finally 00238 { 00239 if (conduit) 00240 conduit.close(); 00241 } 00242 return false; 00243 } 00244 } 00245 00246