001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jul 12, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.ui.servlet;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.util.List;
024:
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.pentaho.core.repository.IContentItem;
031: import org.pentaho.core.repository.ISolutionRepository;
032: import org.pentaho.core.runtime.IBackgroundExecution;
033: import org.pentaho.core.runtime.IRuntimeContext;
034: import org.pentaho.ui.servlet.HttpServletRequestHandler;
035: import org.pentaho.core.session.IPentahoSession;
036: import org.pentaho.core.solution.HttpOutputHandler;
037: import org.pentaho.core.solution.HttpRequestParameterProvider;
038: import org.pentaho.core.solution.HttpSessionParameterProvider;
039: import org.pentaho.core.solution.IActionDefinition;
040: import org.pentaho.core.solution.IOutputHandler;
041: import org.pentaho.core.solution.IParameterProvider;
042: import org.pentaho.core.subscribe.SubscriptionHelper;
043: import org.pentaho.core.solution.IActionSequence;
044: import org.pentaho.core.system.ISystemSettings;
045: import org.pentaho.core.system.PentahoSystem;
046: import org.pentaho.core.ui.SimpleUrlFactory;
047: import org.pentaho.core.util.IUITemplater;
048: import org.pentaho.core.util.UIUtil;
049: import org.pentaho.messages.Messages;
050: import org.pentaho.messages.util.LocaleHelper;
051:
052: /**
053: * Servlet Class
054: *
055: * web.servlet name="ViewAction" display-name="Name for ViewAction" description="Description for ViewAction" web.servlet-mapping url-pattern="/ViewAction"
056: * web.servlet-init-param name="A parameter" value="A value"
057: */
058: public class ViewAction extends ServletBase {
059:
060: private static final long serialVersionUID = 4680027723733552639L;
061:
062: private static final Log logger = LogFactory
063: .getLog(ViewAction.class);
064:
065: public Log getLogger() {
066: return logger;
067: }
068:
069: /**
070: *
071: */
072: public ViewAction() {
073: super ();
074: }
075:
076: protected void setupSession(IPentahoSession userSession) {
077:
078: PentahoSystem.sessionStartup(userSession);
079:
080: }
081:
082: protected void removeUserSession(IPentahoSession userSession) {
083: userSession.destroy();
084: }
085:
086: protected boolean doBackgroundExecution(HttpServletRequest request,
087: HttpServletResponse response, IPentahoSession userSession)
088: throws ServletException, IOException {
089: if ("true".equals(request.getParameter("background"))) { //$NON-NLS-1$ //$NON-NLS-2$
090: IBackgroundExecution backgroundExecutionHandler = PentahoSystem
091: .getBackgroundExecutionHandler(userSession);
092: if (backgroundExecutionHandler != null) {
093: HttpRequestParameterProvider parameterProvider = new HttpRequestParameterProvider(
094: request);
095: String backgroundResponse = backgroundExecutionHandler
096: .backgroundExecuteAction(userSession,
097: parameterProvider);
098: String intro = ""; //$NON-NLS-1$
099: String footer = ""; //$NON-NLS-1$
100: IUITemplater templater = PentahoSystem
101: .getUITemplater(userSession);
102: if (templater != null) {
103: String sections[] = templater.breakTemplate(
104: "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
113: .getString("UI.ERROR_0002_BAD_TEMPLATE_OBJECT"); //$NON-NLS-1$
114: }
115:
116: response.getWriter().print(intro);
117: response.getWriter().print(backgroundResponse);
118: response.getWriter().print(footer);
119: return true;
120: } else {
121: error(Messages
122: .getErrorString("BackgroundExecuteHelper.ERROR_0001_BACKGROUND_EXECUTE_NOT_SUPPORTED")); //$NON-NLS-1$
123: }
124: }
125: return false;
126: }
127:
128: protected OutputStream getOutputStream(
129: HttpServletResponse response, boolean doMessages)
130: throws ServletException, IOException {
131: OutputStream outputStream = null;
132: if (doMessages) {
133: outputStream = new ByteArrayOutputStream();
134: } else {
135: outputStream = response.getOutputStream();
136: }
137:
138: return outputStream;
139: }
140:
141: protected boolean doMessages(HttpServletRequest request) {
142: return "true".equalsIgnoreCase(request.getParameter("debug")); //$NON-NLS-1$ //$NON-NLS-2$
143: }
144:
145: protected void handleActionRequest(HttpServletRequest request,
146: HttpServletResponse response,
147: HttpOutputHandler outputHandler,
148: HttpServletRequestHandler requestHandler,
149: OutputStream outputStream, IContentItem contentItem)
150: throws ServletException, IOException {
151: IRuntimeContext runtime = null;
152: try {
153: runtime = requestHandler.handleActionRequest(0, 0);
154:
155: // see if we need to provide feedback to the caller
156: if (!outputHandler.contentDone() || doMessages(request)) {
157: outputHandler.getOutputContent().setMimeType(
158: "text/html");//$NON-NLS-1$
159: outputStream = outputHandler.getOutputContent()
160: .getOutputStream(null);
161:
162: response.setContentType("text/html"); //$NON-NLS-1$
163:
164: StringBuffer buffer = new StringBuffer();
165: if ((runtime != null)
166: && (runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
167: boolean doWrapper = !("false".equals(request.getParameter("wrapper"))); //$NON-NLS-1$ //$NON-NLS-2$
168: UIUtil
169: .formatSuccessMessage(
170: "text/html", runtime, buffer, doMessages(request), doWrapper); //$NON-NLS-1$
171: } else {
172: // we need an error message...
173: UIUtil
174: .formatFailureMessage(
175: "text/html", runtime, buffer, requestHandler.getMessages()); //$NON-NLS-1$
176: }
177: outputStream.write(buffer.toString().getBytes(
178: LocaleHelper.getSystemEncoding()));
179: outputHandler.getOutputContent().closeOutputStream();
180: }
181: } finally {
182: if (runtime != null) {
183: runtime.dispose();
184: }
185: }
186: if (contentItem != null) {
187: contentItem.closeOutputStream();
188: }
189: }
190:
191: protected void setupRequestHandler(HttpServletRequest request,
192: HttpServletRequestHandler requestHandler) {
193: String prompt = request.getParameter("prompt"); //$NON-NLS-1$
194: String solutionName = request.getParameter("solution"); //$NON-NLS-1$
195: String actionPath = request.getParameter("path"); //$NON-NLS-1$
196: String actionName = request.getParameter("action"); //$NON-NLS-1$
197: String processId = this .getClass().getName();
198: String instanceId = request.getParameter("instance-id"); //$NON-NLS-1$
199: requestHandler.setInstanceId(instanceId);
200: requestHandler.setProcessId(processId);
201: requestHandler.setAction(actionPath, actionName);
202: requestHandler.setSolutionName(solutionName);
203: requestHandler.setForcePrompt(prompt != null
204: && prompt.equalsIgnoreCase("yes")); //$NON-NLS-1$
205: }
206:
207: protected void setupOutputHandler(HttpOutputHandler outputHandler,
208: IParameterProvider requestParameters) {
209: int outputPreference = IOutputHandler.OUTPUT_TYPE_DEFAULT;
210: if (doSubscribe(requestParameters)) {
211: outputPreference = IOutputHandler.OUTPUT_TYPE_PARAMETERS;
212: }
213: outputHandler.setOutputPreference(outputPreference);
214: }
215:
216: protected HttpServletRequestHandler getRequestHandler(
217: HttpServletRequest request, HttpServletResponse response,
218: IPentahoSession userSession,
219: IParameterProvider requestParameters,
220: OutputStream outputStream, HttpOutputHandler outputHandler,
221: SimpleUrlFactory urlFactory) throws ServletException,
222: IOException {
223: HttpServletRequestHandler requestHandler = new HttpServletRequestHandler(
224: userSession, null, request, outputHandler, urlFactory);
225: setupRequestHandler(request, requestHandler, requestParameters,
226: userSession);
227: return requestHandler;
228: }
229:
230: protected HttpOutputHandler createOutputHandler(
231: HttpServletResponse response, OutputStream outputStream) {
232: return new HttpOutputHandler(response, outputStream, true);
233: }
234:
235: protected void doGet(HttpServletRequest request,
236: HttpServletResponse response) throws ServletException,
237: IOException {
238: PentahoSystem.systemEntryPoint();
239: IContentItem contentItem = null;
240: try {
241: IPentahoSession userSession = getPentahoSession(request);
242: if (!doBackgroundExecution(request, response, userSession)) {
243: OutputStream outputStream = getOutputStream(response,
244: doMessages(request));
245: ISolutionRepository repository = PentahoSystem
246: .getSolutionRepository(userSession);
247: String solutionName = request.getParameter("solution"); //$NON-NLS-1$
248: String actionPath = request.getParameter("path"); //$NON-NLS-1$
249: String actionName = request.getParameter("action"); //$NON-NLS-1$
250: IActionSequence actionSequence = repository
251: .getActionSequence(solutionName, actionPath,
252: actionName, PentahoSystem.loggingLevel,
253: ISolutionRepository.ACTION_EXECUTE);
254: String fileName = null;
255: if (actionSequence != null) {
256: String title = actionSequence.getTitle();
257: if (title != null && title.length() > 0) {
258: fileName = title;
259: } else {
260: String sequenceName = actionSequence
261: .getSequenceName();
262:
263: if (sequenceName != null
264: && sequenceName.length() > 0) {
265: fileName = sequenceName;
266: } else {
267: List actionDefinitionsList = actionSequence
268: .getActionDefinitionsAndSequences();
269: int i = 0;
270: boolean done = false;
271:
272: while (actionDefinitionsList.size() > i
273: && !done) {
274: IActionDefinition actionDefinition = (IActionDefinition) actionDefinitionsList
275: .get(i);
276: String componentName = actionDefinition
277: .getComponentName();
278: if (componentName != null
279: && componentName.length() > 0) {
280: fileName = componentName;
281: done = true;
282: } else {
283: i++;
284: }
285: }
286: }
287: }
288: }
289:
290: HttpOutputHandler outputHandler = createOutputHandler(
291: response, outputStream);
292: outputHandler.setSession(userSession);
293: if (contentItem != null && fileName != null) {
294: outputHandler.setOutputContent(contentItem);
295: }
296:
297: HttpMimeTypeListener listener = new HttpMimeTypeListener(
298: request, response, fileName);
299: outputHandler.setMimeTypeListener(listener);
300: SimpleUrlFactory urlFactory = new SimpleUrlFactory(
301: PentahoSystem.getApplicationContext()
302: .getBaseUrl()
303: + "ViewAction?"); //$NON-NLS-1$
304: IParameterProvider requestParameters = new HttpRequestParameterProvider(
305: request);
306: setupOutputHandler(outputHandler, requestParameters);
307: if (!handleSubscriptions(request, response,
308: userSession, requestParameters, outputStream,
309: urlFactory)) {
310: HttpServletRequestHandler requestHandler = getRequestHandler(
311: request, response, userSession,
312: requestParameters, outputStream,
313: outputHandler, urlFactory);
314: handleActionRequest(request, response,
315: outputHandler, requestHandler,
316: outputStream, contentItem);
317: }
318: }
319: } finally {
320: PentahoSystem.systemExitPoint();
321: }
322: }
323:
324: protected void doPost(HttpServletRequest request,
325: HttpServletResponse response) throws ServletException,
326: IOException {
327:
328: doGet(request, response);
329: }
330:
331: ///////////////////// Merge
332: protected boolean doSubscribe(IParameterProvider requestParameters) {
333: return requestParameters.getStringParameter(
334: "subscribepage", "no").equalsIgnoreCase("yes"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
335: }
336:
337: protected boolean handleSubscriptions(HttpServletRequest request,
338: HttpServletResponse response, IPentahoSession userSession,
339: IParameterProvider requestParameters,
340: OutputStream outputStream, SimpleUrlFactory urlFactory)
341: throws ServletException, IOException {
342: // see if we have any subscription information to process
343: String subscribeAction = request.getParameter("subscribe"); //$NON-NLS-1$
344:
345: if ("run".equals(subscribeAction)) { //$NON-NLS-1$
346: String name = requestParameters.getStringParameter(
347: "subscribe-name", null); //$NON-NLS-1$
348: HttpSessionParameterProvider sessionParameters = new HttpSessionParameterProvider(
349: userSession);
350: HttpOutputHandler outputHandler = new HttpOutputHandler(
351: response, outputStream, true);
352: SubscriptionHelper.runSubscription(name, userSession,
353: sessionParameters, urlFactory, outputHandler);
354: return true;
355: } else if ("archived".equals(subscribeAction)) { //$NON-NLS-1$
356: String name = requestParameters.getStringParameter(
357: "subscribe-name", null); //$NON-NLS-1$
358: int pos = name.lastIndexOf(':');
359: if (pos != -1) {
360: String fileId = name.substring(pos + 1);
361: name = name.substring(0, pos);
362: HttpOutputHandler outputHandler = new HttpOutputHandler(
363: response, outputStream, true);
364: SubscriptionHelper.getArchived(name, fileId,
365: userSession, outputHandler);
366: }
367: return true;
368: } else if ("archive".equals(subscribeAction)) { //$NON-NLS-1$
369: String name = requestParameters.getStringParameter(
370: "subscribe-name", null); //$NON-NLS-1$
371: HttpSessionParameterProvider sessionParameters = new HttpSessionParameterProvider(
372: userSession);
373: String resp = SubscriptionHelper.createSubscriptionArchive(
374: name, userSession, null, sessionParameters);
375: HttpOutputHandler outputHandler = new HttpOutputHandler(
376: response, outputStream, true);
377: IContentItem contentItem = outputHandler
378: .getOutputContentItem(IOutputHandler.RESPONSE,
379: IOutputHandler.CONTENT, null, null,
380: "text/html"); //$NON-NLS-1$
381: outputStream = contentItem.getOutputStream(name);
382: outputStream.write(resp.getBytes());
383: contentItem.closeOutputStream();
384: return true;
385: } else if ("save".equals(subscribeAction)) { //$NON-NLS-1$
386: String solutionName = requestParameters.getStringParameter(
387: "solution", null); //$NON-NLS-1$
388: String actionPath = requestParameters.getStringParameter(
389: "path", null); //$NON-NLS-1$
390: String actionName = requestParameters.getStringParameter(
391: "action", null); //$NON-NLS-1$
392: String actionReference = solutionName
393: + "/" + actionPath + "/" + actionName; //$NON-NLS-1$ //$NON-NLS-2$
394: // HttpSessionParameterProvider sessionParameters = new
395: // HttpSessionParameterProvider( userSession );
396: String result = SubscriptionHelper.saveSubscription(
397: requestParameters, actionReference, userSession);
398: outputStream.write(result.getBytes());
399: return true;
400: } else if ("edit".equals(subscribeAction)) { //$NON-NLS-1$
401: // TODO
402: // get the action information from the subscription
403: String name = requestParameters.getStringParameter(
404: "subscribe-name", null); //$NON-NLS-1$
405: SubscriptionHelper.editSubscription(name, userSession,
406: urlFactory, outputStream);
407: /*
408: *
409: * SimpleParameterSetter parameters = new SimpleParameterSetter(); String result = SubscriptionHelper.getSubscriptionParameters( name, parameters,
410: * userSession ); outputPreference = IOutputHandler.OUTPUT_TYPE_PARAMETERS; requestParameters = parameters; SubscriptionHelper.editSubscription if( result !=
411: * null ) { outputStream.write(result.getBytes()); return; }
412: */
413: return true;
414: } else if ("delete".equals(subscribeAction)) { //$NON-NLS-1$
415: String name = requestParameters.getStringParameter(
416: "subscribe-name", null); //$NON-NLS-1$
417: String result = SubscriptionHelper.deleteSubscription(name,
418: userSession);
419: outputStream.write(result.getBytes());
420: return true;
421: } else if ("delete-archived".equals(subscribeAction)) { //$NON-NLS-1$
422: String name = requestParameters.getStringParameter(
423: "subscribe-name", null); //$NON-NLS-1$
424: int pos = name.lastIndexOf(':');
425: if (pos != -1) {
426: String fileId = name.substring(pos + 1);
427: name = name.substring(0, pos);
428: String result = SubscriptionHelper
429: .deleteSubscriptionArchive(name, fileId,
430: userSession);
431: outputStream.write(result.getBytes());
432: return true;
433: }
434: }
435: return false;
436: }
437:
438: protected void setupRequestHandler(HttpServletRequest request,
439: HttpServletRequestHandler requestHandler,
440: IParameterProvider requestParameters,
441: IPentahoSession userSession) {
442: this .setupRequestHandler(request, requestHandler);
443: // first take a secondary action
444: String actionName = requestParameters.getStringParameter(
445: "action2", null); //$NON-NLS-1$
446: if (actionName == null) {
447: // now look for a primary action
448: actionName = requestParameters.getStringParameter(
449: "action", null); //$NON-NLS-1$
450: }
451: /*
452: // TODO: DM test code
453: if (ISolutionEngine.RUNTIME_SOLUTION_NAME.equals(requestHandler.getSolutionName()) && "preview.xaction".equalsIgnoreCase(actionName)) {
454: requestHandler.setActionPath(getActionSequence(userSession));
455: }
456: */
457:
458: //Proposed fix for bug BISERVER-97 by Ezequiel Cuellar
459: //Changed to set parameterXsl from the value specified specified in the Pentaho.xml tag "default-parameter-xsl"
460: //Proposed fix for bug BISERVER-238 by Ezequiel Cuellar
461: //Code refactoring. DefaultParameterForm.xsl was always getting set so I just refactored the code
462: //by adding a default value of DefaultParameterForm.xsl when getting the value of default-parameter-xsl
463: ISystemSettings systemSettings = PentahoSystem
464: .getSystemSettings();
465: String defaultParameterXsl = systemSettings.getSystemSetting(
466: "default-parameter-xsl", "DefaultParameterForm.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
467: requestHandler.setParameterXsl(defaultParameterXsl);
468: if (doSubscribe(requestParameters)) {
469: requestHandler.setForcePrompt(true);
470: requestHandler.setParameterProvider(
471: "PRO_EDIT_SUBSCRIPTION", requestParameters); //$NON-NLS-1$
472: }
473: }
474:
475: /*
476: // TODO: Test Code Only!!! DM - returns an actionsequence as a string
477: private String getActionSequence(IPentahoSession session) {
478: try {
479: return (PentahoSystem.getSolutionRepository(session).getResourceAsString("test/metadata/EmbeddedReport.xaction")); //$NON-NLS-1$
480: } catch (Throwable t) {
481: }
482: return (""); //$NON-NLS-1$
483: // File f = new File( "test/metadata/EmbeddedReport.xaction" );
484: // return( FileHelper.getStringFromFile(f) );
485: }
486: */
487:
488: }
|