/* * Copyright (c) 2015 Villu Ruusmann * * This file is part of Openscoring * * Openscoring is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Openscoring is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Openscoring. If not, see <http://www.gnu.org/licenses/>. */ package org.openscoring.service; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; import org.openscoring.common.SimpleResponse; @Provider public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> { @Override public Response toResponse(WebApplicationException exception){ Response response = exception.getResponse(); Throwable throwable = exception; for(Throwable cause = throwable.getCause(); (cause != null && cause != throwable); throwable = cause){ // Empty block } String message = throwable.getMessage(); if(message == null || ("").equals(message)){ Response.Status status = (Response.Status)response.getStatusInfo(); message = status.getReasonPhrase(); } // End if // Strip the HTTP status code prefix message = message.replaceFirst("HTTP\\s(\\d)+\\s", ""); SimpleResponse entity = new SimpleResponse(); entity.setMessage(message); return (Response.fromResponse(response).entity(entity).type(MediaType.APPLICATION_JSON)).build(); } }