KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > content > FileContentServlet


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.content;
6
7 import java.io.IOException JavaDoc;
8 import javax.servlet.ServletConfig JavaDoc;
9 import javax.servlet.ServletContext JavaDoc;
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.ServletOutputStream JavaDoc;
12 import javax.servlet.http.HttpServlet JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import org.exoplatform.commons.utils.ExceptionUtil;
16 import org.exoplatform.commons.utils.IOUtil;
17
18 /**
19  * Created by The eXo Platform SARL .
20  * Sun, Dec 28, 2003 @ 14:57
21  * @author: Tuan Nguyen
22  * @version: $Id: FileContentServlet.java,v 1.3 2004/06/23 03:19:01 tuan08 Exp $
23  * @email: tuan08@yahoo.com
24  */

25 public class FileContentServlet extends HttpServlet JavaDoc {
26   static private int LENGTH = "/content/file".length() + 1;
27   private String JavaDoc reposistory_ = null ;
28   
29   public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
30     ServletContext JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
41     ServletOutputStream JavaDoc os = response.getOutputStream() ;
42     try {
43       //long headerValue = request.getDateHeader("If-Modified-Since");
44
//System.out.println("if modified since....... " + headerValue);
45
String JavaDoc servletPath = request.getPathInfo() ;
46       String JavaDoc path = reposistory_ + servletPath ;
47       if(path == null) {
48         response.setContentType("text/plain");
49         os.println("path: " + path + " is not found") ;
50       } else {
51         String JavaDoc mimeType = MimeTypeManager.getInstance().getMimeTypeByOfFile(path).getMimeType() ;
52         //System.out.println("mime type = " + mimeType);
53
response.setContentType(mimeType);
54         byte[] buf = IOUtil.getFileContentAsBytes(path) ;
55         //System.out.println("buf length = " + buf.length);
56
//response.setHeader("Cache-Control", "public, private max-age=600, s-maxage=120");
57
//System.out.println("write bin content");
58
os.write(buf) ;
59       }
60     } catch (Exception JavaDoc ex) {
61       String JavaDoc s = ExceptionUtil.getStackTrace(ex, 20) ;
62       os.println(s) ;
63     }
64   }
65 }
66
Popular Tags