KickJava   Java API By Example, From Geeks To Geeks.

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


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 Jul 12, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.ui.servlet;
19
20 import java.io.*;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.pentaho.core.runtime.IRuntimeContext;
27 import org.pentaho.ui.servlet.HttpServletRequestHandler;
28 import org.pentaho.core.session.IPentahoSession;
29 import org.pentaho.core.solution.HttpOutputHandler;
30 import org.pentaho.core.solution.HttpRequestParameterProvider;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.core.ui.SimpleUrlFactory;
33 import org.pentaho.core.util.IUITemplater;
34 import org.pentaho.core.util.UIUtil;
35 import org.pentaho.messages.Messages;
36 import org.pentaho.messages.util.LocaleHelper;
37 import org.pentaho.core.runtime.IBackgroundExecution;
38
39 /**
40  * Servlet Class
41  *
42  * web.servlet name="ViewAction" display-name="Name for ViewAction"
43  * description="Description for ViewAction" web.servlet-mapping
44  * url-pattern="/ViewAction" web.servlet-init-param name="A parameter" value="A
45  * value"
46  */

47 public class ViewAction extends ServletBase {
48
49     /**
50      *
51      */

52     private static final long serialVersionUID = 4680027723733552639L;
53
54     private static final Log logger = LogFactory.getLog(ViewAction.class);
55
56     public Log getLogger() {
57         return logger;
58     }
59
60     /**
61      *
62      */

63     public ViewAction() {
64         super();
65     }
66
67     protected void setupSession(IPentahoSession userSession) {
68
69         PentahoSystem.sessionStartup(userSession);
70
71     }
72
73     protected void removeUserSession(IPentahoSession userSession) {
74         userSession.destroy();
75     }
76
77     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException {
78
79         String JavaDoc apparentMime = request.getParameter("mimetype"); //$NON-NLS-1$
80
if (apparentMime != null) {
81             response.setContentType(apparentMime);
82         }
83         response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
84         String JavaDoc file = request.getParameter("mimefile"); //$NON-NLS-1$
85
if (file != null) {
86             response.setHeader("content-disposition", "attachment;filename=" + file); //$NON-NLS-1$ //$NON-NLS-2$
87
}
88
89         IPentahoSession userSession = getPentahoSession(request);
90         String JavaDoc baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();
91
92         PentahoSystem.systemEntryPoint();
93         try {
94             
95             if( "true".equals( request.getParameter( "background" ) ) ) { //$NON-NLS-1$ //$NON-NLS-2$
96
IBackgroundExecution backgroundExecutionHandler = PentahoSystem.getBackgroundExecutionHandler(userSession);
97               if (backgroundExecutionHandler != null) {
98                 HttpRequestParameterProvider parameterProvider = new HttpRequestParameterProvider(request);
99                 String JavaDoc backgroundResponse = backgroundExecutionHandler.backgroundExecuteAction(userSession, parameterProvider);
100                     String JavaDoc intro = ""; //$NON-NLS-1$
101
String JavaDoc footer = ""; //$NON-NLS-1$
102
IUITemplater templater = PentahoSystem.getUITemplater( userSession );
103                     if( templater != null ) {
104                         String JavaDoc sections[] = templater.breakTemplate( "template-dialog.html", "", userSession ); //$NON-NLS-1$ //$NON-NLS-2$
105
if( sections != null && sections.length > 0 ) {
106                             intro = sections[0];
107                         }
108                         if( sections != null && sections.length > 1 ) {
109                             footer = sections[1];
110                         }
111                     } else {
112                         intro = Messages.getString( "UI.ERROR_0002_BAD_TEMPLATE_OBJECT" ); //$NON-NLS-1$
113
}
114
115                     response.getWriter().print(intro);
116                     response.getWriter().print(backgroundResponse);
117                     response.getWriter().print(footer);
118                     return;
119               } else {
120                 error(Messages.getErrorString("BackgroundExecuteHelper.ERROR_0001_BACKGROUND_EXECUTE_NOT_SUPPORTED")); //$NON-NLS-1$
121
}
122             }
123
124             String JavaDoc prompt = request.getParameter("prompt"); //$NON-NLS-1$
125
String JavaDoc solutionName = request.getParameter("solution"); //$NON-NLS-1$
126
String JavaDoc actionPath = request.getParameter("path"); //$NON-NLS-1$
127
String JavaDoc actionName = request.getParameter("action"); //$NON-NLS-1$
128
String JavaDoc processId = this.getClass().getName();
129             String JavaDoc instanceId = request.getParameter("instance-id"); //$NON-NLS-1$
130
boolean doMessages = "true".equalsIgnoreCase(request.getParameter("debug")); //$NON-NLS-1$ //$NON-NLS-2$
131

132             OutputStream outputStream = null;
133             if (doMessages) {
134                 outputStream = new ByteArrayOutputStream();
135             } else {
136                 outputStream = response.getOutputStream();
137             }
138
139             HttpOutputHandler outputHandler = new HttpOutputHandler(response, outputStream, true);
140
141             SimpleUrlFactory urlFactory = new SimpleUrlFactory(baseUrl + "ViewAction?"); //$NON-NLS-1$
142

143             HttpServletRequestHandler requestHandler = new HttpServletRequestHandler(userSession, null, request, outputHandler, urlFactory);
144
145             requestHandler.setInstanceId(instanceId);
146             requestHandler.setProcessId(processId);
147             requestHandler.setAction(actionPath, actionName);
148             requestHandler.setSolutionName(solutionName);
149             requestHandler.setForcePrompt(prompt != null && prompt.equalsIgnoreCase( "yes" )); //$NON-NLS-1$
150

151             IRuntimeContext runtime = null;
152             try {
153                 runtime = requestHandler.handleActionRequest(0, 0);
154
155                 if (doMessages) {
156                     outputStream = response.getOutputStream();
157                 }
158
159                 // see if we need to provide feedback to the caller
160
if (!outputHandler.contentDone() || doMessages) {
161                     response.setContentType("text/html"); //$NON-NLS-1$
162

163                     if ((runtime != null) && (runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
164                             if( "false".equals( request.getParameter( "wrapper" ) ) ) { //$NON-NLS-1$ //$NON-NLS-2$
165
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
166                                 UIUtil.formatSuccessMessage("text/html", runtime, buffer, doMessages, false); //$NON-NLS-1$
167
outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
168                             } else {
169                                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
170                                 UIUtil.formatSuccessMessage("text/html", runtime, buffer, doMessages, true); //$NON-NLS-1$
171
outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
172                             }
173                     } else {
174                         // we need an error message...
175
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
176                         UIUtil.formatFailureMessage("text/html", runtime, buffer, requestHandler.getMessages()); //$NON-NLS-1$
177
outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
178
179                     }
180
181                 }
182             } finally {
183                 if (runtime != null) {
184                     runtime.dispose();
185                 }
186             }
187         } finally {
188             PentahoSystem.systemExitPoint();
189         }
190     }
191
192     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException {
193
194         doGet(request, response);
195
196     }
197
198 }
Popular Tags