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; 018 019import java.net.*; 020import java.util.*; 021 022import org.apache.http.client.utils.*; 023import org.apache.juneau.http.*; 024 025/** 026 * Aggregates the HTTP method, URL, and optional body into a single bean. 027 * 028 * <h5 class='section'>See Also:</h5><ul> 029 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClientBasics">juneau-rest-client Basics</a> 030 * </ul> 031 */ 032public class RestOperation { 033 034 /** 035 * A placeholder for a non-existent body. 036 * Used to identify when form-data should be used in a request body. 037 * Note that this is different than a <jk>null</jk> body since a <jk>null</jk> can be a serialized request. 038 */ 039 public static final Object NO_BODY = "NO_BODY"; 040 041 /** 042 * Creator. 043 * 044 * @param method The HTTP method. 045 * @param url 046 * The URI of the remote REST resource. 047 * <br>Can be any of the following types: 048 * <ul> 049 * <li>{@link URIBuilder} 050 * <li>{@link URI} 051 * <li>{@link URL} 052 * <li>{@link String} 053 * <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c> 054 * </ul> 055 * @return A new {@link RestOperation} object. 056 */ 057 public static RestOperation of(String method, Object url) { 058 return new RestOperation(method, url, NO_BODY); 059 } 060 061 /** 062 * Creator. 063 * 064 * @param method The HTTP method. 065 * @param url 066 * The URI of the remote REST resource. 067 * <br>Can be any of the following types: 068 * <ul> 069 * <li>{@link URIBuilder} 070 * <li>{@link URI} 071 * <li>{@link URL} 072 * <li>{@link String} 073 * <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c> 074 * </ul> 075 * @param body The HTTP body. 076 * @return A new {@link RestOperation} object. 077 */ 078 public static RestOperation of(String method, Object url, Object body) { 079 return new RestOperation(method, url, body); 080 } 081 082 private final Object url; 083 private final String method; 084 085 private final Object content; 086 087 private boolean hasContent; 088 089 /** 090 * Constructor. 091 * 092 * @param method The HTTP method. 093 * @param url 094 * The URI of the remote REST resource. 095 * <br>Can be any of the following types: 096 * <ul> 097 * <li>{@link URIBuilder} 098 * <li>{@link URI} 099 * <li>{@link URL} 100 * <li>{@link String} 101 * <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c> 102 * </ul> 103 * @param body The HTTP body. 104 */ 105 public RestOperation(String method, Object url, Object body) { 106 this.url = url; 107 this.method = method.toUpperCase(Locale.ENGLISH); 108 this.content = body; 109 this.hasContent = HttpMethod.hasContent(method); 110 } 111 112 /** 113 * Bean property getter: <property>content</property>. 114 * 115 * @return 116 * The value of the <property>content</property> property on this bean. 117 * <br>Returns {@link #NO_BODY} if the request does not have a body set. 118 * <br>A <jk>null</jk> value means <jk>null</jk> should be the serialized response. 119 */ 120 public Object getContent() { return content; } 121 122 /** 123 * Bean property getter: <property>method</property>. 124 * 125 * @return The value of the <property>method</property> property on this bean, or <jk>null</jk> if it is not set. 126 */ 127 public String getMethod() { return method; } 128 129 /** 130 * Bean property getter: <property>url</property>. 131 * 132 * @return The value of the <property>url</property> property on this bean, or <jk>null</jk> if it is not set. 133 */ 134 public Object getUri() { return url; } 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 hasContent = value; 153 return this; 154 } 155}