use of org.eclipse.smarthome.ui.chart.ChartProvider in project smarthome by eclipse.
the class ChartServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
logger.debug("Received incoming chart request: {}", req);
int width = defaultWidth;
try {
width = Integer.parseInt(req.getParameter("w"));
} catch (Exception e) {
}
int height = defaultHeight;
try {
String h = req.getParameter("h");
if (h != null) {
Double d = Double.parseDouble(h) * scale;
height = d.intValue();
}
} catch (Exception e) {
}
// To avoid ambiguity you are not allowed to specify period, begin and end time at the same time.
if (req.getParameter("period") != null && req.getParameter("begin") != null && req.getParameter("end") != null) {
throw new ServletException("Do not specify the three parameters period, begin and end at the same time.");
}
// Read out the parameter period, begin and end and save them.
Date timeBegin = null;
Date timeEnd = null;
Long period = PERIODS.get(req.getParameter("period"));
if (period == null) {
// use a day as the default period
period = PERIODS.get("D");
}
if (req.getParameter("begin") != null) {
try {
timeBegin = new SimpleDateFormat(DATE_FORMAT).parse(req.getParameter("begin"));
} catch (ParseException e) {
throw new ServletException("Begin and end must have this format: " + DATE_FORMAT + ".");
}
}
if (req.getParameter("end") != null) {
try {
timeEnd = new SimpleDateFormat(DATE_FORMAT).parse(req.getParameter("end"));
} catch (ParseException e) {
throw new ServletException("Begin and end must have this format: " + DATE_FORMAT + ".");
}
}
// Set begin and end time and check legality.
if (timeBegin == null && timeEnd == null) {
timeEnd = new Date();
timeBegin = new Date(timeEnd.getTime() - period);
logger.debug("No begin or end is specified, use now as end and now-period as begin.");
} else if (timeEnd == null) {
timeEnd = new Date(timeBegin.getTime() + period);
logger.debug("No end is specified, use begin + period as end.");
} else if (timeBegin == null) {
timeBegin = new Date(timeEnd.getTime() - period);
logger.debug("No begin is specified, use end-period as begin");
} else if (timeEnd.before(timeBegin)) {
throw new ServletException("The end is before the begin.");
}
// If a persistence service is specified, find the provider
String serviceName = req.getParameter("service");
ChartProvider provider = getChartProviders().get(providerName);
if (provider == null) {
throw new ServletException("Could not get chart provider.");
}
// Read out the parameter 'dpi'
Integer dpi = null;
if (req.getParameter("dpi") != null) {
try {
dpi = Integer.valueOf(req.getParameter("dpi"));
} catch (NumberFormatException e) {
throw new ServletException("dpi parameter is invalid");
}
if (dpi <= 0) {
throw new ServletException("dpi parameter is <= 0");
}
}
// Read out parameter 'legend'
Boolean legend = null;
if (req.getParameter("legend") != null) {
legend = BooleanUtils.toBoolean(req.getParameter("legend"));
}
if (maxWidth > 0 && width > maxWidth) {
height = Math.round((float) height / (float) width * maxWidth);
if (dpi != null) {
dpi = Math.round((float) dpi / (float) width * maxWidth);
}
width = maxWidth;
}
// Set the content type to that provided by the chart provider
res.setContentType("image/" + provider.getChartType());
logger.debug("chart building with width {} height {} dpi {}", width, height, dpi);
try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(res.getOutputStream())) {
BufferedImage chart = provider.createChart(serviceName, req.getParameter("theme"), timeBegin, timeEnd, height, width, req.getParameter("items"), req.getParameter("groups"), dpi, legend);
ImageIO.write(chart, provider.getChartType().toString(), imageOutputStream);
logger.debug("Chart successfully generated and written to the response.");
} catch (ItemNotFoundException e) {
logger.debug("{}", e.getMessage());
res.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} catch (IllegalArgumentException e) {
logger.warn("Illegal argument in chart: {}", e.getMessage());
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument in chart: " + e.getMessage());
} catch (RuntimeException e) {
if (logger.isDebugEnabled()) {
// we also attach the stack trace
logger.warn("Chart generation failed: {}", e.getMessage(), e);
} else {
logger.warn("Chart generation failed: {}", e.getMessage());
}
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
Aggregations