]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/server/TExtensibleServlet.java
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / src / org / apache / thrift / server / TExtensibleServlet.java
CommitLineData
f67539c2
TL
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package org.apache.thrift.server;
21
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.OutputStream;
25import java.util.ArrayList;
26import java.util.Collection;
27import java.util.Map;
28
29import javax.servlet.ServletConfig;
30import javax.servlet.ServletContext;
31import javax.servlet.ServletException;
32import javax.servlet.http.HttpServlet;
33import javax.servlet.http.HttpServletRequest;
34import javax.servlet.http.HttpServletResponse;
35
36import org.apache.thrift.TException;
37import org.apache.thrift.TProcessor;
38import org.apache.thrift.protocol.TProtocol;
39import org.apache.thrift.protocol.TProtocolFactory;
40import org.apache.thrift.transport.TIOStreamTransport;
41import org.apache.thrift.transport.TTransport;
42
43/**
44 * Servlet implementation class ThriftServer, that allows {@link TProcessor} and
45 * {@link TProtocolFactory} to be supplied after the {@link #init()} method has
46 * finished. <br>
47 * Subclasses must implement the abstract methods that return the TProcessor and
48 * two TProtocolFactory. Those methods are guaranteed to be called exactly once,
49 * and that {@link ServletContext} is available.
50 */
51public abstract class TExtensibleServlet extends HttpServlet {
52 private static final long serialVersionUID = 1L;
53
54 private TProcessor processor;
55
56 private TProtocolFactory inFactory;
57
58 private TProtocolFactory outFactory;
59
60 private Collection<Map.Entry<String, String>> customHeaders;
61
62 /**
63 * Returns the appropriate {@link TProcessor}. This will be called <b>once</b> just
64 * after the {@link #init()} method
65 *
66 * @return the appropriate {@link TProcessor}
67 */
68 protected abstract TProcessor getProcessor();
69
70 /**
71 * Returns the appropriate in {@link TProtocolFactory}. This will be called
72 * <b>once</b> just after the {@link #init()} method
73 *
74 * @return the appropriate in {@link TProtocolFactory}
75 */
76 protected abstract TProtocolFactory getInProtocolFactory();
77
78 /**
79 * Returns the appropriate out {@link TProtocolFactory}. This will be called
80 * <b>once</b> just after the {@link #init()} method
81 *
82 * @return the appropriate out {@link TProtocolFactory}
83 */
84 protected abstract TProtocolFactory getOutProtocolFactory();
85
86 @Override
87 public final void init(ServletConfig config) throws ServletException {
88 super.init(config); //no-args init() happens here
89 this.processor = getProcessor();
90 this.inFactory = getInProtocolFactory();
91 this.outFactory = getOutProtocolFactory();
92 this.customHeaders = new ArrayList<Map.Entry<String, String>>();
93
94 if (processor == null) {
95 throw new ServletException("processor must be set");
96 }
97 if (inFactory == null) {
98 throw new ServletException("inFactory must be set");
99 }
100 if (outFactory == null) {
101 throw new ServletException("outFactory must be set");
102 }
103 }
104
105 /**
106 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
107 * response)
108 */
109 @Override
110 protected void doPost(HttpServletRequest request, HttpServletResponse response)
111 throws ServletException, IOException {
112 TTransport inTransport = null;
113 TTransport outTransport = null;
114
115 try {
116 response.setContentType("application/x-thrift");
117
118 if (null != this.customHeaders) {
119 for (Map.Entry<String, String> header : this.customHeaders) {
120 response.addHeader(header.getKey(), header.getValue());
121 }
122 }
123
124 InputStream in = request.getInputStream();
125 OutputStream out = response.getOutputStream();
126
127 TTransport transport = new TIOStreamTransport(in, out);
128 inTransport = transport;
129 outTransport = transport;
130
131 TProtocol inProtocol = inFactory.getProtocol(inTransport);
132 TProtocol outProtocol = inFactory.getProtocol(outTransport);
133
134 processor.process(inProtocol, outProtocol);
135 out.flush();
136 } catch (TException te) {
137 throw new ServletException(te);
138 }
139 }
140
141 /**
142 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
143 * response)
144 */
145 @Override
146 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
147 throws ServletException, IOException {
148 doPost(req, resp);
149 }
150
151 public void addCustomHeader(final String key, final String value) {
152 this.customHeaders.add(new Map.Entry<String, String>() {
153 public String getKey() {
154 return key;
155 }
156
157 public String getValue() {
158 return value;
159 }
160
161 public String setValue(String value) {
162 return null;
163 }
164 });
165 }
166
167 public void setCustomHeaders(Collection<Map.Entry<String, String>> headers) {
168 this.customHeaders.clear();
169 this.customHeaders.addAll(headers);
170 }
171}