Spring: Difference between revisions
Jump to navigation
Jump to search
Line 43: | Line 43: | ||
public static final String FORWARD_INDEX_HTML = "forward:/index.html"; | public static final String FORWARD_INDEX_HTML = "forward:/index.html"; | ||
/* | |||
@RequestMapping( | @RequestMapping( | ||
value = { | value = { | ||
Line 54: | Line 55: | ||
method = {RequestMethod.GET, RequestMethod.POST} | method = {RequestMethod.GET, RequestMethod.POST} | ||
) | ) | ||
public ModelAndView index() throws Exception { | public ModelAndView index( | ||
final HttpServletRequest request, | |||
@PathVariable Optional<String> lang, | |||
@PathVariable Optional<String> route | |||
) throws Exception { | |||
return new ModelAndView(FORWARD_INDEX_HTML); | return new ModelAndView(FORWARD_INDEX_HTML); | ||
}*/ | |||
@ResponseBody | |||
@RequestMapping( | |||
value = { | |||
"/", | |||
"/**", | |||
"/{lang:en|my}", | |||
"/{lang:en|my}/**", | |||
"/{route:^(?!api$).*$}", | |||
"/{route:^(?!api$).*$}/**" | |||
}, | |||
method = {RequestMethod.GET, RequestMethod.POST} | |||
) | |||
public Map<String, String> index( | |||
final HttpServletRequest request, | |||
@PathVariable Optional<String> lang, | |||
@PathVariable Optional<String> route | |||
) { | |||
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); | |||
Map<String, String> response = new HashMap<>(); | |||
response.put("route", route.orElse("")); | |||
response.put("lang", lang.orElse("")); | |||
response.put("path", path); | |||
return response; | |||
} | } | ||
} | } |
Revision as of 10:51, 24 March 2023
Object Storage File Path
@RestController
@RequestMapping("/api/file")
public class FileController {
@Autowired
FileService fileService;
/**
* File download requests handler
*
* @param prefix of relative file path
* <ul>
* <li>certificates/Policy-8691-191939b9-526b-4933-b05c-61e672609dcc.pdf</li>
* <li>certificates/extras/Policy_Wording.pdf</li>
* </ul>
*/
@GetMapping("/{prefix}/**")
public void fetch(
@PathVariable("prefix") final String prefix,
final HttpServletResponse response,
final HttpServletRequest request
) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String name = (!ObjectUtils.isEmpty(prefix)) ? path.substring(path.indexOf(prefix)) : null;
try {
if (!ObjectUtils.isEmpty(name)) {
InputStream inputStream = fileService.fetch(name);
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
} else throw new FileNotFoundException(name);
} catch (IOException e) {
throw new InternalServerException(e.getMessage(), e);
}
}
}
ReactJS Route
@Controller
public class HomeController {
public static final String FORWARD_INDEX_HTML = "forward:/index.html";
/*
@RequestMapping(
value = {
"/",
"/**",
"/{lang:en|my}",
"/{lang:en|my}/**",
"/{route:^(?!api$).*$}",
"/{route:^(?!api$).*$}/**"
},
method = {RequestMethod.GET, RequestMethod.POST}
)
public ModelAndView index(
final HttpServletRequest request,
@PathVariable Optional<String> lang,
@PathVariable Optional<String> route
) throws Exception {
return new ModelAndView(FORWARD_INDEX_HTML);
}*/
@ResponseBody
@RequestMapping(
value = {
"/",
"/**",
"/{lang:en|my}",
"/{lang:en|my}/**",
"/{route:^(?!api$).*$}",
"/{route:^(?!api$).*$}/**"
},
method = {RequestMethod.GET, RequestMethod.POST}
)
public Map<String, String> index(
final HttpServletRequest request,
@PathVariable Optional<String> lang,
@PathVariable Optional<String> route
) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
Map<String, String> response = new HashMap<>();
response.put("route", route.orElse(""));
response.put("lang", lang.orElse(""));
response.put("path", path);
return response;
}
}
References
| ||