package com.krishagni.catissueplus.rest.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.krishagni.catissueplus.core.common.errors.CommonErrorCode;
import com.krishagni.catissueplus.core.common.errors.OpenSpecimenException;
import com.krishagni.catissueplus.core.common.util.AuthUtil;
import com.krishagni.catissueplus.core.common.util.ConfigUtil;
import com.krishagni.catissueplus.core.common.util.Utility;

public abstract class AbstractLabelPrinter {

	@RequestMapping(method = RequestMethod.GET, value = "/output-file")
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public void getLabelPrintFiles(
		@RequestParam(value = "jobId")
		Long jobId,

		@RequestParam(value = "filename", required = false, defaultValue = "")
		String filename,

		HttpServletResponse httpResp) {

		File printJobDir = new File(ConfigUtil.getInstance().getDataDir(), "print-jobs");
		File result      = new File(printJobDir, AuthUtil.getCurrentUser().getId() + "_" + jobId + ".csv");
		if (!result.exists()) {
			throw OpenSpecimenException.userError(CommonErrorCode.FILE_NOT_FOUND, result.getName());
		}

		filename = Utility.escapeXss(filename);
		if (StringUtils.isBlank(filename)) {
			filename = result.getName();
		}

		httpResp.setContentType("application/csv");
		httpResp.setHeader("Content-Disposition", "attachment; filename=" + filename);

		InputStream in = null;
		try {
			in = new FileInputStream(result);
			IOUtils.copy(in, httpResp.getOutputStream());
		} catch (IOException e) {
			throw OpenSpecimenException.userError(CommonErrorCode.FILE_SEND_ERROR, e.getMessage());
		} finally {
			IOUtils.closeQuietly(in);
		}
	}
}

@@ -1653,16 +1653,18 @@
 );%0A%09%09%7D%0A%0A
+//
 %09%09filena
@@ -1771,16 +1771,84 @@
 e();%0A%09%09%7D
+ else %7B%0A%09%09%09filename = filename.replaceAll(%22%5C%5Cs%7B2,%7D%22, %22%22).trim();%0A%09%09%7D
 %0A%0A%09%09http

Replace function call with inline code to replace CRLF sequences.