Making an Ajax request in Lift - scala

I am trying out the Lift web framework, but I am having a hard time understanding a basic aspect.
I have lift 2.5 M4 running on my localhost:8080. How can I make a CURL request with some parameters, for instance localhost:8080?category=apples&name=flowers, get those paremeters inside Scala, perform a Mongo query based on those paremeters and display JSON output for an Ajax client?
A very basic example of creating a page and getting the request header and paremeters would be awesome.
The page must be a REST service type. Which means it's not using Lift's templating system to generate the buttons etc, as the Lift Cookbook examples illustrate.

There is an example at Simply Lift, it should work and give you a JSON / XNK output without involving template system.
If you need get the parameter in your callback, just use net.liftweb.http.S.param(name), it will give you a Box[String], where holds the parameter if it exists.

Related

Decorator or dispather for RESTful URL routing in CherryPy

I want to route requests in a way similar to how Python web.py or Python bottle does it.
With these frameworks you can simply add tags into the URL which can be used to pass parameters, eg:
#route('/api/part1/<tag1name>/part2/<tag2name>')
def handlerfunction(self, tag1name, tag2name):
CherryPy does have the popargs decorator, eg
#cherrypy.popargs('name')
However when there is more than one value encoded in the URI it becomes quite inconvenient to get them assigned to the parameters the right way.
In addition I want to be able to route based on the HTTP request method, as per the CherryPy MethodDispatcher.
The answer is to use a URL Route-mapping module such as "Routes". "Routes" is developed firstly for Pylons but does have support for CherryPy.
From the Documentation:
Routes is a Python re-implementation of the Rails routes system for mapping URLs to application actions, and conversely to generate URLs. Routes makes it easy to create pretty and concise URLs that are RESTful with little effort.
Routes allows conditional matching based on domain, cookies, HTTP method, or a custom function. Sub-domain support is built in. Routes comes with an extensive unit test suite.
After working through the documentation and getting it to work with CherryPy I will update this answer with some CherryPy specific implementation examples.

How to start with writing a RESTful service?

I am trying to create a RESTful web service that accepts JSON arguments and gives out a JSON response.
What I want is to accept HTTP requests made to my URL endpoint.
Something like,
POST /the/endpoint HTTP/1.1
Host: mywebsite.com
{"name":"yourname", "department":"your_department"}
Do a DB read at the backend and give relevant parameters like, say Manager name, salary etc as a JSON object, as the response.
What's the best way to go about it? I was thinking of using Java servlets for this? Is there a better way?
PS - I am just getting started so detailed answers or links to tutorials as to how to implement it would be much appreciated.
Thanks.
Yes you can easily do this with Servlets and some Json Libs for Marshalling /unmarshalling the Json Object to Java Object.
You can make use of Json libs like
Jackson ,
Gson etc
But you must know that REST application doesnt end with just handling the request and response , but it needs to take care of other non-functional requirements like
Authentication
Authorization
Security etc
Building this from a scratch from a Servlet is overkill and waste of time when there are ready made frameworks that these things for you
My favorite is Spring MVC 3.0
Check their project site for more details
Just to show you how easy to set up one in Spring MVC , check this below tutorial
Spring 3 REST Tutorial
Pls rate the post if it helps , Cheers.
If you want to go with Java, I suggest that you take a look at JAX-RS... And since REST is a complex topic, here is a url with tons of informations on it. http://code.google.com/p/implementing-rest/
As a complete beginner, I believe the best way to implement a (nearly) RESTful API without having to read a lot is simply to implement the API just using HTML pages and HTML forms with the back-end processing to handle them.
The rules are:
Use <a> tags to provide links to related resources. (navigation)
Use <form> tags to initiate any kind of processing operation on the server. (actions)
You can then make it properly RESTful by using progressive enhancement to add Javascript AJAX requests that perform PUT, PATCH, and DELETE instead of using POST for those three (of course, keeping POST for creating resources where the client doesn't know the resultant URI).
You can then click around and test the API in a web browser! Tools like Selenium can automate this.
If you need to provide JSON, this can be added after the API has been designed and tested, although libraries exist to process HTML or XHTML responses too, so JSON isn't necessarily required for machine readability.
if you are using php with symfony try:https://github.com/FriendsOfSymfony/FOSRestBundle this lets you create a real REST full servicer very quick.
Vogella made my day very easy when i started Web services with an super example here with eclipse screen shots ..Have a look here.

How to get the result of a JSP in a java method?

I am working on a restaurant web portal which will send periodic emails to the registered users such as changes in menu, discounts and offers, birthday wishes etc.
Here is how I am doing it-
I create a JSP page with the HTML formatting. This page accepts certain parameters that will customize the page.
The MailDaemon scans the db and upon a certain trigger will make a GET request to the JSP.
The returned JSP represents the final mail content which is then sent using the JavaMail API.
My questions are these -
Do I really need to make a GET request? Or can I obtain the JSP result directly in my Daemon class?
Does my approach have any drawbacks? Any better solutions (architecturally speaking)
Any other templating solutions for the job?
It's possible to buffer a response from your JSP using javax.servlet.RequestDispatcher#include by wrapping original response in javax.servlet.ServletResponseWrapper (in case your MailDaemon is a servlet)
Your approach is OK. Though it'll be easier to leverage another templating engine.
Apache Velocity or FreeMarker. These two are not tied to web, so you can easily use them without servlet container.
If I understand correctly, you're using the JSP as a templating engine to generate the body of an HTML email. If you want to keep using JSPs for that, I don't think there's a better solution than making a GET request to get the generated HTML.
But you could also use an embeddable templating engine like Velocity or FreeMarker to generate the HTML to a String, directly from the mail daemon.

Managing state with scalatra

I understand that Scalatra is a lightweight framework. However, I'm wondering if there are any tricks for managing small amounts of state. I have a form with a textarea and a few checkboxes. For example, suppose the textarea contains a math equation and the checkboxes some additional options. When I submit (method=POST), I would like to display a result beneath the form, but maintain the same options (same text in the textarea, same checkboxes checked).
Is there a good way to accomplish this? Or do I need to manually set the values when generating the HTML? I'm presently generating HTML using Jade.
Do it server side with a resource returning JSON, and call it dynamically using JavaScript XHR.
No need of state! Go away Evil State!
EDIT
~A bit more explanation~
You create a webservice that take computation parameter as input (using ?= in url with GET, or better using JSON payload with POST), then the service do the computation and return a JSON representation of the result
You call that scalatra webservice using AJAX from your web application and you update the content of the webpage using the JSON result returned by the web server
If you don't already know much about AJAX, read tutorials and I think you will understand better what I mean. You can too search for example of Scalatra resource that return JSON representation (using lift-json i.e.)
As a side note, you can take a look at Bowler: http://www.bowlerframework.org
which make REST development on scalatra easier.

What's causing this SoapMapper error in VBA Soap Service call?

I'm trying to consume a Java based SOAP web service from VBA code in an Excel 2003 workbook. There are two methods available. One retrieves data, the other uploads data. The service for retrieving data works fine. However, when we try to upload data, we're running into a strange error that I just cannot find good information about on Google. The error message is:
Run-time error '-2147221504 (800040000)';
SoapMapper: Putting data into SoapMapper element failed
If we try to consume the same web service from a .NET application, it looks like the generated classes and methods are slightly different. For example, the fetch data service call takes a DataContainer object which has some identifying properties to determine what data to get. Then it returns the same type of object with the data filled in.
In the VBA classes generated by the Web Reference Toolkit, both fetch and save take the same type of object. But in .NET, the save takes a SaveDataContainerDetails object.
So a couple questions really:
Has anyone seen the SoapMapper error before in their VBA work?
Has anyone seen .NET and VBA generate different method signatures from the same WSDL? What could cause that and how could I work around it?
Is there a better way to call SOAP services from VBA rather than using the Web Reference Toolkit and the SOAP Toolkit?
I've figured out the issue here. The VBA Soap Toolkit and Web References Toolkit just can't handle array's of complex types. The error message above was somewhat misleading because "element" was actually the name of the XML element in the Soap envelope. So it was failing to put data into an element called "element". That was masking the issue a bit.
So I've determined it all boiled down to arrays of complex types. I've also determined that it's MUCH easier to just craft the soap envelopes my self in the VBA. The XML required is generally pretty well structured and not too difficult to create and or parse. So, if you're having trouble with the Soap Toolkit or the Web References Toolkit in VBA... just don't use it! :)
The best way to consume web services from VBA, VBSCRIPT, Classic ASP, etc. is to create your client using .NET. Then turn your client into a COM object. These are easily consumed by these older technologies.