Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
feign/core/src/main/java/feign/DefaultContract.java at master · OpenFeign/feign · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
OpenFeign
feign
Repository navigation
Code
Issues
209
(209)
Pull requests
23
(23)
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
feign
/
core
/
src
/
main
/
java
/
feign
/
DefaultContract.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
153 lines (146 loc) · 5.85 KB
master
Breadcrumbs
feign
/
core
/
src
/
main
/
java
/
feign
/
DefaultContract.java
Copy path
Top
File metadata and controls
Code
Blame
153 lines (146 loc) · 5.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
*
* 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
.
checkState
;
import
static
feign
.
Util
.
emptyToNull
;
import
feign
.
Request
.
HttpMethod
;
import
java
.
lang
.
reflect
.
Parameter
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collection
;
import
java
.
util
.
LinkedHashMap
;
import
java
.
util
.
Map
;
import
java
.
util
.
regex
.
Matcher
;
import
java
.
util
.
regex
.
Pattern
;
public
class
DefaultContract
extends
DeclarativeContract
{
static
final
Pattern
REQUEST_LINE_PATTERN
=
Pattern
.
compile
(
"^([A-Z]+)[ ]*(.*)$"
);
public
DefaultContract
() {
super
.
registerClassAnnotation
(
Headers
.
class
,
(
header
,
data
) -> {
final
String
[]
headersOnType
=
header
.
value
();
checkState
(
headersOnType
.
length
>
0
,
"Headers annotation was empty on type %s."
,
data
.
configKey
());
final
Map
<
String
,
Collection
<
String
>>
headers
=
toMap
(
headersOnType
);
headers
.
putAll
(
data
.
template
().
headers
());
data
.
template
().
headers
(
null
);
// to clear
data
.
template
().
headers
(
headers
);
});
super
.
registerMethodAnnotation
(
RequestLine
.
class
,
(
ann
,
data
) -> {
final
String
requestLine
=
ann
.
value
();
checkState
(
emptyToNull
(
requestLine
) !=
null
,
"RequestLine annotation was empty on method %s."
,
data
.
configKey
());
final
Matcher
requestLineMatcher
=
REQUEST_LINE_PATTERN
.
matcher
(
requestLine
);
if
(!
requestLineMatcher
.
find
()) {
throw
new
IllegalStateException
(
String
.
format
(
"RequestLine annotation didn't start with an HTTP verb on method %s"
,
data
.
configKey
()));
}
else
{
data
.
template
().
method
(
HttpMethod
.
valueOf
(
requestLineMatcher
.
group
(
1
)));
data
.
template
().
uri
(
requestLineMatcher
.
group
(
2
));
}
data
.
template
().
decodeSlash
(
ann
.
decodeSlash
());
data
.
template
().
collectionFormat
(
ann
.
collectionFormat
());
});
super
.
registerMethodAnnotation
(
Body
.
class
,
(
ann
,
data
) -> {
final
String
body
=
ann
.
value
();
checkState
(
emptyToNull
(
body
) !=
null
,
"Body annotation was empty on method %s."
,
data
.
configKey
());
if
(
body
.
indexOf
(
'{'
) == -
1
) {
data
.
template
().
body
(
body
);
}
else
{
data
.
template
().
bodyTemplate
(
body
);
}
});
super
.
registerMethodAnnotation
(
Headers
.
class
,
(
header
,
data
) -> {
final
String
[]
headersOnMethod
=
header
.
value
();
checkState
(
headersOnMethod
.
length
>
0
,
"Headers annotation was empty on method %s."
,
data
.
configKey
());
data
.
template
().
headers
(
toMap
(
headersOnMethod
));
});
super
.
registerParameterAnnotation
(
Param
.
class
,
(
paramAnnotation
,
data
,
paramIndex
) -> {
final
String
annotationName
=
paramAnnotation
.
value
();
final
Parameter
parameter
=
data
.
method
().
getParameters
()[
paramIndex
];
final
String
name
;
if
(
emptyToNull
(
annotationName
) ==
null
&&
parameter
.
isNamePresent
()) {
name
=
parameter
.
getName
();
}
else
{
name
=
annotationName
;
}
checkState
(
emptyToNull
(
name
) !=
null
,
"Param annotation was empty on param %s.
\n
Hint: %s"
,
paramIndex
,
"Prefer using @Param(value=
\"
name
\"
), or compile your code with the -parameters flag.
\n
"
+
"If the value is missing, Feign attempts to retrieve the parameter name from bytecode, "
+
"which only works if the class was compiled with the -parameters flag."
);
nameParam
(
data
,
name
,
paramIndex
);
final
Class
<?
extends
Param
.
Expander
>
expander
=
paramAnnotation
.
expander
();
if
(
expander
!=
Param
.
ToStringExpander
.
class
) {
data
.
indexToExpanderClass
().
put
(
paramIndex
,
expander
);
}
if
(!
data
.
template
().
hasRequestVariable
(
name
)) {
data
.
formParams
().
add
(
name
);
}
});
super
.
registerParameterAnnotation
(
QueryMap
.
class
,
(
queryMap
,
data
,
paramIndex
) -> {
checkState
(
data
.
queryMapIndex
() ==
null
,
"QueryMap annotation was present on multiple parameters."
);
data
.
queryMapIndex
(
paramIndex
);
data
.
queryMapEncoder
(
queryMap
.
mapEncoder
().
instance
());
});
super
.
registerParameterAnnotation
(
HeaderMap
.
class
,
(
queryMap
,
data
,
paramIndex
) -> {
checkState
(
data
.
headerMapIndex
() ==
null
,
"HeaderMap annotation was present on multiple parameters."
);
data
.
headerMapIndex
(
paramIndex
);
});
}
private
static
Map
<
String
,
Collection
<
String
>>
toMap
(
String
[]
input
) {
final
Map
<
String
,
Collection
<
String
>>
result
=
new
LinkedHashMap
<
String
,
Collection
<
String
>>(
input
.
length
);
for
(
final
String
header
:
input
) {
final
int
colon
=
header
.
indexOf
(
':'
);
final
String
name
=
header
.
substring
(
0
,
colon
);
if
(!
result
.
containsKey
(
name
)) {
result
.
put
(
name
,
new
ArrayList
<
String
>(
1
));
}
result
.
get
(
name
).
add
(
header
.
substring
(
colon
+
1
).
trim
());
}
return
result
;
}
}
You can’t perform that action at this time.