com.carlomicieli.jtrains.infrastructure.web.Responses.java Source code

Java tutorial

Introduction

Here is the source code for com.carlomicieli.jtrains.infrastructure.web.Responses.java

Source

/*
 * Copyright 2014 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the 'License');
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an 'AS IS' BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.carlomicieli.jtrains.infrastructure.web;

import org.springframework.hateoas.Link;
import org.springframework.hateoas.VndErrors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * @author Carlo Micieli
 */
public interface Responses {

    public static <E extends Exception> ResponseEntity<VndErrors> error(E error, HttpStatus httpStatus,
            String logref) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        return new ResponseEntity<>(new VndErrors(logref, error.getMessage()), httpHeaders, httpStatus);
    }

    public static ResponseEntity<VndErrors> notFound(String logref) {
        return new ResponseEntity<>(new VndErrors(logref, "Resource not found"), HttpStatus.NOT_FOUND);
    }

    public static <T> ResponseEntity<T> ok(T body) {
        return new ResponseEntity<>(body, HttpStatus.OK);
    }

    public static ResponseEntity<Void> created(Link location) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Location", location.getHref());
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    public static ResponseEntity<Void> noContent() {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}