How to upload form data to google app engine - java

To upload data to the datastore I use this java code :
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Entity entity = new Entity("mydetail");
entity.setProperty("entry", "entry");
ds.put(entity);
For uploading form based data is this the correct method of uploading data, ie using similar code above or is there another API I should be using ?

Yes, this the direct API to the AppEngine Datastore.
You can also use JDO interface which allows for directly storing a Java object without dealing with the Datastore API:
import javax.jdo.annotations.Persistent;
#PersistenceCapable
public class MyDetail {
// ...
#Persistent
private String entry;
// ...
There is also the JPA interface. Both of the interfaces are described on the App Engine website.
The Objectify interface is very easy and for many situations easier. It is not part of the official SDK.
You can use whichever makes more sense for you application.

Related

Is there a .NET equivalent of DynamoDBMapper?

I'd like to construct a CreateTableRequest object from data annotations in a class. It looks like Amazon provides DynamoDBMapper in the Java SDK, which makes this process simple.
How can I do the same in .NET/C#?
AmazonDynamoDB dynamoDBClient = new AmazonDynamoDBClient();
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
CreateTableRequest req = mapper.generateCreateTableRequest(TestClass.class);
// Table provision throughput is still required since it cannot be specified in your POJO
req.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L));
// Fire off the CreateTableRequest using the low-level client
dynamoDBClient.createTable(req);
generateCreateTableRequest is unique to the Java SDK with no equivalent (yet) for the .NET SDK.
The general .NET equivalent for Java's DynamoDBMapper is the DynamoDBContext class (docs 1, docs 2). This class provides a high-level .NET object persistence model that enables you to map your client-side classes to Amazon DynamoDB tables.
However, as per docs:
The object persistence model does not provide an API to create, update, or delete tables. It provides only data operations. You can use only the AWS SDK for .NET low-level API to create, update, and delete tables.
You will have to use the low-level .NET API to create tables.

Using Mono<T> in Spring to populate a Cloud Firestore entity

I am using spring-cloud-gcp-starter-data-firestore for accessing Google Cloud Firestore in my Java Spring application.
Currently, my entity looks like this:
public class Subscription {
public String userId;
public String companyId;
// other properties
}
However, I obtain the userId and companyId via a reactor.core.publisher.Mono in org.springframework.security.core.context.ReactiveSecurityContextHolder.
How can I persist both properties which are nested inside Monos without resorting to Mono#block?
I am now using Mono#zipWith to combine both Monos. Then I am creating the entity inside Mono#flatMap.
service.getCompanyId()
.zipWith(service.getUserId())
.flatMap(objects -> createEntity(objects.getT1(), objects.getT2()))
I suggest following the tutorial in the Google codelab.
Here you can find that Firestore can be in Datastore mode which makes the previous tutorial suitable for you.

How to sanitize a string in google appengine?

I have an endpoint in Google appengine in java for letting user send me a message on my website.
The endpoint are generating with google SDK from a class like :
#Entity
public class Message {}
The message is persist with :
EntityManager mgr = getEntityManager();
mgr.persist(message);
How can i sanitize the string before persist it ?
I found this related post :Handling of HTML forms in App Engine (Java)
In advance thanks.
You can sanitize the text string using an existing library like Jsoup (#clean).
The choice of the specific tool depends on your requirements, but writing your own utility to sanitize text is not that simple unless you allow no tags at all.

Creating new kind in Google datastore using java

I have created a web application with google app engine.
I have a kind named userbean. It has details related to user such as name, age and email.
I have creater a bean class for it.
I have added and deleted entities to it.
But when I create a new kind called wordbean and tried to store data of this type to datastore, it doesn't save them in datastore. How to create a new kind and store it in google datastore?
Is there any rule that only one kind can exist in a datastore?
//controller class
//function which requests parameters from user through html, jquery
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn=datastore.beginTransaction();
Entity Word = new Entity("WordBean");
Word.setProperty("word", "அறிவியல்");
Word.setProperty("length", "5");
Word.setProperty("start", "அ");
Word.setProperty("starttwo", "அறி");
Word.setProperty("Detail", "பாடங்களில் ஒன்று.");
datastore.put(Word);
txn.commit();
//end

Caching with Google App Engine

When using the App Engine datastore for storing entities, what is the applied technique for caching.
I mean, without caching we simply do, something like this:
DatastoreService _ds = DatastoreServiceFactory.getDatastoreService();
public void put(String key, String value){
try {
Entity e = new Entity(createKey(key));
e.setProperty("key", key);
e.setProperty("value", value);
_ds.put(e);
} catch (Exception e) {
// handle exception
}
}
So where does caching kicks in? Also how does caching play during get methods.
Update:
Simply put my question would be when to do caching. My basic
implementation does not do caching at all, just plain put and get to
the Datastore.
Should caching be implemented on the lowest level API in my code or in a high level API, in my case, the lowest level API I have is this, the put and get to the Datastore.
There are really two kinds of caching to think about in app engine: memcache for ds entities and edge caching for static assets. This video from google covers both nicely with specific code examples:
Google I/O 2012 - Optimizing Your Google App Engine App
For edge caching you can also check out this post by Brandon Wirtz, as the documentation is a bit thin: enabling edge caching

Categories

Resources