Get specific data from protobuf's message - serialization

I understand probuffer as a kind of serialization like JSON. I try to parse and extract GeneratedMessageV3 data(generated by probuf) in Java.
public final class ProtoBufMessage extends GeneratedMessageV3 implements ProductJoinOrBuilder {
/*Some kind of code autogenerated by protoc*/
}
In case of JSON, there is JsonPointer to extract specific data. As we know, we can use JsonPointer just defining the path of the data we want to extract.
JsonPointer jsonPointer = Json.createPointer("/books/1");
Is there any way to extract specific data from protobuf's message just like JsonPointer?
Or is there any official api do same job??

Related

How to parse a custom schema

I have a custom schema, stored in a json file. I want to parse that schema. Unfortunately, that schema is an enterprise product, so I cannot find a way to parse it. Any approach how to parse a custom schema, I am quite stuck at this point.
One of the example of such schema is following:
{
typeid:org.name:prop1.0.0,
properties : {
typevalue:Float64,
length:4,
typeid:element,
description:"List to store elements"
}
}
One thing I was able to figure out that the typeid is analogous to $id in json-schema. So I think I can parse that. But I am unsure about the others.

Id Encryption in spring-hateoas or Spring Rest Data

I have a question about a standard pattern or mechanism in spring-hateoas or Spring Rest Data about encrypting the IDs of the Resources/Entities.
The reason I am asking, a requirement to our project is that we don't deliver the id's of our objects to the outside world and they should not be used in GET Requests as Parameters.
I know, Spring Rest Data and spring-hateoas does not give the ids of the objects unless they are configured so but even that case I can see the ids in links.
I know I can use PropertyEditors or Converters to encrypt/decrypt ids before and after Json serialisation/deseritalisation but I just like to know is there a more standard way?
Thx for answers...
If you have the unique 'business id' property of your resource you can configure SDR to use it instead of the entity ID.
First you have to create lookup method of your entity with this unique property:
public interface MyEntityRepo extends JpaRepository<MyEntity, Long> {
#RestResource(exported = false)
Optional<CatalogResource> findByMyUniqueProperty(String myUniqueProperty);
}
Then use it to configure SDR:
#Component
public class DataRestConfig extends RepositoryRestConfigurerAdapter {
#Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.withCustomEntityLookup()
.forRepository(MyEntityRepo.class, MyEntity::getMyUniqueProperty, MyEntityRepo::findByMyUniqueProperty);
super.configureRepositoryRestConfiguration(config);
}
}
After this customization you will have resource URI like this:
http://localhost:8080/myEntities/myUniquePropertyValue1

RESTful API endpoint for adding/removing array elements?

I have RESTful API built on top of a MongoDB store, so it's nice that you can store arrays. It's straightforward to create a new resource like this:
POST /users
{
items: [
1001, 1002, 1003
]
}
But how would the HTTP endpoint for adding a new item or removing an item would look like?
Right now, I have to specify the entire array, including elements that I don't want to touch:
PATCH /users/{id}
{
name: 'Bruce Wayne',
items: [
1001, 1002
]
}
Or pass in a mongodb query directly:
PATCH /users/{id}?query[$push][items]=1003
Is there a better way to do this?
Edit:
I like how StackMob's API does it. How do I update the name and remove an element from items at the same time though? For example, when I'm updating a bunch of the user's details on an admin dashboard? I don't think replacing the entire array is a good idea in mongodb?
Passing a mongodb query seems like a bad idea. Depending on your backend implementation it could lead to an attacker doing bad things to your data as in SQL Injection
You can model the modification of an attribute on a resource with PUT or PATCH with some limitations:
When using PUT the client is expected to send the whole representation of the resource. IT works for you but it may be cumbersome.
When using PATCH the client is expected to send the attributes that are intended to change, instead of the whole resource. Yet, you have to send the whole value, not just the additions or deletions of items to the value. Again it works but you are not in love with it.
I think you are looking for a way to model adding and removing items to the array:
I would model the array as a resource on its own: /users/:id/items
Accept POSTto add an item to the array and DELETE to remove from the array.
It's simple and RESTful.
As per the REST standards to create and delete a new request -->
POST -Create a new resource in a collection
and
DELETE -Delete a resource
I can give you an example of how the high-level HTTP endpoint in java looks like using Jersey.
You can have a Resource class with the HTTP Path specified and specific Paths for methods doing different operations.
So the URL could look like --
/rest/MyResource/Resource accompanied by a request JSON or XML(that contains your input data)
Here is a sample Resource class that would be your entry point(ofcourse you would have to do your configuration in web.xml to do URL mapping for this class) -->
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONObject;
public class SampleRESTServiceResource {
/**
* #param incomingJsonString
* #return Response
*/
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response createNewResource(JSONObject myJson) {
// Do a call to a DAO Implementation that does a JDBC call to insert into Mongo based on JSON
return null;
}
/**
* #param incomingJsonString
* #return Return response
*/
#DELETE
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public Response deleteResource(JSONObject myJson) {
// Do a call to a DAO Implementation that does a JDBC call to delete resource from Mongo based on JSON
return null;
}
}
If you want to try out an example you can refer to this page -->
https://www.ibm.com/developerworks/library/wa-aj-tomcat/

ORM for Spring-MongoDB integration with native querying support

I am a new to using Mongo DB and exploring the frameworks around for migrating from mysql to mongodb. So far from my findings I have been able to figure out SpringMongo as the best solution to my requirements.
The only problem is that instead of using a DSL based or abstract querying mechanism, I wished the framework allowed me to pass plain json string as arguments to the different methods exposed by the API(find, findOne) so that the query parameters can be written out to an external file (using a key to refer) and passed to the methods by reading and parsing at run time. But the framework should be capable of mapping the results to the domain objects.
Is there a way in spring-mongo to achieve this? Or is there any other frameworks on the same lines
You could use Spring Data to do that, just use the BasicQuery class instead of Query class. Your code will look like the following:
/* Any arbitrary string that could to parsed to DBObject */
Query q = new BasicQuery("{ filter : true }");
List<Entity> entities = this.template.find(q, Entity.class);
If you want more details:
http://static.springsource.org/spring-data/data-mongo/docs/current/reference/html/#mongo.query
http://static.springsource.org/spring-data/data-mongodb/docs/current/api/org/springframework/data/mongodb/core/query/BasicQuery.html
Well I got to find this one in the Spring data MongoOperations...
String jsonCommand = "{username: 'mickey'}";
MongoOperations mongoOps = //get mongooperations implemantation
mongoOps.executeCommand(jsonCommand)
It returns an instance of CommandResult that encapsulates the result.

Replace WCF default JSON serialization

Is it possible to replace the default JSON serialization of WCF (I'm currently testing with the webHttp behaviour), and passing application/json as the MIME type. In particular, I don't like that by default every property is a key/value pair like:
{"Key":"PropertyName", "Value":"PropertyValue"}
I'm using the service only for JSON-enabled endpoints (requesting data with jQuery + WCF).
You can use a message formatter to change the serializer used to deal with JSON. The post at https://learn.microsoft.com/en-us/archive/blogs/carlosfigueira/wcf-extensibility-message-formatters shows an example on how to change the default serializer (DataContractJsonSerializer) to another one (JSON.NET).
Consider creating classes corresponding to your JSON object structure. In that case you don't have to use Dictionary<> like:
[DataContract]
public class Customer
{
[DataMember(Name="name")]
public string Name{get;set;}
[DataMember(Name="id")]
public int ID{get;set;}
}
This get serialized as:
{"name": "name-value", "id": "id-value"}
Of course, this is just an alternative to what you already have and may not be applicable.