THttpClient.as 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.thrift.transport {

  import flash.errors.EOFError;
  import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.events.SecurityErrorEvent;
  import flash.net.URLLoader;
  import flash.net.URLLoaderDataFormat;
  import flash.net.URLRequest;
  import flash.net.URLRequestMethod;
  import flash.system.Capabilities;
  import flash.utils.ByteArray;
  
  /**
   * HTTP implementation of the TTransport interface. Used for working with a
   * Thrift web services implementation.
   */
  public class THttpClient extends TTransport {

    private var request_:URLRequest = null;
    private var requestBuffer_:ByteArray = new ByteArray();
    private var responseBuffer_:ByteArray = null;
    private var traceBuffers_:Boolean = Capabilities.isDebugger;

    
    public function getBuffer():ByteArray {
      return requestBuffer_;
    }
    
    public function THttpClient(request:URLRequest, traceBuffers:Boolean=true):void {
      request.contentType = "application/x-thrift";
      request_ = request;
      if(traceBuffers == false) {
        traceBuffers_ = traceBuffers;
      }
    }
    
    public override function open():void {
    }

    public override function close():void {
    }
 
    public override function isOpen():Boolean {
      return true;
    }
    
    public override function read(buf:ByteArray, off:int, len:int):int {
      if (responseBuffer_ == null) {
        throw new TTransportError(TTransportError.UNKNOWN, "Response buffer is empty, no request.");
      }
        try {
            responseBuffer_.readBytes(buf, off, len);
            if (traceBuffers_) {
              dumpBuffer(buf, "READ");
            }
            return len;
          }
          catch (e:EOFError) {
            if (traceBuffers_) {
              dumpBuffer(requestBuffer_, "FAILED-RESPONSE-REQUEST");
              dumpBuffer(responseBuffer_, "FAILED-RESPONSE");
            }
            throw new TTransportError(TTransportError.UNKNOWN, "No more data available.");
        }
        return 0;
    }

    public override function write(buf:ByteArray, off:int, len:int):void {
      requestBuffer_.writeBytes(buf, off, len);
    }

    public override function flush(callback:Function=null):void {
      var loader:URLLoader = new URLLoader();
      if (callback != null) {
        loader.addEventListener(Event.COMPLETE, function(event:Event):void {
         responseBuffer_ = URLLoader(event.target).data;
         if (traceBuffers_) {
           dumpBuffer(responseBuffer_, "RESPONSE_BUFFER");
         }
         callback(null);
         responseBuffer_ = null;
        });
        loader.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void {
          callback(new TTransportError(TTransportError.UNKNOWN, "IOError: " + event.text));
          responseBuffer_ = null;
        });
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event:SecurityErrorEvent):void {
          callback(new TTransportError(TTransportError.UNKNOWN, "SecurityError: " + event.text));
          responseBuffer_ = null;
        });
      }
      request_.method = URLRequestMethod.POST;
      loader.dataFormat = URLLoaderDataFormat.BINARY;
      requestBuffer_.position = 0;
      request_.data = requestBuffer_;
      loader.load(request_);
    }

    private function dumpBuffer(buf:ByteArray, prefix:String):String {
      var debugString : String = prefix + " BUFFER ";
      if (buf != null) {
        debugString += "length: " + buf.length + ", ";
        for (var i : int = 0; i < buf.length; i++) {
          debugString += "[" + buf[i].toString(16) + "]";
        }
      } else {
        debugString = "null";
      }
      trace(debugString);
      return debugString;
    }

  }
}