KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > control > ContentRepositoryFileView


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.control;
22
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.springframework.web.servlet.view.AbstractView;
31
32 import com.jaspersoft.jasperserver.api.JSException;
33 import com.jaspersoft.jasperserver.api.metadata.common.domain.ContentResource;
34 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
35 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResourceData;
36
37 public class ContentRepositoryFileView extends AbstractView
38 {
39     public static final String JavaDoc REPOSITORY_PATH = "repositoryPath";
40
41     RepositoryService repository;
42
43     public ContentRepositoryFileView(RepositoryService repository)
44     {
45         this.repository = repository;
46     }
47
48
49     protected void renderMergedOutputModel(Map JavaDoc map, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc
50     {
51         String JavaDoc pathinfo = request.getPathInfo();
52         int start = pathinfo.indexOf('/', 1);
53         String JavaDoc repoPath = pathinfo.substring(start, pathinfo.length());
54
55         if (repoPath == null || repoPath.length() == 0)
56             return;
57
58         OutputStream JavaDoc out = response.getOutputStream();
59
60         ContentResource file = (ContentResource) repository.getResource(null, repoPath);
61         String JavaDoc fileType = file.getFileType();
62
63         if (fileType == null)
64             throw new JSException("Undefined file type");
65
66         FileResourceData fileData = repository.getContentResourceData(null, repoPath);
67
68         if (fileType.equals(ContentResource.TYPE_PDF)) {
69             response.setContentType("application/pdf");
70         }
71         else if (fileType.equals(ContentResource.TYPE_XLS)) {
72             response.setContentType("application/xls");
73             response.setHeader("Content-Disposition", "inline; filename=\"file.xls\"");
74         } else if (fileType.equals(ContentResource.TYPE_RTF)) {
75             response.setContentType("application/rtf");
76         }
77
78         response.setContentLength(fileData.getData().length);
79
80         InputStream JavaDoc in = fileData.getDataStream();
81
82         byte[] b = new byte[1024];
83         while (in.read(b) != -1) {
84             out.write(b);
85         }
86
87         out.flush();
88
89     }
90 }
91
Popular Tags