MCPcopy Create free account
hub / github.com/apache/thrift / handle

Method handle

lib/js/test/src/test/Httpd.java:111–207  ·  view source on GitHub ↗
(final HttpRequest request, final HttpResponse response, final HttpContext context)

Source from the content-addressed store, hash-verified

109 }
110
111 public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException {
112
113 String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
114 if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
115 throw new MethodNotSupportedException(method + " method not supported");
116 }
117 String target = request.getRequestLine().getUri();
118
119 if (request instanceof HttpEntityEnclosingRequest && target.equals("/service")) {
120 HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
121 byte[] entityContent = EntityUtils.toByteArray(entity);
122 System.out.println("Incoming content: " + new String(entityContent));
123
124 final String output = this.thriftRequest(entityContent);
125
126 System.out.println("Outgoing content: "+output);
127
128 EntityTemplate body = new EntityTemplate(new ContentProducer() {
129
130 public void writeTo(final OutputStream outstream) throws IOException {
131 OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
132 writer.write(output);
133 writer.flush();
134 }
135
136 });
137 body.setContentType("text/html; charset=UTF-8");
138 response.setEntity(body);
139 } else {
140 if(target.indexOf("?") != -1) {
141 target = target.substring(1, target.indexOf("?"));
142 }
143
144 final File file = new File(this.docRoot, URLDecoder.decode(target, "UTF-8"));
145
146 if (!file.exists()) {
147
148 response.setStatusCode(HttpStatus.SC_NOT_FOUND);
149 EntityTemplate body = new EntityTemplate(new ContentProducer() {
150
151 public void writeTo(final OutputStream outstream) throws IOException {
152 OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
153 writer.write("<html><body><h1>");
154 writer.write("File ");
155 writer.write(file.getPath());
156 writer.write(" not found");
157 writer.write("</h1></body></html>");
158 writer.flush();
159 }
160
161 });
162 body.setContentType("text/html; charset=UTF-8");
163 response.setEntity(body);
164 System.out.println("File " + file.getPath() + " not found");
165
166 } else if (!file.canRead() || file.isDirectory()) {
167
168 response.setStatusCode(HttpStatus.SC_FORBIDDEN);

Callers

nothing calls this directly

Calls 6

thriftRequestMethod · 0.95
equalsMethod · 0.45
toByteArrayMethod · 0.45
decodeMethod · 0.45
getPathMethod · 0.45
getNameMethod · 0.45

Tested by

no test coverage detected