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.fasterxml.jackson.core.JsonParseException; 021import com.fasterxml.jackson.core.JsonProcessingException; 022import com.fasterxml.jackson.databind.JsonMappingException; 023import com.unboundid.scim2.common.exceptions.BadRequestException; 024import com.unboundid.scim2.common.exceptions.ScimException; 025import com.unboundid.scim2.common.exceptions.ServerErrorException; 026import com.unboundid.scim2.common.messages.ErrorResponse; 027import com.unboundid.scim2.server.utils.ServerUtils; 028 029import javax.ws.rs.core.Context; 030import javax.ws.rs.core.HttpHeaders; 031import javax.ws.rs.core.Request; 032import javax.ws.rs.core.Response; 033import javax.ws.rs.ext.ExceptionMapper; 034import javax.ws.rs.ext.Provider; 035 036/** 037 * A JAX-RS ExceptionMapper for to convert Jackson JsonProcessingException to 038 * SCIM ErrorResponses. 039 */ 040@Provider 041public class JsonProcessingExceptionMapper implements 042 ExceptionMapper<JsonProcessingException> 043{ 044 @Context 045 private Request request; 046 @Context 047 private HttpHeaders headers; 048 049 /** 050 * {@inheritDoc} 051 */ 052 public Response toResponse(final JsonProcessingException exception) 053 { 054 ErrorResponse errorResponse; 055 if((exception instanceof JsonParseException) || 056 (exception instanceof JsonMappingException)) 057 { 058 StringBuilder builder = new StringBuilder(); 059 builder.append("Unable to parse request: "); 060 builder.append(exception.getOriginalMessage()); 061 if(exception.getLocation() != null) 062 { 063 builder.append(" at line: "); 064 builder.append(exception.getLocation().getLineNr()); 065 builder.append(", column: "); 066 builder.append(exception.getLocation().getColumnNr()); 067 } 068 errorResponse = 069 BadRequestException.invalidSyntax(builder.toString()).getScimError(); 070 } 071 else 072 { 073 if(exception.getCause() != null && 074 exception.getCause() instanceof ScimException) 075 { 076 errorResponse = ((ScimException) exception.getCause()).getScimError(); 077 } 078 else 079 { 080 errorResponse = 081 new ServerErrorException(exception.getMessage()).getScimError(); 082 } 083 } 084 085 return ServerUtils.setAcceptableType( 086 Response.status(errorResponse.getStatus()).entity(errorResponse), 087 headers.getAcceptableMediaTypes()).build(); 088 } 089}