Search in sources :

Example 1 with EncoderWrapper

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);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) EncoderWrapper(com.netflix.discovery.converters.wrappers.EncoderWrapper)

Example 2 with EncoderWrapper

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;
}
Also used : IOException(java.io.IOException) EncoderWrapper(com.netflix.discovery.converters.wrappers.EncoderWrapper)

Example 3 with EncoderWrapper

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);
}
Also used : DecoderWrapper(com.netflix.discovery.converters.wrappers.DecoderWrapper) Applications(com.netflix.discovery.shared.Applications) EncoderWrapper(com.netflix.discovery.converters.wrappers.EncoderWrapper) Test(org.junit.Test)

Example 4 with EncoderWrapper

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);
            }
        }
    }
}
Also used : DecoderWrapper(com.netflix.discovery.converters.wrappers.DecoderWrapper) IOException(java.io.IOException) EncoderWrapper(com.netflix.discovery.converters.wrappers.EncoderWrapper)

Example 5 with EncoderWrapper

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);
}
Also used : DecoderWrapper(com.netflix.discovery.converters.wrappers.DecoderWrapper) CodecWrapper(com.netflix.discovery.converters.wrappers.CodecWrapper) Applications(com.netflix.discovery.shared.Applications) EncoderWrapper(com.netflix.discovery.converters.wrappers.EncoderWrapper) CodecWrappers(com.netflix.discovery.converters.wrappers.CodecWrappers) Test(org.junit.Test)

Aggregations

EncoderWrapper (com.netflix.discovery.converters.wrappers.EncoderWrapper)16 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)14 Test (org.junit.Test)13 InstanceInfo (com.netflix.appinfo.InstanceInfo)9 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)7 CodecWrapper (com.netflix.discovery.converters.wrappers.CodecWrapper)3 Applications (com.netflix.discovery.shared.Applications)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 MyDataCenterInfo (com.netflix.appinfo.MyDataCenterInfo)1 Application (com.netflix.discovery.shared.Application)1 ReplicationInstance (com.netflix.eureka.cluster.protocol.ReplicationInstance)1 ReplicationInstanceResponse (com.netflix.eureka.cluster.protocol.ReplicationInstanceResponse)1 ReplicationList (com.netflix.eureka.cluster.protocol.ReplicationList)1 ReplicationListResponse (com.netflix.eureka.cluster.protocol.ReplicationListResponse)1 WebApplicationException (javax.ws.rs.WebApplicationException)1