Saturday, 18 February 2012

Wrapping and Unwrapping JSON response in CXF

Q. How to drop root element in the JSON response?
A. You need to set the JSONProvider's dropRootName property to true. Here is a sample cxf config for this:

<jaxrs:server id="${service.name}" address="/">
	<jaxrs:schemaLocations>
	<jaxrs:schemaLocation>classpath:${service.def}.xsd</jaxrs:schemaLocation>
	</jaxrs:schemaLocations>
	<jaxrs:serviceBeans>
	<ref bean="${service.ref}" />
	</jaxrs:serviceBeans>
	<jaxrs:providers>
	<bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
	<property name="dropRootName" value="true"></property>
	</bean>
	</jaxrs:providers>
	<jaxrs:extensionMappings>
	<entry key="json" value="application/json" />
	</jaxrs:extensionMappings>
</jaxrs:server>
Q. How to wrap the JSON response with a name?
A. You need to set the JSONProvider's supportUnwrapped property to true and also set the wrapperName for the root element (If you have more than one wrapperName then use wrapperMap property). Here is a sample cxf config for this:

<jaxrs:client id="${service.name}" 
	address="${service.url}" 
	serviceClass="${service.class}"
	inheritHeaders="true">
	<jaxrs:headers>
	<entry key="Content-Type" value="application/json" />
	</jaxrs:headers>
	<jaxrs:providers>
	<bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
	<property name="supportUnwrapped" value="true"></property>
	<property name="wrapperName" value="${response.wrapper.name}"></property>
	</bean>
	</jaxrs:providers>
</jaxrs:client>
In case you are intersted in knowing how JSONProvider uses the wrapperName mentioned above while reading the JSON data, here is the code-snippet:

protected String getRootName(Class cls, Type type) throws Exception {
	String name = null;
	if (wrapperName != null) {
	name = wrapperName;
	} else if (wrapperMap != null) {
	name = wrapperMap.get(cls.getName());
	}
	if (name == null) {
	QName qname = getQName(cls, type, null, false);
	if (qname != null) {
	name = qname.getLocalPart();
	String prefix = qname.getPrefix();
	if (prefix.length() > 0) {
	name = prefix + "." + name;
	}
	}
	}
	if (name == null) {
	throw new WebApplicationException(500);
	}
	return "{\"" + name + "\":";
}

Tuesday, 14 February 2012

CAP THEOREM


Wikipedia (http://en.wikipedia.org/wiki/CAP_theorem) - In theoretical computer science the CAP theorem, also known as Brewer's theorem, states that it is impossible for a distributed computer system to simultaneously provide all three of the following guarantees: Consistency, Availability and Partition tolerance.

There is a nice diagram available on the web (http://www.nijee.com/2011/02/golden-key-to-distributed-computing-is.html) - "Visual Guide to NoSQL Systems" which talks about which NoSQL DB fits where based on CAP theorem. This theorem was put forward by Eric Brewer. CAP theorem in brief states that a system can have only two out of three properties- Consistency, Availability and Partition-tolerance.

An application is considered to be consistent if an operation is performed fully or not at all. If the operation gets performed half-way, inconsistency gets created. To give you an example, there are many deals sites at present. Some of the deals are limited to a specific count say 100. Suppose you are the 100th consumer of the deal, you are about to pay for it and in the meanwhile someone else also selects the same deals and makes payment. Both of you cannot become the 100th consumer of the deal. So while you've confirmed to your selection of a deal, the second one should not be proceeded to make payment at all.

Availability states that the service remains available. Taking the same example as above, if you want to buy something in a deal but the site itself refuses to open, may be because of heavy traffic i.e.non-availability of service, is of no use.

Partitioning will not be needed if all your data resides and runs in a single box. But where the amount of data is huge it is very likely that there will be partition and data will be distributed. For a system to be partition-tolerant, it should respond correctly in every case except the case of total network failure.

In most of the NoSQL DBs, consistency is being compromised. Typically choosing one DB may not solve all your problems. Select one keeping certain features of your application in mind and feel free to go for another one for other modules. Also this doesn't mean removing relational database completely. Personally I always prefer to keep relational DB in my applications and use it at certain places where it really makes sense to use it!

In fact if your application is large enough, you can use a combination of NoSQL databases- utilizing the required features of each one of them, plus use a traditional database.

Monday, 6 February 2012

Mobile Apps


Recently I was looking into some of the cross-platform mobile development tools like RhoMobile, PhoneGap, Titanium, Worklight, Sencha Touch, appMobi etc and during that I developed some of the apps just to test the compatibility with various mobile devices and the features these frameworks support. Though some of the feature-rich applications are under development, I developed some very basic applications just for fun. You may want to have a look at these:

Bollywood Tweets
https://market.android.com/details?id=com.viracct.tweets



TechCrunch Updates







Friday, 3 February 2012

Popular Chrome Extension

We are glad to let you know that our Chrome extension 'Java Populars' has really become popular! It has got 10K+ active users with an average rating of 5 stars!!

Those who have not tried it yet, must be now definitely keen to know what is this extension all about. Here is a brief summary of it.

Very useful for Java developers- Java Populars! It is very likely that you'll have doubts in Java or have issues during development or deployment. You google it and you get thousands of related results. You may get lost in those thousands of sites to pick up the good solution. Rather go to the sites listed under Java Populars.

Have a glance:
Right click on the extension's icon to navigate to the options page. Options page gives you a way to customize your list. You can remove from the default list by unchecking the respective. To add your own links enter the url in the textbox and select the respective checkbox.
 




























 
 You get to see the customized list as shown:















Here is the link to download the extension:
https://chrome.google.com/webstore/detail/oapojegdcjjaeehmicinhimakliedchj