com.xlrit.gears.plugin.scim.ScimController Maven / Gradle / Ivy
package com.xlrit.gears.plugin.scim;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import de.captaingoldfish.scim.sdk.common.constants.HttpHeader;
import de.captaingoldfish.scim.sdk.common.constants.enums.HttpMethod;
import de.captaingoldfish.scim.sdk.common.response.ScimResponse;
import de.captaingoldfish.scim.sdk.server.endpoints.Context;
import de.captaingoldfish.scim.sdk.server.endpoints.ResourceEndpoint;
import de.captaingoldfish.scim.sdk.server.endpoints.authorize.Authorization;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/scim2")
@RequiredArgsConstructor
public class ScimController {
private static final Logger LOG = LoggerFactory.getLogger(ScimController.class);
private final ResourceEndpoint resourceEndpoint;
private final Authorization authorization;
private final ScimProperties properties;
/**
* The rest-endpoint for SCIM that is accessible under the path ${basepath}/scim2/**
*
* @param request the request object created by the underlying tomcat
* @param requestBody the request body
* @return the scim response that will automatically be converted to json by spring
*/
@RequestMapping(
value = "/**",
method = {RequestMethod.POST, RequestMethod.GET, RequestMethod.PUT, RequestMethod.PATCH, RequestMethod.DELETE},
produces = HttpHeader.SCIM_CONTENT_TYPE)
@ResponseBody
@Transactional
public String handleScimRequest(HttpServletRequest request,
HttpServletResponse response,
@RequestBody(required = false) String requestBody) {
HttpMethod method = HttpMethod.valueOf(request.getMethod());
String query = request.getQueryString() == null ? "" : "?" + request.getQueryString();
String urlWithQuery = request.getRequestURL().toString() + query;
Map httpHeaders = getHttpHeaders(request);
LOG.info("handleScimRequest: method={}, url={}, headers={}", request.getMethod(), urlWithQuery, httpHeaders);
Context context = Context.builder()
.authorization(authorization)
.build();
ScimResponse scimResponse = resourceEndpoint.handleRequest(urlWithQuery, method, requestBody, httpHeaders, context);
response.setContentType(HttpHeader.SCIM_CONTENT_TYPE);
scimResponse.getHttpHeaders().forEach(response::setHeader);
response.setStatus(scimResponse.getHttpStatus());
return scimResponse.toPrettyString();
}
private Map getHttpHeaders(HttpServletRequest request) {
Map httpHeaders = new HashMap<>();
Enumeration enumeration = request.getHeaderNames();
while (enumeration != null && enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
httpHeaders.put(headerName, request.getHeader(headerName));
}
return httpHeaders;
}
}