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 22, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.ui.servlet;
019:
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import javax.servlet.http.HttpSession;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.pentaho.messages.Messages;
029: import org.pentaho.ui.BaseUIComponent;
030: import org.pentaho.ui.servlet.HttpServletRequestHandler;
031: import org.pentaho.core.session.IPentahoSession;
032: import org.pentaho.core.solution.HttpOutputHandler;
033: import org.pentaho.core.system.PentahoSystem;
034: import org.pentaho.core.ui.SimpleUrlFactory;
035: import org.pentaho.core.util.UIUtil;
036: import org.pentaho.messages.util.LocaleHelper;
037:
038: /**
039: * @author James Dixon
040: *
041: * TODO To change the template for this generated type comment go to Window -
042: * Preferences - Java - Code Style - Code Templates
043: */
044: public class UIServlet extends ServletBase {
045:
046: /**
047: *
048: */
049: private static final long serialVersionUID = 7018489258697145705L;
050:
051: private static final Log logger = LogFactory
052: .getLog(UIServlet.class);
053:
054: public Log getLogger() {
055: return logger;
056: }
057:
058: protected void doGet(HttpServletRequest request,
059: HttpServletResponse response) throws ServletException,
060: IOException {
061:
062: OutputStream outputStream = response.getOutputStream();
063:
064: String path = request.getContextPath();
065:
066: IPentahoSession userSession = getPentahoSession(request);
067: HttpSession session = request.getSession();
068:
069: String user = request.getRemoteUser();
070:
071: if (user != null && !userSession.isAuthenticated()) {
072: // the user was not logged in before but is now....
073: userSession.setAuthenticated(user);
074: }
075:
076: String type = request.getParameter("type"); //$NON-NLS-1$
077: if (type == null) {
078: type = "text/html"; //$NON-NLS-1$
079: }
080:
081: // find out which component is going to fulfill this request
082: String componentName = request.getParameter("component"); //$NON-NLS-1$
083: if (componentName == null) {
084: response.setContentType("text/html"); //$NON-NLS-1$
085: StringBuffer buffer = new StringBuffer();
086: UIUtil
087: .formatErrorMessage(
088: "text/html", Messages.getString("UIServlet.ACTION_FAILED"), Messages.getErrorString("UIServlet.ERROR_0001_COMPONENT_NOT_SPECIFIED"), buffer); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
089: outputStream.write(buffer.toString().getBytes(
090: LocaleHelper.getSystemEncoding()));
091: return;
092:
093: }
094: response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
095: // TODO switch this to the interface once stable
096: BaseUIComponent component = (BaseUIComponent) session
097: .getAttribute(componentName);
098: if (component == null) {
099: component = getComponent(componentName);
100: if (component == null) {
101: response.setContentType("text/html"); //$NON-NLS-1$
102: StringBuffer buffer = new StringBuffer();
103: UIUtil
104: .formatErrorMessage(
105: "text/html", Messages.getString("UIServlet.ACTION_FAILED"), Messages.getErrorString("UIServlet.ERROR_0002_COMPONENT_INVALID"), buffer); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
106: outputStream.write(buffer.toString().getBytes(
107: LocaleHelper.getSystemEncoding()));
108: return;
109: }
110: session.setAttribute(componentName, component);
111: }
112:
113: if (!component.validate()) {
114: // TODO need an error here
115: return;
116: }
117: String baseUrl = request.getScheme()
118: + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/content?type=" + type + "&component=" + componentName + "&"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
119:
120: response.setContentType(type);
121: HttpOutputHandler outputHandler = new HttpOutputHandler(
122: response, outputStream, true);
123:
124: SimpleUrlFactory urlFactory = new SimpleUrlFactory(baseUrl);
125:
126: HttpServletRequestHandler requestHandler = new HttpServletRequestHandler(
127: userSession, null, request, outputHandler, urlFactory);
128:
129: requestHandler.handleUIRequest(component, type);
130: }
131:
132: private BaseUIComponent getComponent(String componentName) {
133: return (BaseUIComponent) PentahoSystem.createObject(
134: componentName, this );
135: }
136:
137: protected void doPost(HttpServletRequest request,
138: HttpServletResponse response) throws ServletException,
139: IOException {
140:
141: doGet(request, response);
142:
143: }
144:
145: }
|