RestEasy: Validate that path parameter is integer before conversion to Integer object fails - resteasy

When I have a rest endpoint like this:
#GET
#Path("/user/{id}")
public User getUser(#PathParam("id") Integer id)
and the request URI is:
/user/abc
RestEasy tries to convert abc to Integer and fails with an exception:
org.jboss.resteasy.spi.BadRequestException: Unable to extract parameter from
http request: javax.ws.rs.PathParam("id") value is 'abc' for public org.mypkg.restapi.User
org.mypkg.restapi.resources.UserResource.getUser(java.lang.Integer)
Exceptions that describe java classes and methods are not suppose to be sent to client because he knows only URI, not how the URI is mapped and handled by java classes and methods.
It seems to me that every Rest API needs this (pre)validation, so I may be overlooking some mechanism in RestEasy. The same problem occurs with any other type that is not String, like for example Date.
So my question would be: What is the best practice to perform validation that the path parameter is a number before the conversion to Integer is attempted? And return an error message suited for client like "Path parameter id should be an integer" in case it is not?
In case RestEasy does not have a mechanism for this, what about other Rest frameworks?

Related

Parameter subscription must conform to pattern ^projects/[^/]+subscriptions/[^/]+$

.I am trying to create a data fusion pipeline which fetches data from pub sub and the used projection to convert message into string (as told by you) and thens ave it to GCS.
howeer,its giving me error: Parameter subscription must conform to pattern ^projects/[^/]+subscriptions/[^/]+$
I am using correct topic and subsctiption name projects/test/subscriptions/dataFusionSubscribe projects/test/topics/DataFusionTopic
You don't need to include the full subscription and topic name in the config, you only need to provide project id as test, subscription id as dataFusionSubscribe, and topic as DataFusionTopic
Attached is a screenshot showing the config for the Pubsub

Get query(search handler) configuration for a query using SolrJ

Is it possible to get the query configuration values(default + request parameters) using SolrJ ?
For example: If I direct a request to the RequestHandler using SolrJ, I would like to get a list of parameters(default + overridden request parameters) used on the query. I need this to log the current configuration when the query was made.
Try adding the parameter echoParams=all.
The echoParams parameter tells Solr what kinds of Request parameters
should be included in the response for debugging purposes, legal
values include:
none - don't include any request parameters for debugging
explicit -
include the parameters explicitly specified by the client in the
request
all - include all parameters involved in this request, either
specified explicitly by the client, or implicit because of the request
handler configuration.
Take a look at Common Query Parameters

What is the size limit of email body in messages.send method of Gmail API?

I am using official .net api client to send emails with attachments by messages.send method. When I attach a file of size more than approximately 5mb, I've come to
[JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.]
Newtonsoft.Json.JsonTextReader.ParseValue() +1187
Newtonsoft.Json.JsonTextReader.ReadInternal() +65
Newtonsoft.Json.JsonTextReader.Read() +28
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) +237
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) +783
Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) +293
Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) +274
Newtonsoft.Json.JsonConvert.DeserializeObject(String value, JsonSerializerSettings settings) +57
Google.Apis.Json.NewtonsoftJsonSerializer.Deserialize(String input) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Core\Apis\Json\NewtonsoftJsonSerializer.cs:120
Google.Apis.Services.<DeserializeError>d__8.MoveNext() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis\Apis\Services\BaseClientService.cs:286
[GoogleApiException: An Error occurred, but the error response could not be deserialized]
Google.Apis.Requests.ClientServiceRequest`1.Execute() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis\Apis\Requests\ClientServiceRequest.cs:102
I think client use Metadata URI, for metadata-only requests. A am going to try another option: Upload URI, for media upload requests.
It looks like there is a limit on email size that leads to exception of parsing error response in the client library.
So, the first question: is there a size limit?
Second, there is no info about how to use different upload methods via client, do you know any client library documentation?
Update: I hacked that request produced by
var request = gmailService.Users.Messages.Send(message, AccountUserId);
is going to https://www.googleapis.com/gmail/v1/users/me/messages/send. As I supposed it didn't use media upload request.
I ended up with limit on attachments total size. Here is code snippet.
Class level:
public const int MaxAttachmentsSize = 5 * 1024 * 1024;
Method level:
var attachmentsSize = 0;
foreach (var attachment in attachments)
{
attachmentsSize += attachment.Item1;
if (attachmentsSize > MaxAttachmentsSize) break;
mailMessage.Attachments.Add(attachment.Item2);
}
There is limit in MB of whole message. Google API allows you for quite big email but it may get timeout when sending if your service because of connection speed will be doing it too long.
According to this google docs it is 35MB:
google api send docs
For anything (uploading) over a few MB you should definitely use the /upload version of the method, otherwise yes you may run into those size limitations.
In response to the second part of your question...
Second, there is no info about how to use different upload methods via client, do you know any client library documentation?
I did a little poking around in the API, and I see that there is also a method that takes a stream as the 3rd parameter.
services.Users.Messages.Send( body, userId, stream, contentType)
Digging into the source code of that, I see that it seems to use a URL that looks like:
upload/....
I haven't tried it yet, and I don't know (yet) what it wants for a "stream", but this looks like a good possibility for getting a resumable upload with a bigger limit.

Amazon MWS Connection String Not Working

The connection string below is returning an 'Invalid Address' error. The error message indicates that the API Version is missing, but it is included in the string (see the last parameter). Not sure what the issue is.
https://mws.amazonservices.com/AWSAccessKeyId=[ID Hidden]&Action=GetFeedSubmissionList
&Marketplace=ATVPDKIKX0DER&Merchant=[Merchant Hidden]&Signature=[Signature Hidden]
&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2013-10-17T00:37:34.100Z&Version=2009-01-01
I usually send my MWS calls through a HTTP POST command, because that's the only way to do a SubmitFeed. The following assumes that a GET works at all (I didn't test):
Your call is missing the ? separator between the query and the rest of the URL. So as a minimum, this should read:
https://mws.amazonservices.com/?AWSAccessKeyId....

Sending Date and time via Cloud Endpoints

I have an object with a java.util.Date attribute I am serializing with the AE Cloud Endpoints service. From the client, when I just send a date, everything works fine (ex: '2013-05-27'). When I try adding a time, it fails:
{
"error" : {
"message" : "com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Invalid date/time format: 2013-05-27T12:00 (through reference chain: com.foo.MyObject[\"date\"])"
}
}
The best resource for jackson's default date format I have been able to find is here: http://wiki.fasterxml.com/JacksonFAQDateHandling. I tried the full ISO-8601 "1970-01-01T00:00:00.000+0000" which failed as well. I also tried an UNIX timestamp which didn't fail on parsing, but set a date in the 1372's.
So two part question. One, what's the correct default format to use? And two, can we modify (do we have access to) the jackson config so we can set our own serialization format?
Looks like it's close to the RFC 3339 standard, the fractional second appears to require 3 digits of precision, e.g.:
1985-04-12T23:20:50.520Z
This matches what's returned by the APIs Explorer, if you try it with your own API that includes a Date field.
With regards to handling the serialization yourself, you do not have access to the Jackson config, but you do have access to custom Endpoints serialization options (see #ApiSerializationProperty for instance).

Resources