Force.com Toolkit For PHP >= 5.3 Released

code mitte GmbH is pleased to announce the release of a first version of the (inofficial) Force.com Toolkit For PHP 5.3.

The Toolkit is a complete rewrite of it’s originator, Force.com Toolkit For PHP, and actually supports API version 24.0 (that’s Spring ’12, as far as i remember, though it should be almost provide bc for earlier versions). It utilizes all these exciting improvements shipped with the latest stable PHP 5.3 releases like namespaces, closures, late static binding, tbc.

Additionally, the sourcecode is well structured and at least clean enough to take advantage of the psr-0 class loading de-facto standard. Following this convention, the library should neatly integrate into your shiny symfony 2 project or similar state-of-the-art framework or architecture.

The toolkit is still under heavy inhouse-development  and will be freezed in a state that works for us (which intrinsically means “for me”), likely somewhere in the near future.

So feel free to create a fork, provide new features and/or simply try it out and report or fix bugs that are certainly shipped with it. And all of that for free!! (in the sense of “free beer” ;) )
Continue reading

(Salesforce) Fancy HTML5 Upload Form using REST

On my quest for a fancy upload-form to meet a customer requirement I found a nice looking HTML5-Solution on  tehnrd.com.

It gave me a nice “quick-start”. But at the end we couldn’t live with the combination of overhead-data and memory-limitations.
Later on I found an article about using binary data with REST, which for me, seems to be a better approach than using a standard controller and Base64-encoded strings.

Sadly I’m not able to upload more than ~5MB still, but that will do it for now.
(I haven’t tested for the maximum possible file size using this approach yet; when I try to upload 10MB I get this error though:
https://xxx.visual.force.com/services/proxy 400 Unable to forward request due to: Request timedout after: 30189 )

Lets dive into Code

The Controller is pretty simple. Since our Request-Body is the pure binary,
we use a “custom” HTTP-header to transfer the filename and the Id of the record we want to attach the file to.

 
@RestResource(urlMapping='/attach')
global class cmAttachmentResource {

   @HttpPost
    global static String attach(){
    	RestRequest req = RestContext.request;
		RestResponse res = RestContext.response;

		if( req.headers.get('upload_pid') == null || req.headers.get('upload_filename') == null){
			res.statusCode = 400;
			return 'missing upload headers';
		}

    	Blob bin = req.requestBody;
        Attachment myAttachment  = new Attachment();
        myAttachment.Body = bin;
        myAttachment.Name = req.headers.get('upload_filename');
        myAttachment.ParentId = req.headers.get('upload_pid');
        insert myAttachment;
        return ' id = '+myAttachment.Id;
    }

}

On the client side, it gets a bit more complicated since you may not able to use XmlHttpRequest directly, due to the same origin policy. But there already is code to solve this problem on github:

Force.com-JavaScript-REST-Toolkit 

After getting thru the “Configuration” and “Using the Toolkit” -Part of the Documentation, you can use this script for a nice “drag and drop”-Box. It is pretty raw and straightforward, therefore it should be easy to customize it for your needs.
It will let you upload attachments for any object you desire, and even allow the user to drop multiple Documents at once.

Usage:

var client = new forcetk.Client();
client.setSessionToken('{!$Api.Session_ID}');
// Setup the dropzone
var dropZone = document.getElementById('drop_zone');
bindDropBox('{!yourObject.id}',dropZone,client);

Session Support for the Play Framework

Am Anfang dieses kleinen Tutorials sollte ich erwähnen, dass Sessions bei den meisten Play-Projekten nicht unbedingt nötig sind. Play basiert auf einem “Share-Nothing”-Prinzip und ist darauf ausgelegt, auf mehreren Server-Instanzen parallel laufen zu können. Durch die integrierte Memcache-Implementierung und dem auf Cookies basierenden Security-Modul sind die meisten Projekte auch ohne Session-Objekte problemlos umsetzbar.
Bei Webseiten, die auf intensive Benutzerinteraktion abzielen,  kann ein Session-Object aber durchaus von Nutzen sein. Ausserdem finde ich es nicht besonders elegant, den Benutzernamen in einem Cookie als Plaintext abzuspeichern, wie es das Play Security-Modul macht. Auch wenn der Cookie signiert ist, präferiere ich eine Identifikation über eine Session-ID.
Ziel dieses Tutorials ist es daher, eine serverseitige Sessionverwaltung zu implementieren. Dafür nutzen wir die von Play gelieferte Cache Funktionalität. Dadurch wird die Session, je nach Konfiguration, entweder im Speicher der Anwendung verwaltet, oder über einen externen Memcache.
Falls Ihr Projekt später auf mehren Instanzen laufen soll, sollten Sie auf eine hybride Session-Verwaltung von Memcache und Datenbank setzen, um einen Single Point of Failure zu vermeiden.