View Javadoc

1   
2   package de.iqser.contentprovider.servlet;
3   
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.io.OutputStream;
8   import java.io.PrintWriter;
9   import java.text.SimpleDateFormat;
10  import java.util.Date;
11  
12  import javax.servlet.ServletConfig;
13  import javax.servlet.ServletException;
14  import javax.servlet.http.HttpServlet;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  public class DmsServlet extends HttpServlet {
19  	static final long serialVersionUID= 1;		// to avoid warning
20  	private String myName= "de.iqser.contentprovider.servlet.DmsServlet";
21  	
22  	private static final String PARAM_COMPANY_ID= "companyId";
23  	private static final String PARAM_GROUP_ID= "groupId";
24  	private static final String PARAM_FILE_NAME= "fileName";
25  	private static final String PARAM_VERSION= "version";
26  	private static final String PARAM_EXTENSION= "extension";
27  	
28  	private static final String PARAM_LIBRARY_PATH= "librarypath";
29  	
30  	private static String libraryPath= null;
31  	public void init(ServletConfig config) {
32  		try {
33  			super.init(config);
34  			libraryPath= config.getInitParameter(PARAM_LIBRARY_PATH);
35  			System.out.println(myName + " DmsServlet servlet initialized");
36  			System.out.println(myName + " path to Liferay document libraty is: " + libraryPath);
37  		}
38  		catch (Exception e) {
39  			System.out.println(myName + " DmsServlet servlet configure failed " + e);
40  			System.err.println(myName + " DmsServlet servlet configure failed " + e);
41  		}
42  	}
43  	
44  	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
45  
46  		// call with http://localhost:8080/<webname>DmsServlet/
47  		// in the file system, the structure is /companyId/groupId/file/name/version
48  		String companyIdStr= request.getParameter(PARAM_COMPANY_ID);
49  		String groupId= request.getParameter(PARAM_GROUP_ID);
50  		String fileName= request.getParameter(PARAM_FILE_NAME);
51  		String version= request.getParameter(PARAM_VERSION);
52  		String fileExtension= request.getParameter(PARAM_EXTENSION);
53  		
54  		System.out.println("call getDocumentInputStream");
55  		InputStream is= getDocumentInputStream(libraryPath, companyIdStr, groupId, fileName, version);
56  		
57  		if( is == null) {
58  			// something went wrong
59  			PrintWriter out= response.getWriter();
60  			response.setContentType("text/html");
61  			
62  			Date dNow= new Date();
63  			String servletVersion= "1.10.00";
64  		
65  			SimpleDateFormat formatter= new SimpleDateFormat("dd.MMM yyyy kk:mm:ss");
66  			String now= formatter.format(dNow);
67  		
68  			out.println("<html>");
69  			out.println("<head>");
70  			out.println("<title>Dms Servlet</title>");
71  			out.println("</head>");
72  			out.println("<body>");
73  		
74  			out.println("<H3 style='margin-bottom: 3px'>Dms Servlet Version " + servletVersion + "</H3><br>");
75  			out.println("");
76  			out.println("Datum: " + now + "<br>");
77  			out.println("");
78  			out.println("<b>Failed to get InputStream</b>");
79  			out.println("");
80  			out.println("</body>");
81  			out.println("</html>");
82  		}
83  		else {
84  			// we got a stream
85  			String mimeType= "text/plain; charset=ISO-8859-1";		// default
86  			if( fileExtension.equalsIgnoreCase("pdf")) {
87  				mimeType= "application/pdf";
88  			}
89  			else if( fileExtension.equalsIgnoreCase("doc")) {
90  				mimeType= "application/msword";
91  			}
92  			
93  			response.setContentType(mimeType);
94  			response.setHeader("Content-Disposition", "inline; fileName=" + fileName);
95  			
96  			OutputStream os= response.getOutputStream();
97  			
98  			int fileSize= is.available();
99  			System.out.println("DmsServlet sends " + fileSize + " bytes for file " + fileName);
100 			byte[] byteArray= new byte[fileSize];
101 			is.read(byteArray);		// read up to byteArray.length bytes
102 			is.close();
103 			
104 			os.write(byteArray);
105 			os.close();
106 
107 		}
108 	}
109 	
110 	private InputStream getDocumentInputStream(String libraryPath, String companyId, String groupId, String fileName, String version) {
111 		InputStream is = null;
112 		
113 		try {
114 			// method with liferay hook does not work in Liferay 6 ==> get stream from file system
115 			StringBuffer buf= new StringBuffer(libraryPath);
116 			buf.append(companyId);
117 			buf.append('/');
118 			buf.append(groupId);
119 			buf.append('/');
120 			buf.append(fileName);
121 			buf.append('/');
122 			buf.append(version);
123 			String fullFileName= buf.toString();
124 			System.out.println("Get stream for file :" + fullFileName);
125 			is= new FileInputStream(fullFileName);
126 		}
127 		catch (Exception e) {
128 		}
129 		if( is == null) {
130 			System.out.println("DmsServlet failed to get InputStream");
131 			return null;
132 		}
133 		return is;
134 	}
135 	
136 }