Created
June 17, 2010 08:46
-
-
Save volca/441864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package server; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.facebook.thrift.TException; | |
import com.facebook.thrift.TProcessor; | |
import com.facebook.thrift.protocol.TBinaryProtocol; | |
import com.facebook.thrift.protocol.TProtocol; | |
import com.facebook.thrift.protocol.TProtocolFactory; | |
import com.facebook.thrift.transport.TIOStreamTransport; | |
import com.facebook.thrift.transport.TTransport; | |
import com.facebook.thrift.transport.TTransportException; | |
import com.facebook.thrift.transport.TTransportFactory; | |
/** | |
* <p> | |
* A servlet for exposing Thrift services over HTTP. To use, create a subclass that | |
* supplies a {@link TProcessor}. For example, | |
* </p> | |
* <pre> | |
* public class CalculatorTServlet extends TServlet { | |
* public CalculatorTServlet() { | |
* super(new Calculator.Processor(new CalculatorHandler())); | |
* } | |
* } | |
* </pre> | |
* <p> | |
* This code is based heavily on {@link com.facebook.thrift.server.TSimpleServer}. | |
* </p> | |
* @author Tom White | |
*/ | |
public class TServlet extends HttpServlet { | |
protected TProcessor processor_ = null; | |
protected TTransportFactory inputTransportFactory_ = new TTransportFactory(); | |
protected TTransportFactory outputTransportFactory_ = new TTransportFactory(); | |
protected TProtocolFactory inputProtocolFactory_ = new TBinaryProtocol.Factory(); | |
protected TProtocolFactory outputProtocolFactory_ = new TBinaryProtocol.Factory(); | |
public TServlet(TProcessor processor) { | |
processor_ = processor; | |
} | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
response.setContentType("application/x-thrift"); | |
InputStream in = request.getInputStream(); | |
OutputStream out = response.getOutputStream(); | |
TTransport client = new TIOStreamTransport(in, out); | |
TProcessor processor = null; | |
TTransport inputTransport = null; | |
TTransport outputTransport = null; | |
TProtocol inputProtocol = null; | |
TProtocol outputProtocol = null; | |
try { | |
processor = processor_; | |
inputTransport = inputTransportFactory_.getTransport(client); | |
outputTransport = outputTransportFactory_.getTransport(client); | |
inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); | |
outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); | |
while (processor.process(inputProtocol, outputProtocol)) {} | |
} catch (TTransportException ttx) { | |
// Client died, just move on | |
} catch (TException tx) { | |
tx.printStackTrace(); | |
} catch (Exception x) { | |
x.printStackTrace(); | |
} | |
if (inputTransport != null) { | |
inputTransport.close(); | |
} | |
if (outputTransport != null) { | |
outputTransport.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment