001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.isis.viewer.restfulobjects.rendering;
020
021import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
022import org.apache.isis.viewer.restfulobjects.applib.RestfulResponse.HttpStatusCode;
023
024public class RestfulObjectsApplicationException
025extends RuntimeException
026implements ExceptionWithHttpStatusCode, ExceptionWithBody {
027
028    private static final long serialVersionUID = 1L;
029
030    public static final RestfulObjectsApplicationException create(final HttpStatusCode httpStatusCode) {
031        return createWithCause(httpStatusCode, null);
032    }
033
034    public static RestfulObjectsApplicationException createWithMessage(
035            final HttpStatusCode httpStatusCode,
036            final String message, final Object... args) {
037        return createWithCauseAndMessage(httpStatusCode, null, message, args);
038    }
039
040    public static RestfulObjectsApplicationException createWithCause(
041            final HttpStatusCode httpStatusCode,
042            final Exception cause) {
043        return createWithCauseAndMessage(httpStatusCode, cause, null);
044    }
045
046    public static RestfulObjectsApplicationException createWithCauseAndMessage(
047            final HttpStatusCode httpStatusCode,
048            final Exception cause,
049            final String message, final Object... args) {
050        return new RestfulObjectsApplicationException(httpStatusCode, formatString(message, args), cause, null);
051    }
052
053    public static RestfulObjectsApplicationException createWithBody(
054            final HttpStatusCode httpStatusCode,
055            final JsonRepresentation body,
056            final String message, final Object... args) {
057        return new RestfulObjectsApplicationException(httpStatusCode, formatString(message, args), null, body);
058    }
059
060    private static String formatString(final String formatStr, final Object... args) {
061        return formatStr != null ? String.format(formatStr, args) : null;
062    }
063
064    private final HttpStatusCode httpStatusCode;
065    private final JsonRepresentation body;
066
067    protected RestfulObjectsApplicationException(
068            final HttpStatusCode httpStatusCode,
069            final String message,
070            final Throwable cause,
071            final JsonRepresentation body) {
072        super(message, cause);
073        this.httpStatusCode = httpStatusCode;
074        this.body = body;
075    }
076
077    @Override
078    public HttpStatusCode getHttpStatusCode() {
079        return httpStatusCode;
080    }
081
082    @Override
083    public JsonRepresentation getBody() {
084        return body;
085    }
086
087}