KickJava   Java API By Example, From Geeks To Geeks.

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


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 26, 2005
14  * @author Gretchen Moran
15  *
16  */

17
18 package org.pentaho.ui.servlet;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.pentaho.core.repository.ISolutionRepository;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.core.util.UIUtil;
33 import org.pentaho.messages.Messages;
34 import org.pentaho.messages.util.LocaleHelper;
35
36 public class GetMondrianModel extends ServletBase {
37
38     private static final long serialVersionUID = 1L;
39
40     protected void doGet(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws ServletException JavaDoc, IOException JavaDoc {
41         doPost(arg0, arg1);
42     }
43
44     private static final Log logger = LogFactory.getLog(GetMondrianModel.class);
45
46     public Log getLogger() {
47         return logger;
48     }
49
50     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
51
52         // TODO perform any authorization here...
53
getPentahoSession(request);
54
55         String JavaDoc model = null;
56         if (request.getParameter("model") != null) { //$NON-NLS-1$
57
model = request.getParameter("model"); //$NON-NLS-1$
58
}
59
60         if (model == null) {
61             error(Messages.getErrorString("MondrianModel.ERROR_0001_MODEL_PARAMETER_MISSING")); //$NON-NLS-1$
62
return;
63         }
64
65         if (!model.endsWith(".mondrian.xml")) { //$NON-NLS-1$
66
error(Messages.getErrorString("MondrianModel.ERROR_0002_INVALID_FILE", model)); //$NON-NLS-1$
67
return;
68         }
69         
70         // Open the input and output streams
71
ISolutionRepository repository = PentahoSystem.getSolutionRepository(UIUtil.getPentahoSession(request));
72         if (repository != null) {
73             String JavaDoc mimeType = "text/xml"; //$NON-NLS-1$
74
response.setContentType(mimeType);
75             response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
76
77
78             InputStream JavaDoc in = repository.getResourceInputStream(model);
79             OutputStream JavaDoc out = response.getOutputStream();
80              
81             try {
82                 // Copy the contents of the file to the output stream
83
byte[] buf = new byte[2048];
84                 int count = 0;
85                 int length = 0;
86                 while ((count = in.read(buf)) >= 0) {
87                     out.write(buf, 0, count);
88                     length += count;
89                 }
90                 response.setContentLength(length);
91             } finally {
92                 in.close();
93                 out.close();
94             }
95         } else {
96                 error(Messages.getErrorString("MondrianModel.ERROR_0004_INVALID_REPOSITORY")); //$NON-NLS-1$
97
}
98     }
99
100 }
101
Popular Tags