/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * 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 org.usergrid.rest.exceptions;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.OK;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.usergrid.rest.utils.JSONPUtils.isJavascript;
import static org.usergrid.rest.utils.JSONPUtils.wrapJSONPResponse;
import static org.usergrid.utils.JsonUtils.mapToJsonString;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;

import org.usergrid.rest.ApiResponse;

public abstract class AbstractExceptionMapper<E extends java.lang.Throwable>
		implements ExceptionMapper<E> {

	@Context
	HttpHeaders hh;

	@Context
	protected HttpServletRequest httpServletRequest;

	public boolean isJSONP() {
		return isJavascript(hh.getAcceptableMediaTypes());
	}

	@Override
	public Response toResponse(E e) {
		return toResponse(BAD_REQUEST, e);
	}

	public Response toResponse(Status status, E e) {
		ApiResponse response = new ApiResponse();
		AuthErrorInfo authError = AuthErrorInfo.getForException(e);
		if (authError != null) {
			response.setError(authError.getType(), authError.getMessage(), e);
		} else {
			response.setError(e);
		}
		String jsonResponse = mapToJsonString(response);
		return toResponse(status, jsonResponse);
	}

	public Response toResponse(Status status, String jsonResponse) {
		String callback = httpServletRequest.getParameter("callback");
		if (isJSONP() && isNotBlank(callback)) {
			jsonResponse = wrapJSONPResponse(callback, jsonResponse);
			return Response.status(OK).type("application/javascript")
					.entity(jsonResponse).build();
		} else {
			return Response.status(status).type(APPLICATION_JSON_TYPE)
					.entity(jsonResponse).build();

		}
	}

}

@@ -1451,16 +1451,73 @@
 apper;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -1546,16 +1546,16 @@
 sponse;%0A
-
 %0Apublic 
@@ -1659,16 +1659,138 @@
 r%3CE%3E %7B%0A%0A
+%09public static final Logger logger = LoggerFactory%0A%09%09%09.getLogger(AbstractExceptionMapper.class.getPackage().toString());%0A%0A
 %09@Contex
@@ -2092,16 +2092,72 @@
  E e) %7B%0A
+%09%09logger.error(%22Error in request (%22 + status + %22)%22, e);%0A
 %09%09ApiRes
@@ -2481,32 +2481,32 @@
 nResponse);%0A%09%7D%0A%0A
-
 %09public Response
@@ -2555,16 +2555,87 @@
 onse) %7B%0A
+%09%09logger.error(%22Error in request (%22 + status + %22):%5Cn%22 + jsonResponse);%0A
 %09%09String

add error logging to AbstractExceptionMapper