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, Angelo Rodriguez, Steven Barkdull
015: *
016: */
017:
018: package org.pentaho.ui.servlet;
019:
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.StringTokenizer;
025:
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.lang.StringEscapeUtils;
031: import org.apache.commons.lang.StringUtils;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.dom4j.Document;
035: import org.dom4j.DocumentHelper;
036: import org.dom4j.Element;
037: import org.pentaho.core.cache.CacheManager;
038: import org.pentaho.core.repository.ISolutionRepository;
039: import org.pentaho.core.session.IPentahoSession;
040: import org.pentaho.core.solution.ISolutionFile;
041: import org.pentaho.core.system.PentahoSystem;
042: import org.pentaho.core.util.SoapUtil;
043: import org.pentaho.messages.Messages;
044: import org.pentaho.messages.util.LocaleHelper;
045:
046: public class SolutionRepositoryService extends ServletBase {
047:
048: private static final Log logger = LogFactory
049: .getLog(SolutionRepositoryService.class);
050:
051: public Log getLogger() {
052: return logger;
053: }
054:
055: public SolutionRepositoryService() {
056: super ();
057: }
058:
059: protected void doGet(HttpServletRequest request,
060: HttpServletResponse response) throws ServletException,
061: IOException {
062: PentahoSystem.systemEntryPoint();
063: OutputStream outputStream = response.getOutputStream();
064: try {
065: boolean wrapWithSoap = !"true".equals(request.getParameter("ajax")); //$NON-NLS-1$ //$NON-NLS-2$
066: String component = request.getParameter("component"); //$NON-NLS-1$
067: String filter = request.getParameter("filter"); //$NON-NLS-1$
068: List filters = new ArrayList();
069: if (!StringUtils.isEmpty(filter)) {
070: StringTokenizer st = new StringTokenizer(filter, "*.,");
071: while (st.hasMoreTokens()) {
072: filters.add(st.nextToken());
073: }
074: }
075: response.setContentType("text/xml"); //$NON-NLS-1$
076: response.setCharacterEncoding(LocaleHelper
077: .getSystemEncoding());
078:
079: IPentahoSession userSession = getPentahoSession(request);
080: // send the header of the message to prevent time-outs while we are working
081: response.setHeader("expires", "0"); //$NON-NLS-1$ //$NON-NLS-2$
082:
083: dispatch(request, response, component, (String[]) filters
084: .toArray(new String[] {}), outputStream,
085: userSession, wrapWithSoap);
086: } catch (Exception ioEx) {
087: String msg = Messages
088: .getErrorString("HttpWebService.ERROR_0001_ERROR_DURING_WEB_SERVICE"); //$NON-NLS-1$
089: error(msg, ioEx);
090: writeDocument(outputStream, SolutionRepositoryService
091: .getErrorXml(msg), true);
092: } finally {
093: PentahoSystem.systemExitPoint();
094: }
095: if (debug) {
096: debug(Messages
097: .getString("HttpWebService.DEBUG_WEB_SERVICE_END")); //$NON-NLS-1$
098: }
099: }
100:
101: protected void dispatch(HttpServletRequest request,
102: HttpServletResponse response, String component,
103: String[] filters, OutputStream outputStream,
104: IPentahoSession userSession, boolean wrapWithSOAP)
105: throws IOException {
106: if ("getSolutionRepositoryDoc".equals(component)) { //$NON-NLS-1$
107: Document doc = getSolutionRepositoryDoc(userSession,
108: filters);
109: writeDocument(outputStream, doc.asXML(), wrapWithSOAP);
110: } else {
111: throw new RuntimeException(
112: Messages
113: .getErrorString(
114: "HttpWebService.UNRECOGNIZED_COMPONENT_REQUEST", component)); //$NON-NLS-1$
115: }
116: }
117:
118: protected void doPost(HttpServletRequest request,
119: HttpServletResponse response) throws ServletException,
120: IOException {
121: doGet(request, response);
122: }
123:
124: private static String getErrorXml(String errorMsg) {
125: errorMsg = StringEscapeUtils.escapeXml(errorMsg);
126: return "<error msg=\"" + errorMsg + "\"/>"; //$NON-NLS-1$ //$NON-NLS-2$
127: }
128:
129: public void writeDocument(OutputStream outputStream, String doc,
130: boolean wrapWithSOAP) throws IOException {
131: int headerPos = doc.indexOf("?>"); //$NON-NLS-1$
132: if (headerPos > -1) {
133: doc = doc.substring(headerPos + 2);
134: }
135: if (wrapWithSOAP) {
136: outputStream.write(SoapUtil.getSoapHeader().getBytes(
137: LocaleHelper.getSystemEncoding()));
138: outputStream.write(SoapUtil.openSoapResponse().getBytes(
139: LocaleHelper.getSystemEncoding()));
140: outputStream
141: .write("<content>".getBytes(LocaleHelper.getSystemEncoding())); //$NON-NLS-1$
142: }
143: outputStream.write(doc.getBytes(LocaleHelper
144: .getSystemEncoding()));
145: if (wrapWithSOAP) {
146: outputStream
147: .write("</content>".getBytes(LocaleHelper.getSystemEncoding())); //$NON-NLS-1$
148: outputStream.write(SoapUtil.closeSoapResponse().getBytes(
149: LocaleHelper.getSystemEncoding()));
150: outputStream.write(SoapUtil.getSoapFooter().getBytes(
151: LocaleHelper.getSystemEncoding()));
152: }
153: }
154:
155: private boolean acceptFilter(String name, String[] filters) {
156: if (filters == null || filters.length == 0) {
157: return true;
158: }
159: for (int i = 0; i < filters.length; i++) {
160: if (name.endsWith(filters[i])) {
161: return true;
162: }
163: }
164: return false;
165: }
166:
167: private void processRepositoryFile(Element parentElement,
168: ISolutionFile parentFile, String[] filters) {
169: ISolutionFile children[] = parentFile.listFiles();
170: for (int i = 0; children != null && i < children.length; i++) {
171: String name = children[i].getFileName();
172: if (!name.startsWith(".")
173: && (children[i].isDirectory() || acceptFilter(name,
174: filters))) {
175: Element child = parentElement.addElement("file");
176: child.addAttribute("name", name);
177: child.addAttribute("isDirectory", ""
178: + children[i].isDirectory());
179: if (children[i].isDirectory()) {
180: processRepositoryFile(child, children[i], filters);
181: }
182: }
183: }
184: }
185:
186: private Document getSolutionRepositoryDoc(
187: IPentahoSession userSession, String[] filters) {
188: ISolutionRepository repository = PentahoSystem
189: .getSolutionRepository(userSession);
190: ISolutionFile rootFile = repository.getRootFolder();
191: Document document = null;
192: CacheManager cacheManager = PentahoSystem.getCacheManager();
193: if (null != cacheManager) {
194: document = (Document) cacheManager.getFromSessionCache(
195: userSession, rootFile.getFileName());
196: if (document == null) {
197: document = DocumentHelper.createDocument();
198: Element root = document.addElement("repository");
199: root.addAttribute("path", rootFile.getFullPath());
200: processRepositoryFile(root, rootFile, filters);
201: cacheManager.putInSessionCache(userSession, rootFile
202: .getFileName(), document);
203: }
204: } else {
205: document = DocumentHelper.createDocument();
206: Element root = document.addElement("repository");
207: root.addAttribute("path", rootFile.getFullPath());
208: processRepositoryFile(root, rootFile, filters);
209: }
210: return document;
211: }
212:
213: }
|