1 17 18 package org.pentaho.ui.servlet; 19 20 import java.io.*; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 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 47 public class ViewAction extends ServletBase { 48 49 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 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 request, HttpServletResponse response) throws ServletException , IOException { 78 79 String apparentMime = request.getParameter("mimetype"); if (apparentMime != null) { 81 response.setContentType(apparentMime); 82 } 83 response.setCharacterEncoding(LocaleHelper.getSystemEncoding()); 84 String file = request.getParameter("mimefile"); if (file != null) { 86 response.setHeader("content-disposition", "attachment;filename=" + file); } 88 89 IPentahoSession userSession = getPentahoSession(request); 90 String baseUrl = PentahoSystem.getApplicationContext().getBaseUrl(); 91 92 PentahoSystem.systemEntryPoint(); 93 try { 94 95 if( "true".equals( request.getParameter( "background" ) ) ) { IBackgroundExecution backgroundExecutionHandler = PentahoSystem.getBackgroundExecutionHandler(userSession); 97 if (backgroundExecutionHandler != null) { 98 HttpRequestParameterProvider parameterProvider = new HttpRequestParameterProvider(request); 99 String backgroundResponse = backgroundExecutionHandler.backgroundExecuteAction(userSession, parameterProvider); 100 String intro = ""; String footer = ""; IUITemplater templater = PentahoSystem.getUITemplater( userSession ); 103 if( templater != null ) { 104 String sections[] = templater.breakTemplate( "template-dialog.html", "", userSession ); 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" ); } 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")); } 122 } 123 124 String prompt = request.getParameter("prompt"); String solutionName = request.getParameter("solution"); String actionPath = request.getParameter("path"); String actionName = request.getParameter("action"); String processId = this.getClass().getName(); 129 String instanceId = request.getParameter("instance-id"); boolean doMessages = "true".equalsIgnoreCase(request.getParameter("debug")); 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?"); 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" )); 151 IRuntimeContext runtime = null; 152 try { 153 runtime = requestHandler.handleActionRequest(0, 0); 154 155 if (doMessages) { 156 outputStream = response.getOutputStream(); 157 } 158 159 if (!outputHandler.contentDone() || doMessages) { 161 response.setContentType("text/html"); 163 if ((runtime != null) && (runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) { 164 if( "false".equals( request.getParameter( "wrapper" ) ) ) { StringBuffer buffer = new StringBuffer (); 166 UIUtil.formatSuccessMessage("text/html", runtime, buffer, doMessages, false); outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding())); 168 } else { 169 StringBuffer buffer = new StringBuffer (); 170 UIUtil.formatSuccessMessage("text/html", runtime, buffer, doMessages, true); outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding())); 172 } 173 } else { 174 StringBuffer buffer = new StringBuffer (); 176 UIUtil.formatFailureMessage("text/html", runtime, buffer, requestHandler.getMessages()); 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 request, HttpServletResponse response) throws ServletException , IOException { 193 194 doGet(request, response); 195 196 } 197 198 } | Popular Tags |