001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client;
014
015import java.net.*;
016import java.util.*;
017
018import org.apache.http.client.utils.*;
019import org.apache.juneau.http.*;
020
021/**
022 * Aggregates the HTTP method, URL, and optional body into a single bean.
023 *
024 * <h5 class='section'>See Also:</h5><ul>
025 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-client">juneau-rest-client</a>
026 * </ul>
027 */
028public class RestOperation {
029
030   /**
031    * A placeholder for a non-existent body.
032    * Used to identify when form-data should be used in a request body.
033    * Note that this is different than a <jk>null</jk> body since a <jk>null</jk> can be a serialized request.
034    */
035   public static final Object NO_BODY = "NO_BODY";
036
037   private final Object url;
038   private final String method;
039   private final Object content;
040   private boolean hasContent;
041
042   /**
043    * Creator.
044    *
045    * @param method The HTTP method.
046    * @param url
047    *    The URI of the remote REST resource.
048    *    <br>Can be any of the following types:
049    *    <ul>
050    *       <li>{@link URIBuilder}
051    *       <li>{@link URI}
052    *       <li>{@link URL}
053    *       <li>{@link String}
054    *       <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c>
055    *    </ul>
056    * @return A new {@link RestOperation} object.
057    */
058   public static RestOperation of(String method, Object url) {
059      return new RestOperation(method, url, NO_BODY);
060   }
061
062   /**
063    * Creator.
064    *
065    * @param method The HTTP method.
066    * @param url
067    *    The URI of the remote REST resource.
068    *    <br>Can be any of the following types:
069    *    <ul>
070    *       <li>{@link URIBuilder}
071    *       <li>{@link URI}
072    *       <li>{@link URL}
073    *       <li>{@link String}
074    *       <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c>
075    *    </ul>
076    * @param body The HTTP body.
077    * @return A new {@link RestOperation} object.
078    */
079   public static RestOperation of(String method, Object url, Object body) {
080      return new RestOperation(method, url, body);
081   }
082
083   /**
084    * Constructor.
085    *
086    * @param method The HTTP method.
087    * @param url
088    *    The URI of the remote REST resource.
089    *    <br>Can be any of the following types:
090    *    <ul>
091    *       <li>{@link URIBuilder}
092    *       <li>{@link URI}
093    *       <li>{@link URL}
094    *       <li>{@link String}
095    *       <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c>
096    *    </ul>
097    * @param body The HTTP body.
098    */
099   public RestOperation(String method, Object url, Object body) {
100      this.url = url;
101      this.method = method.toUpperCase(Locale.ENGLISH);
102      this.content = body;
103      this.hasContent = HttpMethod.hasContent(method);
104   }
105
106   /**
107    * Bean property getter:  <property>url</property>.
108    *
109    * @return The value of the <property>url</property> property on this bean, or <jk>null</jk> if it is not set.
110    */
111   public Object getUri() {
112      return url;
113   }
114
115   /**
116    * Bean property getter:  <property>method</property>.
117    *
118    * @return The value of the <property>method</property> property on this bean, or <jk>null</jk> if it is not set.
119    */
120   public String getMethod() {
121      return method;
122   }
123
124   /**
125    * Bean property getter:  <property>content</property>.
126    *
127    * @return
128    *    The value of the <property>content</property> property on this bean.
129    *    <br>Returns {@link #NO_BODY} if the request does not have a body set.
130    *    <br>A <jk>null</jk> value means <jk>null</jk> should be the serialized response.
131    */
132   public Object getContent() {
133      return content;
134   }
135
136   /**
137    * Identifies whether this HTTP method typically has content.
138    *
139    * @return <jk>true</jk> if this HTTP method typically has content.
140    */
141   public boolean hasContent() {
142      return hasContent;
143   }
144
145   /**
146    * Overrides the default value for the {@link #hasContent()} method.
147    *
148    * @param value The new value.
149    * @return This object.
150    */
151   public RestOperation hasContent(boolean value) {
152      this.hasContent = value;
153      return this;
154   }
155}