Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Licensed to the Apache Software Foundation (ASF) under one | |
3 * or more contributor license agreements. See the NOTICE file | |
4 * distributed with this work for additional information | |
5 * regarding copyright ownership. The ASF licenses this file | |
6 * to you under the Apache License, Version 2.0 (the | |
7 * "License"); you may not use this file except in compliance | |
8 * with the License. You may obtain a copy of the License at | |
9 * | |
10 * http://www.apache.org/licenses/LICENSE-2.0 | |
11 * | |
12 * Unless required by applicable law or agreed to in writing, | |
13 * software distributed under the License is distributed on an | |
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 * KIND, either express or implied. See the License for the | |
16 * specific language governing permissions and limitations | |
17 * under the License. | |
18 */ | |
19 package org.apache.shindig.gadgets.servlet.localfile; | |
20 | |
21 import java.io.BufferedInputStream; | |
22 import java.io.BufferedOutputStream; | |
23 import java.io.BufferedReader; | |
24 import java.io.File; | |
25 import java.io.FileInputStream; | |
26 import java.io.FileReader; | |
27 import java.io.IOException; | |
28 import java.io.Writer; | |
29 | |
30 import javax.servlet.ServletException; | |
31 import javax.servlet.http.HttpServlet; | |
32 import javax.servlet.http.HttpServletRequest; | |
33 import javax.servlet.http.HttpServletResponse; | |
34 | |
35 /** | |
36 * A Servlet to serve up files from the local hard drive via HTTP. Only | |
37 * requests from localhost will be honored. | |
louiscryan
2008/12/12 02:09:36
Add a comment making it obvious that this should o
| |
38 */ | |
39 public class LocalFileServlet extends HttpServlet { | |
40 ·· | |
41 private static String LOCAL_IP = "127.0.0.1";· | |
42 ·· | |
43 private static int BUFFER_SIZE = 1024; | |
44 ·· | |
45 @Override | |
46 protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
47 throws ServletException, IOException { | |
48 ···· | |
49 if (!LOCAL_IP.equals(request.getRemoteAddr())) { | |
50 response.sendError(HttpServletResponse.SC_FORBIDDEN); | |
51 return; | |
52 } | |
53 ···· | |
54 String path = request.getPathInfo(); | |
55 ···· | |
56 File file = new File(path); | |
57 if (!file.exists()) { | |
58 response.sendError(HttpServletResponse.SC_NOT_FOUND); | |
59 return; | |
60 } | |
61 ···· | |
62 String contentType = getServletContext().getMimeType(path); | |
63 ···· | |
64 BufferedInputStream in = null; | |
65 BufferedOutputStream out = null; | |
66 ···· | |
67 try { | |
68 ······ | |
69 in = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE); | |
louiscryan
2008/12/12 02:09:36
Use the IOUtils class to do the IO handling, it ha
| |
70 out = new BufferedOutputStream(response.getOutputStream(), BUFFER_SIZE); | |
71 if (contentType != null) { | |
72 response.setContentType(contentType); | |
73 } | |
74 response.setContentLength(in.available()); | |
75 byte[] buffer = new byte[BUFFER_SIZE]; | |
76 for (int read; (read = in.read(buffer)) != -1;) { | |
77 out.write(buffer, 0, read); | |
78 } | |
79 out.flush(); | |
80 ······ | |
81 } finally { | |
82 if (in != null) { | |
83 in.close(); | |
84 } | |
85 if (out != null) { | |
86 out.close(); | |
87 } | |
88 } | |
89 } | |
90 } | |
OLD | NEW |