/*
 * 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 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 java.text.MessageFormat;

import li.strolch.exception.StrolchAccessDeniedException;
import li.strolch.privilege.model.Restrictable;
import li.strolch.rest.helper.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@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 ResponseUtil.toResponse(Status.NOT_FOUND, ex);

		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);
				// use the message
				sb.append(e.getMessage());
			} 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 ResponseUtil.toResponse(ex);
	}
}
@@ -52,18 +52,17 @@
 t.ch%3E%0A *
- 
 %0A
+
  * Licen
@@ -233,17 +233,16 @@
 se at%0A *
- 
 %0A *     
@@ -286,17 +286,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -946,24 +946,86 @@
 dException;%0A
+import li.strolch.exception.StrolchNotAuthenticatedException;%0A
 import li.st
@@ -2199,16 +2199,358 @@
 ;%0A%09%09%09%7D%0A%0A
+%09%09%09return Response.status(Status.FORBIDDEN).entity(sb.toString()).type(MediaType.TEXT_PLAIN).build();%0A%0A%09%09%7D else if (ex instanceof StrolchNotAuthenticatedException) %7B%0A%09%09%09StrolchNotAuthenticatedException e = (StrolchNotAuthenticatedException) ex;%0A%09%09%09logger.error(%22User tried to access resource, but was not authenticated: %22 + ex.getMessage());%0A
 %09%09%09retur
@@ -2591,35 +2591,36 @@
 ZED).entity(
-sb.toString
+e.getMessage
 ()).type(Med

Return 403 instead of 401 for StrolchAccessDeniedException