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.server;
020
021import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
022import org.apache.isis.viewer.restfulobjects.applib.client.RestfulResponse.HttpStatusCode;
023
024public class RestfulObjectsApplicationException extends RuntimeException implements HasHttpStatusCode {
025
026    public static final RestfulObjectsApplicationException create(final HttpStatusCode httpStatusCode) {
027        return createWithCause(httpStatusCode, null);
028    }
029
030    public static RestfulObjectsApplicationException createWithMessage(final HttpStatusCode httpStatusCode, final String message, final Object... args) {
031        return createWithCauseAndMessage(httpStatusCode, (Exception) null, message, args);
032    }
033
034    public static RestfulObjectsApplicationException createWithCause(final HttpStatusCode httpStatusCode, final Exception cause) {
035        return createWithCauseAndMessage(httpStatusCode, cause, null);
036    }
037
038    public static RestfulObjectsApplicationException createWithCauseAndMessage(final HttpStatusCode httpStatusCode, final Exception cause, final String message, final Object... args) {
039        return new RestfulObjectsApplicationException(httpStatusCode, formatString(message, args), cause, null);
040    }
041
042    public static RestfulObjectsApplicationException createWithBody(final HttpStatusCode httpStatusCode, final JsonRepresentation body, final String message, final Object... args) {
043        return new RestfulObjectsApplicationException(httpStatusCode, formatString(message, args), null, body);
044    }
045
046    private static String formatString(final String formatStr, final Object... args) {
047        return formatStr != null ? String.format(formatStr, args) : null;
048    }
049
050    private static final long serialVersionUID = 1L;
051    private final HttpStatusCode httpStatusCode;
052    private final JsonRepresentation body;
053
054    private RestfulObjectsApplicationException(final HttpStatusCode httpStatusCode, final String message, final Throwable cause, final JsonRepresentation body) {
055        super(message, cause);
056        this.httpStatusCode = httpStatusCode;
057        this.body = body;
058    }
059
060    @Override
061    public HttpStatusCode getHttpStatusCode() {
062        return httpStatusCode;
063    }
064
065    public JsonRepresentation getBody() {
066        return body;
067    }
068
069}