diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntityBuilder.java b/spring-web/src/main/java/org/springframework/http/ResponseEntityBuilder.java new file mode 100644 index 0000000000..90f31aa9ee --- /dev/null +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntityBuilder.java @@ -0,0 +1,286 @@ +/* + * Copyright 2002-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 org.springframework.http; + +import java.net.URI; +import java.util.Set; + +/** + * A builder for {@link ResponseEntity} objects. Enforces common HTTP response practices + * through a fluent API. + * + *
This class is typically used in @Controller methods through a static import. + * For instance: + *
+ * import static org.springframework.http.ResponseEntityBuilder.*; + * + * @Controller + * public class MyController { + * + * @RequestMapping(value="/entity", method=RequestMethod.GET) + * public ResponseEntity<MyEntity> myBean() { + * MyEntity entity = ... + * long lastModifiedDate = ... + * ResponseEntity<MyEntity> responseEntity = status(HttpStatus.OK) + * .header("Last-Modified", lastModifiedDate).body(entity); + * return responseEntity; + * } + * }+ * + * Or, using some of the convenience methods: + * + *
+ * ok().lastModified(lastModifiedDate).body(entity); + *+ * + * @author Arjen Poutsma + * @since 4.1 + * @see ResponseEntity + */ +public class ResponseEntityBuilder { + + private ResponseEntityBuilder() { + } + + /** + * Creates a new {@code ResponseEntityBuilder} with the given status. + * @param status the response status + * @return the new response entity builder + */ + public static ResponseBodyBuilder status(HttpStatus status) { + return new DefaultResponseBuilder(status); + } + + /** + * Creates a new {@code ResponseEntityBuilder} with the given status. + * @param status the response status + * @return the new response entity builder + */ + public static ResponseBodyBuilder status(int status) { + return status(HttpStatus.valueOf(status)); + } + + /** + * Creates a new {@code ResponseEntityBuilder} with the status set to + * {@linkplain HttpStatus#OK OK}. + * @return the new response entity builder + */ + public static ResponseBodyBuilder ok() { + return status(HttpStatus.OK); + } + + /** + * Creates a new {@code ResponseEntity} with the given body and the status set to + * {@linkplain HttpStatus#OK OK}. + * @return the new response entity + */ + public static ResponseHeadersBuilder ok(Object body) { + ResponseBodyBuilder builder = ok(); + builder.body(body); + return builder; + } + + /** + * Creates a new {@code ResponseEntityBuilder} with a + * {@linkplain HttpStatus#CREATED CREATED} status and a location header set to the + * given URI. + * @param location the location URI + * @return the new response entity builder + */ + public static ResponseBodyBuilder created(URI location) { + ResponseBodyBuilder builder = status(HttpStatus.CREATED); + return builder.location(location); + } + + /** + * Creates a new {@code ResponseEntityBuilder} with an + * {@link HttpStatus#ACCEPTED ACCEPTED} status. + * @return the new response entity builder + */ + public static ResponseBodyBuilder accepted() { + return status(HttpStatus.ACCEPTED); + } + + /** + * Creates a new {@code ResponseEntityBuilder} with an + * {@link HttpStatus#NO_CONTENT NO_CONTENT} status. + * @return the new response entity builder + */ + public static ResponseHeadersBuilder noContent() { + return status(HttpStatus.NO_CONTENT); + } + + + /** + * Defines a builder that adds headers to the response entity. + * @param the builder subclass + */ + public interface ResponseHeadersBuilder> { + + /** + * Add the given, single header value under the given name. + * @param headerName the header name + * @param headerValue the header value(s) + * @return this builder + * @see HttpHeaders#add(String, String) + */ + B header(String headerName, String... headerValues); + + /** + * Set the set of allowed {@link HttpMethod HTTP methods}, as specified by the + * {@code Allow} header. + * @param allowedMethods the allowed methods + * @see HttpHeaders#setAllow(Set) + */ + B allow(Set
The date should be specified as the number of milliseconds since January 1, 1970 GMT.
+ * @param lastModified the last modified date
+ * @see HttpHeaders#setLastModified(long)
+ */
+ B lastModified(long lastModified);
+
+ /**
+ * Set the location of a resource, as specified by the {@code Location} header.
+ * @param location the location
+ * @see HttpHeaders#setLocation(URI)
+ */
+ B location(URI location);
+
+ /**
+ * Builds the response entity.
+ * @return the response entity
+ * @see ResponseBodyBuilder#body(Object)
+ */
+ ResponseEntity> build();
+
+ }
+
+
+ /**
+ * Defines a builder that adds a body to the response entity.
+ */
+ public interface ResponseBodyBuilder extends ResponseHeadersBuilder