All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.elide.ElideResponse Maven / Gradle / Ivy

There is a newer version: 7.1.4
Show newest version
/*
 * Copyright 2015, Yahoo Inc.
 * Licensed under the Apache License, Version 2.0
 * See LICENSE file in project root for terms.
 */
package com.yahoo.elide;

import com.yahoo.elide.core.exceptions.HttpStatus;

/**
 * Elide Response.
 * 

* Builder example: *


 * ElideResponse.status(400)
 *     .body(body)
 *     .build();
 * 
* * @param the body type */ public class ElideResponse { /** * The HTTP status code. */ private final int status; /** * The body. */ private final T body; /** * Constructor. * * @param status HTTP response status * @param body the body */ public ElideResponse(int status, T body) { this.status = status; this.body = body; } /** * Returns the HTTP status code of the response. * * @return the HTTP status code of the response */ public int getStatus() { return this.status; } /** * Returns the body of the response. * * @return the body of the response */ public T getBody() { return this.body; } /** * Returns the body of the response if it is of the appropriate type. * * @param the expected type of the response * @param clazz the expected class of the response * @return the body of the response */ public V getBody(Class clazz) { if (clazz.isInstance(this.body)) { return clazz.cast(this.body); } return null; } /** * Builds a response with this HTTP status code. * * @param status the HTTP status code * @return the builder */ public static ElideResponseBuilder status(int status) { return new ElideResponseBuilder(status); } /** * Build a response with 200. * * @return the builder */ public static ElideResponseBuilder ok() { return status(HttpStatus.SC_OK); } /** * Build a response with 200. * * @param the body type * @param body the body * @return the response */ public static ElideResponse ok(T body) { return ok().body(body); } /** * Build a response with 400. * * @return the builder */ public static ElideResponseBuilder badRequest() { return status(HttpStatus.SC_BAD_REQUEST); } /** * Build a response with 400. * * @param the body type * @param body the body * @return the response */ public static ElideResponse badRequest(T body) { return badRequest().body(body); } /** * Builder for building a {@link ElideResponse}. */ public static class ElideResponseBuilder { protected int status; public ElideResponseBuilder(int status) { this.status = status; } /** * Sets the body of the response. * * @param the body type * @param body the body * @return the response */ public ElideResponse body(T body) { return new ElideResponse<>(status, body); } /** * Build the response with no body. * * @param the body type * @return the response */ public ElideResponse build() { return new ElideResponse<>(status, null); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy