GWT Restlet Parameters Always Null - google-app-engine

I am brand new to both REST and RESTlet- I got everything up and communicating last night but what I found this morning is that everything I pass into the server is always becoming null.
just as a sample app i have the following - a User Objectify entity (id, emailAddress, and version), and a RESTUserProxy object (id, emailAddress) - I wasn't originally sure if i could pass Objectify Entities back and after not being able to see anything switched it to the Proxy object - if i can get it to work this way I will try switching it back
the front end is as follows:
public interface RESTUserResourceProxy extends ClientProxy {
#Get
public void find(String emailAddress, Result<RESTUserProxy> callback);
#Put
public void persist(RESTUserProxy user, Result<Void> callback);
#Delete
public void delete(RESTUserProxy user, Result<Void> callback);
}
the backend code is as follows (this is currently extremely ugly - i got a little frustrated just trying to see something and put in a ton of sysouts)
public class RESTUserServerResource extends ServerResource implements RESTUserResource {
private final UserDao userDao;
public RESTUserServerResource() {
System.out.println("CREATED USER RESOURCE IMPL");
userDao = new UserDao();
}
#Override
#Get
public RESTUserProxy find() {
System.out.println("reference = " + getReference());
Form queryParams = getReference().getQueryAsForm();
System.out.println("query params = " + queryParams);
System.out.println("query = " + getQuery());
System.out.println("query string = " + getQuery().getQueryString());
String searchQuery = (String) getRequest().getAttributes().get("searchQuery");
System.out.println("search query = " + searchQuery) ;
return null;
// if (emailAddress == null) {
// return null;
// }
// System.out.println("user resource impl find [" + emailAddress + "]");
// final User user = userDao.find(emailAddress.getText());
// if (user != null) {
// System.out.println("found user ");
// return new RESTUserProxy(user.getId(), user.getEmailAddress());
// } else {
// System.out.println("found absolutely nothing");
// return null;
// }
}
#Override
#Put
public void persist(RESTUserProxy userProxy) {
System.out.println("user proxy = " + userProxy);
if (userProxy == null) {
return;
}
final User user = userDao.find(userProxy.getId());
user.setEmailAddress(userProxy.getEmailAddress());
user.setId(userProxy.getId());
userDao.persist(user);
}
#Override
#Delete
public void delete(RESTUserProxy userProxy) {
final User user = userDao.find(userProxy.getId());
userDao.delete(user);
}
}
what im having problems with is that eerythings coming through as null - a lot of other answers on here said to get the query to get the params - but here the query is null
below is the output of calling find and persist
reference = http://127.0.0.1:8888/users/123
query params = []
query = []
query string =
search query = null
i'm sure i'm doing something stupid here i just have no idea how to proceed right now. Any help as to what i'm doing wrong would be greatly appreciated.

This is due to GAE not supporting chunked encoding. See workaround here:
http://wiki.restlet.org/docs_2.1/13-restlet/21-restlet/318-restlet/303-restlet.html#dsy303-restlet_gwt

Related

SalesForce: Classic to Lighting

