001: /*
002: * Copyright 2005 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 Jan 10, 2006
014: */
015: package org.pentaho.ui.servlet;
016:
017: import java.io.IOException;
018: import java.io.Writer;
019:
020: import javax.servlet.ServletConfig;
021: import javax.servlet.ServletException;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.dom4j.Document;
028: import org.dom4j.DocumentFactory;
029: import org.dom4j.Element;
030: import org.dom4j.Namespace;
031: import org.dom4j.QName;
032: import org.dom4j.io.OutputFormat;
033: import org.dom4j.io.XMLWriter;
034: import org.pentaho.core.session.IPentahoSession;
035: import org.pentaho.core.system.PentahoSystem;
036: import org.pentaho.messages.Messages;
037: import org.pentaho.messages.util.LocaleHelper;
038:
039: import com.pentaho.repository.subscribe.ISubscriptionRepository;
040:
041: /**
042: * Provides a web interface for the management of users. The service should be
043: * called by external systems to notify Pentaho when a user has been remove,
044: * allowing for proper clean-up of objects owned by the user.
045: * <p>
046: * <b>Parameters</b> <table cellspacing='5' cellpadding='3'>
047: * <tr>
048: * <td valign='top'>command</td>
049: * <td valign='top'>the command to exeucte. The only valid value is <i>delete</i>.</td>
050: * <td valign='top'>required</td>
051: * </tr>
052: * <tr>
053: * <td valign='top'>user</td>
054: * <td valign='top'>the user name of the user for whom the action is taken. For
055: * example, this would be the user to delete.</td>
056: * <td valign='top'>required</td>
057: * </tr>
058: * <tr>
059: * <td valign='top'>password</td>
060: * <td valign='top'>the password for authenticating the system invoking the
061: * call. This password must match the value of the <i>parameter</i> optional
062: * initialization parameter for the servlet and specified in the web.xml.</td>
063: * <td valign='top'>optional</td>
064: * </tr>
065: * </table>
066: *
067: * @author Anthony de Shazor
068: *
069: */
070: public class SubscriptionUserCleanup extends ServletBase {
071:
072: private static final int SERVICE_SUCCESS = 0;
073:
074: private static final int SERVICE_FAILURE = 1;
075:
076: private static final int REQUEST_FAILURE = 2;
077:
078: private static final long serialVersionUID = -3249803751706281261L;
079:
080: private static final Log logger = LogFactory
081: .getLog(SubscriptionUserCleanup.class);
082:
083: private String password;
084:
085: /**
086: * Constructs an instance.
087: */
088: public SubscriptionUserCleanup() {
089: super ();
090: }
091:
092: /*
093: * (non-Javadoc)
094: *
095: * @see org.pentaho.core.ui.servlet.ServletBase#getLogger()
096: */
097: public Log getLogger() {
098: return logger;
099: }
100:
101: /*
102: * (non-Javadoc)
103: *
104: * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
105: */
106: public void init(ServletConfig config) throws ServletException {
107: super .init(config);
108:
109: password = config.getInitParameter("password"); //$NON-NLS-1$
110: }
111:
112: /*
113: * (non-Javadoc)
114: *
115: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
116: * javax.servlet.http.HttpServletResponse)
117: */
118: protected void doGet(HttpServletRequest request,
119: HttpServletResponse response) throws ServletException,
120: IOException {
121:
122: PentahoSystem.systemEntryPoint();
123:
124: try {
125: String userName;
126: String passwordParam;
127: String command;
128: Element soap;
129:
130: command = request.getParameter("command"); //$NON-NLS-1$
131: userName = request.getParameter("user"); //$NON-NLS-1$
132: passwordParam = request.getParameter("password"); //$NON-NLS-1$
133:
134: if (command == null) {
135: soap = generateSOAPMessage(
136: REQUEST_FAILURE,
137: Messages
138: .getErrorString("ManageUsers.ERROR_0004_MISSING_COMMAND")); //$NON-NLS-1$
139: } else if (userName == null) {
140: soap = generateSOAPMessage(
141: REQUEST_FAILURE,
142: Messages
143: .getErrorString("ManageUsers.ERROR_0001_MISSING_USERNAME")); //$NON-NLS-1$
144: } else if ((password != null) && (password.length() > 0)
145: && !password.equals(passwordParam)) {
146: soap = generateSOAPMessage(
147: REQUEST_FAILURE,
148: Messages
149: .getErrorString("ManageUsers.ERROR_0002_UNAUTHORIZED_ACCESS")); //$NON-NLS-1$
150: } else {
151: if ("delete".equals(command)) { //$NON-NLS-1$
152: soap = deleteUser(request, userName);
153: } else {
154: soap = generateSOAPMessage(
155: REQUEST_FAILURE,
156: Messages
157: .getErrorString(
158: "ManageUsers.ERROR_0005_UNKNOWN_COMMAND", command)); //$NON-NLS-1$
159: }
160: }
161:
162: generateResponse(response, soap);
163: } finally {
164: PentahoSystem.systemExitPoint();
165: }
166: }
167:
168: private void generateResponse(HttpServletResponse response,
169: Element soap) throws IOException {
170: Writer output;
171: XMLWriter xmlWriter;
172: OutputFormat format;
173:
174: response.setContentType("text/xml"); //$NON-NLS-1$
175: response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
176:
177: output = response.getWriter();
178: format = OutputFormat.createCompactFormat();
179: xmlWriter = new XMLWriter(output, format);
180: xmlWriter.write(soap);
181: }
182:
183: private Element deleteUser(HttpServletRequest request, String user) {
184: Element soap = null;
185:
186: try {
187: ISubscriptionRepository repository;
188: IPentahoSession pentahoSession;
189:
190: pentahoSession = getPentahoSession(request);
191: repository = PentahoSystem
192: .getSubscriptionRepository(pentahoSession);
193: if (repository == null) {
194: soap = generateSOAPMessage(
195: SERVICE_FAILURE,
196: Messages
197: .getErrorString("ManageUsers.ERROR_0006_REPOSITORY_ERROR")); //$NON-NLS-1$
198: } else {
199: repository.deleteUserSubscriptions(user);
200: soap = generateSOAPMessage(SERVICE_SUCCESS, user);
201: }
202: } catch (Exception ex) {
203: String message = ex.toString();
204:
205: soap = generateSOAPMessage(
206: SERVICE_FAILURE,
207: Messages
208: .getErrorString(
209: "ManageUsers.ERROR_0003_SUBSCRIPTION_DELETE_FAILURE_MESSAGE", user, message)); //$NON-NLS-1$
210: }
211:
212: return soap;
213: }
214:
215: private static Element generateSOAPMessage(int status,
216: String message) {
217: DocumentFactory factory = DocumentFactory.getInstance();
218: Document soap = factory.createDocument();
219: Element envelope;
220: Element body;
221: QName qname;
222: Namespace namespace;
223:
224: namespace = new Namespace(
225: "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"); //$NON-NLS-1$ //$NON-NLS-2$
226: envelope = soap.addElement(new QName("Envelope", namespace)); //$NON-NLS-1$
227: qname = new QName("encodingStyle", namespace); //$NON-NLS-1$
228: envelope.addAttribute(qname,
229: "http://schemas.xmlsoap.org/soap/encoding/"); //$NON-NLS-1$
230: body = envelope.addElement(new QName("Body", namespace)); //$NON-NLS-1$
231:
232: if (status == SERVICE_SUCCESS) {
233: Element response = body.addElement("ManageUsers-response"); //$NON-NLS-1$
234: response.addCDATA(message);
235: } else {
236: Element fault = body.addElement(new QName(
237: "Fault", namespace)); //$NON-NLS-1$
238: Element code = fault
239: .addElement(new QName("Code", namespace)); //$NON-NLS-1$
240: Element value = code.addElement(new QName(
241: "Value", namespace)); //$NON-NLS-1$
242: Element subcode;
243: Element reason;
244: Element text;
245:
246: if (status == REQUEST_FAILURE) {
247: value.setText("SOAP-ENV:Sender"); //$NON-NLS-1$
248: } else {
249: value.setText("SOAP-ENV:Reciever"); //$NON-NLS-1$
250: }
251:
252: subcode = code.addElement(new QName("Subcode", namespace)); //$NON-NLS-1$
253: value = subcode.addElement(new QName("Value", namespace)); //$NON-NLS-1$
254: value.addCDATA(message);
255: reason = fault.addElement(new QName("Reason", namespace)); //$NON-NLS-1$
256: text = reason.addElement(new QName("Text", namespace)); //$NON-NLS-1$
257: text.addAttribute(
258: "lang", LocaleHelper.getDefaultLocale().toString()); //$NON-NLS-1$
259: text.addCDATA(message);
260:
261: // Add empty Detail section
262: fault.addElement(new QName("Detail", namespace)); //$NON-NLS-1$
263: }
264:
265: return envelope;
266: }
267: }
|