1 5 package org.exoplatform.portlets.content; 6 7 import java.io.IOException ; 8 import javax.servlet.ServletConfig ; 9 import javax.servlet.ServletContext ; 10 import javax.servlet.ServletException ; 11 import javax.servlet.ServletOutputStream ; 12 import javax.servlet.http.HttpServlet ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import org.exoplatform.commons.utils.ExceptionUtil; 16 import org.exoplatform.commons.utils.IOUtil; 17 18 25 public class FileContentServlet extends HttpServlet { 26 static private int LENGTH = "/content/file".length() + 1; 27 private String reposistory_ = null ; 28 29 public void init(ServletConfig config) throws ServletException { 30 ServletContext context = config.getServletContext() ; 31 reposistory_ = context.getInitParameter("file.portlet.reposistory") ; 32 if(reposistory_ == null || reposistory_.length() ==0 || reposistory_.equals("default")) { 33 reposistory_= context.getRealPath("/") ; 34 } 35 if(reposistory_.endsWith("/")) { 36 reposistory_ = reposistory_.substring(0, reposistory_.length() - 1) ; 37 } 38 } 39 40 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 41 ServletOutputStream os = response.getOutputStream() ; 42 try { 43 String servletPath = request.getPathInfo() ; 46 String path = reposistory_ + servletPath ; 47 if(path == null) { 48 response.setContentType("text/plain"); 49 os.println("path: " + path + " is not found") ; 50 } else { 51 String mimeType = MimeTypeManager.getInstance().getMimeTypeByOfFile(path).getMimeType() ; 52 response.setContentType(mimeType); 54 byte[] buf = IOUtil.getFileContentAsBytes(path) ; 55 os.write(buf) ; 59 } 60 } catch (Exception ex) { 61 String s = ExceptionUtil.getStackTrace(ex, 20) ; 62 os.println(s) ; 63 } 64 } 65 } 66 | Popular Tags |