66
77import com .androthink .server .helper .ServerHelper ;
88
9- import java .io .BufferedInputStream ;
109import java .io .DataOutputStream ;
1110import java .io .IOException ;
1211import java .util .Date ;
@@ -26,6 +25,10 @@ private ResponseHandler(Context context, DataOutputStream responseStream) {
2625 this .responseStream = responseStream ;
2726 }
2827
28+ public Context getContext () {
29+ return this .context ;
30+ }
31+
2932 /**
3033 * @param json Json to be sent as Response .
3134 * @throws IOException throws exception if response channel closed .
@@ -89,136 +92,203 @@ public void sendJsonResponse(int code, String json) throws IOException {
8992
9093 /**
9194 * @param code custom response code
92- * @param filename filename for the html file to be sent .
95+ * @param type Content-Type
96+ * @param assetPath filename for the assets file to be sent .
9397 * @param customHeaders headers to be sent with the response .
9498 * @throws IOException throws exception if response channel closed .
9599 */
96- // HTML Response ..
97- public void sendHtmlFileResponseWithCustomHeader (int code , String filename , Map <String , String > customHeaders ) throws IOException {
100+ public void sendAssetFile (int code , String type , String assetPath , Map <String , String > customHeaders ) throws IOException {
98101
99- String page = ServerHelper .getHtmlFromAsset (context , filename );
102+ byte [] data = ServerHelper .loadFileFromAsset (context , assetPath , true );
103+ sendResponseHeader (code , type , data .length , customHeaders );
104+ this .responseStream .write (data );
105+ this .responseStream .flush ();
106+ this .responseStream .close ();
107+ }
100108
101- byte [] data = page .getBytes ("UTF-8" );
102- sendResponseHeader (code , ServerHelper .CONTENT_TYPE .HTML , data .length , customHeaders );
109+ /**
110+ * @param code custom response code
111+ * @param type Content-Type
112+ * @param filename filename for the file to be sent .
113+ * @param customHeaders headers to be sent with the response .
114+ * @throws IOException throws exception if response channel closed .
115+ */
116+ public void sendFile (int code , String type , String filename , Map <String , String > customHeaders ) throws IOException {
117+
118+ byte [] data = ServerHelper .loadFileAsBytes (filename , true );
119+ sendResponseHeader (code , type , data .length , customHeaders );
120+ this .responseStream .write (data );
121+ this .responseStream .flush ();
122+ this .responseStream .close ();
123+ }
124+
125+ /**
126+ * @param code custom response code
127+ * @param type Content-Type
128+ * @param assetPath filename for the assets file to be sent .
129+ * @param customHeaders headers to be sent with the response .
130+ * @throws IOException throws exception if response channel closed .
131+ */
132+ public void sendAssetMediaFile (int code , String type , String assetPath , Map <String , String > customHeaders ) throws IOException {
103133
134+ byte [] data = ServerHelper .loadFileFromAsset (context , assetPath , false );
135+ sendResponseHeader (code , type , data .length , customHeaders );
136+ this .responseStream .write (data );
137+ this .responseStream .flush ();
138+ this .responseStream .close ();
139+ }
140+
141+ /**
142+ * @param code custom response code
143+ * @param type Content-Type
144+ * @param filename filename for the file to be sent .
145+ * @param customHeaders headers to be sent with the response .
146+ * @throws IOException throws exception if response channel closed .
147+ */
148+ public void sendMediaFile (int code , String type , String filename , Map <String , String > customHeaders ) throws IOException {
149+
150+ byte [] data = ServerHelper .loadFileAsBytes (filename , false );
151+ sendResponseHeader (code , type , data .length , customHeaders );
152+ this .responseStream .write (data );
153+ this .responseStream .flush ();
154+ this .responseStream .close ();
155+ }
156+
157+ /**
158+ * @param code custom response code
159+ * @param type Content-Type
160+ * @param assetPath filename for the assets file to be sent .
161+ * @throws IOException throws exception if response channel closed .
162+ */
163+ public void sendAssetFile (int code , String type , String assetPath ) throws IOException {
164+
165+ byte [] data = ServerHelper .loadFileFromAsset (context , assetPath , true );
166+ sendResponseHeader (code , type , data .length );
104167 this .responseStream .write (data );
105168 this .responseStream .flush ();
106169 this .responseStream .close ();
107170 }
108171
109172 /**
110173 * @param code custom response code
111- * @param filename filename for the html file to be sent .
174+ * @param type Content-Type
175+ * @param filename filename for the file to be sent .
112176 * @throws IOException throws exception if response channel closed .
113177 */
114- public void sendHtmlFileResponse (int code , String filename ) throws IOException {
178+ public void sendFile (int code , String type , String filename ) throws IOException {
115179
116- String page = ServerHelper .getHtmlFromAsset (context , filename );
180+ byte [] data = ServerHelper .loadFileAsBytes (filename , true );
181+ sendResponseHeader (code , type , data .length );
182+ this .responseStream .write (data );
183+ this .responseStream .flush ();
184+ this .responseStream .close ();
185+ }
117186
118- byte [] data = page .getBytes ("UTF-8" );
119- sendResponseHeader (code , ServerHelper .CONTENT_TYPE .HTML , data .length );
187+ /**
188+ * @param code custom response code
189+ * @param type Content-Type
190+ * @param assetPath filename for the assets file to be sent .
191+ * @throws IOException throws exception if response channel closed .
192+ */
193+ public void sendAssetMediaFile (int code , String type , String assetPath ) throws IOException {
120194
195+ byte [] data = ServerHelper .loadFileFromAsset (context , assetPath , false );
196+ sendResponseHeader (code , type , data .length );
197+ this .responseStream .write (data );
198+ this .responseStream .flush ();
199+ this .responseStream .close ();
200+ }
201+
202+ /**
203+ * @param code custom response code
204+ * @param type Content-Type
205+ * @param filename filename for the file to be sent .
206+ * @throws IOException throws exception if response channel closed .
207+ */
208+ public void sendMediaFile (int code , String type , String filename ) throws IOException {
209+
210+ byte [] data = ServerHelper .loadFileAsBytes (filename , false );
211+ sendResponseHeader (code , type , data .length );
121212 this .responseStream .write (data );
122213 this .responseStream .flush ();
123214 this .responseStream .close ();
124215 }
125216
126217 /**
127218 * @param code custom response code
219+ * @param type Content-Type
128220 * @param filename filename for the html file to be sent .
129221 * @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
130222 * @throws IOException throws exception if response channel closed .
131223 */
132- public void sendHtmlFileResponse (int code , String filename , @ NonNull Map <String , String > placeHolders ) throws IOException {
224+ public void sendFileWithPlaceHolder (int code , String type , String filename , @ NonNull Map <String , String > placeHolders ) throws IOException {
133225
134- String page = ServerHelper .getHtmlFromAsset ( context , filename );
226+ String page = new String ( ServerHelper .loadFileAsBytes ( filename , true ), "UTF-8" );
135227 String value ;
136228 for (String key : placeHolders .keySet ()) {
137229 value = placeHolders .get (key );
138230 page = page .replace (key , (value != null ? value : "" ));
139231 }
140232
141233 byte [] data = page .getBytes ("UTF-8" );
142- sendResponseHeader (code , ServerHelper . CONTENT_TYPE . HTML , data .length );
234+ sendResponseHeader (code , type , data .length );
143235
144236 this .responseStream .write (data );
145237 this .responseStream .flush ();
146238 this .responseStream .close ();
147239 }
148240
149241 /**
150- * @param code custom response code
151- * @param filename filename for the html file to be sent .
152- * @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
153- * @param customHeaders headers to be sent with the response .
242+ * @param code custom response code
243+ * @param type Content-Type
244+ * @param filename filename for the html file to be sent .
245+ * @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
154246 * @throws IOException throws exception if response channel closed .
155247 */
156- public void sendHtmlFileResponse (int code , String filename , @ NonNull Map <String , String > placeHolders , Map < String , String > customHeaders ) throws IOException {
248+ public void sendAssetFileWithPlaceHolder (int code , String type , String filename , @ NonNull Map <String , String > placeHolders ) throws IOException {
157249
158- String page = ServerHelper .getHtmlFromAsset (context , filename );
250+ String page = new String ( ServerHelper .loadFileFromAsset (context , filename , true ), "UTF-8" );
159251 String value ;
160252 for (String key : placeHolders .keySet ()) {
161253 value = placeHolders .get (key );
162254 page = page .replace (key , (value != null ? value : "" ));
163255 }
164256
165257 byte [] data = page .getBytes ("UTF-8" );
166- sendResponseHeader (code , ServerHelper . CONTENT_TYPE . HTML , data .length , customHeaders );
258+ sendResponseHeader (code , type , data .length );
167259
168260 this .responseStream .write (data );
169261 this .responseStream .flush ();
170262 this .responseStream .close ();
171263 }
172264
173265 /**
174- * @param filename filename for the html file to be sent .
175- * @param contentType response content type .
266+ * @param code custom response code
267+ * @param type Content-Type
268+ * @param response Response to be sent .
176269 * @throws IOException throws exception if response channel closed .
177270 */
178- public void sendResourceFileResponse (String filename , String contentType ) throws IOException {
179-
180- BufferedInputStream file = ServerHelper .getResourceFromAsset (context , filename );
181- sendResponseHeader (ServerHelper .RESPONSE_CODE .OK , contentType , file .available ());
271+ public void sendResponse (int code , String type , @ NonNull byte [] response ) throws IOException {
182272
183- int u ;
184- byte [] buffer = new byte [1024 ];
185- while ((u = file .read (buffer , 0 , buffer .length )) != -1 ) {
186- this .responseStream .write (buffer , 0 , u );
187- }
188- file .close ();
273+ sendResponseHeader (code , type , response .length );
274+ this .responseStream .write (response );
189275 this .responseStream .flush ();
190276 this .responseStream .close ();
191277 }
192278
193- public Context getContext () {
194- return this .context ;
195- }
196-
197- /**
198- * @param image image to be sent .
199- * @throws IOException throws exception if response channel closed .
200- */
201- // Response With Image (Response closed after image sent)
202- public void sendImageResponse (@ NonNull byte [] image ) throws IOException {
203-
204- sendResponseHeader (ServerHelper .RESPONSE_CODE .OK , ServerHelper .CONTENT_TYPE .JPEG , image .length );
205-
206- responseStream .write (image );
207- responseStream .flush ();
208- responseStream .close ();
209- }
210-
211279 /**
212- * @param sound sound to be sent .
280+ * @param code custom response code
281+ * @param type Content-Type
282+ * @param response Response to be sent .
283+ * @param customHeaders headers to be sent with the response .
213284 * @throws IOException throws exception if response channel closed .
214285 */
215- public void sendSoundResponse (@ NonNull byte [] sound ) throws IOException {
216-
217- sendResponseHeader (ServerHelper .RESPONSE_CODE .OK , ServerHelper .CONTENT_TYPE .MPEG , sound .length );
286+ public void sendResponse (int code , String type , @ NonNull byte [] response , Map <String , String > customHeaders ) throws IOException {
218287
219- responseStream .write (sound );
220- responseStream .flush ();
221- responseStream .close ();
288+ sendResponseHeader (code , type , response .length , customHeaders );
289+ this .responseStream .write (response );
290+ this .responseStream .flush ();
291+ this .responseStream .close ();
222292 }
223293
224294 // Stream (Response not closed after image sent)
0 commit comments