feign.Target Maven / Gradle / Ivy
/*
* Copyright 2012-2023 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign;
import static feign.Util.checkNotNull;
import static feign.Util.emptyToNull;
/**
*
*
* relationship to JAXRS 2.0
*
* Similar to {@code
* javax.ws.rs.client.WebTarget}, as it produces requests. However, {@link RequestTemplate} is a
* closer match to {@code WebTarget}.
*
* @param type of the interface this target applies to.
*/
public interface Target {
/* The type of the interface this target applies to. ex. {@code Route53}. */
Class type();
/* configuration key associated with this target. For example, {@code route53}. */
String name();
/* base HTTP URL of the target. For example, {@code https://api/v2}. */
String url();
/**
* Targets a template to this target, adding the {@link #url() base url} and any target-specific
* headers or query parameters.
*
* For example:
*
*
* public Request apply(RequestTemplate input) {
* input.insert(0, url());
* input.replaceHeader("X-Auth", currentToken);
* return input.asRequest();
* }
*
*
*
*
*
* relationship to JAXRS 2.0
*
* This call is similar to {@code
* javax.ws.rs.client.WebTarget.request()}, except that we expect transient, but necessary
* decoration to be applied on invocation.
*/
public Request apply(RequestTemplate input);
public static class HardCodedTarget implements Target {
private final Class type;
private final String name;
private final String url;
public HardCodedTarget(Class type, String url) {
this(type, url, url);
}
public HardCodedTarget(Class type, String name, String url) {
this.type = checkNotNull(type, "type");
this.name = checkNotNull(emptyToNull(name), "name");
this.url = checkNotNull(emptyToNull(url), "url");
}
@Override
public Class type() {
return type;
}
@Override
public String name() {
return name;
}
@Override
public String url() {
return url;
}
/* no authentication or other special activity. just insert the url. */
@Override
public Request apply(RequestTemplate input) {
if (input.url().indexOf("http") != 0) {
input.target(url());
}
return input.request();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof HardCodedTarget) {
HardCodedTarget> other = (HardCodedTarget) obj;
return type.equals(other.type)
&& name.equals(other.name)
&& url.equals(other.url);
}
return false;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + type.hashCode();
result = 31 * result + name.hashCode();
result = 31 * result + url.hashCode();
return result;
}
@Override
public String toString() {
if (name.equals(url)) {
return "HardCodedTarget(type=" + type.getSimpleName() + ", url=" + url + ")";
}
return "HardCodedTarget(type=" + type.getSimpleName() + ", name=" + name + ", url=" + url
+ ")";
}
}
public static final class EmptyTarget implements Target {
private final Class type;
private final String name;
EmptyTarget(Class type, String name) {
this.type = checkNotNull(type, "type");
this.name = checkNotNull(emptyToNull(name), "name");
}
public static EmptyTarget create(Class type) {
return new EmptyTarget(type, "empty:" + type.getSimpleName());
}
public static EmptyTarget create(Class type, String name) {
return new EmptyTarget(type, name);
}
@Override
public Class type() {
return type;
}
@Override
public String name() {
return name;
}
@Override
public String url() {
throw new UnsupportedOperationException("Empty targets don't have URLs");
}
@Override
public Request apply(RequestTemplate input) {
if (input.url().indexOf("http") != 0) {
throw new UnsupportedOperationException(
"Request with non-absolute URL not supported with empty target");
}
return input.request();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EmptyTarget) {
EmptyTarget> other = (EmptyTarget) obj;
return type.equals(other.type)
&& name.equals(other.name);
}
return false;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + type.hashCode();
result = 31 * result + name.hashCode();
return result;
}
@Override
public String toString() {
if (name.equals("empty:" + type.getSimpleName())) {
return "EmptyTarget(type=" + type.getSimpleName() + ")";
}
return "EmptyTarget(type=" + type.getSimpleName() + ", name=" + name + ")";
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy