KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > servlet > GetContent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created March 7, 2005
14  * @author Doug Moran
15  *
16  */

17
18 package org.pentaho.ui.servlet;
19
20
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletOutputStream JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import org.jfree.io.IOUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.pentaho.core.repository.IContentItem;
33 import org.pentaho.core.repository.IContentRepository;
34 import org.pentaho.core.session.IPentahoSession;
35 import org.pentaho.core.system.PentahoSystem;
36 import org.pentaho.messages.Messages;
37 import org.pentaho.messages.util.LocaleHelper;
38
39 public class GetContent extends ServletBase {
40
41     /**
42      *
43      */

44     private static final long serialVersionUID = 9019152264205996418L;
45
46     private static final Log logger = LogFactory.getLog(GetContent.class);
47
48     public Log getLogger() {
49         return logger;
50     }
51
52     public GetContent() {
53         super();
54     }
55
56     protected void setupSession(IPentahoSession userSession) {
57         PentahoSystem.sessionStartup(userSession);
58     }
59
60     protected void removeUserSession(IPentahoSession userSession) {
61         userSession.destroy();
62     }
63
64     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
65         doGet(request, response);
66     }
67
68     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
69
70         response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
71
72         PentahoSystem.systemEntryPoint();
73         try {
74             IPentahoSession userSession = getPentahoSession(request);
75
76             String JavaDoc id = request.getParameter("id"); //$NON-NLS-1$
77
if (id == null) {
78                 returnError(response, Messages.getErrorString("GetContent.ERROR_0001_ID_PARAMETER_EMPTY")); //$NON-NLS-1$
79
return;
80             }
81
82             IContentRepository contentRepos = PentahoSystem.getContentRepository(userSession);
83             if (contentRepos == null) {
84                 returnError(response, Messages.getString("GetContent.ERROR_0002_CONTENT_REPOS_UNAVAILABLE")); //$NON-NLS-1$
85
return;
86             }
87
88             try {
89                 IContentItem contentItem = contentRepos.getContentItemById(id);
90                 if (contentItem == null) {
91                     returnError(response, Messages.getString("GetContent.ERROR_0005_CONTENT_NOT_FOUND", id)); //$NON-NLS-1$
92
return;
93                 }
94
95                 String JavaDoc mimetype = contentItem.getMimeType();
96                 if ((mimetype == null) || (mimetype.length() < 1)) {
97                     mimetype = request.getParameter("mimetype"); //$NON-NLS-1$
98
}
99
100                 // Set it if we know what it is
101
if ((mimetype != null) && (mimetype.length() > 0)) {
102                     response.setContentType(mimetype);
103                 }
104
105                 // Send it back
106
InputStream JavaDoc inStr = contentItem.getInputStream();
107                 ServletOutputStream JavaDoc outStr = response.getOutputStream();
108
109
110               try {
111                 IOUtils.getInstance().copyStreams(inStr, outStr);
112               } finally {
113                 inStr.close();
114                 outStr.close();
115               }
116             } catch (Throwable JavaDoc t) {
117                 error(Messages.getString("GetContent.ERROR_0003_CONTENT_READ_ERROR")); //$NON-NLS-1$
118
}
119
120         } finally {
121             PentahoSystem.systemExitPoint();
122         }
123     }
124
125     void returnError(HttpServletResponse JavaDoc response, String JavaDoc message) {
126         error(message);
127
128         response.setContentType("text/plain"); //$NON-NLS-1$
129
try {
130             response.getWriter().println(Messages.getString("GetContent.ERROR_0004_RETURN_MESSAGE") + message); //$NON-NLS-1$
131
} catch (Throwable JavaDoc t) {
132         }
133     }
134
135 }
136
Popular Tags