Skip to content

Commit

Permalink
Added Jackson support (#351)
Browse files Browse the repository at this point in the history
* Added Jackson support

Signed-off-by: Laird Nelson <[email protected]>

* Fixed Javadoc error

Signed-off-by: Laird Nelson <[email protected]>

* Making PR changes per suggestions

Signed-off-by: Laird Nelson <[email protected]>

* Corrected checkstyle errors

Signed-off-by: Laird Nelson <[email protected]>
  • Loading branch information
ljnelson authored Feb 8, 2019
1 parent e7fc358 commit ff2e4e9
Show file tree
Hide file tree
Showing 14 changed files with 646 additions and 6 deletions.
10 changes: 10 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
<artifactId>helidon-media-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.media.jackson</groupId>
<artifactId>helidon-media-jackson-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.media.jackson</groupId>
<artifactId>helidon-media-jackson-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.media.jsonp</groupId>
<artifactId>helidon-media-jsonp-common</artifactId>
Expand Down
58 changes: 58 additions & 0 deletions media/jackson/common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
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.
-->

<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">
<parent>
<artifactId>helidon-media-jackson-project</artifactId>
<groupId>io.helidon.media.jackson</groupId>
<version>0.11.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-media-jackson-common</artifactId>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-http</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.media</groupId>
<artifactId>helidon-media-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.media.jackson.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.function.Function;

import io.helidon.common.http.DataChunk;
import io.helidon.common.http.MediaType;
import io.helidon.common.http.Reader;
import io.helidon.common.reactive.Flow;
import io.helidon.media.common.ContentReaders;
import io.helidon.media.common.ContentWriters;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Media type support for Jackson.
*/
public final class JacksonProcessing {

private JacksonProcessing() {
super();
}

/**
* Check whether the type is supported by Jackson.
*
* @param type media type to check
* @return true if the media type checked is supported by Jackson
*/
public static boolean isSupported(MediaType type) {
// See https://github.com/FasterXML/jackson-jaxrs-providers/blob/jackson-jaxrs-providers-2.9.4/json/src/main/java/com/fasterxml/jackson/jaxrs/json/JacksonJsonProvider.java#L167-L192
final boolean returnValue;
if (type == null) {
returnValue = true;
} else {
final String subtype = type.subtype();
if (subtype == null) {
returnValue = false;
} else {
returnValue = "json".equalsIgnoreCase(subtype)
|| subtype.endsWith("+json")
|| "javascript".equals(subtype)
|| "x-javascript".equals(subtype)
|| "x-json".equals(subtype);
}
}
return returnValue;
}

/**
* Returns a {@link Reader} that converts a {@link Flow.Publisher Publisher} of {@link java.nio.ByteBuffer}s to
* a Java object.
*
* <p>This method is intended for the derivation of other, more specific readers.</p>
*
* @param objectMapper the {@link ObjectMapper} to use; must not be {@code null}
* @return the byte array content reader that transforms a publisher of byte buffers to a completion stage that
* might end exceptionally with a {@link RuntimeException} in case of I/O error
* @exception NullPointerException if {@code objectMapper} is {@code null}
*/
public static Reader<Object> reader(final ObjectMapper objectMapper) {
Objects.requireNonNull(objectMapper);
return (publisher, cls) -> ContentReaders.byteArrayReader()
.apply(publisher)
.thenApply(bytes -> {
try {
return objectMapper.readValue(bytes, cls);
} catch (final IOException wrapMe) {
throw new JacksonRuntimeException(wrapMe.getMessage(), wrapMe);
}
});
}

/**
* Returns a function (writer) converting {@link Object}s to {@link Flow.Publisher Publisher}s
* of {@link DataChunk}s by using the supplied {@link ObjectMapper}.
*
* @param objectMapper the {@link ObjectMapper} to use; must not be {@code null}
* @return created function
* @exception NullPointerException if {@code objectMapper} is {@code null}
*/
public static Function<Object, Flow.Publisher<DataChunk>> writer(final ObjectMapper objectMapper) {
Objects.requireNonNull(objectMapper);
return payload -> {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
objectMapper.writeValue(baos, payload);
} catch (final IOException wrapMe) {
throw new JacksonRuntimeException(wrapMe.getMessage(), wrapMe);
}
return ContentWriters.byteArrayWriter(false)
.apply(baos.toByteArray());
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.media.jackson.common;

/**
* A {@link RuntimeException} that indicates a problem was encountered
* while performing JSON manipulation with Jackson.
*/
public class JacksonRuntimeException extends RuntimeException {

/**
* Creates a new {@link JacksonRuntimeException}.
*/
JacksonRuntimeException(final String message, final Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*/
/**
* Jackson media type support.
*/
package io.helidon.media.jackson.common;
31 changes: 31 additions & 0 deletions media/jackson/common/src/main/java9/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*/

/**
* Jackson support common classes.
*
* @see io.helidon.media.jackson.common.JacksonProcessing
*/
module io.helidon.media.jackson.common {

requires com.fasterxml.jackson.databind;
requires io.helidon.common;
requires io.helidon.common.http;
requires io.helidon.common.reactive;
requires io.helidon.media.common;

exports io.helidon.media.jackson.common;
}
37 changes: 37 additions & 0 deletions media/jackson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
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.
-->

<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.helidon.media</groupId>
<artifactId>helidon-media-project</artifactId>
<version>0.11.1-SNAPSHOT</version>
</parent>

<packaging>pom</packaging>
<groupId>io.helidon.media.jackson</groupId>
<artifactId>helidon-media-jackson-project</artifactId>

<modules>
<module>common</module>
<module>server</module>
</modules>
</project>
67 changes: 67 additions & 0 deletions media/jackson/server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
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.
-->

<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">
<parent>
<artifactId>helidon-media-jackson-project</artifactId>
<groupId>io.helidon.media.jackson</groupId>
<version>0.11.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-media-jackson-server</artifactId>
<name>Helidon Media Jackson WebServer Support</name>

<description>
Jackson Support for WebServer
</description>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.media.jackson</groupId>
<artifactId>helidon-media-jackson-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-test-support</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit ff2e4e9

Please sign in to comment.