Java tutorial
/* * Copyright 2016 The Rebolt Framework * * The Rebolt Framework licenses this file to you 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 io.rebolt.http; import com.fasterxml.jackson.databind.JsonNode; import com.google.common.net.MediaType; import io.rebolt.core.models.IModel; import io.rebolt.core.utils.HashUtil; import io.rebolt.core.utils.ObjectUtil; import io.rebolt.http.converters.Converter; import io.rebolt.http.converters.ConverterTable; import lombok.Getter; import lombok.ToString; import static io.rebolt.http.HttpMethod.Get; /** * HttpRequest * * @since 1.0.0 */ @ToString @Getter public class HttpRequest implements IModel<HttpRequest> { private static final long serialVersionUID = -8573892752367366044L; private final Converter converter; private final Class<?> requestType; private final Class<?> responseType; private HttpHeader header; private HttpMethod method; private String uri; private HttpForm form; private Object body; /** * HttpRequest ?. Request, Respone ? ?. * * @return {@link HttpRequest} */ public static HttpRequest create() { return new HttpRequest(); } /** * HttpRequest ?. Request ? ?. * * @param responseType Response , Accept ?? ?? . * @return {@link HttpRequest} */ public static HttpRequest create(Class<?> responseType) { return new HttpRequest(responseType); } /** * HttpRequest ?. * * @param requestType Request , Content-Type ?? ?? . * @param responseType Response , Accept ?? ?? . * @return {@link HttpRequest} */ public static HttpRequest create(Class<?> requestType, Class<?> responseType) { return new HttpRequest(requestType, responseType); } private HttpRequest() { this(void.class, JsonNode.class); } private HttpRequest(Class<?> responseType) { this(void.class, responseType); } private HttpRequest(Class<?> requestType, Class<?> responseType) { this.method = Get; this.requestType = requestType; this.responseType = responseType; this.converter = ConverterTable.get(requestType, responseType); this.header = HttpHeader.create(converter); } // region builders public HttpRequest header(HttpHeader header) { if (!ObjectUtil.isEmpty(header)) { this.header.addAll(header); } return this; } public HttpRequest method(HttpMethod method) { this.method = method; if (Get != method && ObjectUtil.isEmpty(header.getContentType())) { header.addContentType(MediaType.FORM_DATA); } return this; } public HttpRequest accept(MediaType accept) { this.header.addAccept(accept); return this; } public HttpRequest accept(String accept) { this.header.addAccept(accept); return this; } public HttpRequest contentType(MediaType contentType) { this.header.addContentType(contentType); return this; } public HttpRequest contentType(String contentType) { this.header.addContentType(contentType); return this; } public HttpRequest uri(String uri) { this.uri = uri; return this; } public HttpRequest form(HttpForm form) { if (Get != method) { this.body = form; } else { this.form = form; } return this; } public HttpRequest body(Object body) { if (Get != method && body != null) { this.body = body; } return this; } // endregion /** * ?? Endpoint . {@link HttpMethod} Get??, {@link HttpForm}? ? QueryString . * * @return Endpoint URI ? * @since 1.0.0 */ public String getEndpointUri() { StringBuilder endpoint = new StringBuilder(); endpoint.append(uri); ObjectUtil.thenNonEmpty(form, form -> endpoint.append("?").append(form.toFormString())); return endpoint.toString(); } public boolean isBody() { return !ObjectUtil.isNull(body); } @Override public boolean isEmpty() { return ObjectUtil.isOrNull(method, body) || ObjectUtil.isEmpty(uri); } @Override public long deepHash() { return HashUtil.deepHash(header, method, uri, body); } @Override public boolean equals(HttpRequest httpRequest) { return httpRequest != null && httpRequest.deepHash() == this.deepHash(); } }