Playing with Play: Json POJO binding

13. Oktober 2011

0 Kommentare

Since I am used to the handy and easy way of binding HTTP requests parameters to POJOs in Spring I despredly searched for a possibilities to do that in play! It is possible by default for JPA like parameters like obj[1].key=value&obj[2].key=value, but not for json objects. Since Gson is very handy and powerful in parsing json to POJOs I decided to extend play by that functionality.

For this I needed to write a PlayPlugin, since play is treating HTTP parameters based on the naming convention key=value, where key matches the controller parameter name (which btw. is not accessible with Java Reflection, but with a own crafted Java.parameterNames(Method) byte code parser, which only works when play compiles and controls the classes), as a JPA Model object, which is then converted to an empty POJO by the JPAPlugin, and so returned to the Controller method. In order to prevent that I needed to sneak between the other data binding plugins in order to convert the params.

My work is based on this stackoverflow answer. First you place a play.plugins file in your application conf directory with a line like: 101:controllers.JsonDataBindingPlugin, where 101 is the plugin id which is pretty at the beginning of the data binding chain then you extend the PlayPlugin class like that:

package controllers;

import play.PlayPlugin;
...

public class JsonDataBindingPlugin extends PlayPlugin {

	private Gson gson;

	// 101:controllers.JsonDataBindingPlugin
	private Gson getGson() {
		if (gson == null) {
			gson = new Gson();
		}
		return gson;
	}

	@Override
	public Object bind(String name, Class clazz, Type type,
			Annotation[] annotations, Map<String, String[]> params) {

		if (params.containsKey(name)) {

			String[] strings = params.get(name);
			if (strings.length == 1) {
				String param = strings[0];
				if (param.startsWith("{")) {
					try {
						Object fromJson = getGson().fromJson(param, type);
						return fromJson;
					} catch (JsonSyntaxException e) {
						return null;
					}
				}
			}
		}
		return super.bind(name, clazz, type, annotations, params);
	}
}

¬ geschrieben von joecks in Tech, Uncategorized

Soziale Netzwerke, Privatsphäre und Werbung - OpenComercial? Oder was?

16. Oktober 2010

0 Kommentare

ich habe mir gersten den Film über Mark Zuckerberg angeschaut und ich denke das hat etwas auf mich abgefärbt, nicht so sehr der fiktive Charakter, aber die simple zielorientierte Art wie er mit Technik umgegangen ist, vielleicht auch ein Grund dafür das php sich so verbreiten konnte. Dabei ist mir etwas aufgefallen:

Ich denke wir sollten unseren Umgang mit sozialen Netzwerken unserer Privatsphäre und wie man damit Geld mach überdenken; ein guter Ansatz ist die Entwicklung von Diaspora, ein verteiltes soziales Netzwerk, dass es jedem erlauben soll selbst zu entscheiden was mit all den Daten passiert die ausgetauscht werden. Man hat quasi sein facebook unter seiner eigenen Kontrolle, ich finde die Idee richtig. Doch vielleicht fehlt noch etwas. Man denke daran, dass viele sich nicht leisten können einen eigenen peer server aufrecht zu erhalten, allein für eine Funktion die Andere ihnen kostenlos anbieten (auch wenn man nicht weiss was andere damit machen). Ausser. Ausser man schließt einen Vertrag mit einem Werbeunternehmen, dass dein persöhnliches Netz nutzten darf um darin Werbung zu schalten. Warum sollte ich soetwas perverses machen??? Nun einerseit weil wir im Internt eh schon überschwemmt werden von Werbemüll, den wir dazu nicht gut kontrollieren können und zum anderen ist es mir persöhnlich lieber, dass ich mich direkt verkaufe als dass jemand anders, für mich tut.

Nun die idee könnte so funktionieren: Ich habe einen Server auf dem einige meiner Freunde ihr soziales Netzt mit mir teilen, a la diaspora, wir kommunizieren mit anderen Servern wie es in der inter-server kommunikation vorgesehen ist. Nun jeder Nutzter müsste regelmäßig einen Betrag an mich “spenden”/flattr’n um den Betrieb des Servers aufrecht zu erhalten. Als Nutzer könnte ich jedoch frei entscheiden ob ich nicht einwenig Privatsphäre aufgebe und einem Werbebetreiber etwas über mich verrate, damit er meine Rechnung zahlt. Vom Werbebetreiber würde das jedoch so aussehen . Ein teil seines Werbebudgets würde er freistellen. Diesen betrag wird an die Benutzter ausgezahlt die bei sich Werbung freischalten. Für den Werbebetreiber wäre das Konzept identisch zu der klassischen Werbemethode. Er würde immer noch eine Zielgruppe zwischen X und Y, ledig und mit viel Vorliebe für Filme bewerben, jedoch kann ich selbst entscheiden ab wann ich das alles wieder abschalte. Alles wäre transparenter und die welt etwas weniger willkürlich.

¬ geschrieben von joecks in Uncategorized

lunching ijusthadabeer.com

19. Juli 2010

0 Kommentare

what, why and how for the  lunch  of the  crowd-sourcing website ijusthadabeer.com

What

This is a crowdsourcing game, to determine the local current local consumption  of beer. It should be fun, but also provide the idea of sensing an complicated and interessting fact based on a relative simple sensing algorithm. You as a participant is doing nothing special than telling the world that you had a beer, this is then available on this map, showing the big picture of overall thirst.

The interpretation is yours, wheather a place is more populare to get a beer or if an area is more despaired for alohol.

Why

I’m intressted in harvesing little umimportant not-personalized facts to build big pictures, available to everyone, for more of an\ scietific intressted than  comercial use. So lay back and have fun watching how people get involved.

How

This experiment works by allowing the browser to share your location, this might be a problem to some of you, so just disallow and look at the map in read-only mode. By pressing the “I just had a beer” - button you are allowed to tell the world about you current consume, or if you sensed someones else consue close to you. Choosing your place is conveniet to map the sensed information with a current location. And to make clear where what happend.

Cheeres and enjoy!

¬ geschrieben von joecks in Uncategorized

Importing Cisco pcf settings to Mac OS 10.6 ’s Cisco IPSec Client

1. Dezember 2009

0 Kommentare

So far importing the pcf config file is not supported. But you just need to use the informations provided by the pcf file itself:
- group name
- group Secret
for that you need to decode the hash using this cisco vpnclient password decoder. Based on the cisco-decrypt.c code, which you might like to compile by your own.

Then you type in you password and username, and everything should work.

¬ geschrieben von joecks in Uncategorized, mac

xkcd

24. Mai 2009

0 Kommentare

meine zwei lieblings xkcd’s bis jetzt:

To be wanted. To be wanted.

¬ geschrieben von joecks in Uncategorized

Theme von BenediktRB • Powered by Wordpress • Abonniere den RSS Feed