Playing with Play: Json POJO binding
13. Oktober 2011
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);
}
}
Soziale Netzwerke, Privatsphäre und Werbung - OpenComercial? Oder was?
16. Oktober 2010
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.
lunching ijusthadabeer.com
19. Juli 2010
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!
Importing Cisco pcf settings to Mac OS 10.6 ’s Cisco IPSec Client
1. Dezember 2009
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.
View All Icons in android.R.drawable
23. Oktober 2009
Based on an IconViewer I found in the web I desigend this app to show all icons on android.R.drawable. If you need you might change the Class easily.
Icon Viewer App
package de.dailab.iconviewer;
import java.lang.reflect.Field;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
public class IconViewer extends Activity {
private int[] IDS;
private int[] getIDS(){
// android.R.drawable.ic_menu_zoom
// 17301504
// 17301655
// android.R.drawable.ic_menu_zoom
int[] ids = new int[800];
for (int i = 0; i < ids.length; i++) {
ids[i]= 17301504+i;
}
return ids;
}
/**
* Determines the Name of a Resource,
* by passing the R.xyz.class and
* the resourceID of the class to it.
* @param aClass : like R.drawable.class
* @param resourceID : like R.drawable.icon
* @throws IllegalArgumentException if field is not found.
* @throws NullPointerException if aClass-Parameter is null.
* <br><br>
* <b>Example-Call:</b><br>
* String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);<br>
* Then resName would be '<b>icon</b>'.*/
public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
throws IllegalArgumentException{
/* Get all Fields from the class passed. */
Field[] drawableFields = aClass.getFields();
/* Loop through all Fields. */
for(Field f : drawableFields){
try {
/* All fields within the subclasses of R
* are Integers, so we need no type-check here. */
/* Compare to the resourceID we are searching. */
if (resourceID == f.getInt(null))
return f.getName(); // Return the name.
} catch (Exception e) {
e.printStackTrace();
}
}
/* Throw Exception if nothing was found*/
throw new IllegalArgumentException();
}
private OnClickListener onClicker;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
IDS = getIDS();
requestWindowFeature(Window.FEATURE_NO_TITLE);
onClicker = new OnClickListener(){
@Override
public void onClick(View v) {
myImageView mv = (myImageView) v;
String resName = "noname";
try {
resName = getResourceNameFromClassByID(android.R.drawable.class, mv.resId);
} catch (IllegalArgumentException e) {
// TODO: handle exception
}
String str = "this image is: " + mv.getResId() + "\n And name: "+ resName;
System.out.println(str);
Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
}
};
AbsoluteLayout layout=new AbsoluteLayout(this);
layout.setBackgroundColor(Color.rgb(255,255,255));
setContentView(layout);
GridView gridView=new GridView(this);
setALParams(gridView,0,0);
gridView.setNumColumns(4);
gridView.setGravity(Gravity.CENTER);
gridView.setAdapter(new ImageAdapter(this));
layout.addView(gridView);
}
public class myImageView extends ImageView{
private int resId;
public int getResId() {
return resId;
}
public myImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void setImageResource(int res){
super.setImageResource(res);
resId = res;
}
}
public class ImageAdapter extends BaseAdapter {
private Context context;//コンテキスト
public ImageAdapter(Context c) {
context=c;
}
public int getCount() {
return IDS.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
ImageView imageView;
if (convertView==null) {
imageView=new myImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(45,45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
} else {
imageView=(ImageView)convertView;
}
imageView.setImageResource(IDS[position]);
imageView.setOnClickListener(onClicker);
return imageView;
}
}
private static void setALParams(View view,int x,int y) {
setALParams(view,x,y,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT);
}
private static void setALParams(View view,int x,int y,int w,int h) {
view.setLayoutParams(
new AbsoluteLayout.LayoutParams(w,h,x,y));
}
}