use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.
the class DashboardWidgetIT method testWidget2.
public void testWidget2() {
// $NON-NLS-1$
SimpleUrlFactory urlFactory = new SimpleUrlFactory("");
ArrayList messages = new ArrayList();
DashboardWidgetComponent widget = new DashboardWidgetComponent(DashboardWidgetComponent.TYPE_DIAL, getSolutionPath() + "/samples/charts/dashboardwidget1.dial.xml", 200, 200, urlFactory, // $NON-NLS-1$
messages);
widget.setValue(49);
// $NON-NLS-1$
widget.setTitle("test widget 1");
// $NON-NLS-1$
widget.setUnits("");
// $NON-NLS-1$
StandaloneSession session = new StandaloneSession("BaseTest.DEBUG_JUNIT_SESSION");
widget.validate(session, null);
SimpleParameterProvider requestParameters = new SimpleParameterProvider();
SimpleParameterProvider sessionParameters = new SimpleParameterProvider();
widget.setParameterProvider(HttpRequestParameterProvider.SCOPE_REQUEST, requestParameters);
widget.setParameterProvider(HttpSessionParameterProvider.SCOPE_SESSION, sessionParameters);
// $NON-NLS-1$
String content = widget.getContent("text/html");
// $NON-NLS-1$//$NON-NLS-2$
OutputStream outputStream = getOutputStream("DashboardWidgetTest.testWidget1", ".html");
try {
outputStream.write(content.getBytes());
} catch (Exception e) {
// content check will test this
}
}
use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.
the class ChartHelper method doPieChart.
/**
* doPieChart generates the images and html necessary to render pie charts. It provides a simple wrapper around
* the class org.pentaho.ui.component.charting.PieDatasetChartComponent
*
* @param solutionName
* the solution name
* @param actionPath
* the action path
* @param chartName
* the xml file describing the chart
* @param parameterProvider
* the collection of parameters to customize the chart
* @param outputStream
* the output string buffer for the content
* @param userSession
* the user session object
* @param messages
* a collection to store error and logging messages
* @param logger
* logging object
*
* @return true if successful
* @deprecated use doChart instead
*/
@Deprecated
public static boolean doPieChart(final String actionPath, final IParameterProvider parameterProvider, final StringBuffer outputStream, final IPentahoSession userSession, final ArrayList messages, final ILogger logger) {
deprecateWarning();
boolean result = true;
// $NON-NLS-1$
String outerParams = parameterProvider.getStringParameter("outer-params", null);
// $NON-NLS-1$
String innerParam = parameterProvider.getStringParameter("inner-param", null);
// $NON-NLS-1$
String urlDrillTemplate = parameterProvider.getStringParameter("drill-url", null);
// $NON-NLS-1$
String imageUrl = parameterProvider.getStringParameter("image-url", null);
IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
if (imageUrl == null) {
// $NON-NLS-1$
imageUrl = requestContext.getContextPath();
}
if (urlDrillTemplate == null) {
// $NON-NLS-1$
urlDrillTemplate = "";
}
// $NON-NLS-1$
int width = (int) parameterProvider.getLongParameter("image-width", 150);
// $NON-NLS-1$
int height = (int) parameterProvider.getLongParameter("image-height", 150);
SimpleUrlFactory urlFactory = new SimpleUrlFactory(urlDrillTemplate);
PieDatasetChartComponent chartComponent = null;
try {
chartComponent = new PieDatasetChartComponent(JFreeChartEngine.PIE_CHART_TYPE, actionPath, width, height, urlFactory, messages);
if (logger != null) {
chartComponent.setLoggingLevel(logger.getLoggingLevel());
}
chartComponent.validate(userSession, null);
chartComponent.setUrlTemplate(urlDrillTemplate);
if (outerParams != null) {
// $NON-NLS-1$
StringTokenizer tokenizer = new StringTokenizer(outerParams, ";");
while (tokenizer.hasMoreTokens()) {
chartComponent.addOuterParamName(tokenizer.nextToken());
}
}
chartComponent.setParamName(innerParam);
chartComponent.setDataAction(actionPath);
chartComponent.setParameterProvider(IParameterProvider.SCOPE_REQUEST, parameterProvider);
// $NON-NLS-1$
String content = chartComponent.getContent("text/html");
if ((content == null) || content.equals("")) {
// $NON-NLS-1$
// $NON-NLS-1$
content = " ";
}
outputStream.append(content);
} finally {
if (chartComponent != null) {
chartComponent.dispose();
}
}
return result;
}
use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.
the class ChartHelper method doDial.
/**
* doDial generates the images and html necessary to render dials. It provides a simple wrapper around the class
* org.pentaho.ui.component.DashboardWidgetComponent
*
* @param solutionName
* the solution name
* @param actionPath
* the action path
* @param chartName
* the xml file describing the chart
* @param parameterProvider
* the collection of parameters to customize the chart
* @param outputStream
* the output string buffer for the content
* @param userSession
* the user session object
* @param messages
* a collection to store error and logging messages
* @param logger
* logging object
*
* @return true if successful
* @deprecated use doChart() instead
*/
@Deprecated
public static boolean doDial(final String solutionName, final String actionPath, final String chartName, final IParameterProvider parameterProvider, final StringBuffer outputStream, final IPentahoSession userSession, final ArrayList messages, final ILogger logger) {
deprecateWarning();
boolean result = true;
// $NON-NLS-1$
String linkUrl = parameterProvider.getStringParameter("drill-url", null);
// $NON-NLS-1$
String imageUrl = parameterProvider.getStringParameter("image-url", null);
IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
if (imageUrl == null) {
// $NON-NLS-1$
imageUrl = requestContext.getContextPath();
}
if (linkUrl == null) {
// $NON-NLS-1$
linkUrl = "";
}
// $NON-NLS-1$
int width = (int) parameterProvider.getLongParameter("image-width", 150);
// $NON-NLS-1$
int height = (int) parameterProvider.getLongParameter("image-height", 150);
SimpleUrlFactory urlFactory = new SimpleUrlFactory(linkUrl);
DashboardWidgetComponent widget = null;
try {
widget = new DashboardWidgetComponent(DashboardWidgetComponent.TYPE_DIAL, solutionName + File.separator + actionPath + File.separator + chartName, width, height, urlFactory, messages);
if (logger != null) {
widget.setLoggingLevel(logger.getLoggingLevel());
}
widget.validate(userSession, null);
widget.setParameterProvider(IParameterProvider.SCOPE_REQUEST, parameterProvider);
// $NON-NLS-1$//$NON-NLS-2$
String value = parameterProvider.getStringParameter("value", "0");
Number numericValue = DataUtilities.toNumber(value, LocaleHelper.getCurrencyFormat(), LocaleHelper.getNumberFormat());
widget.setValue(numericValue.doubleValue());
// $NON-NLS-1$ //$NON-NLS-2$
String title = parameterProvider.getStringParameter("title", "");
widget.setTitle(title);
// $NON-NLS-1$
String content = widget.getContent("text/html");
if (content == null) {
StringBuffer buffer = new StringBuffer();
PentahoSystem.get(IMessageFormatter.class, userSession).formatErrorMessage("text/html", Messages.getInstance().getString("Widget.ERROR_0001_COULD_NOT_CREATE_WIDGET"), messages, // $NON-NLS-1$ //$NON-NLS-2$
buffer);
content = buffer.toString();
result = false;
}
if ((content == null) || content.equals("")) {
// $NON-NLS-1$
// $NON-NLS-1$
content = " ";
}
outputStream.append(content);
} finally {
if (widget != null) {
widget.dispose();
}
}
return result;
}
use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.
the class GenericServlet method doGet.
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
if (showDeprecationMessage) {
String deprecationMessage = "GenericServlet is deprecated and should no longer be handling requests. More detail below..." + "\n | You have issued a {0} request to {1} from referer {2} " + "\n | Please consider using one of the following REST services instead:" + "\n | * GET /api/repos/<pluginId>/<path> to read files from a plugin public dir" + "\n | * POST|GET /api/repos/<pathId>/generatedContent to create content resulting from execution of a " + "repo file" + "\n | * POST|GET /api/repos/<pluginId>/<contentGeneratorId> to execute a content generator by name (RPC " + "compatibility service)" + "\n \\ To turn this message off, set init-param 'showDeprecationMessage' to false in the GenericServlet " + "declaration" + "";
String referer = StringUtils.defaultString(request.getHeader("Referer"), "");
logger.warn(MessageFormat.format(deprecationMessage, request.getMethod(), request.getRequestURL(), referer));
}
PentahoSystem.systemEntryPoint();
IOutputHandler outputHandler = null;
// BISERVER-2767 - grabbing the current class loader so we can replace it at the end
ClassLoader origContextClassloader = Thread.currentThread().getContextClassLoader();
try {
String servletPath = request.getServletPath();
String pathInfo = request.getPathInfo();
// $NON-NLS-1$
String contentGeneratorId = "";
// $NON-NLS-1$
String urlPath = "";
SimpleParameterProvider pathParams = new SimpleParameterProvider();
if (StringUtils.isEmpty(pathInfo)) {
logger.error(// $NON-NLS-1$
Messages.getInstance().getErrorString("GenericServlet.ERROR_0005_NO_RESOURCE_SPECIFIED"));
response.sendError(403);
return;
}
String path = pathInfo.substring(1);
int slashPos = path.indexOf('/');
if (slashPos != -1) {
// $NON-NLS-1$
pathParams.setParameter("path", pathInfo.substring(slashPos + 1));
contentGeneratorId = path.substring(0, slashPos);
} else {
contentGeneratorId = path;
}
// $NON-NLS-1$
urlPath = "content/" + contentGeneratorId;
IParameterProvider requestParameters = new HttpRequestParameterProvider(request);
// $NON-NLS-1$
pathParams.setParameter("query", request.getQueryString());
// $NON-NLS-1$
pathParams.setParameter("contentType", request.getContentType());
InputStream in = request.getInputStream();
// $NON-NLS-1$
pathParams.setParameter("inputstream", in);
// $NON-NLS-1$
pathParams.setParameter("httpresponse", response);
// $NON-NLS-1$
pathParams.setParameter("httprequest", request);
// $NON-NLS-1$
pathParams.setParameter("remoteaddr", request.getRemoteAddr());
if (PentahoSystem.debug) {
// $NON-NLS-1$
debug("GenericServlet contentGeneratorId=" + contentGeneratorId);
// $NON-NLS-1$
debug("GenericServlet urlPath=" + urlPath);
}
IPentahoSession session = getPentahoSession(request);
IPluginManager pluginManager = PentahoSystem.get(IPluginManager.class, session);
if (pluginManager == null) {
OutputStream out = response.getOutputStream();
String message = Messages.getInstance().getErrorString("GenericServlet.ERROR_0001_BAD_OBJECT", // $NON-NLS-1$
IPluginManager.class.getSimpleName());
error(message);
out.write(message.getBytes());
return;
}
// TODO make doing the HTTP headers configurable per content generator
SimpleParameterProvider headerParams = new SimpleParameterProvider();
Enumeration names = request.getHeaderNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = request.getHeader(name);
headerParams.setParameter(name, value);
}
String pluginId = pluginManager.getServicePlugin(pathInfo);
if (pluginId != null && pluginManager.isStaticResource(pathInfo)) {
boolean cacheOn = "true".equals(pluginManager.getPluginSetting(pluginId, "settings/cache", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
"false"));
// $NON-NLS-1$
String maxAge = (String) pluginManager.getPluginSetting(pluginId, "settings/max-age", null);
allowBrowserCache(maxAge, pathParams);
String mimeType = MimeHelper.getMimeTypeFromFileName(pathInfo);
if (mimeType != null) {
response.setContentType(mimeType);
}
OutputStream out = response.getOutputStream();
// do we have this resource cached?
ByteArrayOutputStream byteStream = null;
if (cacheOn) {
byteStream = (ByteArrayOutputStream) cache.getFromRegionCache(CACHE_FILE, pathInfo);
}
if (byteStream != null) {
IOUtils.write(byteStream.toByteArray(), out);
return;
}
InputStream resourceStream = pluginManager.getStaticResource(pathInfo);
if (resourceStream != null) {
try {
byteStream = new ByteArrayOutputStream();
IOUtils.copy(resourceStream, byteStream);
// if cache is enabled, drop file in cache
if (cacheOn) {
cache.putInRegionCache(CACHE_FILE, pathInfo, byteStream);
}
// write it out
IOUtils.write(byteStream.toByteArray(), out);
return;
} finally {
IOUtils.closeQuietly(resourceStream);
}
}
logger.error(Messages.getInstance().getErrorString("GenericServlet.ERROR_0004_RESOURCE_NOT_FOUND", pluginId, // $NON-NLS-1$
pathInfo));
response.sendError(404);
return;
}
// content generators defined in plugin.xml are registered with 2 aliases, one is the id, the other is type
// so, we can still retrieve a content generator by id, even though this is not the correct way to find
// it. the correct way is to look up a content generator by pluginManager.getContentGenerator(type,
// perspectiveName)
IContentGenerator contentGenerator = (IContentGenerator) pluginManager.getBean(contentGeneratorId);
if (contentGenerator == null) {
OutputStream out = response.getOutputStream();
String message = Messages.getInstance().getErrorString("GenericServlet.ERROR_0002_BAD_GENERATOR", // $NON-NLS-1$
Encode.forHtml(contentGeneratorId));
error(message);
out.write(message.getBytes());
return;
}
// set the classloader of the current thread to the class loader of
// the plugin so that it can load its libraries
// Note: we cannot ask the contentGenerator class for it's classloader, since the cg may
// actually be a proxy object loaded by main the WebAppClassloader
Thread.currentThread().setContextClassLoader(pluginManager.getClassLoader(pluginId));
// String proxyClass = PentahoSystem.getSystemSetting( module+"/plugin.xml" ,
// "plugin/content-generators/"+contentGeneratorId,
// "content generator not found");
// see if this is an upload
// File uploading is a service provided by UploadFileServlet where appropriate protections
// are in place to prevent uploads that are too large.
// boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// if (isMultipart) {
// requestParameters = new SimpleParameterProvider();
// // Create a factory for disk-based file items
// FileItemFactory factory = new DiskFileItemFactory();
//
// // Create a new file upload handler
// ServletFileUpload upload = new ServletFileUpload(factory);
//
// // Parse the request
// List<?> /* FileItem */items = upload.parseRequest(request);
// Iterator<?> iter = items.iterator();
// while (iter.hasNext()) {
// FileItem item = (FileItem) iter.next();
//
// if (item.isFormField()) {
// ((SimpleParameterProvider) requestParameters).setParameter(item.getFieldName(), item.getString());
// } else {
// String name = item.getName();
// ((SimpleParameterProvider) requestParameters).setParameter(name, item.getInputStream());
// }
// }
// }
response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
IMimeTypeListener listener = new HttpMimeTypeListener(request, response);
outputHandler = getOutputHandler(response, true);
outputHandler.setMimeTypeListener(listener);
IParameterProvider sessionParameters = new HttpSessionParameterProvider(session);
IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
Map<String, IParameterProvider> parameterProviders = new HashMap<String, IParameterProvider>();
parameterProviders.put(IParameterProvider.SCOPE_REQUEST, requestParameters);
parameterProviders.put(IParameterProvider.SCOPE_SESSION, sessionParameters);
// $NON-NLS-1$
parameterProviders.put("headers", headerParams);
// $NON-NLS-1$
parameterProviders.put("path", pathParams);
SimpleUrlFactory urlFactory = // $NON-NLS-1$ //$NON-NLS-2$
new SimpleUrlFactory(requestContext.getContextPath() + urlPath + "?");
List<String> messages = new ArrayList<String>();
contentGenerator.setOutputHandler(outputHandler);
contentGenerator.setMessagesList(messages);
contentGenerator.setParameterProviders(parameterProviders);
contentGenerator.setSession(session);
contentGenerator.setUrlFactory(urlFactory);
// String contentType = request.getContentType();
// contentGenerator.setInput(input);
contentGenerator.createContent();
if (PentahoSystem.debug) {
// $NON-NLS-1$
debug("Generic Servlet content generate successfully");
}
} catch (Exception e) {
StringBuffer buffer = new StringBuffer();
error(Messages.getInstance().getErrorString("GenericServlet.ERROR_0002_BAD_GENERATOR", request.getQueryString()), // $NON-NLS-1$
e);
List errorList = new ArrayList();
String msg = e.getMessage();
errorList.add(msg);
// $NON-NLS-1$
MessageFormatUtils.formatFailureMessage("text/html", null, buffer, errorList);
response.getOutputStream().write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
} finally {
// reset the classloader of the current thread
Thread.currentThread().setContextClassLoader(origContextClassloader);
PentahoSystem.systemExitPoint();
}
}
use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.
the class HTMLComponentIT method testComponent3.
public void testComponent3() {
startTest();
// $NON-NLS-1$
info(Messages.getInstance().getString("HTMLComponentTest.USER_ERRORS_EXPECTED_URL_INVALID"));
// this should fail because the url is bad
// $NON-NLS-1$
String url = "xttp://a";
// $NON-NLS-1$
SimpleUrlFactory urlFactory = new SimpleUrlFactory("/testurl?");
ArrayList messages = new ArrayList();
HtmlComponent component = new HtmlComponent(HtmlComponent.TYPE_URL, url, Messages.getInstance().getString("HTML.ERROR_0001_NOT_AVAILABLE"), urlFactory, // $NON-NLS-1$
messages);
component.setLoggingLevel(getLoggingLevel());
// $NON-NLS-1$//$NON-NLS-2$
OutputStream outputStream = getOutputStream("HTMLComponentTest.testComponent3", ".html");
// $NON-NLS-1$
String contentType = "text/html";
SimpleParameterProvider requestParameters = new SimpleParameterProvider();
SimpleParameterProvider sessionParameters = new SimpleParameterProvider();
HashMap parameterProviders = new HashMap();
parameterProviders.put(IParameterProvider.SCOPE_REQUEST, requestParameters);
parameterProviders.put(IParameterProvider.SCOPE_SESSION, sessionParameters);
StandaloneSession session = // $NON-NLS-1$
new StandaloneSession(Messages.getInstance().getString("BaseTest.DEBUG_JUNIT_SESSION"));
SimpleOutputHandler outputHandler = new SimpleOutputHandler(outputStream, false);
BaseRequestHandler requestHandler = new BaseRequestHandler(session, null, outputHandler, null, urlFactory);
try {
component.validate(session, requestHandler);
component.handleRequest(outputStream, requestHandler, contentType, parameterProviders);
} catch (IOException e) {
e.printStackTrace();
}
finishTest();
}
Aggregations