Skip to content

Commit 7d4b01a

Browse files
committed
Add Jakarta REST client module for HttpClient
1 parent 8966906 commit 7d4b01a

File tree

17 files changed

+3838
-0
lines changed

17 files changed

+3838
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
====================================================================
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
====================================================================
21+
22+
This software consists of voluntary contributions made by many
23+
individuals on behalf of the Apache Software Foundation. For more
24+
information on the Apache Software Foundation, please see
25+
<http://www.apache.org />.
26+
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
27+
<parent>
28+
<artifactId>httpclient5-parent</artifactId>
29+
<groupId>org.apache.httpcomponents.client5</groupId>
30+
<version>5.7-alpha1-SNAPSHOT</version>
31+
</parent>
32+
<modelVersion>4.0.0</modelVersion>
33+
34+
<artifactId>httpclient5-jakarta-rest-client</artifactId>
35+
<name>Jakarta REST Client for Apache HttpClient</name>
36+
<description>Type-safe Jakarta REST client backed by Apache HttpClient</description>
37+
38+
<properties>
39+
<Automatic-Module-Name>org.apache.hc.client5.http.rest</Automatic-Module-Name>
40+
<maven.compiler.source>11</maven.compiler.source>
41+
<maven.compiler.target>11</maven.compiler.target>
42+
</properties>
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.apache.httpcomponents.client5</groupId>
47+
<artifactId>httpclient5</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>jakarta.ws.rs</groupId>
51+
<artifactId>jakarta.ws.rs-api</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.fasterxml.jackson.core</groupId>
55+
<artifactId>jackson-databind</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.junit.jupiter</groupId>
59+
<artifactId>junit-jupiter</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.apache.httpcomponents.core5</groupId>
64+
<artifactId>httpcore5-testing</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.slf4j</groupId>
69+
<artifactId>slf4j-api</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.apache.logging.log4j</groupId>
74+
<artifactId>log4j-slf4j-impl</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.apache.logging.log4j</groupId>
79+
<artifactId>log4j-core</artifactId>
80+
<scope>test</scope>
81+
</dependency>
82+
</dependencies>
83+
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.codehaus.mojo</groupId>
88+
<artifactId>animal-sniffer-maven-plugin</artifactId>
89+
<configuration>
90+
<skip>true</skip>
91+
</configuration>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
96+
<profiles>
97+
<profile>
98+
<id>skip-on-java8</id>
99+
<activation>
100+
<property>
101+
<name>hc.build.toolchain.version</name>
102+
<value>1.8</value>
103+
</property>
104+
</activation>
105+
<properties>
106+
<maven.main.skip>true</maven.main.skip>
107+
<maven.test.skip>true</maven.test.skip>
108+
<maven.javadoc.skip>true</maven.javadoc.skip>
109+
<maven.source.skip>true</maven.source.skip>
110+
<maven.install.skip>true</maven.install.skip>
111+
<checkstyle.skip>true</checkstyle.skip>
112+
</properties>
113+
</profile>
114+
</profiles>
115+
116+
<reporting>
117+
<plugins>
118+
<plugin>
119+
<artifactId>maven-project-info-reports-plugin</artifactId>
120+
<inherited>false</inherited>
121+
<reportSets>
122+
<reportSet>
123+
<reports>
124+
<report>index</report>
125+
<report>dependencies</report>
126+
<report>dependency-info</report>
127+
<report>summary</report>
128+
</reports>
129+
</reportSet>
130+
</reportSets>
131+
</plugin>
132+
</plugins>
133+
</reporting>
134+
135+
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
package org.apache.hc.client5.http.rest;
28+
29+
import java.util.Locale;
30+
31+
/**
32+
* Builds the value of the {@code Accept} header from the media types declared in a
33+
* {@code @Produces} annotation. Implementations control how multiple media types are
34+
* ordered and whether quality values are assigned.
35+
*
36+
* @since 5.7
37+
*/
38+
public interface AcceptStrategy {
39+
40+
/**
41+
* Default strategy that assigns descending quality values (1.0, 0.9, 0.8, ...) when
42+
* more than one media type is declared. A single type is sent without a quality parameter.
43+
*/
44+
AcceptStrategy QUALITY_DESCENDING = new AcceptStrategy() {
45+
46+
/** Quality-value step. */
47+
private static final double Q_STEP = 0.1;
48+
/** Minimum quality value. */
49+
private static final double Q_MIN = 0.1;
50+
51+
@Override
52+
public String buildAcceptHeader(final String[] produces) {
53+
if (produces.length == 1) {
54+
return produces[0];
55+
}
56+
final StringBuilder sb = new StringBuilder();
57+
for (int i = 0; i < produces.length; i++) {
58+
if (sb.length() > 0) {
59+
sb.append(", ");
60+
}
61+
sb.append(produces[i]);
62+
final double q = 1.0 - i * Q_STEP;
63+
final double qClamped = Math.max(q, Q_MIN);
64+
sb.append(";q=");
65+
if (qClamped == 1.0) {
66+
sb.append("1.0");
67+
} else {
68+
sb.append(String.format(Locale.ROOT, "%.1f", qClamped));
69+
}
70+
}
71+
return sb.toString();
72+
}
73+
};
74+
75+
/**
76+
* Strategy that lists all media types without quality values, giving them equal
77+
* preference.
78+
*/
79+
AcceptStrategy UNWEIGHTED = produces -> {
80+
final StringBuilder sb = new StringBuilder();
81+
for (final String type : produces) {
82+
if (sb.length() > 0) {
83+
sb.append(", ");
84+
}
85+
sb.append(type);
86+
}
87+
return sb.toString();
88+
};
89+
90+
/**
91+
* Formats the given media types into an Accept header value.
92+
*
93+
* @param produces the media types from {@code @Produces}, never empty.
94+
* @return the formatted Accept header value.
95+
*/
96+
String buildAcceptHeader(String[] produces);
97+
98+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
package org.apache.hc.client5.http.rest;
28+
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.lang.reflect.Type;
32+
33+
/**
34+
* Deserializes an HTTP response body into a Java object. Implementations handle a specific
35+
* content type such as JSON or XML.
36+
*
37+
* @since 5.7
38+
*/
39+
public interface EntityReader {
40+
41+
/**
42+
* Reads the response body and converts it to the requested type.
43+
*
44+
* @param stream the response body stream.
45+
* @param rawType the raw return type of the interface method.
46+
* @param genericType the full generic return type, which may be a
47+
* {@link java.lang.reflect.ParameterizedType}.
48+
* @return the deserialized object.
49+
* @throws IOException if reading or deserialization fails.
50+
*/
51+
Object read(InputStream stream, Class<?> rawType, Type genericType) throws IOException;
52+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
package org.apache.hc.client5.http.rest;
28+
29+
import java.io.IOException;
30+
31+
/**
32+
* Serializes a Java object into a byte array for use as an HTTP request body.
33+
* Implementations handle a specific content type such as JSON or XML.
34+
*
35+
* @since 5.7
36+
*/
37+
public interface EntityWriter {
38+
39+
/**
40+
* Serializes the given object into a byte array suitable for use as a request body.
41+
*
42+
* @param body the object to serialize.
43+
* @param mediaType the target media type from the {@code @Consumes} annotation.
44+
* @return the serialized bytes.
45+
* @throws IOException if serialization fails.
46+
*/
47+
byte[] write(Object body, String mediaType) throws IOException;
48+
}

0 commit comments

Comments
 (0)