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 private final Object url; 042 private final String method; 043 private final Object content; 044 private boolean hasContent; 045 046 /** 047 * Creator. 048 * 049 * @param method The HTTP method. 050 * @param url 051 * The URI of the remote REST resource. 052 * <br>Can be any of the following types: 053 * <ul> 054 * <li>{@link URIBuilder} 055 * <li>{@link URI} 056 * <li>{@link URL} 057 * <li>{@link String} 058 * <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c> 059 * </ul> 060 * @return A new {@link RestOperation} object. 061 */ 062 public static RestOperation of(String method, Object url) { 063 return new RestOperation(method, url, NO_BODY); 064 } 065 066 /** 067 * Creator. 068 * 069 * @param method The HTTP method. 070 * @param url 071 * The URI of the remote REST resource. 072 * <br>Can be any of the following types: 073 * <ul> 074 * <li>{@link URIBuilder} 075 * <li>{@link URI} 076 * <li>{@link URL} 077 * <li>{@link String} 078 * <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c> 079 * </ul> 080 * @param body The HTTP body. 081 * @return A new {@link RestOperation} object. 082 */ 083 public static RestOperation of(String method, Object url, Object body) { 084 return new RestOperation(method, url, body); 085 } 086 087 /** 088 * Constructor. 089 * 090 * @param method The HTTP method. 091 * @param url 092 * The URI of the remote REST resource. 093 * <br>Can be any of the following types: 094 * <ul> 095 * <li>{@link URIBuilder} 096 * <li>{@link URI} 097 * <li>{@link URL} 098 * <li>{@link String} 099 * <li>{@link Object} - Converted to <c>String</c> using <c>toString()</c> 100 * </ul> 101 * @param body The HTTP body. 102 */ 103 public RestOperation(String method, Object url, Object body) { 104 this.url = url; 105 this.method = method.toUpperCase(Locale.ENGLISH); 106 this.content = body; 107 this.hasContent = HttpMethod.hasContent(method); 108 } 109 110 /** 111 * Bean property getter: <property>url</property>. 112 * 113 * @return The value of the <property>url</property> property on this bean, or <jk>null</jk> if it is not set. 114 */ 115 public Object getUri() { 116 return url; 117 } 118 119 /** 120 * Bean property getter: <property>method</property>. 121 * 122 * @return The value of the <property>method</property> property on this bean, or <jk>null</jk> if it is not set. 123 */ 124 public String getMethod() { 125 return method; 126 } 127 128 /** 129 * Bean property getter: <property>content</property>. 130 * 131 * @return 132 * The value of the <property>content</property> property on this bean. 133 * <br>Returns {@link #NO_BODY} if the request does not have a body set. 134 * <br>A <jk>null</jk> value means <jk>null</jk> should be the serialized response. 135 */ 136 public Object getContent() { 137 return content; 138 } 139 140 /** 141 * Identifies whether this HTTP method typically has content. 142 * 143 * @return <jk>true</jk> if this HTTP method typically has content. 144 */ 145 public boolean hasContent() { 146 return hasContent; 147 } 148 149 /** 150 * Overrides the default value for the {@link #hasContent()} method. 151 * 152 * @param value The new value. 153 * @return This object. 154 */ 155 public RestOperation hasContent(boolean value) { 156 this.hasContent = value; 157 return this; 158 } 159}