Example usage for org.springframework.web.util UriComponentsBuilder replacePath

List of usage examples for org.springframework.web.util UriComponentsBuilder replacePath

Introduction

In this page you can find the example usage for org.springframework.web.util UriComponentsBuilder replacePath.

Prototype

@Override
public UriComponentsBuilder replacePath(@Nullable String path) 

Source Link

Document

Set the path of this builder overriding all existing path and path segment values.

Usage

From source file:am.ik.categolj2.app.authentication.AuthenticationController.java

@RequestMapping(value = "login", method = RequestMethod.POST)
String login(@RequestParam("username") String username, @RequestParam("password") String password,
        UriComponentsBuilder builder, RedirectAttributes attributes, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    logger.info("attempt to login (username={})", username);
    String tokenEndpoint = builder.path("oauth/token").build().toUriString();
    HttpEntity<MultiValueMap<String, Object>> ropRequest = authenticationHelper.createRopRequest(username,
            password);/*from w  w  w.j  a v a2s  .c  o  m*/
    try {
        ResponseEntity<OAuth2AccessToken> result = restTemplate.postForEntity(tokenEndpoint, ropRequest,
                OAuth2AccessToken.class);
        OAuth2AccessToken accessToken = result.getBody();
        authenticationHelper.saveAccessTokenInCookie(accessToken, response);
        authenticationHelper.writeLoginHistory(accessToken, request, response);
    } catch (HttpStatusCodeException e) {
        authenticationHelper.handleHttpStatusCodeException(e, attributes);
        return "redirect:/login";
    } catch (ResourceAccessException e) {
        // I/O error on POST request for "https://xxxx:8080/oauth/token":Unrecognized SSL message, plaintext connection?
        if (e.getCause() instanceof SSLException) {
            // fallback to another port
            UriComponentsBuilder b = builder.replacePath("").port(httpsPort);
            return login(username, password, b, attributes, request, response);
        } else {
            throw e;
        }
    }
    return "redirect:/admin";
}