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.request;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import org.apache.juneau.http.*;
022
023/**
024 * Default immutable implementation of {@link HttpRequestLine}.
025 *
026 * <p>
027 * Create instances via the static factory methods:
028 * <p class='bjava'>
029 *    <jc>// GET /users/123 HTTP/1.1</jc>
030 *    HttpRequestLine <jv>rl</jv> = HttpRequestLineBean.<jsm>of</jsm>(<js>"GET"</js>, <js>"/users/123"</js>);
031 *
032 *    <jc>// Custom protocol version</jc>
033 *    HttpRequestLine <jv>rl2</jv> = HttpRequestLineBean.<jsm>of</jsm>(<js>"POST"</js>, <js>"/api/echo"</js>, HttpProtocolVersion.<jsf>HTTP_2_0</jsf>);
034 * </p>
035 *
036 * <h5 class='section'>See Also:</h5><ul>
037 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a>
038 * </ul>
039 *
040 * @since 10.0.0
041 */
042public final class HttpRequestLineBean implements HttpRequestLine {
043
044   private final String method;
045   private final String uri;
046   private final HttpProtocolVersion protocolVersion;
047
048   private HttpRequestLineBean(String method, String uri, HttpProtocolVersion protocolVersion) {
049      this.method = reqnn("method", method);
050      this.uri = reqnn("uri", uri);
051      this.protocolVersion = reqnn("protocolVersion", protocolVersion);
052   }
053
054   /**
055    * Creates an {@code HTTP/1.1} request line with the given method and URI.
056    *
057    * @param method The request method (e.g. {@code "GET"}). Must not be <jk>null</jk>.
058    * @param uri The request URI. Must not be <jk>null</jk>.
059    * @return A new instance. Never <jk>null</jk>.
060    */
061   public static HttpRequestLineBean of(String method, String uri) {
062      return new HttpRequestLineBean(method, uri, HttpProtocolVersion.HTTP_1_1);
063   }
064
065   /**
066    * Creates a request line with the given method, URI, and typed protocol version.
067    *
068    * @param method The request method. Must not be <jk>null</jk>.
069    * @param uri The request URI. Must not be <jk>null</jk>.
070    * @param protocolVersion The protocol version. Must not be <jk>null</jk>.
071    * @return A new instance. Never <jk>null</jk>.
072    */
073   public static HttpRequestLineBean of(String method, String uri, HttpProtocolVersion protocolVersion) {
074      return new HttpRequestLineBean(method, uri, protocolVersion);
075   }
076
077   /**
078    * Creates a request line with the given method, URI, and parsed protocol version.
079    *
080    * @param method The request method. Must not be <jk>null</jk>.
081    * @param uri The request URI. Must not be <jk>null</jk>.
082    * @param protocolVersion The protocol version string (e.g. {@code "HTTP/2"}). Must not be <jk>null</jk>.
083    * @return A new instance. Never <jk>null</jk>.
084    */
085   public static HttpRequestLineBean of(String method, String uri, String protocolVersion) {
086      return new HttpRequestLineBean(method, uri, HttpProtocolVersion.parse(protocolVersion));
087   }
088
089   @Override /* HttpRequestLine */
090   public String getMethod() {
091      return method;
092   }
093
094   @Override /* HttpRequestLine */
095   public String getUri() {
096      return uri;
097   }
098
099   @Override /* HttpRequestLine */
100   public HttpProtocolVersion getProtocolVersion() {
101      return protocolVersion;
102   }
103
104   @Override /* Object */
105   public String toString() {
106      return method + ' ' + uri + ' ' + protocolVersion;
107   }
108
109   @Override /* Object */
110   public boolean equals(Object obj) {
111      return (obj instanceof HttpRequestLineBean o2) && eq(this, o2, (x, y) ->
112         eq(x.method, y.method) && eq(x.uri, y.uri) && eq(x.protocolVersion, y.protocolVersion));
113   }
114
115   @Override /* Object */
116   public int hashCode() {
117      return h(method, uri, protocolVersion);
118   }
119}