<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 * (c) 2015 - Interseroh
 */
package de.interseroh.report.server.controller;

import com.ibm.wsdl.extensions.mime.MIMEContentImpl;
import de.interseroh.report.server.birt.BirtReportException;
import de.interseroh.report.server.birt.BirtReportService;
import org.eclipse.birt.report.engine.api.EngineException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.wsdl.extensions.mime.MIMEContent;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author <NAME> (Crowdcode)
 */
@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private BirtReportService reportService;

    @RequestMapping(value="/api/{reportName}", method = RequestMethod.GET)
    public void generateReport(@PathVariable("reportName") String reportName, HttpServletRequest request, HttpServletResponse response) throws IOException, BirtReportException {
        response.setContentType("text/html; charset=UTF-8");
        String reportFileName = "/reports/" + reportName + ".rptdesign";
        reportService.renderHtmlReport(reportFileName, new HashMap<String, Object>(), response.getOutputStream());
    }

    @RequestMapping(value="/{reportName}", method = RequestMethod.GET)
    public ModelAndView reportView(@PathVariable("reportName") String reportName, HttpServletRequest request, HttpServletResponse reponse) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/secured/report");
        modelAndView.addObject("reportUrl", "/api/"+reportName);
        return modelAndView;
    }
}<commit_msg>Added REST-API to render reports in separate formats.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 * (c) 2015 - Interseroh
 */
package de.interseroh.report.server.controller;

import de.interseroh.report.server.birt.BirtOutputFormat;
import de.interseroh.report.server.birt.BirtReportException;
import de.interseroh.report.server.birt.BirtReportService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author <NAME> (Crowdcode)
 */
@Controller
@RequestMapping("/report")
public class ReportController {

    private static final Logger logger = Logger.getLogger(ReportController.class);

    @Autowired
    private BirtReportService reportService;

    @RequestMapping(value = "/api/{reportName}", method = RequestMethod.GET)
    public void renderReportInDefaultFormat(@PathVariable("reportName") String reportName, @PathVariable("format") String format, HttpServletResponse response) throws IOException, BirtReportException {
        renderReport(reportName, BirtOutputFormat.HTML5.getFormatName(), response);
    }

    @RequestMapping(value = "/api/{reportName}/{format}", method = RequestMethod.GET)
    public void renderReport(@PathVariable("reportName") String reportName, @PathVariable("format") String format, HttpServletResponse response) throws IOException, BirtReportException {
        logger.debug("Rendering " + reportName + " in " + format + ".");
        BirtOutputFormat outputFormat = BirtOutputFormat.from(format);
        response.setContentType(outputFormat.getContentType());

        // TODO idueppe - need configurable folder
        String reportFileName = "/reports/" + reportName + ".rptdesign";

        HashMap<String, Object> parameters = new HashMap<String, Object>();

        switch (outputFormat) {
            case HTML5:
                reportService.renderHtmlReport(reportFileName, parameters, response.getOutputStream());
                break;
            case PDF:
                response.setHeader("Content-disposition", "inline; filename=" + reportName + ".pdf");
                reportService.renderPDFReport(reportFileName, parameters, response.getOutputStream());
                break;
            case EXCEL2010:
                response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".xlsx");
                reportService.renderExcelReport(reportFileName, parameters, response.getOutputStream());
            case EXCEL:
                response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".xls");
                reportService.renderExcelReport(reportFileName, parameters, response.getOutputStream());
        }
        // TODO idueppe - need exception handling
    }

    @RequestMapping(value = "/{reportName}", method = RequestMethod.GET)
    public ModelAndView reportView(@PathVariable("reportName") String reportName, HttpServletRequest request, HttpServletResponse reponse) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/secured/report");
        modelAndView.addObject("reportUrl", "/api/" + reportName);
        return modelAndView;
    }
}
{'fraction_non_alphanumeric': 0.09769392033542977, 'fraction_numerical': 0.0033542976939203353, 'mean_word_length': 4.336316181953766, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6722668', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1787, 'n_words': 507}