org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType Java Examples

The following examples show how to use org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RestResponseMarshallingTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal the response.
 */
@Test
public void testJsonMarshalling() throws Exception {
	final R expected = getTestResponseInstance();

	ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
	final String marshalled = objectMapper.writeValueAsString(expected);

	final Collection<Class<?>> typeParameters = getTypeParameters();
	final JavaType type;

	if (typeParameters.isEmpty()) {
		type = objectMapper.getTypeFactory().constructType(getTestResponseClass());
	} else {
		type = objectMapper.getTypeFactory().constructParametricType(
			getTestResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	final R unmarshalled = objectMapper.readValue(marshalled, type);
	assertOriginalEqualsToUnmarshalled(expected, unmarshalled);
}
 
Example #2
Source File: RestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #3
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #4
Source File: RestResponseMarshallingTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal the response.
 */
@Test
public void testJsonMarshalling() throws Exception {
	final R expected = getTestResponseInstance();

	ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
	final String marshalled = objectMapper.writeValueAsString(expected);

	final Collection<Class<?>> typeParameters = getTypeParameters();
	final JavaType type;

	if (typeParameters.isEmpty()) {
		type = objectMapper.getTypeFactory().constructType(getTestResponseClass());
	} else {
		type = objectMapper.getTypeFactory().constructParametricType(
			getTestResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	final R unmarshalled = objectMapper.readValue(marshalled, type);
	assertOriginalEqualsToUnmarshalled(expected, unmarshalled);
}
 
Example #5
Source File: RestResponseMarshallingTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal the response.
 */
@Test
public void testJsonMarshalling() throws Exception {
	final R expected = getTestResponseInstance();

	ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
	final String marshalled = objectMapper.writeValueAsString(expected);

	final Collection<Class<?>> typeParameters = getTypeParameters();
	final JavaType type;

	if (typeParameters.isEmpty()) {
		type = objectMapper.getTypeFactory().constructType(getTestResponseClass());
	} else {
		type = objectMapper.getTypeFactory().constructParametricType(
			getTestResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	final R unmarshalled = objectMapper.readValue(marshalled, type);
	assertOriginalEqualsToUnmarshalled(expected, unmarshalled);
}
 
Example #6
Source File: RestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #7
Source File: JobResultDeserializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public JobResultDeserializer() {
	super(JobResult.class);
	final JavaType objectSerializedValueType = TypeFactory.defaultInstance()
		.constructType(new TypeReference<SerializedValue<Object>>() {
		});
	serializedValueDeserializer = new SerializedValueDeserializer(objectSerializedValueType);
}
 
Example #8
Source File: JobResultDeserializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public JobResultDeserializer() {
	super(JobResult.class);
	final JavaType objectSerializedValueType = TypeFactory.defaultInstance()
		.constructType(new TypeReference<SerializedValue<Object>>() {
		});
	serializedValueDeserializer = new SerializedValueDeserializer(objectSerializedValueType);
}
 
Example #9
Source File: SerializedValueSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	objectMapper = new ObjectMapper();
	final SimpleModule simpleModule = new SimpleModule();
	final JavaType serializedValueWildcardType = objectMapper
		.getTypeFactory()
		.constructType(new TypeReference<SerializedValue<?>>() {
		});
	simpleModule.addSerializer(new SerializedValueSerializer(serializedValueWildcardType));
	simpleModule.addDeserializer(
		SerializedValue.class,
		new SerializedValueDeserializer(serializedValueWildcardType));
	objectMapper.registerModule(simpleModule);
}
 
Example #10
Source File: JobResultDeserializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public JobResultDeserializer() {
	super(JobResult.class);
	final JavaType objectSerializedValueType = TypeFactory.defaultInstance()
		.constructType(new TypeReference<SerializedValue<Object>>() {
		});
	serializedValueDeserializer = new SerializedValueDeserializer(objectSerializedValueType);
}
 
Example #11
Source File: JobResultSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public JobResultSerializer() {
	super(JobResult.class);

	final JavaType objectSerializedValueType = TypeFactory.defaultInstance()
		.constructType(new TypeReference<SerializedValue<Object>>() {
		});
	serializedValueSerializer = new SerializedValueSerializer(objectSerializedValueType);
}
 
Example #12
Source File: JobResultSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public JobResultSerializer() {
	super(JobResult.class);

	final JavaType objectSerializedValueType = TypeFactory.defaultInstance()
		.constructType(new TypeReference<SerializedValue<Object>>() {
		});
	serializedValueSerializer = new SerializedValueSerializer(objectSerializedValueType);
}
 
Example #13
Source File: SerializedValueSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	objectMapper = new ObjectMapper();
	final SimpleModule simpleModule = new SimpleModule();
	final JavaType serializedValueWildcardType = objectMapper
		.getTypeFactory()
		.constructType(new TypeReference<SerializedValue<?>>() {
		});
	simpleModule.addSerializer(new SerializedValueSerializer(serializedValueWildcardType));
	simpleModule.addDeserializer(
		SerializedValue.class,
		new SerializedValueDeserializer(serializedValueWildcardType));
	objectMapper.registerModule(simpleModule);
}
 
Example #14
Source File: JobResultSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public JobResultSerializer() {
	super(JobResult.class);

	final JavaType objectSerializedValueType = TypeFactory.defaultInstance()
		.constructType(new TypeReference<SerializedValue<Object>>() {
		});
	serializedValueSerializer = new SerializedValueSerializer(objectSerializedValueType);
}
 
Example #15
Source File: SerializedValueSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	objectMapper = new ObjectMapper();
	final SimpleModule simpleModule = new SimpleModule();
	final JavaType serializedValueWildcardType = objectMapper
		.getTypeFactory()
		.constructType(new TypeReference<SerializedValue<?>>() {
		});
	simpleModule.addSerializer(new SerializedValueSerializer(serializedValueWildcardType));
	simpleModule.addDeserializer(
		SerializedValue.class,
		new SerializedValueDeserializer(serializedValueWildcardType));
	objectMapper.registerModule(simpleModule);
}
 
Example #16
Source File: RestClient.java    From flink with Apache License 2.0 4 votes vote down vote up
public <M extends MessageHeaders<R, P, U>, U extends MessageParameters, R extends RequestBody, P extends ResponseBody> CompletableFuture<P> sendRequest(
		String targetAddress,
		int targetPort,
		M messageHeaders,
		U messageParameters,
		R request,
		Collection<FileUpload> fileUploads,
		RestAPIVersion apiVersion) throws IOException {
	Preconditions.checkNotNull(targetAddress);
	Preconditions.checkArgument(0 <= targetPort && targetPort < 65536, "The target port " + targetPort + " is not in the range (0, 65536].");
	Preconditions.checkNotNull(messageHeaders);
	Preconditions.checkNotNull(request);
	Preconditions.checkNotNull(messageParameters);
	Preconditions.checkNotNull(fileUploads);
	Preconditions.checkState(messageParameters.isResolved(), "Message parameters were not resolved.");

	if (!messageHeaders.getSupportedAPIVersions().contains(apiVersion)) {
		throw new IllegalArgumentException(String.format(
			"The requested version %s is not supported by the request (method=%s URL=%s). Supported versions are: %s.",
			apiVersion,
			messageHeaders.getHttpMethod(),
			messageHeaders.getTargetRestEndpointURL(),
			messageHeaders.getSupportedAPIVersions().stream().map(RestAPIVersion::getURLVersionPrefix).collect(Collectors.joining(","))));
	}

	String versionedHandlerURL = "/" + apiVersion.getURLVersionPrefix() + messageHeaders.getTargetRestEndpointURL();
	String targetUrl = MessageParameters.resolveUrl(versionedHandlerURL, messageParameters);

	LOG.debug("Sending request of class {} to {}:{}{}", request.getClass(), targetAddress, targetPort, targetUrl);
	// serialize payload
	StringWriter sw = new StringWriter();
	objectMapper.writeValue(sw, request);
	ByteBuf payload = Unpooled.wrappedBuffer(sw.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));

	Request httpRequest = createRequest(targetAddress + ':' + targetPort, targetUrl, messageHeaders.getHttpMethod().getNettyHttpMethod(), payload, fileUploads);

	final JavaType responseType;

	final Collection<Class<?>> typeParameters = messageHeaders.getResponseTypeParameters();

	if (typeParameters.isEmpty()) {
		responseType = objectMapper.constructType(messageHeaders.getResponseClass());
	} else {
		responseType = objectMapper.getTypeFactory().constructParametricType(
			messageHeaders.getResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	return submitRequest(targetAddress, targetPort, httpRequest, responseType);
}
 
Example #17
Source File: SerializedValueSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
public SerializedValueSerializer(final JavaType javaType) {
	super(javaType);
}
 
Example #18
Source File: RestClient.java    From flink with Apache License 2.0 4 votes vote down vote up
public <M extends MessageHeaders<R, P, U>, U extends MessageParameters, R extends RequestBody, P extends ResponseBody> CompletableFuture<P> sendRequest(
		String targetAddress,
		int targetPort,
		M messageHeaders,
		U messageParameters,
		R request,
		Collection<FileUpload> fileUploads,
		RestAPIVersion apiVersion) throws IOException {
	Preconditions.checkNotNull(targetAddress);
	Preconditions.checkArgument(NetUtils.isValidHostPort(targetPort), "The target port " + targetPort + " is not in the range [0, 65535].");
	Preconditions.checkNotNull(messageHeaders);
	Preconditions.checkNotNull(request);
	Preconditions.checkNotNull(messageParameters);
	Preconditions.checkNotNull(fileUploads);
	Preconditions.checkState(messageParameters.isResolved(), "Message parameters were not resolved.");

	if (!messageHeaders.getSupportedAPIVersions().contains(apiVersion)) {
		throw new IllegalArgumentException(String.format(
			"The requested version %s is not supported by the request (method=%s URL=%s). Supported versions are: %s.",
			apiVersion,
			messageHeaders.getHttpMethod(),
			messageHeaders.getTargetRestEndpointURL(),
			messageHeaders.getSupportedAPIVersions().stream().map(RestAPIVersion::getURLVersionPrefix).collect(Collectors.joining(","))));
	}

	String versionedHandlerURL = "/" + apiVersion.getURLVersionPrefix() + messageHeaders.getTargetRestEndpointURL();
	String targetUrl = MessageParameters.resolveUrl(versionedHandlerURL, messageParameters);

	LOG.debug("Sending request of class {} to {}:{}{}", request.getClass(), targetAddress, targetPort, targetUrl);
	// serialize payload
	StringWriter sw = new StringWriter();
	objectMapper.writeValue(sw, request);
	ByteBuf payload = Unpooled.wrappedBuffer(sw.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));

	Request httpRequest = createRequest(targetAddress + ':' + targetPort, targetUrl, messageHeaders.getHttpMethod().getNettyHttpMethod(), payload, fileUploads);

	final JavaType responseType;

	final Collection<Class<?>> typeParameters = messageHeaders.getResponseTypeParameters();

	if (typeParameters.isEmpty()) {
		responseType = objectMapper.constructType(messageHeaders.getResponseClass());
	} else {
		responseType = objectMapper.getTypeFactory().constructParametricType(
			messageHeaders.getResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	return submitRequest(targetAddress, targetPort, httpRequest, responseType);
}
 
Example #19
Source File: SerializedValueDeserializer.java    From flink with Apache License 2.0 4 votes vote down vote up
public SerializedValueDeserializer(final JavaType valueType) {
	super(valueType);
}
 
Example #20
Source File: SerializedValueSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
public SerializedValueSerializer(final JavaType javaType) {
	super(javaType);
}
 
Example #21
Source File: SerializedValueDeserializer.java    From flink with Apache License 2.0 4 votes vote down vote up
public SerializedValueDeserializer(final JavaType valueType) {
	super(valueType);
}
 
Example #22
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public <M extends MessageHeaders<R, P, U>, U extends MessageParameters, R extends RequestBody, P extends ResponseBody> CompletableFuture<P> sendRequest(
		String targetAddress,
		int targetPort,
		M messageHeaders,
		U messageParameters,
		R request,
		Collection<FileUpload> fileUploads,
		RestAPIVersion apiVersion) throws IOException {
	Preconditions.checkNotNull(targetAddress);
	Preconditions.checkArgument(0 <= targetPort && targetPort < 65536, "The target port " + targetPort + " is not in the range (0, 65536].");
	Preconditions.checkNotNull(messageHeaders);
	Preconditions.checkNotNull(request);
	Preconditions.checkNotNull(messageParameters);
	Preconditions.checkNotNull(fileUploads);
	Preconditions.checkState(messageParameters.isResolved(), "Message parameters were not resolved.");

	if (!messageHeaders.getSupportedAPIVersions().contains(apiVersion)) {
		throw new IllegalArgumentException(String.format(
			"The requested version %s is not supported by the request (method=%s URL=%s). Supported versions are: %s.",
			apiVersion,
			messageHeaders.getHttpMethod(),
			messageHeaders.getTargetRestEndpointURL(),
			messageHeaders.getSupportedAPIVersions().stream().map(RestAPIVersion::getURLVersionPrefix).collect(Collectors.joining(","))));
	}

	String versionedHandlerURL = "/" + apiVersion.getURLVersionPrefix() + messageHeaders.getTargetRestEndpointURL();
	String targetUrl = MessageParameters.resolveUrl(versionedHandlerURL, messageParameters);

	LOG.debug("Sending request of class {} to {}:{}{}", request.getClass(), targetAddress, targetPort, targetUrl);
	// serialize payload
	StringWriter sw = new StringWriter();
	objectMapper.writeValue(sw, request);
	ByteBuf payload = Unpooled.wrappedBuffer(sw.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));

	Request httpRequest = createRequest(targetAddress + ':' + targetPort, targetUrl, messageHeaders.getHttpMethod().getNettyHttpMethod(), payload, fileUploads);

	final JavaType responseType;

	final Collection<Class<?>> typeParameters = messageHeaders.getResponseTypeParameters();

	if (typeParameters.isEmpty()) {
		responseType = objectMapper.constructType(messageHeaders.getResponseClass());
	} else {
		responseType = objectMapper.getTypeFactory().constructParametricType(
			messageHeaders.getResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	return submitRequest(targetAddress, targetPort, httpRequest, responseType);
}
 
Example #23
Source File: SerializedValueSerializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public SerializedValueSerializer(final JavaType javaType) {
	super(javaType);
}
 
Example #24
Source File: SerializedValueDeserializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public SerializedValueDeserializer(final JavaType valueType) {
	super(valueType);
}