001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.http;
018
019import org.apache.juneau.http.response.*;
020
021/**
022 * Static factory methods for creating pre-defined HTTP response and exception objects.
023 *
024 * <p>
025 * Import statically for clean DSL-style usage in REST server code:
026 * <p class='bjava'>
027 *    import static HttpResponses.*;
028 *
029 *    <ja>@RestGet</ja>(<js>"/users/{id}"</js>)
030 *    <jk>public</jk> User getUser(<ja>@Path</ja> String id) {
031 *       User <jv>user</jv> = userService.find(id);
032 *       <jk>if</jk> (<jv>user</jv> == <jk>null</jk>) <jk>throw</jk> notFound(<js>"User not found: "</js> + id);
033 *       <jk>return</jk> <jv>user</jv>;
034 *    }
035 * </p>
036 *
037 * <h5 class='section'>See Also:</h5><ul>
038 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a>
039 * </ul>
040 *
041 * @since 9.2.1
042 */
043@SuppressWarnings({
044   "java:S1192" // Duplicate string literals are HTTP response reason phrases and status labels; intentional
045})
046public final class HttpResponses {
047
048   private HttpResponses() {}
049
050   // ------------------------------------------------------------------------------------------------------------------
051   // 1xx Informational
052   // ------------------------------------------------------------------------------------------------------------------
053
054   /** @return A new {@code 100 Continue} response. */
055   public static Continue continueResponse() { return new Continue(); }
056
057   /** @return A new {@code 101 Switching Protocols} response. */
058   public static SwitchingProtocols switchingProtocols() { return new SwitchingProtocols(); }
059
060   // ------------------------------------------------------------------------------------------------------------------
061   // 2xx Success
062   // ------------------------------------------------------------------------------------------------------------------
063
064   /** @return A new {@code 200 OK} response. */
065   public static Ok ok() { return new Ok(); }
066
067   /** @param body The response body. Can be <jk>null</jk> for no response body. @return A new {@code 200 OK} response. */
068   public static Ok ok(String body) { return new Ok(body); }
069
070   /** @return A new {@code 201 Created} response. */
071   public static Created created() { return new Created(); }
072
073   /** @param body The response body. Can be <jk>null</jk> for no response body. @return A new {@code 201 Created} response. */
074   public static Created created(String body) { return new Created(body); }
075
076   /** @return A new {@code 202 Accepted} response. */
077   public static Accepted accepted() { return new Accepted(); }
078
079   /** @return A new {@code 204 No Content} response. */
080   public static NoContent noContent() { return new NoContent(); }
081
082   /** @return A new {@code 206 Partial Content} response. */
083   public static PartialContent partialContent() { return new PartialContent(); }
084
085   // ------------------------------------------------------------------------------------------------------------------
086   // 3xx Redirection
087   // ------------------------------------------------------------------------------------------------------------------
088
089   /** @param location The redirect location. Can be <jk>null</jk> to omit the {@code Location} header. @return A new {@code 301 Moved Permanently} response. */
090   public static MovedPermanently movedPermanently(String location) { return new MovedPermanently().withHeader("Location", location); }
091
092   /** @param location The redirect location. Can be <jk>null</jk> to omit the {@code Location} header. @return A new {@code 302 Found} response. */
093   public static Found found(String location) { return new Found().withHeader("Location", location); }
094
095   /** @param location The redirect location. Can be <jk>null</jk> to omit the {@code Location} header. @return A new {@code 303 See Other} response. */
096   public static SeeOther seeOther(String location) { return new SeeOther().withHeader("Location", location); }
097
098   /** @return A new {@code 304 Not Modified} response. */
099   public static NotModified notModified() { return new NotModified(); }
100
101   /** @param location The redirect location. Can be <jk>null</jk> to omit the {@code Location} header. @return A new {@code 307 Temporary Redirect} response. */
102   public static TemporaryRedirect temporaryRedirect(String location) { return new TemporaryRedirect().withHeader("Location", location); }
103
104   /** @param location The redirect location. Can be <jk>null</jk> to omit the {@code Location} header. @return A new {@code 308 Permanent Redirect} response. */
105   public static PermanentRedirect permanentRedirect(String location) { return new PermanentRedirect().withHeader("Location", location); }
106
107   // ------------------------------------------------------------------------------------------------------------------
108   // 4xx Client Errors
109   // ------------------------------------------------------------------------------------------------------------------
110
111   /** @return A new {@code 400 Bad Request} exception. */
112   public static BadRequest badRequest() { return new BadRequest(); }
113
114   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 400 Bad Request} exception. */
115   public static BadRequest badRequest(String message) { return new BadRequest(message); }
116
117   /** @return A new {@code 401 Unauthorized} exception. */
118   public static Unauthorized unauthorized() { return new Unauthorized(); }
119
120   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 401 Unauthorized} exception. */
121   public static Unauthorized unauthorized(String message) { return new Unauthorized(message); }
122
123   /** @return A new {@code 403 Forbidden} exception. */
124   public static Forbidden forbidden() { return new Forbidden(); }
125
126   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 403 Forbidden} exception. */
127   public static Forbidden forbidden(String message) { return new Forbidden(message); }
128
129   /** @return A new {@code 404 Not Found} exception. */
130   public static NotFound notFound() { return new NotFound(); }
131
132   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 404 Not Found} exception. */
133   public static NotFound notFound(String message) { return new NotFound(message); }
134
135   /** @return A new {@code 405 Method Not Allowed} exception. */
136   public static MethodNotAllowed methodNotAllowed() { return new MethodNotAllowed(); }
137
138   /** @return A new {@code 409 Conflict} exception. */
139   public static Conflict conflict() { return new Conflict(); }
140
141   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 409 Conflict} exception. */
142   public static Conflict conflict(String message) { return new Conflict(message); }
143
144   /** @return A new {@code 410 Gone} exception. */
145   public static Gone gone() { return new Gone(); }
146
147   /** @return A new {@code 412 Precondition Failed} exception. */
148   public static PreconditionFailed preconditionFailed() { return new PreconditionFailed(); }
149
150   /** @return A new {@code 415 Unsupported Media Type} exception. */
151   public static UnsupportedMediaType unsupportedMediaType() { return new UnsupportedMediaType(); }
152
153   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 415 Unsupported Media Type} exception. */
154   public static UnsupportedMediaType unsupportedMediaType(String message) { return new UnsupportedMediaType(message); }
155
156   /** @return A new {@code 422 Unprocessable Entity} exception. */
157   public static UnprocessableEntity unprocessableEntity() { return new UnprocessableEntity(); }
158
159   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 422 Unprocessable Entity} exception. */
160   public static UnprocessableEntity unprocessableEntity(String message) { return new UnprocessableEntity(message); }
161
162   /** @return A new {@code 429 Too Many Requests} exception. */
163   public static TooManyRequests tooManyRequests() { return new TooManyRequests(); }
164
165   // ------------------------------------------------------------------------------------------------------------------
166   // 5xx Server Errors
167   // ------------------------------------------------------------------------------------------------------------------
168
169   /** @return A new {@code 500 Internal Server Error} exception. */
170   public static InternalServerError internalServerError() { return new InternalServerError(); }
171
172   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 500 Internal Server Error} exception. */
173   public static InternalServerError internalServerError(String message) { return new InternalServerError(message); }
174
175   /** @param cause The cause. Can be <jk>null</jk> (no cause is chained). @return A new {@code 500 Internal Server Error} exception. */
176   public static InternalServerError internalServerError(Throwable cause) { return new InternalServerError(cause); }
177
178   /** @return A new {@code 501 Not Implemented} exception. */
179   public static NotImplemented notImplemented() { return new NotImplemented(); }
180
181   /** @return A new {@code 503 Service Unavailable} exception. */
182   public static ServiceUnavailable serviceUnavailable() { return new ServiceUnavailable(); }
183
184   /** @param message The error message. Can be <jk>null</jk> for no message body. @return A new {@code 503 Service Unavailable} exception. */
185   public static ServiceUnavailable serviceUnavailable(String message) { return new ServiceUnavailable(message); }
186}