Permalink
Browse files

Update to include the as3httpclient library to properly make PUT and …

…DELETE requests.
  • Loading branch information...
1 parent b98980f commit 981da375d62a76b6ff694064695cf29808121ac7 Todd Anderson committed Mar 19, 2010
@@ -0,0 +1,47 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: IServiceMediator.as</p>
+ * <p>Version: 0.2</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.mediator
+{
+ import com.custardbelly.as3couchdb.core.CouchModel;
+ import com.custardbelly.as3couchdb.service.ICouchRequest;
+
+ /**
+ * IServiceMediator is a base mediator class representing a middle tier in communication between a CouchModel
+ * instance and the CouchDB service.
+ * @author toddanderson
+ */
+ public interface IServiceMediator
+ {
+ /**
+ * Initializes mediator to establish service on which to communicate actions related to target model.
+ * @param taret CouchModel
+ * @param baseUrl String
+ * @param databaseName String
+ */
+ function initialize( taret:CouchModel, baseUrl:String, databaseName:String, request:ICouchRequest = null ):void;
+ }
+}
@@ -0,0 +1,62 @@
+package com.custardbelly.as3couchdb.responder
+{
+ import com.custardbelly.as3couchdb.core.CouchDocument;
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.enum.CouchActionType;
+ import com.custardbelly.as3couchdb.serialize.CouchDatabaseReader;
+ import com.custardbelly.as3couchdb.serialize.CouchDocumentReader;
+
+ public class ReadAllDocumentsResponder implements ICouchServiceResponder
+ {
+ protected var _documentClass:String;
+ protected var _responder:ICouchServiceResponder;
+ protected var _status:int;
+
+ protected var _databaseReader:CouchDatabaseReader;
+ protected var _documentReader:CouchDocumentReader;
+
+ public function ReadAllDocumentsResponder( documentClass:String, responder:ICouchServiceResponder )
+ {
+ _documentClass = documentClass;
+ _responder = responder;
+
+ _databaseReader = new CouchDatabaseReader();
+ _documentReader = new CouchDocumentReader();
+ }
+
+ public function handleResult( value:CouchServiceResult ):void
+ {
+ // TODO: Check for fault.
+ var documents:Array = _databaseReader.getDocumentListFromResult( value.data );
+
+ var i:int;
+ var document:CouchDocument;
+ var documentList:Array = [];
+ for( i = 0; i < documents.length; i++ )
+ {
+ // Documents are returned form /_all_docs as {doc:Object, id:String, key:String, value:Object}
+ // Supply the doc property to the reader.
+ document = _documentReader.createDocumentFromResult( _documentClass, documents[i].doc );
+ documentList.push( document );
+ }
+
+ if( _responder ) _responder.handleResult( new CouchServiceResult( CouchActionType.READ_ALL, documentList ) );
+ }
+
+ public function handleFault( value:CouchServiceFault ):void
+ {
+ if( _responder ) _responder.handleFault( value );
+ }
+
+ public function get status():int
+ {
+ return _status;
+ }
+
+ public function set status(value:int):void
+ {
+ _status = value;
+ }
+ }
+}
@@ -0,0 +1,111 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: AbstractCouchRequest.as</p>
+ * <p>Version: 0.2</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.service
+{
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.event.CouchEvent;
+ import com.custardbelly.as3couchdb.responder.ICouchServiceResponder;
+
+ import flash.net.URLRequest;
+
+ /**
+ * AbstractCouchRequest is an ICouchRequest implementation with abstract and stubbed methods.
+ * @author toddanderson
+ */
+ public class AbstractCouchRequest implements ICouchRequest
+ {
+ protected var _isInactive:Boolean;
+ protected var _responder:ICouchServiceResponder;
+
+ /**
+ * Constructor.
+ */
+ public function AbstractCouchRequest() {}
+
+ /**
+ * @private
+ *
+ * Responds to status code of service request.
+ * @param statusCode int
+ */
+ protected function respondToStatus( statusCode:int ):void
+ {
+ if( _isInactive || _responder == null ) return;
+
+ _responder.status = statusCode;
+ }
+
+ /**
+ * @private
+ *
+ * Responds the result from service request.
+ * @param type String
+ * @param result Object
+ */
+ protected function respondToResult( type:String, result:Object ):void
+ {
+ if( _isInactive || _responder == null ) return;
+
+ _responder.handleResult( new CouchServiceResult( type, result ) );
+ }
+
+ /**
+ * @private
+ *
+ * Responds to a fault in service request.
+ * @param type String
+ * @param message String
+ */
+ protected function respondToFault( type:String, message:String ):void
+ {
+ if( _isInactive || _responder == null ) return;
+
+ _responder.handleFault( new CouchServiceFault( type, message ) );
+ }
+
+ /**
+ * Executes a request on the service.
+ * @param request URLRequest
+ * @param requestType String
+ * @param responder ICouchServiceResponder
+ */
+ public function execute(request:URLRequest, requestType:String, responder:ICouchServiceResponder):void
+ {
+ _responder = responder;
+ // abstract.
+ }
+
+ /**
+ * Performs any cleanup.
+ */
+ public function dispose():void
+ {
+ _isInactive = true;
+ }
+ }
+}
@@ -0,0 +1,155 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: ExInCouchRequest.as</p>
+ * <p>Version: 0.2</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.service
+{
+ import com.adobe.serialization.json.JSON;
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.enum.CouchRequestMethod;
+ import com.custardbelly.as3couchdb.event.CouchEvent;
+ import com.custardbelly.as3couchdb.responder.ICouchServiceResponder;
+
+ import flash.external.ExternalInterface;
+ import flash.net.URLRequest;
+
+ /**
+ * ExInCouchRequest is an ICouchRequest implementation that uses ExternalInterface calls to communicate with a CouchDB instance.
+ * @author toddanderson
+ */
+ public class ExInCouchRequest extends AbstractCouchRequest
+ {
+ /**
+ * Constructor.
+ */
+ public function ExInCouchRequest()
+ {
+ if( ExternalInterface.available )
+ {
+ ExternalInterface.addCallback( "PUTResponse", handlePutResponse );
+ ExternalInterface.addCallback( "POSTResponse", handlePostResponse );
+ ExternalInterface.addCallback( "GETResponse", handleGetResponse );
+ ExternalInterface.addCallback( "DELETEResponse", handleDeleteResponse );
+ ExternalInterface.addCallback( "FaultResponse", handleFault );
+ }
+ else
+ {
+ trace( "ExternalInterface is not available. COUCHDB service requests will not be permittable." );
+ }
+ }
+
+ /**
+ * @private
+ *
+ * Notifies ICouchServiceResponder instance of result.
+ * @param result Object The resulting object recieve form EI.
+ */
+ protected function notifyOfResult( result:Object ):void
+ {
+ respondToResult( CouchEvent.RESULT, JSON.encode( result ) );
+ }
+
+ /**
+ * @private
+ *
+ * Method callback from EI for PUT response.
+ * @param result Object
+ */
+ protected function handlePutResponse( result:Object ):void
+ {
+ notifyOfResult( result );
+ }
+
+ /**
+ * @private
+ *
+ * Method callback from EI for POST response.
+ * @param result Object
+ */
+ protected function handlePostResponse( result:Object ):void
+ {
+ notifyOfResult( result );
+ }
+
+ /**
+ * @private
+ *
+ * Method callback from EI for GET response.
+ * @param result Object
+ */
+ protected function handleGetResponse( result:Object ):void
+ {
+ notifyOfResult( result );
+ }
+
+ /**
+ * @private
+ *
+ * Method callback from EI for DELETE response.
+ * @param result Object
+ */
+ protected function handleDeleteResponse( result:Object ):void
+ {
+ notifyOfResult( result );
+ }
+
+ /**
+ * @private
+ *
+ * Method callback for fault from EI for service request.
+ * @param result Object
+ */
+ protected function handleFault( result:Object ):void
+ {
+ // TODO: Decipher fault for correct message.
+ respondToFault( CouchEvent.FAULT, "error" );
+ }
+
+ /**
+ * @inherit
+ */
+ override public function execute( request:URLRequest, requestType:String, responder:ICouchServiceResponder ):void
+ {
+ super.execute( request, requestType, responder );
+
+ switch( requestType )
+ {
+ case CouchRequestMethod.PUT:
+ ExternalInterface.call( "PUTRequest", request.url, request.data );
+ break;
+ case CouchRequestMethod.POST:
+ ExternalInterface.call( "POSTRequest", request.url, request.data );
+ break;
+ case CouchRequestMethod.GET:
+ ExternalInterface.call( "GETRequest", request.url );
+ break;
+ case CouchRequestMethod.DELETE:
+ ExternalInterface.call( "DELETERequest", request.url );
+ break;
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 981da37

Please sign in to comment.