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.rest.client.classic; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.net.*; 022 023import org.apache.http.client.utils.*; 024import org.apache.juneau.http.*; 025 026/** 027 * Aggregates the HTTP method, URL, and optional body into a single bean. 028 * 029 * <h5 class='section'>See Also:</h5><ul> 030 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClient">juneau-rest-client-classic</a> 031 * </ul> 032 */ 033public class RestOperation { 034 035 /** 036 * A placeholder for a non-existent body. 037 * Used to identify when form-data should be used in a request body. 038 * Note that this is different than a <jk>null</jk> body since a <jk>null</jk> can be a serialized request. 039 */ 040 public static final Object NO_BODY = "NO_BODY"; 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 private final Object url; 084 private final String method; 085 086 private final Object content; 087 088 private boolean hasContent; 089 090 /** 091 * Constructor. 092 * 093 * @param method The HTTP method. 094 * @param url 095 * The URI of the remote REST resource. 096 * <br>Can be any of the following types: 097 * <ul> 098 * <li>{@link URIBuilder} 099 * <li>{@link URI} 100 * <li>{@link URL} 101 * <li>{@link String} 102 * <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c> 103 * </ul> 104 * @param body The HTTP body. 105 */ 106 public RestOperation(String method, Object url, Object body) { 107 this.url = url; 108 this.method = ucr(method); 109 this.content = body; 110 this.hasContent = HttpMethod.hasContent(method); 111 } 112 113 /** 114 * Bean property getter: <property>content</property>. 115 * 116 * @return 117 * The value of the <property>content</property> property on this bean. 118 * <br>Returns {@link #NO_BODY} if the request does not have a body set. 119 * <br>A <jk>null</jk> value means <jk>null</jk> should be the serialized response. 120 */ 121 public Object getContent() { return content; } 122 123 /** 124 * Bean property getter: <property>method</property>. 125 * 126 * @return The value of the <property>method</property> property on this bean, or <jk>null</jk> if it is not set. 127 */ 128 public String getMethod() { return method; } 129 130 /** 131 * Bean property getter: <property>url</property>. 132 * 133 * @return The value of the <property>url</property> property on this bean, or <jk>null</jk> if it is not set. 134 */ 135 public Object getUri() { return url; } 136 137 /** 138 * Identifies whether this HTTP method typically has content. 139 * 140 * @return <jk>true</jk> if this HTTP method typically has content. 141 */ 142 public boolean hasContent() { 143 return hasContent; 144 } 145 146 /** 147 * Overrides the default value for the {@link #hasContent()} method. 148 * 149 * @param value The new value. 150 * @return This object. 151 */ 152 public RestOperation hasContent(boolean value) { 153 hasContent = value; 154 return this; 155 } 156}