// 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;
/**
 * Copyright gradecak.com

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gradecak.alfresco.mvc.rest;




/**
 * 
 * class used to process the response with alfresco rest API behavior only if
 * the anootaion {@link AlfrescoRestResponse} is used
 */
@ControllerAdvice
public class AlfrescoApiResponseInterceptor implements ResponseBodyAdvice<Object> {

	private final ResourceWebScriptHelper webscriptHelper;
	private final boolean globalAlfrescoResponse;;

	public AlfrescoApiResponseInterceptor(final ResourceWebScriptHelper webscriptHelper) {
		this(webscriptHelper, false);
	}

	public AlfrescoApiResponseInterceptor(final ResourceWebScriptHelper webscriptHelper,
			final boolean globalAlfrescoResponse) {
		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);
// 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;

/**
 * Copyright gradecak.com

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gradecak.alfresco.mvc.rest;




/**
 * 
 * class used to process the response with alfresco rest API behavior only if
 * the anootaion {@link AlfrescoRestResponse} is used
 */
@ControllerAdvice
public class AlfrescoApiResponseInterceptor implements ResponseBodyAdvice<Object> {

	private final ResourceWebScriptHelper webscriptHelper;
	private final boolean globalAlfrescoResponse;;

	public AlfrescoApiResponseInterceptor(final ResourceWebScriptHelper webscriptHelper) {
		this(webscriptHelper, false);
	}

	public AlfrescoApiResponseInterceptor(final ResourceWebScriptHelper webscriptHelper,
			final boolean globalAlfrescoResponse) {
		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) {