/*
 * Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
 * 
 * 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 li.strolch.rest;

import java.text.MessageFormat;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import li.strolch.exception.StrolchAccessDeniedException;
import li.strolch.privilege.model.Restrictable;
import li.strolch.rest.model.Result;
import li.strolch.utils.helper.StringHelper;

@Provider
public class StrolchRestfulExceptionMapper implements ExceptionMapper<Exception> {

	private static final Logger logger = LoggerFactory.getLogger(StrolchRestfulExceptionMapper.class);

	@Override
	public Response toResponse(Exception ex) {

		logger.error(MessageFormat.format("Handling exception {0}", ex.getClass()), ex); //$NON-NLS-1$

		if (ex instanceof NotFoundException)
			return Response.status(Status.NOT_FOUND).entity(new Result(ex)).type(MediaType.APPLICATION_JSON).build();

		if (ex instanceof StrolchAccessDeniedException) {
			StrolchAccessDeniedException e = (StrolchAccessDeniedException) ex;
			StringBuilder sb = new StringBuilder();
			sb.append("User ");
			sb.append(e.getCertificate().getUsername());
			sb.append(" does not have access to ");
			Restrictable restrictable = e.getRestrictable();
			if (restrictable == null) {
				sb.append(StringHelper.NULL);
			} else {
				sb.append(restrictable.getPrivilegeName());
				sb.append(" - ");
				sb.append(restrictable.getPrivilegeValue());
			}

			return Response.status(Status.UNAUTHORIZED).entity(sb.toString()).type(MediaType.TEXT_PLAIN).build();
		}

		return Response.serverError().entity(new Result(ex)).type(MediaType.APPLICATION_JSON).build();
	}
}
@@ -1100,53 +1100,8 @@
 ult;
-%0Aimport li.strolch.utils.helper.StringHelper;
 %0A%0A@P
@@ -1959,24 +1959,27 @@
 null) %7B%0A%09%09%09%09
+// 
 sb.append(St
@@ -1996,16 +1996,70 @@
 .NULL);%0A
+%09%09%09%09// use the message%0A%09%09%09%09sb.append(e.getMessage());%0A
 %09%09%09%7D els

use execpetion message instead of empty text, if no restrictable is given for unauthorized access (#2)