Just a quickie. For you and myself so I don’t forget. There is a bunch of post about this but they all seem abit convoluted where the jquery’s rails plugin documentation actually does the trick. The only thing is after you added the gem to Gemfile you need to issue ‘bundle install’ before generate. Thats it 😉
This is part 3 and the final part of this series of how to use Spring 3 rest template and paypal’s adaptive API. You can find part 1 and part 2 here and here. This part is all about unmarshalling paypal’s responds back to your expected objects. The problem need to be solve here is that paypal replies with 200 even if the pay request have failed for what ever reason (that is not HTTP related). Compounded by the fact that the the success response have a different parent tag to a failed response even tho the object structure is the same (JAXB doesn’t support multiple XMLRootElements).
There could be another way to skin this cat if you have post a alternative solution here will give readers a alternative option 😉
Solution Objective:
I wanted to keep my code as clean and transparent as possible in relation the code that is making the REST call, which should do not more then this line:
ResponseEntity
responseEntity = restOperations.postForEntity(paypalProperties.getAdaptiveUrl(), entity, PaypalPayReply.class);
Also to keep non business related unit tests at a minium I need to eliminate any if else statements by pushing all type mapping decisions to spring.
Solution Resources:
In this part you will be using Spring OXM and writing your own HttpMessageConverter to unmarshall the correct object back to the caller. All code is available at github.
Paypal XML Success/Fail similarities:
Both success and failure messages has a ‘responseEnvelope’ element that contains a ‘ack’ element which indicates Success or failure of the request. Take a look at this (success) and this (failure) Jaxb2 Objects. I use the ‘ack’ element to determine different business rules.
Setting up a custom HttpMessageConverter:
The line of code you see in ‘solution objective’  returns PaypalPayReply interface, inorder for spring to know how to unmashall this we have to use a custom HttpMessageConverter. Implementing your own give you a chance to hook into the spring rest unmarshalling/marshalling process. The class that does this is at rest.reply.PaypalReplyHttpConvertor. Once you have set it up make sure you have added it to your spring config.
Finally setting up OXM:
In order for the the HttpMessageConverter to work properly you need to set up your OXM mapper, and its relative config and code here.
Thats it the final piece of information is about paypal’s IPN which you are likley to be using. When testing this make sure you always receive the related IPN message, otherwise paypal will degrade the frequency of messages and makes testing very difficult.
So wellcome to part 2 🙂 This is where we start getting into the meat of the implementation. Assume you have set up the project described from part 1, this part will focus on getting the request done.
All the code describe here can be found at my github account. So I wont write everything here and you can check out the code to work that out. Hopefully I have made the code descriptive enough to follow 😉 if  not you can sent it  to here 😀
Set up your rest template definitions:
Add this to your existing definition. The 2 convertors here are very useful. This tells spring to either use Jaxb2 or String when unmarshalling the data. Spring rest template will find the best fit for your needs.. this is very cool as it save loads of if statements and therefore some tests too 😉 In part 3 this is also where we will add our custom converter definition:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</list>
</property>
</bean>
Go back to MainPost class:
Add the following dependancy from RestTemplate
Then in makeRequest() you need to use the postForEntity method. This is because you need to set some custom headers for paypal and the only way you can do this is to create a HttpEntity yourself. Have a look at the generateHttpHeader() in the code. What you want at the end of a successful invocation is to get a paykey from paypal so you can finally construct  a URL to pass back to the browser for a redirect. From here also take a look at the DSOs for mapping the request to be send
On thing to note if the request fail it throws a HttpStatusCodeException Runtime exception. This is different to how you will be use to if you used apache httpClient directly, even tho I am not sure I totally agree how spring deal with this but is good to know nonetheless.
Stay tune for part 3 where we will work with rest templates HttpConvertor
Recently I have totally rewrote how we use paypal. Switching to use the newish adaptive payment API, and IPN. My serverside setup is using spring and recently switch to version 3 (if you haven’t heard about it for some reason try this link). One of the new things available for spring 3 is rest templates, very useful bit of API. Allow you to use a range of binding API, I use JAXB2 and it comes working out of the box 😉
I am going to give a full example of how to use spring 3 rest template with adaptive payment API. This example will be split into 3 parts. First part is setting up your project. Second is making the request and finally unmarshalling the response back to a JAXB2 annotated object.
So without further deplay, lets get on with it!
Setting up paypal:
So I would expect you have a active paypal account. If not you will need one to carryon. Once you have that you will need to do the following to setup a sandbox envioment for you to test your implementation:
- Login to developer.paypal.com and set up some test accounts. You will need at lest one Seller account (so you can take payments) and one Buyer account (so you can make payments). I personally just use the preconfigured options as works well for me.
- Once you have these accounts you can test them by login into sandbox.paypal.com using your test accounts.
- To set up to use the adaptive payment API you will need a API key.
- Login to x.com using your normal paypal account.
- Go to this link and follow the instructions
- You do need to submit a application request that paypal will approve, but you can carry on testing while this is happening.
Setting up the project:
I use eclipse helios with the IAM maven plugin. Here is my pom.xml
Last week I needed to add some security on my CXF REST services. I have 2 main contenders Spring security or the new Apache Shiro. I ended up with Shiro but it have some particulars. This should save you some time!
Common sense will tell me that Spring Security will be the default way forward as it will slot neatly in the world of spring. However since the scared last year or so about Spring wanting to charge for bug fix releases I am now always looking for a back door just in case I get screwed. So I played with Shiro, a direct ancestor of JSecruity there are more then enough to have my end points secured. (That was the other thing Spring security was a bit too heavy weight for this purpose anyways)
On to setting things up.
Installing the jars:
At the time of writing Shiro haven’t made any major releases yet so is not available in maven repo. But you can download the source from their SVN and issues ‘mvn install’ (or include ‘deploy’ so it makes it into you own maven remote repos). After successful install you will find a few modules available for you. In my case I needed shiro-core, shiro-web, shiro-spring. O, i will also add ‘javadoc:javadoc’, it is very good especially if you want to know more about the IniShiroFilter
Setting up your web.xml
Apart form the normal CSF config. I use the ‘org.springframework.web.filter.DelegatingFilterProxy‘. This makes things cleaner as now you can leave web.xml and have everything done in the spring.xml
Setting up the spring wiring.
Saving me repeat myself. Have a look the shiro source you checked out. In ‘shiro-trunk/samples/spring’ there is most of what you need. The only thing that is really missing is the IniShiroFilter config (In the sample it all use ShiroFilter instead, however if you read the javadoc it is deprecated and will be removed for version 1 release!). One thing that needs to be done in the IniShiroFilter Spring config you need to add the securityManager reference to the ‘securityManager’ property i.e.:
<bean id=”shiroFilter” class=”org.apache.shiro.web.servlet.IniShiroFilter”>
<property name=”securityManager” ref=”securityManager”/>
Otherwise it will throw a null pointer when you make a request.
The actual config of the filter itslef really depends on what you want to do and what you environments are. But before you go further I really recommend that you read the javadoc you have for IniShiroFilter, have a cup of something, think it over then make your move as it is really flexible! My REST services serves different front ends, flex, iphone, rails a and some calls are poxied to facebook, netlog, etc. So using their Standard Authentication filters is not very useful for me and i wrote my own and override isAccessAllow methods.
If you are using a CustomAuthFilter
I had to create my own AuthFilter cuz of the social network session pass throughs. This presented a problem. As the AuthFilter have to be inside the config property of the IniShrioFilter. At the moment it cannot use a spring bean. So if you are doing this and inside the filter you want to use a spring bean you will need to do that yourself using the usual ‘ApplicationContext‘ way.
Avoiding the BASIC Authentication challenge Browser Popup
Again this might not apply to you. However due to the hybride nature of the clients sending the BASIC challenge wasn’t great for my user experenience. All the apps provide a different experience when a 401 occurs (specially the flex apps in a browser). But if you use/extends one of the BasicHttpAuthenticationFilters this is the default behavior, when a 401 fails the browser gets the BASIC challenge then a dialog pops up for user name and password appears. to override this you can either over ride the ‘sendChallenge’ method in your AuthFilter and don’t set a BASIC into the response header or in your IniShiroFilter config add this to your filter ‘authFilter.authcScheme=NONE’. This way you client browser get a 401 but not a awful popup for user and pwd entry.
I was searching a round on how to get rails running on lenny which is our production OS. I have found this post to be really useful http://articles.slicehost.com/2009/4/9/debian-lenny-ruby-on-rails however the aptitude install statment fro ruby is for 1.8. After searching for the correct packages for 1.9 this is what I have:
sudo aptitude install ruby1.9-dev ruby1.9 rdoc1.9 ri1.9 libreadline-ruby1.9 libruby1.9 libopenssl-ruby1.9 sqlite3 libsqlite3-ruby libsqlite-dev libsqlite3-dev
It installs both 1.8 and 1.9 so i also added a soft link making sure that 1.9 is default:
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
Recently I got a job to work for a company that makes apps for social network photo printing call hotprints.. I dusted off the 2 applications I wrote to be run in facebook nearly 4 years  ago to remind myslef what I done and get reacquainted with my virtual social well being. I see how much  facebook have moved on with their platform, now they have theses things call ‘pages’ which is place where you can place your company mark on to the site. Members can become a fan and with the ability to add application to as tabs in the pages. Companies can relay on other company to increase exposure.. That all great but what does that meant to me as a web users?
I think this could fundamentally change our way of doing web search. Not only can I search for the things I am interested from something like google (gosh that so last year!) and gives me specific results of things that I can think of,  but I could potentially get more accurate result base on my social graph even suggest related issues that I didn’t think of. Bing.com says it can also share its searches to facebook.. I definitely want to have a go and see what I can do with the FB social graph and the pages integration to automatically gain a better understanding of my search preferences..
The GWT grails plugin is great. I really like GWT and what it can do, plus the promise of Wave integration on both client and appengine should present some interesting possibilities. Working with grails is a breeze once there is a understanding of how it works setting up a site is fast fast fast 🙂
So I been working on using grails, appengine, GWT to work together.. This is what I done:
- create a grails app as normal ‘grails create-app’
- installed the plugins I want. ‘grails install-plugin GWT’ + ‘grails install-plugin app-engine’
- write my GWT client as I would do in Java (under src/gwt). Setting up the Service Interfaces etc.
- created a grails service to act as my GWT servlet as suggested (this bit i love as I don’t need to edit my web.xml)
- create the html page ‘grails create-gwt-page <page>.html <module>
- run the application using ‘grails app-engine run’ (this bit works well too as it invokes the gwt complier, compiles my GWT app and places all files in the right place.. sweet..)
- run the Hosted client ‘grails run-gwt-client’ expecting things will slot in place
- Doesn’t work 😦
A few of things I noticed:
- When doing ‘grails app-engine run’ (which will try to compile your GWT app) the gwt compiler compiles the application that sits under src/gwt but the classes do not get added to the classpath (the classes don’t get put into the classes dir) as grails only puts the classes in src/java. So when you use a GWT component the app blows up. By making sure classes under src/gwt gets compiled in grails solves the problem.
- When creating the html page if you don’t add a sub directory as part of the page creation the gwt gets confuse with rpc calls.
- Also the html page needs to be added into web.xml under src/templates/war as welcome-file-list tag like in a java base GWT application. If it don’t exist use ‘grails install-templates’
- Finally if you include other GWT modules as jars (such as gwtext) it needs to be added to your classpath manually. When using ‘grails compile-gwt-modules’ the script looks into lib/gwt and add the jars to classpath but if you action ‘grails app-engine run’ it doesn’t look into lib/gwt (or gets overridden) tho it does invoke the gwt compiler.
After doing this I have managed to login using the google’s appengine API and displays my app which is using gwtext.
Trying to use maven2 and google eclipse plugin (for appengine and GWT) can be a little tricky. As the plugin insist to create a project structure that is not very maven like. The problem is in the ‘war’ dir that is created under ${basedir}, maven need it to be in under src/main/webapp, if you force the change (i.e. drag and drop) eclipse complain and will not run finally the plugin do not allow you reconfig.
But I would still like to use all the cool integrated feature that the plugin offers with maven2 for building.. so what to do :~
Tried using the maven war plugin and change the source dir that didn’t work. So all i done is to create a symbolic link from /src/main: ln -s ../../war/ webapp
that works.. not ideal tho if anyone find another way before I do please leave a comment…
XMPP allows you to create new room on demand. In openfire set the service setting to allow anyone or a list of JID to do this. Then in XIFF create a new Room object with a JID that don’t exist on the server and do a room.join(). There you have yourself a new room with yourself in it 🙂 When doing this openfire use default room configuration however sometimes you would like to have alternate configurations. Such as creating a private room with a password, or a room that is not listed in the directory so you have control on the people in there. This can all be done in XIFF. Although the documentation is a little thin and the API isn’t very easy to work out on how to do it. So this is a mini guide on how can be done as I just manage to discover it myself after go through the source code.
So firstly create your new room in XIFF do something like this:
public function createNewRoom():void{
var room:Room = new Room(_xmppSocketConnection);
//Add the event listener here for the join room event
room.addEventListener(RoomEvent.ROOM_JOIN, config);
room.roomJID = new JID("exDirectoryRoom@service.server.com");
room.join();
}
After joining the room this is where you exec a room.configure method. This method signature takes in fieldmap:Object so is not very intuitive.. emm.. Digging around in the source code this is the room.configure implmentataion:
public function configure(fieldmap:Object):void
{
var iq:IQ = new IQ(roomJID, IQ.SET_TYPE);
var owner:MUCOwnerExtension = new MUCOwnerExtension();
var form:FormExtension;
if (fieldmap is FormExtension) {
form = FormExtension(fieldmap);
} else {
form = new FormExtension();
fieldmap[“FORM_TYPE”] = [MUCOwnerExtension.NS];
form.setFields(fieldmap);
}
form.type = FormExtension.SUBMIT_TYPE;
owner.addExtension(form);
iq.addExtension(owner);
myConnection.send(iq);
}
So you can pass in a FormExtension class with all the for var and value as of XMPP spec XEP-0045 or just pass in the var and values directly. If you are doing that the API again isn’t very clear on how this can be done but looking into the source of FormExtension the easiest way is to create a new Dictionary class and the key is the form variable. The value however MUST be an Array. So in my example I want the room to be ex-directory here is what to do:
public function config(event:RoomEvent):void{
var fileds:Dictionary = new Dictionary();
fileds["muc#roomconfig_publicroom"] = new Array(false);
room.configure(fileds);
}
