use of com.netflix.discovery.converters.wrappers.EncoderWrapper in project eureka by Netflix.
the class DiscoveryJerseyProvider method writeTo.
@Override
public void writeTo(Object serializableObject, Class serializableClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap headers, OutputStream outputStream) throws IOException, WebApplicationException {
EncoderWrapper encoder = "json".equalsIgnoreCase(mediaType.getSubtype()) ? jsonEncoder : xmlEncoder;
// XML codec may not be available
if (encoder == null) {
throw new WebApplicationException(createErrorReply(400, "No codec available to serialize content type " + mediaType, mediaType));
}
encoder.encode(serializableObject, outputStream);
}
use of com.netflix.discovery.converters.wrappers.EncoderWrapper in project eureka by Netflix.
the class ResponseCacheImpl method getPayLoad.
/**
* Generate pay load with both JSON and XML formats for all applications.
*/
private String getPayLoad(Key key, Applications apps) {
EncoderWrapper encoderWrapper = serverCodecs.getEncoder(key.getType(), key.getEurekaAccept());
String result;
try {
result = encoderWrapper.encode(apps);
} catch (Exception e) {
logger.error("Failed to encode the payload for all apps", e);
return "";
}
if (logger.isDebugEnabled()) {
logger.debug("New application cache entry {} with apps hashcode {}", key.toStringCompact(), apps.getAppsHashCode());
}
return result;
}
use of com.netflix.discovery.converters.wrappers.EncoderWrapper in project eureka by Netflix.
the class EurekaCodecCompatibilityTest method testApplicationsEncodeDecode.
@Test
public void testApplicationsEncodeDecode() throws Exception {
final Applications applications = infoGenerator.takeDelta(2);
Action2 codingAction = new Action2() {
@Override
public void call(EncoderWrapper encodingCodec, DecoderWrapper decodingCodec) throws IOException {
String encodedString = encodingCodec.encode(applications);
Applications decodedValue = decodingCodec.decode(encodedString, Applications.class);
assertThat(EurekaEntityComparators.equal(applications, decodedValue), is(true));
}
};
verifyAllPairs(codingAction, Applications.class, availableJsonWrappers);
verifyAllPairs(codingAction, Applications.class, availableXmlWrappers);
}
use of com.netflix.discovery.converters.wrappers.EncoderWrapper in project eureka by Netflix.
the class EurekaCodecCompatibilityTest method verifyAllPairs.
public void verifyAllPairs(Action2 codingAction, Class<?> typeToEncode, List<CodecWrapper> codecHolders) throws Exception {
for (EncoderWrapper encodingCodec : codecHolders) {
for (DecoderWrapper decodingCodec : codecHolders) {
String pair = "{" + encodingCodec.codecName() + ',' + decodingCodec.codecName() + '}';
System.out.println("Encoding " + typeToEncode.getSimpleName() + " using " + pair);
try {
codingAction.call(encodingCodec, decodingCodec);
} catch (Exception ex) {
throw new Exception("Encoding failure for codec pair " + pair, ex);
}
}
}
}
use of com.netflix.discovery.converters.wrappers.EncoderWrapper in project eureka by Netflix.
the class EurekaCodecCompatibilityTest method testApplicationsJsonEncodeDecodeWithSingleAppItem.
/**
* For backward compatibility with LegacyJacksonJson codec single item arrays shall not be unwrapped.
*/
@Test
public void testApplicationsJsonEncodeDecodeWithSingleAppItem() throws Exception {
final Applications applications = infoGenerator.takeDelta(1);
Action2 codingAction = new Action2() {
@Override
public void call(EncoderWrapper encodingCodec, DecoderWrapper decodingCodec) throws IOException {
String encodedString = encodingCodec.encode(applications);
assertThat(encodedString.contains("\"application\":[{"), is(true));
Applications decodedValue = decodingCodec.decode(encodedString, Applications.class);
assertThat(EurekaEntityComparators.equal(applications, decodedValue), is(true));
}
};
List<CodecWrapper> jsonCodes = Arrays.asList(new CodecWrappers.LegacyJacksonJson(), new CodecWrappers.JacksonJson());
verifyAllPairs(codingAction, Applications.class, jsonCodes);
}
Aggregations