Java tutorial
package net.eusashead.hateoas.response.impl; /* * #[license] * spring-responseentitybuilder * %% * Copyright (C) 2013 Eusa's Head * %% * 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. * %[license] */ import java.util.Date; import javax.servlet.http.HttpServletRequest; import net.eusashead.hateoas.header.Header; import net.eusashead.hateoas.header.impl.CacheControlHeaderImpl; import net.eusashead.hateoas.header.impl.ETagHeaderImpl; import net.eusashead.hateoas.header.impl.ExpiresHeaderImpl; import net.eusashead.hateoas.header.impl.LastModifiedHeaderImpl; import net.eusashead.hateoas.response.ResponseBuilder; import org.springframework.http.HttpHeaders; public abstract class AbstractResponseBuilder<T> implements ResponseBuilder<T> { protected final HttpServletRequest request; protected final HttpHeaders headers; public AbstractResponseBuilder(HttpServletRequest request) { // Validate the request if (request == null) { throw new IllegalArgumentException("HttpServletRequest is null."); } // Set the request this.request = request; // Check the HTTP verb is supported this.assertVerb(); // Create new headers this.headers = new HttpHeaders(); } @Override public HttpServletRequest getRequest() { return this.request; } @Override public ResponseBuilder<T> addHeader(Header<?> header) { this.headers.add(header.getName(), header.getValue().toString()); return this; } @Override public ResponseBuilder<T> addHeader(String name, String value) { this.headers.add(name, value); return this; } /** * All implementations must * provide validation that they can * handle the verb in the inbound request */ protected abstract void assertVerb(); protected void setEtagHeader(Date date) { // Weak ETag this.headers.setETag(new ETagHeaderImpl(date).toString()); } protected void setEtagHeader(Integer version) { // Strong ETag this.headers.setETag(new ETagHeaderImpl(version).toString()); } protected void setEtagHeader(Long version) { // Strong ETag this.headers.setETag(new ETagHeaderImpl(version).toString()); } protected void setLastModifiedHeader(Date date) { this.headers.setLastModified(new LastModifiedHeaderImpl(date).getValue()); } protected void setExpiryHeaders(long millis) { Date now = new Date(); this.headers.setDate(now.getTime()); this.headers.setExpires(new ExpiresHeaderImpl(now, millis).getValue()); this.headers.setCacheControl(new CacheControlHeaderImpl(millis).getValue()); } /** * Compare the supplied ETag * with the ETag in the * request * @param eTag * @return */ protected boolean compareEtagWithIfNoneMatch(String eTag) { return checkEtag(eTag, "If-None-Match"); } /** * Compare outgoing ETag * to incoming "If-None-Match" * header and return true * if they match * @param eTag * @return */ private boolean checkEtag(String eTag, String header) { String providedTag = request.getHeader(header); if (providedTag == null || !eTag.equals(providedTag)) { return false; } else { return true; } } }