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

Method handle

tutorial/js/src/Httpd.java:106–183  ·  view source on GitHub ↗
(final HttpRequest request, final HttpResponse response, final HttpContext context)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected