package nl.surfnet.coin.selfservice.api.rest;


import nl.surfnet.coin.selfservice.domain.CoinUser;
import nl.surfnet.coin.selfservice.util.SpringSecurity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HttpServletBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static nl.surfnet.coin.selfservice.api.rest.Constants.HTTP_X_IDP_ENTITY_ID;

@Controller
@RequestMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UsersController extends BaseController {

  @RequestMapping("/me")
  public ResponseEntity<RestResponse> me(HttpServletRequest request) {
    return new ResponseEntity(this.createRestResponse(SpringSecurity.getCurrentUser()), HttpStatus.OK);
  }

  @RequestMapping("/me/switch-to-idp/{switchToIdp}")
  public ResponseEntity currentIdp(@PathVariable("switchToIdp") String switchToIdp, HttpServletResponse response) {
    SpringSecurity.setCurrentIdp(switchToIdp);
    response.setHeader(HTTP_X_IDP_ENTITY_ID, switchToIdp);
    return new ResponseEntity(HttpStatus.OK);
  }


}

@@ -145,24 +145,92 @@
 ngSecurity;%0A
+import org.owasp.esapi.waf.internal.InterceptingHTTPServletRequest;%0A
 import org.s
@@ -261,16 +261,16 @@
 Status;%0A
-
 import o
@@ -526,16 +526,71 @@
 apping;%0A
+import org.springframework.web.servlet.HandlerMapping;%0A
 import o
@@ -1208,21 +1208,10 @@
 idp/
-%7BswitchToIdp%7D
+**
 %22)%0A 
@@ -1248,88 +1248,243 @@
 Idp(
-@PathVariable(%22switchToIdp%22) String switchToIdp, HttpServletResponse response) %7B
+HttpServletResponse response, HttpServletRequest request) %7B%0A    String path = (String)request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);%0A    String switchToIdp = path.replaceAll(%22/users/me/switch-to-idp/%22, %22%22);
 %0A   

Fix issue with the `switch-to-idp` controller mapping