Apologies if my question my be so dumb but my programming skills are really limited and I'm on a hurry for a PoC.
I have the apex class below which was developed for Classic. I would like to make it work with lighting and I'm not sure if the only thing I need to replace are the url's. I have created a developer account for my PoC and everytime I launch the class I'm redirected to Classic.
public class LookupByUrlParamController {
String accountName;
String accountNumber;
String phone;
String website;
String email;
String socialhandle;
public LookupByUrlParamController () { }
public String redirectToAccount() {
Account account;
Map<String,String> params = ApexPages.currentPage().getParameters();
if(params.size() > 0) {
accountName = params.get('account_name');
accountNumber = params.get('account_number');
phone = params.get('phone');
website = params.get('website');
email = params.get('email');
socialhandle = params.get('SocialHandle');
}
try {
if(accountName != null) {
account = [select ID from Account where name = :accountName limit 1];
}
} catch (System.queryException e) {//no entry found for lookup item, display empty account page
return 'https://na7.salesforce.com/001/e';
}
try {
if(accountNumber != null) {
account = [select ID from Account where AccountNumber = :accountNumber limit 1];
}
} catch (System.queryException e) {//no entry found for lookup item, display empty account page
return 'https://na7.salesforce.com/001/e';
}
try {
if(phone != null) {
String npa;
String nnx;
String extension;
// Added logic for NA phone numbers
if (phone.length() == 10) {
npa = phone.substring(0,3);
nnx = phone.substring(3,6);
extension = phone.substring(6,10);
phone = '(' + npa + ') ' + nnx + '-' + extension;
}
account = [select ID from Account where phone = :phone limit 1];
}
} catch (System.queryException e) {//no entry found for lookup item, display empty account page
return 'https://na7.salesforce.com/001/e';
}
try {
if(website != null) {
account = [select ID from Account where website = :website limit 1];
}
} catch (System.queryException e) {//no entry found for lookup item, display empty account page
return 'https://na7.salesforce.com/001/e';
}
try {
if(email != null) {
account = [select ID from Account where email__c = :email limit 1];
}
} catch (System.queryException e) {//no entry found for lookup item, display empty account page
return 'https://na7.salesforce.com/001/e';
}
try {
if(socialhandle != null) {
account = [select ID from Account where SocialHandle__c = :socialhandle limit 1];
}
} catch (System.queryException e) {//no entry found for twitter handle lookup item, display empty account page
return 'https://na7.salesforce.com/001/e';
}
String accountUrl;
if(account != null) {
accountUrl = '/' + account.Id;
} else {
accountUrl = '/';
}
return accountUrl;
}
public static testMethod void testLookupByUrlParamAccount() {
LookupByUrlParamController controller = new LookupByUrlParamController();
controller.accountName = 'Avaya';
String redirectUrl = controller.redirectToAccount();
System.assertEquals(redirectUrl, '/001A0000007UkkFIAS');
}
public static testMethod void testLookupByUrlParamInvalidAccount() {
LookupByUrlParamController controller = new LookupByUrlParamController();
controller.accountName = '';
String redirectUrl = controller.redirectToAccount();
System.assertEquals(redirectUrl, 'https://na7.salesforce.com/001/e');
}
public static testMethod void testLookupByUrlParamPhone() {
LookupByUrlParamController controller = new LookupByUrlParamController();
controller.phone = '1234';
String redirectUrl = controller.redirectToAccount();
System.assertEquals(redirectUrl, '/001A0000007UkkFIAS');
}
public static testMethod void testLookupByUrlParamWherePhoneNumberIs10Chars() {
LookupByUrlParamController controller = new LookupByUrlParamController();
controller.phone = '1234567891';
String redirectUrl = controller.redirectToAccount();
System.assertEquals(redirectUrl, 'https://na7.salesforce.com/001/e');//no record found
}
public static testMethod void testLookupByUrlParamInvalidPhoneNumber() {
LookupByUrlParamController controller = new LookupByUrlParamController();
controller.phone = '';
String redirectUrl = controller.redirectToAccount();
System.assertEquals(redirectUrl, '/001A0000015EKVPIA4');
}
public static testMethod void testLookupByUrlParamAccountNumber() {
LookupByUrlParamController controller = new LookupByUrlParamController();
controller.accountNumber = '4321';
String redirectUrl = controller.redirectToAccount();
System.assertEquals(redirectUrl, '/001A0000007UkkFIAS');
}
public static testMethod void testLookupByUrlParam() {
LookupByUrlParamController controller = new LookupByUrlParamController();
String redirectUrl = controller.redirectToAccount();
System.assertEquals(redirectUrl, '/');
}
}
In addition if anyone can tell where to being looking in the documentation to simply launch to new customer record form, or what are the redirect URLS?
It's not quite clear what you mean by
everytime I launch the class I'm redirected to Classic
However, this code appears not to have been touched in quite a number of years and there's several things you ought to change.
You are hard-coding non-My Domain Salesforce instance URLs (na7.salesforce.com). You should instead use URL.getSalesforceBaseUrl().toExternalForm(), and you'll need to turn on My Domain sooner or later.
You are using Classic-format URLs, which still work but will result in additional redirects. The Lightning equivalent for the "Create new Account" URL is lightning/o/Account/new and for a specific record is lightning/r/Account/<Id>/view. When you build Lightning components, you can use the navigation service to get these URLs dynamically.
You have inline test methods, which haven't been allowed since an API version well before I started on the platform. Break those out into a separate test class.

