001/* 002 * Copyright 2015-2018 Ping Identity Corporation 003 * 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of the GNU General Public License (GPLv2 only) 006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 007 * as published by the Free Software Foundation. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, see <http://www.gnu.org/licenses>. 016 */ 017 018package com.unboundid.scim2.server.providers; 019 020import com.unboundid.scim2.common.exceptions.BadRequestException; 021import com.unboundid.scim2.common.messages.ErrorResponse; 022import com.unboundid.scim2.server.utils.ServerUtils; 023 024import javax.ws.rs.WebApplicationException; 025import javax.ws.rs.core.Context; 026import javax.ws.rs.core.HttpHeaders; 027import javax.ws.rs.core.NoContentException; 028import javax.ws.rs.core.Request; 029import javax.ws.rs.core.Response; 030import javax.ws.rs.ext.ExceptionMapper; 031import javax.ws.rs.ext.Provider; 032 033/** 034 * A JAX-RS ExceptionMapper for to convert standard WebApplicationExceptions to 035 * SCIM ErrorResponses. 036 */ 037@Provider 038public class RuntimeExceptionMapper implements 039 ExceptionMapper<RuntimeException> 040{ 041 @Context 042 private Request request; 043 @Context 044 private HttpHeaders headers; 045 046 /** 047 * {@inheritDoc} 048 */ 049 public Response toResponse(final RuntimeException exception) 050 { 051 ErrorResponse errorResponse; 052 053 if(exception instanceof WebApplicationException) 054 { 055 if(exception.getCause() != null && exception.getCause() 056 instanceof NoContentException) 057 { 058 errorResponse = new ErrorResponse(400); 059 errorResponse.setScimType(BadRequestException.INVALID_SYNTAX); 060 errorResponse.setDetail("No content provided. A valid SCIM object " + 061 "represented as a json document is required"); 062 } 063 else 064 { 065 errorResponse = new ErrorResponse( 066 ((WebApplicationException) exception).getResponse().getStatus()); 067 errorResponse.setDetail(exception.getMessage()); 068 } 069 } 070 else 071 { 072 errorResponse = new ErrorResponse(500); 073 errorResponse.setDetail(exception.toString()); 074 } 075 076 return ServerUtils.setAcceptableType( 077 Response.status(errorResponse.getStatus()).entity(errorResponse), 078 headers.getAcceptableMediaTypes()).build(); 079 } 080}