Spring: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "Index page of Spring frame work")
 
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
Index page of Spring frame work
==Object Storage File Path==
<source lang="java">
@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);
        }
    }
}
</source>
 
==ReactJS Route==
<source lang="java">
@Configuration
public class WebConfig implements WebMvcConfigurer {
    public static final String FORWARD_INDEX_HTML = "forward:/index.html";
    public static final String[] PATH_OR_PATTERNS = {
            "/",
            "/{x:[\\w\\-]+}",
            "/{x:^(?!api$).*$}/*/{y:[\\w\\-]+}"
    };
   
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        Arrays.stream(PATH_OR_PATTERNS).forEach(path -> {
            registry.addViewController(path).setViewName(FORWARD_INDEX_HTML);
        });
    }
}
</source>
 
<source lang="java">
@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;
    }
}
</source>
 
==References==
{|
| valign="top" |
* [https://stackoverflow.com/questions/22767205/ Spring » Exclude URL Patterns From Security Config]
* [https://keepgrowing.in/tools/how-to-add-x-xsrf-token-header-to-postman-requests/ Spring » Postman » Add X-XSRF-TOKEN header]
* [https://blog.trifork.com/2018/08/28/spring-data-native-queries-and-projections-in-kotlin/ Spring » Kotlin » Native Queries & Projections]
* [https://stackoverflow.com/questions/45230898/ Spring » Path Variable with Multiple Slash]
* [https://stackoverflow.com/questions/6349421/ Spring » Jackson to de-serialize an array]
* [https://openjdk.java.net/jeps/368 Spring » New escape sequences]
* [https://www.baeldung.com/spring-template-engines Spring » Template Engines]
* [https://stackshare.io/stackups/mustache-vs-twig Spring » Mustache vs Twig]
* [https://stackoverflow.com/questions/67531284/ Spring » Ant Path Match]
* [https://spring.io/guides/tutorials/spring-boot-kotlin/ Spring » Kotlin]
 
| valign="top" |
 
| valign="top" |
 
|-
| colspan="3" |
----
|-
| valign="top" |
* [[Spring Exception Handling]]
* [[Spring Cloud OpenFeign]]
* [[Spring Security]]
* [[Apache Camel]]
* [[Netflix Eureka]]
* [[HTTP Security]]
* [[Java Lambda]]
* [[Locust]]
* [[Java]]
* [[Wrk]]
 
| valign="top" |
* [[Java Local Date Time]]
* [[Postman Script]]
* [[Java Interview]]
* [[Flask]]
 
| valign="top" |
 
|}

Latest revision as of 06:50, 9 February 2024

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

@Configuration
public class WebConfig implements WebMvcConfigurer {
    public static final String FORWARD_INDEX_HTML = "forward:/index.html";
    public static final String[] PATH_OR_PATTERNS = {
            "/",
            "/{x:[\\w\\-]+}",
            "/{x:^(?!api$).*$}/*/{y:[\\w\\-]+}"
    };
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        Arrays.stream(PATH_OR_PATTERNS).forEach(path -> {
            registry.addViewController(path).setViewName(FORWARD_INDEX_HTML);
        });
    }
}
@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