Commit 63907b2e authored by theword's avatar theword
Browse files

fix error page when download 50tawi

No related merge requests found
Showing with 35 additions and 20 deletions
+35 -20
......@@ -5,11 +5,13 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.b3.rbl.compensation.controller.BaseController;
import com.b3.rbl.compensation.service.JwtUtil;
......@@ -60,21 +62,36 @@ public class ReportRestApi extends BaseController {
}
@GetMapping("/download50tawi")
public ResponseEntity<byte[]> downloadReport50tawi(@RequestParam("downloadPath") String downloadPath,
public ResponseEntity<?> downloadReport50tawi(@RequestParam("downloadPath") String downloadPath,
@RequestParam("token") String token, @RequestParam("filename") String filename,
@RequestParam("mimeType") String mimeType) throws Exception {
@RequestParam("mimeType") String mimeType, RedirectAttributes redirectAttrs) throws Exception {
if (jwtUtil.isTokenExpired(token)) {
super.response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // HTTP 401.
return null;
}
String path = PATH + reportPath50tawi + "/" + downloadPath.replace(" ", "%20") + ":/content";
byte[] report = onedriveService.getFileFromUrl(path);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "inline; filename=" + filename);
headers.set(HttpHeaders.CONTENT_TYPE, mimeType + ";charset=utf-8");
try {
byte[] report = onedriveService.getFileFromUrl(path);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "inline; filename=" + filename);
headers.set(HttpHeaders.CONTENT_TYPE, mimeType + ";charset=utf-8");
return ResponseEntity.ok().headers(headers).body(report);
} catch (Exception ex) {
redirectAttrs.addFlashAttribute("disable_code", "");
redirectAttrs.addFlashAttribute("header", "File Not Found");
try {
redirectAttrs.addFlashAttribute("error_message",
"ไม่พบ 50 ทวิ สำหรับรหัสตัวแทน [ " + filename.split("_")[0] + " ]");
} catch (Exception e) {
redirectAttrs.addFlashAttribute("error_message", "ไม่พบ 50 ทวิ");
}
HttpHeaders headers = new HttpHeaders();
headers.add("location", "404.html");
return new ResponseEntity<String>(headers, HttpStatus.FOUND);
}
return ResponseEntity.ok().headers(headers).body(report);
}
}
......@@ -88,7 +88,7 @@
<h3>Unauthorized</h3>
<h2>You are not authorized to access this page!</h2>
<!-- use window.history.back(); to go back -->
<a href="./"><button>Go Back</button></a>
<!-- <a href="./"><button>Go Back</button></a> -->
</div>
<scipt>
......
<!DOCTYPE html>
<html lang="th">
<html
lang="th"
>
<head>
<title>Error page</title>
<meta charset="utf-8" />
......@@ -82,16 +83,13 @@
}
</style>
</head>
<body>
<body>
<div class="message">
<h1>404</h1>
<h3>Page Not Found</h3>
<h2>The page you are looking for not avaible!</h2>
<h1 th:text="${disable_code}?: '404'">404</h1>
<h3 th:text="${header} ?: 'Page Not Found'" >Page Not Found</h3>
<h2 th:text="${error_message} ?: 'The page you are looking for not avaible!'">The page you are looking for not avaible!</h2>
<!-- use window.history.back(); to go back -->
<a href="./"><button>Go Back</button></a>
<!-- <a href="./"><button>Go Back</button></a> -->
</div>
<scipt>
</scipt>
</body>
</html>
......@@ -88,7 +88,7 @@
<h3>Internal Server Error</h3>
<h2>An error occurred and your request couldn’t be completed. Please try again.</h2>
<!-- use window.history.back(); to go back -->
<button onclick="window.history.back()">Go Back</button>
<!-- <button onclick="window.history.back()">Go Back</button> -->
</div>
<scipt>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment