Jersey response.readEntity(...) sometimes returns null - jackson

Consider this snippet from my REST client (Jersey 2.26). It's used to post objects and return the response object. If the REST server returns an error (status >= 400), then instead of returning an entity of type T I want to read an entity of type ErrorMessage and throw an exception containing the error message object.
protected <T> T post(final Class<T> type,
final Object entity,
final Map<String, Object> queryParams,
final String methodPath,
final Object... arguments) {
return postResponse(
getInvocationBuilderJson(methodPath,
queryParams,
arguments),
entity
).readEntity(type);
}
protected Response postResponse(final Invocation.Builder invocationBuilder,
final Object entity) {
return handleErrors(
invocationBuilder.post(Entity.entity(entity,
MediaType.APPLICATION_JSON_TYPE))
);
}
protected Response handleErrors(final Response response) {
if (response.getStatus() >= 400) {
throw new InvocationException(response.readEntity(ErrorMessage.class));
}
return response;
}
If no error occurs (status < 400), then my object of type T is returned as expected. However, when an error does occur, response.readEntity(ErrorMessage.class) returns null. But (and this is the strange part), this does get me data (at the handleErrors method):
byte[] data = readAllBytes((InputStream) response.getEntity());
I could use that and deserialize it manually.. but I would first like to know if there are any options to fix this without implementing workarounds.

After switching from the default MOXy JSON (de)serializer (we now are using a GSON provider) the problem was resolved.
We recently had a similar issue with JSON-B. There is turned out we had to add a getter and setter on our error object on order to (de)serialize the object.

Related

Handle Azure Function input binding error

I have an Azure Function (version 4, with C# on .NET 6) that uses Cosmos DB input binding.
When the Cosmos DB input is querying for a document that does not exist it returns HTTP 404 response, however the Azure Function itself does not propagate this response and actually returns a HTTP 500 response.
How do I catch the Cosmos DB response to be able to act accordingly in the Azure function?
Or in other words - how do I perform error/http-response handling on the function's input bindings?
I hoped validating that the input argument is not null would be it,
and also tried to validate that the IEnumerable response is not empty,
but it turns out it doesn't even enter the code in the function when it gets the Cosmos DB 404 response. The function just breaks and the returns HTTP 500:
[FunctionName("GetItems")]
public IActionResult GetItems(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "my-db",
containerName: "my-container",
Connection = "my-conn-string",
PartitionKey = "my-pk")] IEnumerable<MyItem> items)
{
_logger.LogInformation("Get list of items");
if (items == null)
{
_logger.LogInformation("Items object is null");
return new NotFoundResult();
}
var itemsList = items as List<MyItem>;
if (itemsList.Count == 0)
{
_logger.LogInformation("Items object is missing");
return new NotFoundResult();
}
return new OkObjectResult(itemsList.First());
}
}
Below is the example of input binding error.
[FunctionName("EventHubTrigger")]
[FixedDelayRetry(5, "00:00:10")]
public static async Task Run([EventHubTrigger("myHub", Connection = "EventHubConnection")] EventData[] events, ILogger log)
{
// ...
}
and Here is the complete documentation on Handling binding errors.

spring amqp RPC copy headers from request to response

I'm looking for a way to copy some headers from the request message to the response message when I use RabbitMq in RPC mode.
so far I have tried with setBeforeSendReplyPostProcessors but I can only access the response and add headers to it. but I don't have access to the request to get the values I need.
I have also tried with the advice chain, but the returnObject is null after proceeding so I can't modify it (I admit I don't understand why it is null... I thought I could get the object to modify it):
#Bean
public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer simpleRabbitListenerContainerFactoryConfigurer, ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
simpleRabbitListenerContainerFactory.setAdviceChain(new MethodInterceptor() {
#Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object returnObject = invocation.proceed();
//returnObject is null here
return returnObject;
}
});
simpleRabbitListenerContainerFactoryConfigurer.configure(simpleRabbitListenerContainerFactory, connectionFactory);
return simpleRabbitListenerContainerFactory;
}
a working way is to change my method annotated with #RabbitListener so it returns a Message and there I can access both the requesting message (via arguments of the annotated method) and the response.
But I would like to do it automatically, since I need this feature at different places.
Basicaly I want to copy one header from the request message to the response.
this code do the job, but I want to do it through an aspect, or an interceptor.
#RabbitListener(queues = "myQueue"
, containerFactory = "simpleRabbitListenerContainerFactory")
public Message<MyResponseObject> execute(MyRequestObject myRequestObject, #Header("HEADER_TO_COPY") String headerToCopy) {
MyResponseObject myResponseObject = compute(myRequestObject);
return MessageBuilder.withPayload(myResponseObject)
.setHeader("HEADER_RESPONSE", headerToCopy)
.build();
}
The Message<?> return type support was added for this reason, but we could add an extension point to allow this, please open a GitHub issue.
Contributions are welcome.

