// Path: alfresco-mvc-rest/src/main/java/com/gradecak/alfresco/mvc/webscript/DispatcherWebscript.java
// public class WebscriptRequestWrapper extends HttpServletRequestWrapper {
// 
// 	private WebScriptServletRequest origReq;
// 
// 	public WebscriptRequestWrapper(WebScriptServletRequest request) {
// 		super(request.getHttpServletRequest());
// 		this.origReq = request;
// 	}
// 
// 	@Override
// 	public String getRequestURI() {
// 		String uri = super.getRequestURI();
// 		if (uri.contains("$")) {
// 			uri = uri.replaceAll("\\$", "%24");
// 		}
// 
// 		String origUri = origReq.getExtensionPath();
// 		if (origUri.contains("$")) {
// 			origUri = origUri.replaceAll("\\$", "%24");
// 		}
// 
// 		Pattern pattern = Pattern.compile("(^" + origReq.getServiceContextPath() + "/)(.*)(/" + origUri + ")");
// 		Matcher matcher = pattern.matcher(uri);
// 
// 		if (matcher.find()) {
// 			try {
// 				return matcher.group(EXTENSION_PATH_REGEXP_GROUP_INDEX);
// 			} catch (Exception e) {
// 				// let an empty string be returned
// 				LOGGER.warn("no such group (3) in regexp while URI evaluation", e);
// 			}
// 		}
// 
// 		return "";
// 	}
// 
// 	public String getContextPath() {
// 		return origReq.getContextPath();
// 	}
// 
// 	public String getServletPath() {
// 		return "";
// 	}
// 
// 	public WebScriptServletRequest getWebScriptServletRequest() {
// 		return origReq;
// 	}
// }


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.gradecak.alfresco.mvc.rest.annotation.AlfrescoRestResponse;
import com.gradecak.alfresco.mvc.webscript.DispatcherWebscript.WebscriptRequestWrapper;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.springframework.core.MethodParameter;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.servlet.WebScriptServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
		this.webscriptHelper = webscriptHelper;
		this.globalAlfrescoResponse = globalAlfrescoResponse;
	}

	@Override
	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {

		boolean useAlfrescoResponse = globalAlfrescoResponse;

		if (!useAlfrescoResponse) {
			AlfrescoRestResponse methodAnnotation = returnType.getMethodAnnotation(AlfrescoRestResponse.class);
			if (methodAnnotation == null) {
				methodAnnotation = returnType.getContainingClass().getAnnotation(AlfrescoRestResponse.class);
			}

			if (methodAnnotation != null) {
				useAlfrescoResponse = true;
			}
		}

		if (useAlfrescoResponse) {
			if (!(request instanceof ServletServerHttpRequest)) {
				throw new RuntimeException(
						"the request must be an instance of org.springframework.http.server.ServletServerHttpRequest");
			}

			HttpServletRequest r = ((ServletServerHttpRequest) request).getServletRequest();

// Path: alfresco-mvc-rest/src/main/java/com/gradecak/alfresco/mvc/webscript/DispatcherWebscript.java
// public class WebscriptRequestWrapper extends HttpServletRequestWrapper {
// 
// 	private WebScriptServletRequest origReq;
// 
// 	public WebscriptRequestWrapper(WebScriptServletRequest request) {
// 		super(request.getHttpServletRequest());
// 		this.origReq = request;
// 	}
// 
// 	@Override
// 	public String getRequestURI() {
// 		String uri = super.getRequestURI();
// 		if (uri.contains("$")) {
// 			uri = uri.replaceAll("\\$", "%24");
// 		}
// 
// 		String origUri = origReq.getExtensionPath();
// 		if (origUri.contains("$")) {
// 			origUri = origUri.replaceAll("\\$", "%24");
// 		}
// 
// 		Pattern pattern = Pattern.compile("(^" + origReq.getServiceContextPath() + "/)(.*)(/" + origUri + ")");
// 		Matcher matcher = pattern.matcher(uri);
// 
// 		if (matcher.find()) {
// 			try {
// 				return matcher.group(EXTENSION_PATH_REGEXP_GROUP_INDEX);
// 			} catch (Exception e) {
// 				// let an empty string be returned
// 				LOGGER.warn("no such group (3) in regexp while URI evaluation", e);
// 			}
// 		}
// 
// 		return "";
// 	}
// 
// 	public String getContextPath() {
// 		return origReq.getContextPath();
// 	}
// 
// 	public String getServletPath() {
// 		return "";
// 	}
// 
// 	public WebScriptServletRequest getWebScriptServletRequest() {
// 		return origReq;
// 	}
// }


// Path: alfresco-mvc-rest/src/main/java/com/gradecak/alfresco/mvc/rest/AlfrescoApiResponseInterceptor.java
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.gradecak.alfresco.mvc.rest.annotation.AlfrescoRestResponse;
import com.gradecak.alfresco.mvc.webscript.DispatcherWebscript.WebscriptRequestWrapper;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.springframework.core.MethodParameter;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.servlet.WebScriptServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;

		this.webscriptHelper = webscriptHelper;
		this.globalAlfrescoResponse = globalAlfrescoResponse;
	}

	@Override
	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {

		boolean useAlfrescoResponse = globalAlfrescoResponse;

		if (!useAlfrescoResponse) {
			AlfrescoRestResponse methodAnnotation = returnType.getMethodAnnotation(AlfrescoRestResponse.class);
			if (methodAnnotation == null) {
				methodAnnotation = returnType.getContainingClass().getAnnotation(AlfrescoRestResponse.class);
			}

			if (methodAnnotation != null) {
				useAlfrescoResponse = true;
			}
		}

		if (useAlfrescoResponse) {
			if (!(request instanceof ServletServerHttpRequest)) {
				throw new RuntimeException(
						"the request must be an instance of org.springframework.http.server.ServletServerHttpRequest");
			}

			HttpServletRequest r = ((ServletServerHttpRequest) request).getServletRequest();

			if (!(r instanceof WebscriptRequestWrapper)) {