Skip to content

Package: ThriftHttpHandler

ThriftHttpHandler

nameinstructionbranchcomplexitylinemethod
ThriftHttpHandler(TProcessor)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
ThriftHttpHandler(TProcessor, TProtocolFactory)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
ThriftHttpHandler(TProcessor, TProtocolFactory, int)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
ThriftHttpHandler(TProcessor, int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
service(Request, Response)
M: 98 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2014, 2017 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.glassfish.grizzly.thrift.http;
18:
19: import java.io.IOException;
20:
21: import org.apache.thrift.TException;
22: import org.apache.thrift.TProcessor;
23: import org.apache.thrift.protocol.TBinaryProtocol;
24: import org.apache.thrift.protocol.TProtocol;
25: import org.apache.thrift.protocol.TProtocolFactory;
26: import org.apache.thrift.transport.TTransport;
27: import org.glassfish.grizzly.Buffer;
28: import org.glassfish.grizzly.http.server.HttpHandler;
29: import org.glassfish.grizzly.http.server.Request;
30: import org.glassfish.grizzly.http.server.Response;
31: import org.glassfish.grizzly.memory.MemoryManager;
32: import org.glassfish.grizzly.thrift.TGrizzlyServerTransport;
33: import org.glassfish.grizzly.utils.BufferOutputStream;
34:
35: /**
36: * ThriftHttpHandler is a server-side http handler for Thrift RPC processors.
37: * <p>
38: * You can set the specific response size by constructor for optimal
39: * performance.
40: * <p>
41: * Usages:
42: *
43: * <pre>
44: * {@code
45: * final user-generated.thrift.Processor tprocessor = new user-generated.thrift.Processor(new user-generated.thrift.Handler);
46: * final HttpServer server = new HttpServer();
47: * final NetworkListener listener = new NetworkListener("yourServerName", yourHost, yourPort);
48: * server.addListener(listener);
49: * server.getServerConfiguration().addHttpHandler(new ThriftHttpHandler(tprocessor), "/yourUriPath");
50: * server.start();
51: * // release
52: * //...
53: * }
54: * </pre>
55: *
56: * @author Bongjae Chang
57: */
58: public class ThriftHttpHandler extends HttpHandler {
59:
60: private static final int THRIFT_DEFAULT_RESPONSE_BUFFER_SIZE = 40 * 1024; // 40k;
61: private static final String THRIFT_HTTP_CONTENT_TYPE = "application/x-thrift";
62:
63: private final TProcessor processor;
64: private final TProtocolFactory protocolFactory;
65: private final int responseSize;
66:
67: public ThriftHttpHandler(final TProcessor processor) {
68: this(processor, new TBinaryProtocol.Factory(), THRIFT_DEFAULT_RESPONSE_BUFFER_SIZE);
69: }
70:
71: public ThriftHttpHandler(final TProcessor processor, final TProtocolFactory protocolFactory) {
72: this(processor, protocolFactory, THRIFT_DEFAULT_RESPONSE_BUFFER_SIZE);
73: }
74:
75: public ThriftHttpHandler(final TProcessor processor, final int responseSize) {
76: this(processor, new TBinaryProtocol.Factory(), responseSize);
77: }
78:
79: public ThriftHttpHandler(final TProcessor processor, final TProtocolFactory protocolFactory, final int responseSize) {
80: this.processor = processor;
81:• if (protocolFactory == null) {
82: this.protocolFactory = new TBinaryProtocol.Factory();
83: } else {
84: this.protocolFactory = protocolFactory;
85: }
86:• if (responseSize < THRIFT_DEFAULT_RESPONSE_BUFFER_SIZE) {
87: this.responseSize = THRIFT_DEFAULT_RESPONSE_BUFFER_SIZE;
88: } else {
89: this.responseSize = responseSize;
90: }
91: }
92:
93: @Override
94: public void service(Request request, Response response) throws Exception {
95:• if (processor == null) {
96: throw new IllegalStateException("TProcessor should not be null");
97: }
98:
99: final Buffer inputBuffer = request.getInputBuffer().getBuffer();
100:• if (inputBuffer == null) {
101: throw new IOException("input buffer should not be null");
102: }
103:• if (!inputBuffer.hasRemaining()) {
104: throw new IOException("input buffer doesn't have the remaining data");
105: }
106:
107: final MemoryManager memoryManager = request.getContext().getMemoryManager();
108: final BufferOutputStream outputStream = new BufferOutputStream(memoryManager, memoryManager.allocate(responseSize));
109: final TTransport ttransport = new TGrizzlyServerTransport(inputBuffer, outputStream);
110: final TProtocol protocol = protocolFactory.getProtocol(ttransport);
111: try {
112: processor.process(protocol, protocol);
113: } catch (TException te) {
114: ttransport.close();
115: inputBuffer.dispose();
116: outputStream.getBuffer().dispose();
117: throw new IOException(te);
118: }
119: inputBuffer.dispose();
120:
121: final Buffer outputBuffer = outputStream.getBuffer();
122: outputBuffer.trim();
123: outputBuffer.allowBufferDispose(true);
124: response.setContentType(THRIFT_HTTP_CONTENT_TYPE);
125: response.setContentLength(outputBuffer.remaining());
126: response.getNIOOutputStream().write(outputBuffer);
127: try {
128: outputStream.close();
129: } catch (IOException ignore) {
130: }
131:
132: ttransport.close();
133: }
134: }