The underlying connection was closed error while using .Include on EF objects

Following line of code gives me an error saying "The underlying connection was closed".
return this.repository.GetQuery<Countries>().Include(g => g.Cities).AsEnumerable().ToList();
But if I remove .Include(g => g.cities) it works fine.
this code is written in one of the operation in my WCF service, and I try to test it using WCF test client. I tried by calling this operation from MVC application also, and the same issue was occurring there too.
Also, i am using generic repository with entity framework
Repository code (only few important extract)
Constructor:
public GenericRepository(DbContext objectContext)
{
if (objectContext == null)
throw new ArgumentNullException("objectContext");
this._dbContext = objectContext;
this._dbContext.Configuration.LazyLoadingEnabled = false;
this._dbContext.Configuration.ProxyCreationEnabled = false;
}
GetQuery method:
public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
var entityName = GetEntityName<TEntity>();
return ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName);
}
Attempt#1
Created following overloads in repository code:
public IQueryable<TEntity> GetQuery<TEntity>(params string[] includes) where TEntity : class
{
var entityName = GetEntityName<TEntity>();
IQueryable<TEntity> query = ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName);
foreach(string include in includes)
{
query = query.Include(include);
}
return query;
}
public IQueryable<TEntity> GetQuery<TEntity>(Expression<Func<TEntity, bool>> predicate, params string[] includes) where TEntity : class
{
return GetQuery<TEntity>(includes).Where(predicate);
}
WCF is now trying to execute following line of code:
return this.repository.GetQuery<Countries>("Cities").AsEnumerable().ToList()
But it still gives the same error of "The underlying connection was closed". I tested it in WCF test client. However, when I debug the repository code it shows the navigation object getting included in result, but the issue seems occurring while trying to pass the output to client (WCF test client, or any other client)
After looking at the code you've now posted, I can conclude that, indeed, your DbContext is being closed at the end of the GetQuery method, and is thus failing when you try to use include. What you might want to do to solve it is to have an optional params variable for the GetQuery method that will take in some properties to be included, and just do the include right in the GetQuery method itself.

Rest Sharp Execute<Int> fails due to cast exception

I have this:
public Int32 NumberOfLocationsForCompany(int companyId)
{
var response = _curl.ResetRequest()
.WithPath(LOCATION_URL)
.AddParam("companyId", companyId.ToString())
.RequestAsGet()
.ProcessRequest<Int32>();
return response;
}
that calls this at the end.
public T ProcessRequest<T>() where T : new()
{
var response = _client.Execute<T>(_request);
if (response.ErrorException != null)
{
throw response.ErrorException;
}
return response.Data;
}
but I get this error. I don't get why it's trying to map an int to a collection or why it's Int64 vs the 32 I specified.: Unable to cast object of type 'System.Int64' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
When I hit the api directly this is what I get back
<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">17</int>
I feel it's something I'm not understanding about Rest Sharp. I tell the execute method to expect an Int, it receives and int, but is trying to map it to a collection. Why and where does the collection come from?
I have noticed that when I look into the base response object's Content the appropriate result "17" is present, why can't Rest Sharp find it? and still where is it finding the Collection?
When looking at the response object I found the return value was in Content vs in Data. I found this to be true whenever I was not returning an object or list of objects.
So now when I'm expecting an int, string, bool, etc I use the following and cast the type of the return value:
public string ProcessRequestWithValue()
{
var response = _client.Execute(_request);
if (response.ErrorException != null)
{
throw response.ErrorException;
}
return response.Content;
}
Hope this helps!

Converting an object into Dynamic with JSON.Net

What I'm wanting to do, is take an object construct (For example, an Entity Framework object), then convert it to a dynamic object (Thinking JSON.Net JObject may be the best fit), and extending said object with additional properties for sending to the client, or a View template.
dynamic model = JS.ToJObject(myConcreteInstance);
model.AdditionalValue = "I need this stuff on the client... ";
Here's what I have, which works, but would rather not have the try/catch.
//JS.ToJObject
public static JObject ToJObject(object input)
{
try {
//anonymous types throw an exception here
// Could not determine JSON object type for type f__AnonymousType ...
return new JObject(input);
} catch(Exception) {
//fallback to serialize/deserialize, which seems wasteful
var txt = JsonConvert.SerializeObject(
input
,new IsoDateTimeConverter()
,new DataTableConverter()
,new DataSetConverter()
);
return JObject.Parse(txt);
}
}