Read data from Firebase

I'm using Firebase for my Android app, I want read datas. In particular I want to select an user with a specific id. When I use the debugger it seems that the code doesn't execute the onDataChange() instruction.
private User readUserById(){
final User u = new User("","","");
Query query = mDatabaseReferences.child("users").orderByChild("id").equalTo(id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
u.setId(ds.child("id").getValue(User.class).getId());
u.setNumber((ds.child("number").getValue(User.class).getNumber()));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return u;
}
private void initFirebase() {
FirebaseApp.initializeApp(this);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDatabaseReferences = mFirebaseDatabase.getReference();
}
public void sendCode(View v){
id= id.getText().toString();
readUserById();
phoneNumber = phoneText.getText().toString();
if (phoneNumber.equals("") || id.equals("")) {
Toast t = Toast.makeText(this, "Please insert a nickname and a valid phone number", Toast.LENGTH_LONG);
t.show();
} else {
setUpVerificationCallbacks();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,
60,
TimeUnit.SECONDS,
this,
verificationCallbacks
);
}
// }
}
Using the debugger I've seen that the 'id' value is correct.
I used Firebase documentation for sendCode(), the user registration works correctly, just like the sms sending. I want to check if the nickname already exists, and the value is in the 'id' Textview. I call the sendCode() through a button.
I've tried in this way but doesn't work. Running with the debugger the result of user is null
private User readUserByName(){
final User[] user = {new User()};
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("users").child(nick).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
user[0] = dataSnapshot.getValue(User.class);
Log.d("Tag", user[0].toString());
}
else
Log.e("Tag","No such user exists");
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return user[0];
}
This is how I save the User
I've launched the app with the debugger
Assuming that users node is a direct child of your Firebase root, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference testRef = rootRef.child("users").child("test");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String id = dataSnapshot.child("id").getValue(String.class);
String idName = dataSnapshot.child("idName").getValue(String.class);
String number = dataSnapshot.child("number").getValue(String.class);
Log.d("TAG", id + " / " + idName + " / " + number);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
testRef.addListenerForSingleValueEvent(eventListener);
The output will be:
test / f70eb... / number
Assumed that :
node "users" directly under root node.
You already know the id of the user and this id contains all the information under User.class
You only want to read a user, if exists in database.
All the getters and setter exists in User.class and a Public empty constructor exists
Here is how you should do it
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("users").ref(id).addListenerforSingleValueEvent(listener);
And in Listener's OnDataChange(DataSnapshot snap);
if(snap.exists()){
user = snap.getValue(User.class);
Log.d("Tag",user.toString());
}
else
Log.e("Tag","No such user exists");

RequestBody POJO always empty

