@RequestMapping(
      value = "/{timeseriesId}/getData",
      produces = {"image/png"},
      method = GET)
  public void getTimeseriesChart(
      HttpServletResponse response,
      @PathVariable String timeseriesId,
      @RequestParam(required = false) MultiValueMap<String, String> query)
      throws Exception {

    checkIfUnknownTimeseries(timeseriesId);

    IoParameters map = createFromQuery(query);
    TimeseriesMetadataOutput metadata = timeseriesMetadataService.getParameter(timeseriesId, map);
    RenderingContext context =
        createContextForSingleTimeseries(metadata, map.getStyle(), map.getTimespan());
    context.setDimensions(map.getChartDimension());

    UndesignedParameterSet parameters = createForSingleTimeseries(timeseriesId, map.getTimespan());
    checkAgainstTimespanRestriction(parameters.getTimespan());

    parameters.setGeneralize(map.isGeneralize());
    parameters.setBase64(map.isBase64());
    parameters.setExpanded(map.isExpanded());

    IoHandler renderer = IoFactory.createWith(map).createIOHandler(context);
    handleBinaryResponse(response, parameters, renderer);
  }
  @RequestMapping(
      value = "/getData",
      produces = {"application/pdf"},
      method = POST)
  public void getTimeseriesCollectionReport(
      HttpServletResponse response, @RequestBody DesignedParameterSet requestParameters)
      throws Exception {

    checkIfUnknownTimeseries(requestParameters.getTimeseries());

    IoParameters map = createFromQuery(requestParameters);
    UndesignedParameterSet parameters = createFromDesignedParameters(requestParameters);
    checkAgainstTimespanRestriction(parameters.getTimespan());
    parameters.setGeneralize(map.isGeneralize());
    parameters.setExpanded(map.isExpanded());

    String[] timeseriesIds = parameters.getTimeseries();
    TimeseriesMetadataOutput[] timeseriesMetadatas =
        timeseriesMetadataService.getParameters(timeseriesIds, map);
    RenderingContext context = createContextWith(requestParameters, timeseriesMetadatas);

    IoHandler renderer =
        IoFactory.createWith(map).forMimeType(APPLICATION_PDF).createIOHandler(context);

    handleBinaryResponse(response, parameters, renderer);
  }
  @RequestMapping(
      value = "/{timeseriesId}/getData",
      produces = {"application/json"},
      method = GET)
  public ModelAndView getTimeseriesData(
      HttpServletResponse response,
      @PathVariable String timeseriesId,
      @RequestParam(required = false) MultiValueMap<String, String> query) {

    checkIfUnknownTimeseries(timeseriesId);

    IoParameters map = createFromQuery(query);
    Interval timespan = map.getTimespan();
    checkAgainstTimespanRestriction(timespan.toString());
    UndesignedParameterSet parameters = createForSingleTimeseries(timeseriesId, timespan);

    parameters.setGeneralize(map.isGeneralize());
    parameters.setExpanded(map.isExpanded());

    // TODO add paging

    TvpDataCollection timeseriesData = getTimeseriesData(parameters);
    TimeseriesDataCollection<?> formattedDataCollection = format(timeseriesData, map.getFormat());
    if (map.isExpanded()) {
      return new ModelAndView().addObject(formattedDataCollection.getTimeseriesOutput());
    }
    Object formattedTimeseries = formattedDataCollection.getAllTimeseries().get(timeseriesId);
    return new ModelAndView().addObject(formattedTimeseries);
  }
  @RequestMapping(
      value = "/getData",
      produces = {"application/json"},
      method = POST)
  public ModelAndView getTimeseriesCollectionData(
      HttpServletResponse response, @RequestBody UndesignedParameterSet parameters)
      throws Exception {

    checkIfUnknownTimeseries(parameters.getTimeseries());

    TvpDataCollection timeseriesData = getTimeseriesData(parameters);
    TimeseriesDataCollection<?> formattedDataCollection =
        format(timeseriesData, parameters.getFormat());
    return new ModelAndView().addObject(formattedDataCollection.getTimeseriesOutput());
  }
 private TvpDataCollection getTimeseriesData(UndesignedParameterSet parameters) {
   Stopwatch stopwatch = startStopwatch();
   TvpDataCollection timeseriesData =
       parameters.isGeneralize()
           ? composeDataService(timeseriesDataService).getTimeseriesData(parameters)
           : timeseriesDataService.getTimeseriesData(parameters);
   LOGGER.debug("Processing request took {} seconds.", stopwatch.stopInSeconds());
   return timeseriesData;
 }
 /**
  * @param response the response to write binary on.
  * @param parameters the timeseries parameter to request raw data.
  * @param renderer an output renderer.
  * @throws InternalServerException if data processing fails for some reason.
  */
 private void handleBinaryResponse(
     HttpServletResponse response, UndesignedParameterSet parameters, IoHandler renderer) {
   try {
     renderer.generateOutput(getTimeseriesData(parameters));
     if (parameters.isBase64()) {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       renderer.encodeAndWriteTo(baos);
       byte[] imageData = baos.toByteArray();
       byte[] encode = Base64.encodeBase64(imageData);
       response.getOutputStream().write(encode);
     } else {
       renderer.encodeAndWriteTo(response.getOutputStream());
     }
   } catch (IOException e) { // handled by BaseController
     throw new InternalServerException("Error handling output stream.", e);
   } catch (IoParseException e) { // handled by BaseController
     throw new InternalServerException("Could not write binary to stream.", e);
   }
 }