001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http;
018
019import org.springframework.util.MultiValueMap;
020import org.springframework.util.ObjectUtils;
021
022/**
023 * Represents an HTTP request or response entity, consisting of headers and body.
024 *
025 * <p>Typically used in combination with the {@link org.springframework.web.client.RestTemplate},
026 * like so:
027 * <pre class="code">
028 * HttpHeaders headers = new HttpHeaders();
029 * headers.setContentType(MediaType.TEXT_PLAIN);
030 * HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(helloWorld, headers);
031 * URI location = template.postForLocation("https://example.com", entity);
032 * </pre>
033 * or
034 * <pre class="code">
035 * HttpEntity&lt;String&gt; entity = template.getForEntity("https://example.com", String.class);
036 * String body = entity.getBody();
037 * MediaType contentType = entity.getHeaders().getContentType();
038 * </pre>
039 * Can also be used in Spring MVC, as a return value from a @Controller method:
040 * <pre class="code">
041 * &#64;RequestMapping("/handle")
042 * public HttpEntity&lt;String&gt; handle() {
043 *   HttpHeaders responseHeaders = new HttpHeaders();
044 *   responseHeaders.set("MyResponseHeader", "MyValue");
045 *   return new HttpEntity&lt;String&gt;("Hello World", responseHeaders);
046 * }
047 * </pre>
048 *
049 * @author Arjen Poutsma
050 * @author Juergen Hoeller
051 * @since 3.0.2
052 * @see org.springframework.web.client.RestTemplate
053 * @see #getBody()
054 * @see #getHeaders()
055 */
056public class HttpEntity<T> {
057
058        /**
059         * The empty {@code HttpEntity}, with no body or headers.
060         */
061        public static final HttpEntity<?> EMPTY = new HttpEntity<Object>();
062
063
064        private final HttpHeaders headers;
065
066        private final T body;
067
068
069        /**
070         * Create a new, empty {@code HttpEntity}.
071         */
072        protected HttpEntity() {
073                this(null, null);
074        }
075
076        /**
077         * Create a new {@code HttpEntity} with the given body and no headers.
078         * @param body the entity body
079         */
080        public HttpEntity(T body) {
081                this(body, null);
082        }
083
084        /**
085         * Create a new {@code HttpEntity} with the given headers and no body.
086         * @param headers the entity headers
087         */
088        public HttpEntity(MultiValueMap<String, String> headers) {
089                this(null, headers);
090        }
091
092        /**
093         * Create a new {@code HttpEntity} with the given body and headers.
094         * @param body the entity body
095         * @param headers the entity headers
096         */
097        public HttpEntity(T body, MultiValueMap<String, String> headers) {
098                this.body = body;
099                HttpHeaders tempHeaders = new HttpHeaders();
100                if (headers != null) {
101                        tempHeaders.putAll(headers);
102                }
103                this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
104        }
105
106
107        /**
108         * Returns the headers of this entity.
109         */
110        public HttpHeaders getHeaders() {
111                return this.headers;
112        }
113
114        /**
115         * Returns the body of this entity.
116         */
117        public T getBody() {
118                return this.body;
119        }
120
121        /**
122         * Indicates whether this entity has a body.
123         */
124        public boolean hasBody() {
125                return (this.body != null);
126        }
127
128
129        @Override
130        public boolean equals(Object other) {
131                if (this == other) {
132                        return true;
133                }
134                if (other == null || other.getClass() != getClass()) {
135                        return false;
136                }
137                HttpEntity<?> otherEntity = (HttpEntity<?>) other;
138                return (ObjectUtils.nullSafeEquals(this.headers, otherEntity.headers) &&
139                                ObjectUtils.nullSafeEquals(this.body, otherEntity.body));
140        }
141
142        @Override
143        public int hashCode() {
144                return (ObjectUtils.nullSafeHashCode(this.headers) * 29 + ObjectUtils.nullSafeHashCode(this.body));
145        }
146
147        @Override
148        public String toString() {
149                StringBuilder builder = new StringBuilder("<");
150                if (this.body != null) {
151                        builder.append(this.body);
152                        if (this.headers != null) {
153                                builder.append(',');
154                        }
155                }
156                if (this.headers != null) {
157                        builder.append(this.headers);
158                }
159                builder.append('>');
160                return builder.toString();
161        }
162
163}