KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 package org.pentaho.ui.servlet;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.pentaho.messages.Messages;
29 import org.pentaho.ui.BaseUIComponent;
30 import org.pentaho.ui.servlet.HttpServletRequestHandler;
31 import org.pentaho.core.session.IPentahoSession;
32 import org.pentaho.core.solution.HttpOutputHandler;
33 import org.pentaho.core.system.PentahoSystem;
34 import org.pentaho.core.ui.SimpleUrlFactory;
35 import org.pentaho.core.util.UIUtil;
36 import org.pentaho.messages.util.LocaleHelper;
37
38 /**
39  * @author James Dixon
40  *
41  * TODO To change the template for this generated type comment go to Window -
42  * Preferences - Java - Code Style - Code Templates
43  */

44 public class UIServlet extends ServletBase {
45
46     /**
47      *
48      */

49     private static final long serialVersionUID = 7018489258697145705L;
50
51     private static final Log logger = LogFactory.getLog(UIServlet.class);
52
53     public Log getLogger() {
54         return logger;
55     }
56
57     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
58
59         OutputStream JavaDoc outputStream = response.getOutputStream();
60
61         String JavaDoc path = request.getContextPath();
62
63         IPentahoSession userSession = getPentahoSession(request);
64         HttpSession JavaDoc session = request.getSession();
65
66         String JavaDoc user = request.getRemoteUser();
67
68         if (user != null && !userSession.isAuthenticated()) {
69             // the user was not logged in before but is now....
70
userSession.setAuthenticated(user);
71         }
72
73         String JavaDoc type = request.getParameter("type"); //$NON-NLS-1$
74
if (type == null) {
75             type = "text/html"; //$NON-NLS-1$
76
}
77
78         // find out which component is going to fulfill this request
79
String JavaDoc componentName = request.getParameter("component"); //$NON-NLS-1$
80
if (componentName == null) {
81             response.setContentType("text/html"); //$NON-NLS-1$
82
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
83             UIUtil.formatErrorMessage("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$
84
outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
85             return;
86
87         }
88         response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
89         // TODO switch this to the interface once stable
90
BaseUIComponent component = (BaseUIComponent) session.getAttribute(componentName);
91         if (component == null) {
92             component = getComponent(componentName);
93             if (component == null) {
94                 response.setContentType("text/html"); //$NON-NLS-1$
95
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
96                 UIUtil.formatErrorMessage("text/html", Messages.getString("UIServlet.ACTION_FAILED"), Messages.getErrorString("UIServlet.ERROR_0002_COMPONENT_INVALID"), buffer); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
97
outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
98                 return;
99             }
100             session.setAttribute(componentName, component);
101         }
102
103         if (!component.validate()) {
104             // TODO need an error here
105
return;
106         }
107         String JavaDoc baseUrl = request.getScheme() + "://" + 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$
108

109         response.setContentType(type);
110         HttpOutputHandler outputHandler = new HttpOutputHandler(response, outputStream, true);
111
112         SimpleUrlFactory urlFactory = new SimpleUrlFactory(baseUrl);
113
114         HttpServletRequestHandler requestHandler = new HttpServletRequestHandler(userSession, null, request, outputHandler, urlFactory);
115
116         requestHandler.handleUIRequest(component, type);
117     }
118
119     private BaseUIComponent getComponent(String JavaDoc componentName) {
120         return (BaseUIComponent) PentahoSystem.createObject(componentName, this);
121     }
122
123     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
124
125         doGet(request, response);
126
127     }
128
129 }
130
Popular Tags