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 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, April 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.servlet.ServletResponse; 00037 00038 private import mango.io.Uri, 00039 mango.io.Buffer, 00040 mango.io.Exception, 00041 mango.io.FileConduit; 00042 00043 private import mango.io.model.IBuffer; 00044 00045 private import mango.servlet.ServletContext; 00046 00047 private import mango.servlet.model.IServletResponse; 00048 00049 private import mango.http.HttpWriter; 00050 00051 private import mango.http.server.HttpParams, 00052 mango.http.server.HttpCookies, 00053 mango.http.server.HttpHeaders, 00054 mango.http.server.HttpResponse; 00055 00056 private import mango.http.server.model.IProviderBridge; 00057 00058 /****************************************************************************** 00059 00060 ******************************************************************************/ 00061 00062 class ServletResponse : HttpResponse, IServletResponse 00063 { 00064 /********************************************************************** 00065 00066 **********************************************************************/ 00067 00068 this (IProviderBridge bridge) 00069 { 00070 // initialize the HttpRequest 00071 super (bridge); 00072 } 00073 00074 /********************************************************************** 00075 00076 **********************************************************************/ 00077 00078 void reset () 00079 { 00080 // reset HttpRequest 00081 super.reset(); 00082 } 00083 00084 /********************************************************************** 00085 00086 **********************************************************************/ 00087 00088 HttpMutableParams getParameters () 00089 { 00090 return super.getOutputParams(); 00091 } 00092 00093 /********************************************************************** 00094 00095 **********************************************************************/ 00096 00097 HttpMutableCookies getCookies () 00098 { 00099 return super.getOutputCookies(); 00100 } 00101 00102 /********************************************************************** 00103 00104 **********************************************************************/ 00105 00106 HttpMutableHeaders getHeaders () 00107 { 00108 return super.getOutputHeaders(); 00109 } 00110 00111 /*********************************************************************** 00112 00113 ***********************************************************************/ 00114 00115 HttpWriter getWriter() 00116 { 00117 return super.getWriter(); 00118 } 00119 00120 /*********************************************************************** 00121 00122 ***********************************************************************/ 00123 00124 void setContentLength (int len) 00125 { 00126 getHeader().addInt (HttpHeader.ContentLength, len); 00127 } 00128 00129 00130 /*********************************************************************** 00131 00132 ***********************************************************************/ 00133 00134 void setContentType (char[] type) 00135 { 00136 super.setContentType (type); 00137 } 00138 00139 /*********************************************************************** 00140 00141 ***********************************************************************/ 00142 00143 void flushBuffer() 00144 { 00145 super.flush (getWriter ()); 00146 } 00147 00148 /*********************************************************************** 00149 00150 The argument 'status' should be "inout" instead so as to 00151 enforce pass-by-reference semantics. However, one cannot 00152 do that with a const struct. D apparently still requires 00153 further development in this area. 00154 00155 ***********************************************************************/ 00156 00157 void sendError (inout HttpStatus status, char[] msg) 00158 { 00159 super.sendError (status, msg); 00160 } 00161 00162 00163 /*********************************************************************** 00164 00165 The argument 'status' should be "inout" instead so as to 00166 enforce pass-by-reference semantics. However, one cannot 00167 do that with a const struct. D apparently still requires 00168 further development in this area. 00169 00170 ***********************************************************************/ 00171 00172 void sendError (inout HttpStatus status) 00173 { 00174 super.sendError (status); 00175 } 00176 00177 00178 /*********************************************************************** 00179 00180 ***********************************************************************/ 00181 00182 void sendRedirect(char[] location) 00183 { 00184 super.sendRedirect (location); 00185 } 00186 00187 /*********************************************************************** 00188 00189 The argument 'status' should be "inout" instead so as to 00190 enforce pass-by-reference semantics. However, one cannot 00191 do that with a const struct. D apparently still requires 00192 further development in this area. 00193 00194 ***********************************************************************/ 00195 00196 void setStatus (inout HttpStatus status) 00197 { 00198 super.setStatus (status); 00199 } 00200 00201 /*********************************************************************** 00202 00203 ***********************************************************************/ 00204 00205 bool copyFile (ServletContext context, char[] path) 00206 { 00207 FileConduit conduit; 00208 00209 try { 00210 // does the file exist? 00211 conduit = context.getResourceAsFile (path); 00212 00213 // set expected output size 00214 setContentLength (conduit.length()); 00215 00216 // set content-type if not already set 00217 if (super.getContentType() is null) 00218 { 00219 char[] mime = context.getMimeType (conduit.getPath.getExtension()); 00220 if (mime is null) 00221 mime = "text/plain"; 00222 00223 super.setContentType (mime); 00224 } 00225 00226 // copy file to output conduit 00227 getWriter.getBuffer.getConduit.copy (conduit); 00228 return true; 00229 00230 } catch (IOException x) 00231 { 00232 sendError (HttpResponses.NotFound); 00233 } 00234 finally 00235 { 00236 if (conduit) 00237 conduit.close(); 00238 } 00239 return false; 00240 } 00241 } 00242 00243