Skip to content

Commit f083b40

Browse files
committed
Jackson 2 bindings for HttpCore - initial contribution
1 parent abe5512 commit f083b40

File tree

69 files changed

+7882
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+7882
-2
lines changed

httpcore5-jackson2/pom.xml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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>httpcore5-parent</artifactId>
29+
<groupId>org.apache.httpcomponents.core5</groupId>
30+
<version>5.5-alpha1-SNAPSHOT</version>
31+
</parent>
32+
<modelVersion>4.0.0</modelVersion>
33+
34+
<artifactId>httpcore5-jackson2</artifactId>
35+
<name>Jackson 2 bindings for Apache HttpComponents Core</name>
36+
<description>Apache HttpComponents JSON bindings</description>
37+
38+
<properties>
39+
<Automatic-Module-Name>org.apache.httpcomponents.core5.httpcore5.json</Automatic-Module-Name>
40+
</properties>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>org.apache.httpcomponents.core5</groupId>
45+
<artifactId>httpcore5</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.fasterxml.jackson.core</groupId>
49+
<artifactId>jackson-databind</artifactId>
50+
<version>2.21.0</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.junit.jupiter</groupId>
54+
<artifactId>junit-jupiter</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.assertj</groupId>
59+
<artifactId>assertj-core</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-core</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+
<reporting>
85+
<plugins>
86+
<plugin>
87+
<artifactId>maven-project-info-reports-plugin</artifactId>
88+
<inherited>false</inherited>
89+
<reportSets>
90+
<reportSet>
91+
<reports>
92+
<report>index</report>
93+
<report>dependencies</report>
94+
<report>dependency-info</report>
95+
<report>summary</report>
96+
</reports>
97+
</reportSet>
98+
</reportSets>
99+
</plugin>
100+
</plugins>
101+
</reporting>
102+
103+
</project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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.core5.jackson2;
28+
29+
import java.io.IOException;
30+
import java.nio.ByteBuffer;
31+
32+
import com.fasterxml.jackson.core.JsonFactory;
33+
import com.fasterxml.jackson.core.JsonParser;
34+
import com.fasterxml.jackson.core.JsonToken;
35+
import com.fasterxml.jackson.core.JsonTokenId;
36+
import com.fasterxml.jackson.core.async.ByteArrayFeeder;
37+
38+
import org.apache.hc.core5.util.Args;
39+
40+
/**
41+
* Event-driven, reactive JSON tokenizer that consumes data from a sequence
42+
* {@link ByteBuffer} objects and emits events to {@link JsonTokenConsumer}.
43+
*
44+
* @since 5.5
45+
*/
46+
public final class JsonAsyncTokenizer {
47+
48+
private final JsonFactory jsonFactory;
49+
50+
private JsonTokenConsumer consumer;
51+
private JsonParser parser;
52+
private ByteArrayFeeder inputFeeder;
53+
54+
public JsonAsyncTokenizer(final JsonFactory jsonFactory) {
55+
this.jsonFactory = jsonFactory;
56+
}
57+
58+
/**
59+
* Triggered to initialize the tokenizer and pass the token event consumer.
60+
*
61+
* @param consumer the token event consumer.
62+
*/
63+
public void initialize(final JsonTokenConsumer consumer) throws IOException {
64+
Args.notNull(consumer, "Consumer");
65+
this.parser = jsonFactory.createNonBlockingByteArrayParser();
66+
this.inputFeeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();
67+
this.consumer = consumer;
68+
}
69+
70+
private void processData() throws IOException {
71+
for (; ; ) {
72+
final JsonToken jsonToken = parser.nextToken();
73+
if (jsonToken == null || jsonToken == JsonToken.NOT_AVAILABLE) {
74+
break;
75+
} else {
76+
consumer.accept(jsonToken.id(), parser);
77+
}
78+
}
79+
}
80+
81+
/**
82+
* Triggered to feed a chunk of data. As a result of this method call the tokenizer
83+
* may emit events to the token event consumer.
84+
*
85+
* @param data the chunk of data
86+
*/
87+
public void consume(final ByteBuffer data) throws IOException {
88+
if (data == null) {
89+
return;
90+
}
91+
if (consumer == null) {
92+
return;
93+
}
94+
if (data.hasArray()) {
95+
final int off = data.arrayOffset();
96+
inputFeeder.feedInput(data.array(), off + data.position(), off + data.limit());
97+
data.position(data.limit());
98+
} else {
99+
final byte[] tmp = new byte[data.remaining()];
100+
data.get(tmp);
101+
inputFeeder.feedInput(tmp, 0, tmp.length);
102+
}
103+
processData();
104+
}
105+
106+
/**
107+
* Triggered to signal the end of data stream. As a result of this method call the tokenizer
108+
* may emit events to the token event consumer.
109+
*/
110+
public void streamEnd() throws IOException {
111+
if (consumer == null) {
112+
return;
113+
}
114+
inputFeeder.endOfInput();
115+
processData();
116+
consumer.accept(JsonTokenId.ID_NO_TOKEN, parser);
117+
inputFeeder = null;
118+
parser = null;
119+
consumer = null;
120+
}
121+
122+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.core5.jackson2;
28+
29+
import java.io.IOException;
30+
31+
/**
32+
* Represents an event recipient that can react to JSON input.
33+
*
34+
* @since 5.5
35+
*/
36+
@FunctionalInterface
37+
public interface JsonConsumer<T> {
38+
39+
void accept(T input) throws IOException;
40+
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.core5.jackson2;
28+
29+
import com.fasterxml.jackson.core.JsonProcessingException;
30+
31+
/**
32+
* Signals a failure processing a malformed or unexpected JSON message
33+
*
34+
* @since 5.5
35+
*/
36+
public class JsonContentException extends JsonProcessingException {
37+
38+
public JsonContentException(final String msg) {
39+
super(msg);
40+
}
41+
42+
public JsonContentException(final String msg, final Throwable rootCause) {
43+
super(msg, rootCause);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)