Skip to content

Commit

Permalink
Merge pull request #634 from tbak/master
Browse files Browse the repository at this point in the history
Make XML dependencies optional in the client
  • Loading branch information
tbak committed Sep 8, 2015
2 parents 2e6665f + 8040e92 commit 13249e0
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 218 deletions.
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
buildscript {
repositories { jcenter() }

dependencies {
classpath 'com.netflix.nebula:gradle-extra-configurations-plugin:2.2.+'
}
}

plugins {
id 'nebula.netflixoss' version '2.2.7'
}
Expand All @@ -19,7 +27,7 @@ ext {
junit_version='4.10'
mockserverVersion='3.9.2'
jetty_version='7.2.0.v20101020'
jacksonVersion='2.4.3'
jacksonVersion='2.5.4'
woodstoxVersion='4.4.1'
}

Expand Down
12 changes: 10 additions & 2 deletions eureka-client/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'nebula-test-jar'
apply plugin: 'provided-base'

dependencies {
compile "com.netflix.netflix-commons:netflix-eventbus:0.1.2"
Expand All @@ -12,9 +13,16 @@ dependencies {
compile "org.apache.httpcomponents:httpclient:${apacheHttpClientVersion}"
compile "com.google.inject:guice:${guiceVersion}"
compile "com.netflix.governator:governator-annotations:${governatorVersion}"
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jacksonVersion}"

compile "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"

// Eureka client uses JSON encoding by default
provided "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jacksonVersion}"
// Prefered jackson Stax serializer. Default Oracle has issues (adds empty namespace) and is slower
compile "org.codehaus.woodstox:woodstox-core-asl:${woodstoxVersion}"
provided "org.codehaus.woodstox:woodstox-core-asl:${woodstoxVersion}"

runtime "org.codehaus.jettison:jettison:${jettisonVersion}"

testCompile project(':eureka-test-utils')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.ActionType;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.converters.wrappers.CodecWrappers;
import com.netflix.discovery.provider.DiscoveryJerseyProvider;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import com.netflix.discovery.shared.EurekaJerseyClient;
Expand Down Expand Up @@ -338,17 +336,14 @@ public synchronized BackupRegistry get() {
}

EurekaJerseyClientBuilder clientBuilder = new EurekaJerseyClientBuilder()
.withUserAgent("Java EurekaClient")
.withUserAgent("Java-EurekaClient")
.withConnectionTimeout(clientConfig.getEurekaServerConnectTimeoutSeconds() * 1000)
.withReadTimeout(clientConfig.getEurekaServerReadTimeoutSeconds() * 1000)
.withMaxConnectionsPerHost(clientConfig.getEurekaServerTotalConnectionsPerHost())
.withMaxTotalConnections(clientConfig.getEurekaServerTotalConnections())
.withConnectionIdleTimeout(clientConfig.getEurekaConnectionIdleTimeoutSeconds());

DiscoveryJerseyProvider discoveryJerseyProvider = new DiscoveryJerseyProvider(
CodecWrappers.getEncoder(clientConfig.getEncoderName()),
CodecWrappers.resolveDecoder(clientConfig.getDecoderName(), clientConfig.getClientDataAccept())
);
.withConnectionIdleTimeout(clientConfig.getEurekaConnectionIdleTimeoutSeconds())
.withEncoder(clientConfig.getEncoderName())
.withDecoder(clientConfig.getDecoderName(), clientConfig.getClientDataAccept());

clientAccept = EurekaAccept.fromString(clientConfig.getClientDataAccept());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.netflix.discovery.converters.jackson;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.netflix.appinfo.InstanceInfo;

/**
* @author Tomasz Bak
*/
public abstract class AbstractEurekaJacksonCodec {

protected static final Set<String> MINI_AMAZON_INFO_INCLUDE_KEYS = new HashSet<>(
Arrays.asList("instance-id", "public-ipv4", "public-hostname", "local-ipv4", "availability-zone")
);

public abstract ObjectMapper getObjectMapper();

public <T> void writeTo(T object, OutputStream entityStream) throws IOException {
getObjectMapper().writeValue(entityStream, object);
}

protected void addMiniConfig(ObjectMapper mapper) {
mapper.addMixInAnnotations(InstanceInfo.class, MiniInstanceInfoMixIn.class);
bindAmazonInfoFilter(mapper);
}

private void bindAmazonInfoFilter(ObjectMapper mapper) {
SimpleFilterProvider filters = new SimpleFilterProvider();
final String filterName = "exclude-amazon-info-entries";
mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
public Object findFilterId(Annotated a) {
if (Map.class.isAssignableFrom(a.getRawType())) {
return filterName;
}
return super.findFilterId(a);
}
});
filters.addFilter(filterName, new SimpleBeanPropertyFilter() {
@Override
protected boolean include(BeanPropertyWriter writer) {
return true;
}

@Override
protected boolean include(PropertyWriter writer) {
return MINI_AMAZON_INFO_INCLUDE_KEYS.contains(writer.getName());
}
});
mapper.setFilters(filters);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2015 Netflix, Inc.
*
* 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 com.netflix.discovery.converters.jackson;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.netflix.discovery.converters.KeyFormatter;

/**
* @author Tomasz Bak
*/
public class EurekaJsonJacksonCodec extends AbstractEurekaJacksonCodec {

private final ObjectMapper jsonMapper = new ObjectMapper();

public EurekaJsonJacksonCodec() {
this(KeyFormatter.defaultKeyFormatter(), false);
}

public EurekaJsonJacksonCodec(final KeyFormatter keyFormatter, boolean compact) {
// JSON
SimpleModule jsonModule = new SimpleModule();
jsonModule.setSerializerModifier(EurekaJacksonModifiers.createJsonSerializerModifier(keyFormatter));
jsonModule.setDeserializerModifier(EurekaJacksonModifiers.createJsonDeserializerModifier(keyFormatter, compact));
jsonMapper.registerModule(jsonModule);
jsonMapper.setSerializationInclusion(Include.NON_NULL);
jsonMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
jsonMapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
jsonMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
jsonMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

if (compact) {
addMiniConfig(jsonMapper);
}
}

@Override
public ObjectMapper getObjectMapper() {
return jsonMapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2015 Netflix, Inc.
*
* 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 com.netflix.discovery.converters.jackson;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.netflix.appinfo.DataCenterInfo;
import com.netflix.discovery.converters.KeyFormatter;

/**
* @author Tomasz Bak
*/
public class EurekaXmlJacksonCodec extends AbstractEurekaJacksonCodec {

private final XmlMapper xmlMapper;

public EurekaXmlJacksonCodec() {
this(KeyFormatter.defaultKeyFormatter(), false);
}

public EurekaXmlJacksonCodec(final KeyFormatter keyFormatter, boolean compact) {
xmlMapper = new XmlMapper() {
public ObjectMapper registerModule(Module module) {
setSerializerFactory(
getSerializerFactory().withSerializerModifier(EurekaJacksonModifiers.createXmlSerializerModifier(keyFormatter))
);
return super.registerModule(module);
}
};
xmlMapper.setSerializationInclusion(Include.NON_NULL);
xmlMapper.addMixInAnnotations(DataCenterInfo.class, DataCenterInfoXmlMixIn.class);
SimpleModule xmlModule = new SimpleModule();
xmlModule.setDeserializerModifier(EurekaJacksonModifiers.createXmlDeserializerModifier(keyFormatter, compact));
xmlMapper.registerModule(xmlModule);

if (compact) {
addMiniConfig(xmlMapper);
}
}

public ObjectMapper getObjectMapper() {
return xmlMapper;
}
}
Loading

0 comments on commit 13249e0

Please sign in to comment.