I want to store some data from an HTML form (working with AngularJS) into my database, using Spring.
For this, I'm using the #RequestBody annotation with a POJO, but I can't make it work: my POJO is instancied, but it looks like the POJO attributes are not mapped with my form values (they are all null).
Controller :
#RequestMapping(value = "/createEntities", method = RequestMethod.POST)
#ResponseBody
public List<Entity> createEntities(#RequestBody final EntityList resource, #RequestParam final String kind) {
System.out.println("Creating entity for: " + kind);
Preconditions.checkNotNull(resource);
List<Entity> newEntities = new ArrayList<Entity>();
System.out.println("Entity test = " + resource.getTest()); // Prints "Entity test = null"
// Code below returns NullException
//System.out.println("Entity list nb = " + resource.getEntity().size());
if (resource.getEntities() != null && !resource.getEntities().isEmpty()) {
System.out.println("Entity list is OK");
for (EntityForm eForm : resource.getEntities()) {
if (eForm.getGrant() != null) {
Entity ent = new Entity();
if ("RTS".equals(kind)) {
ent.setDept(deptService.findByAbr(DeptEnum.RTS.name()));
} else {
ent.setDept(deptService.findByAbr(DeptEnum.RTB.name()));
}
ent.setGrant(eForm.getGrant());
ent.setCountry(eForm.getCountry());
ent.setName(eForm.getName());
ent = service.create(ent);
newEntities.add(ent);
}
}
}
return newEntities;
}
EntityList is the POJO for my form. This POJO contains a list of EntityForm (+ a string for test purpose), which is a DTO for my database entity Entity.
EntityList POJO :
public class EntityList implements Serializable {
private static final long serialVersionUID = 6335318686899794229L;
private List<EntityForm> entities;
private String test;
public EntityList() {
super();
}
public EntityList(List<EntityForm> entities, String test) {
super();
this.entities = entities;
this.test = test;
}
public List<EntityForm> getEntities() {
return entities;
}
public void setEntities(List<EntityForm> entities) {
this.entities = entities;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
I thought the problem came from a bad mapping between my list of entities in my form and my List<EntityForm> in my POJO, that's why I added a simple String to my POJO.
AngularJS side
Service :
app.factory("Entities", function($resource) {
return $resource("api/auth/entities", null,
{
createEntities: {method:'POST', url: "api/auth/entities/createEntities", params: { kind: '#kind' }, isArray:true}
});
})
Controller :
$scope.EntForm = {};
$scope.EntForm.entities = [];
$scope.EntForm.test = "myTest";
/* ... */
$scope.saveEnt= function() {
console.log($scope.EntForm);
Entities.createEntities($scope.EntForm,{kind:"RTS"},function(res) {
var msg = 'Entities created...';
ngToast.create(msg);
$location.path("/entities");
});
}
In my firefox console, I see that $scope.EntForm is correctly set (I have all my entity objects with the fields set, as well as the test string defined in the controller).
Result
All this code will display :
Creating entity for: RTS
Entity test = null
What am I doing wrong ?
Have you checked out the POST payload with Firefox developer tools, is your custom createEntities method working correctly?
(Would have added this as a comment, but unfortunately I don't yet have enough reputation for that.)
I had to remove the #RequestParam final String kind part from my Spring controller, and the param in AngularJS code.
To get the kind, I just added $scope.EntForm.kind = "theValueIWant" in my AngularJS controller.
I don't know if it's a good way to make it work in terms of good practice, but I get the #RequestBody content now.

I want to make a bean session scope using spring mvc and it works fine in my local host PC but do not work in google app engine

In my application, When one user will use a bean object it's life cycle have to be maintained as session in controller class also. Different user have to be different bean object. So I add session scope like:
<bean id="constants" class="com.project.barlexamquize.model.Constants" scope="session" >
<aop:scoped-proxy/>
</bean>
It works fine in local PC checked in different browser at a same time. But it does not work while this apps is deployed in Google App Engine. Here it behaves like request scope ie. in each http request it create new bean object. But my needs to make the bean session one time initialize for each user. My code are given bellow. Please visit: my site
#Controller
#RequestMapping("/barlexamquize")
public class BarlExamQuizeController {
public static long totalQuestion=390;
#Autowired Constants constants;
#RequestMapping(value = "/giveAnswer", method = RequestMethod.GET)
public String practiceQuestions(HttpServletRequest request, ModelMap model) {
PersistenceManager pm=null;
Query q=null;
List<BarlQuestion> barlQuestion = null;
pm = PMF.get().getPersistenceManager();
q= pm.newQuery(BarlQuestion.class, "id == idParameter");
q.declareParameters("long idParameter");
try {
barlQuestion = (List<BarlQuestion>) q.execute(constants.getId());
if (barlQuestion.isEmpty()) {
model.addAttribute("barlQuestion", null);
} else {
model.addAttribute("barlQuestion", barlQuestion.get(0));
}
} finally {
q.closeAll();
pm.close();
}
return "giveAnswer";
}
#RequestMapping(value = "/checkAnswer", method = RequestMethod.POST)
public String checkQuestionAnswer(#RequestParam String result,
ModelMap model) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(BarlQuestion.class, "id == idParameter");
q.declareParameters("long idParameter");
List<BarlQuestion> barlQuestion = null;
try {
barlQuestion = (List<BarlQuestion>) q.execute(constants.getId());
if (result.equals(barlQuestion.get(0).getAnswer())) {
constants.setRs("Right Choice");
constants.incrementRightAns();
} else
constants.setRs("Wrong!!! Correct: " + barlQuestion.get(0).getAnswer());
model.addAttribute("status", constants.getRs());
} finally {
q.closeAll();
pm.close();
}
return "questionResult";
}
#RequestMapping(value = "/nextQuestion", method = RequestMethod.POST)
public String nextQuestion(HttpServletRequest request, ModelMap model) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(BarlQuestion.class, "id == idParameter");
q.declareParameters("long idParameter");
List<BarlQuestion> barlQuestion = null;
if (constants.getId() < totalQuestion) {
constants.incrementId();
try {
barlQuestion = (List<BarlQuestion>) q.execute(constants.getId());
if (barlQuestion.isEmpty()) {
model.addAttribute("barlQuestion", null);
} else {
model.addAttribute("barlQuestion", barlQuestion.get(0));
}
} finally {
q.closeAll();
pm.close();
}
return "giveAnswer";
} else {
String score = ((constants.getRightAns() * 100) / totalQuestion) + "%";
model.addAttribute("score", score);
constants.setRightAns(0);
constants.setId(1);
return "examScore";
}
}
}
Please help me to find the problems of my code. For understanding my thoughts, My application has a Datastore of questions. Application shows one question at a time and take answer of that question, then shows next question. In the controller the constants.getId() is the concern. It is increment in the next question. So the incremental value should be remain in one session.
Make the Constants bean Serializable.

Updating properties for multiple users

How do I update a list of different Telephone, IPPhone using this
static void Main(string[] args)
{
Console.Write("Enter userid : "); // I would pass this in from the first
//Field in the .csv file 2439009
String username = Console.ReadLine();
try
{
DirectoryEntry myLdapConnection = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(cn=" + uid + ")";
search.PropertiesToLoad.Add("Telephone","IPPhone");
SearchResult result = search.FindOne();
if (result != null)
{
// create new object from search result
DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
// show existing title
Console.WriteLine("Current title : " + entryToUpdate.Properties["Telephone][0].ToString());
Console.Write("\n\nEnter new title : ");
// get new title and write to AD
String newTitle = Console.ReadLine();
entryToUpdate.Properties["Telephone"].Value = newTelePhone;
entryToUpdate.Properties["IPPhone"].Value = newIPPhone;
entryToUpdate.CommitChanges();
Console.WriteLine("\n\n...new title saved");
}
else Console.WriteLine("User not found!");
}
catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
}
}
static DirectoryEntry createDirectoryEntry()
{
// create and return new LDAP connection with desired settings
DirectoryEntry ldapConnection = new DirectoryEntry("mydomain.dm.com");
ldapConnection.Path = "LDAP://OU=myusers,DC=sales,DC=US,DC=US";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
I'm guessing you've grabbed someone else's code and don't know how to use it?
You should understand that this code can (will?) cause serious server problems as the DirectoryEntry resources are not closed correctly.
Every DirectoryEntry variable in your Main method should be wrapped in a using(){} statement.
Try something like this:
You define a class CSVRecord which holds your data from the CSV - read that in using FileHelpers. The class looks like this:
public class CSVRecord
{
public string EmployeeNumber { get; set; }
public string TelephoneNumber { get; set; }
public string IPPhoneNumber { get; set; }
}
Once you've read that class in, you need to iterate over its elements, and do the update for each of them.
CSVRecord[] listOfEmployees = (read in via FileHelpers)
// define root for searching your user accounts
using (DirectoryEntry root = new DirectoryEntry("LDAP://dc=yourcompany,dc=com"))
{
// set up directory searcher to find users by employeeId
using (DirectorySearcher searcher = new DirectorySearcher(root))
{
searcher.SearchScope = SearchScope.Subtree;
// iterate over all entries in your list of employees
foreach (CSVRecord csvEntry in listOfEmployees)
{
searcher.Filter = string.Format("(&(objectCategory=user)(employeeId={0}))", csvEntry.EmployeeNumber);
// search for that employee
SearchResult result = searcher.FindOne();
// if found - access the DirectoryEntry
if (result != null)
{
DirectoryEntry foundUser = result.GetDirectoryEntry();
// update properties from values in CSV
foundUser.Properties["telephoneNumber"].Value = csvEntry.TelephoneNumber;
foundUser.Properties["ipPhone"].Value = csvEntry.IPPhoneNumber;
// save changes back to directory
foundUser.CommitChanges();
}
}
}
}
Does that work for you??

Resources