-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #634 from tbak/master
Make XML dependencies optional in the client
- Loading branch information
Showing
15 changed files
with
322 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...nt/src/main/java/com/netflix/discovery/converters/jackson/AbstractEurekaJacksonCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
135 changes: 0 additions & 135 deletions
135
...a-client/src/main/java/com/netflix/discovery/converters/jackson/EurekaJacksonCodecNG.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
...client/src/main/java/com/netflix/discovery/converters/jackson/EurekaJsonJacksonCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...-client/src/main/java/com/netflix/discovery/converters/jackson/EurekaXmlJacksonCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.