Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package sk.seges.acris.reporting.server.service;
  
  import java.io.File;
 import java.net.URL;
 import java.util.Date;
 import java.util.Map;
 
 
 
 import  org.gwtwidgets.server.spring.ServletUtils;
 
communication with jasper server
export of jasper reports

Author(s):
marta
 
 public class ReportingService implements IReportingService {
 
 	private static final String OUTPUT = "output";
 
 	private static final String REPORTS_DIR = "reports";
 
 	private static final long serialVersionUID = -7961507798873119425L;
 
 	private static final Logger LOG = Logger.getLogger(ReportingService.class);
 
 
 	@Qualifier("reportDescriptionService")
 
 	public ReportingService() {}
 	
 	public ReportingService(ConfigurationProvider configurationIReportDescriptionService reportDescriptionService) {
 		this. = configuration;
 		this. = reportDescriptionService;
 	}
 
 	public String exportReportToHtml(Long reportDescriptionIdMap<StringObjectparameters) {
 		ReportDescriptionData report = .findById(reportDescriptionId);
 		WSClient client = new WSClient(.getJasperServerUrl() + "/services/repository",
 		try {
 			// parameters.put(Argument.RUN_OUTPUT_IMAGES_URI, "/images");
 			JasperPrint print = client.runReport(report.getReportUrl(), parameters);
 			HttpServletRequest request = ServletUtils.getRequest();
 			HttpServletResponse response = ServletUtils.getResponse();
 
 			JRExporter exporter = null;
 			response.setContentType("text/html");
 			response.setCharacterEncoding("UTF-8");
 
 			exporter = new JRHtmlExporter();
 			StringWriter out = new StringWriter();
 			exporter.setParameter(."image?image=");
 			exporter.exportReport();
 			return out.getBuffer().toString();
 		} catch (Exception ex) {
 			.error("error in proxying request to jasperserver"ex);
 		}
 		return null;
 	}
 	
	public String exportReport(Long reportDescriptionIdString exportTypeMap<StringObjectparameters,
			String web_id) {
		String params = "";
		params += generateParamsString(parameters);
		if (exportType == null) {
			exportType = ((Stringparameters.get()).toLowerCase();
		}
		String urlPath = generateCompleteReportUrl(exportTypereportparams.getJasperServerUser(),
			.debug("Complete url = " + urlPath);
		}
		try {
			URL url = new URL(urlPath);
			URLConnection connection = url.openConnection();
			connection.setAllowUserInteraction(false);
			HttpURLConnection httpConnection = null;
			if (connection instanceof HttpURLConnection) {
				httpConnection = (HttpURLConnectionconnection;
				httpConnection.setRequestMethod("POST");
				httpConnection.setInstanceFollowRedirects(false);
			}
			connection.connect();
			InputStream proxyIn = null;
			int code = 500;
			if (httpConnection != null) {
				proxyIn = httpConnection.getErrorStream();
				code = httpConnection.getResponseCode();
				if (code != .) {
					copy(proxyInbaos);
					baos.close();
					throw new Exception("Status code from export " + code + ", content = " + baos.toString());
				}
			}
			if (proxyIn == null) {
				proxyIn = connection.getInputStream();
			}
			if (proxyIn != null) {
				String fileName = "report" + "_" + new Date().getTime() + "." + exportType.toLowerCase();
				String reportDir = ;
				String directory = .resolveRootDirectoryPath(web_id) + "/" + reportDir;
				File d = new File(directory);
				if (!d.exists() || !d.isDirectory()) {
					d.mkdir();
				}
				String filePath = directory + "/" + fileName;
					.debug("Report output = " + filePath);
				}
				File f = new File(filePath);
				copy(proxyInfos);
				fos.close();
				return reportDir + "/" + fileName;
			}
catch (Exception ex) {
			.error("error in proxying request to jasperserver"ex);
		}
		return null;
	}
	protected String generateParamsString(Map<StringObjectparameters) {
		String params = "";
		if (parameters != null) {
			for (String key : parameters.keySet()) {
				if (key != ) {
					params += "&" + key + "=" + parameters.get(key);
				}
			}
		}
		return params;
	}
	protected String generateCompleteReportUrl(String exportTypeReportDescriptionData reportString params,
			String usernameString password) {
		params += "&j_username=" + username + "&j_password=" + password;
		String parameters = "/flow.html?_flowId=viewReportFlow&ndefined=&standAlone=true&ParentFolderUri=";
		String reportUrl = report.getReportUrl();
		if (!reportUrl.startsWith("/")) {
			reportUrl = "/" + reportUrl;
		}
		String parentUrl = reportUrl.substring(0, reportUrl.lastIndexOf("/"));
		if (parentUrl.length() <= 0)
		 {
			parentUrl = "undefined";
		// reportUrl = reportUrl.substring(reportUrl.lastIndexOf("/"));
		}
		String urlPath = prefix + parameters + parentUrl + "&reportUnit=" + reportUrl;
		return urlPath + "&output=" + exportType + params;
	}

Copy all of in to out.

Parameters:
in input stream to read from
out output stream to write to
Throws:
IOException thrown if things go wrong
	private void copy(InputStream inOutputStream outthrows IOException {
		byte[] buffer = new byte[2 * 88192];
		int len = 0;
		while (true) {
			len = in.read(buffer, 0, buffer.length);
			if (len < 0) {
				break;
			}
			out.write(buffer, 0, len);
		}
	}
New to GrepCode? Check out our FAQ X