@RequestMapping(value = "/documents/**", method = RequestMethod.GET)
public ResponseEntity<byte[]> customerDetails(final HttpServletRequest request, final Model model) throws Exception {
final RequestData requestData = requestUtil.getRequestData(request);
final String requestURL = request.getRequestURL().toString();
final Customer customer = requestData.getCustomer();
if (customer != null) {
final List<OrderCustomer> orders = orderCustomerService.findOrdersByCustomerId(customer.getId().toString());
for (Iterator<OrderCustomer> iterator = orders.iterator(); iterator.hasNext();) {
OrderCustomer order = (OrderCustomer) iterator.next();
if(requestURL.contains(order.getPrefixHashFolder())){
String filename = null;
String filePath = null;
if(requestURL.contains(OrderDocumentType.ORDER_CONFIRMATION.getPropertyKey())){
filename = documentService.buildOrderConfirmationFileName(order);
filePath = documentService.getOrderConfirmationFilePath(order);
} else if(requestURL.contains(OrderDocumentType.SHIPPING_CONFIRMATION.getPropertyKey())){
filename = documentService.buildShippingConfirmationFileName(order);
filePath = documentService.getShippingConfirmationFilePath(order);
} else if(requestURL.contains(OrderDocumentType.INVOICE.getPropertyKey())){
filename = documentService.buildInvoiceFileName(order);
filePath = documentService.getInvoiceFilePath(order);
}
if(StringUtils.isNotEmpty(filename)
&& StringUtils.isNotEmpty(filePath)){
Path path = Paths.get(filePath);
byte[] contents = Files.readAllBytes(path);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
return response;
}
}
}
logger.warn("This request can't be display, customer " + customer.getEmail() + " is logged, but the Hash doesn't matched:" + requestURL);
} else {
logger.warn("This request can't be display, customer is not logged:" + requestURL);
}
return null;
}