Mercurial > hg > orthanc-java
comparison Samples/FHIR/src/main/java/FhirRequestHandler.java @ 11:8d876a4f541b
added sample FHIR server
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 21 Oct 2023 09:53:25 +0200 |
parents | |
children | 0dc05fe76bd5 |
comparison
equal
deleted
inserted
replaced
10:6b9433432ee0 | 11:8d876a4f541b |
---|---|
1 /** | |
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium | |
3 * SPDX-License-Identifier: GPL-3.0-or-later | |
4 */ | |
5 | |
6 /** | |
7 * Java plugin for Orthanc | |
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium | |
9 * | |
10 * This program is free software: you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, either version 3 of the | |
13 * License, or (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, but | |
16 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 import ca.uhn.fhir.rest.server.RestfulServer; | |
26 import be.uclouvain.orthanc.Callbacks; | |
27 import be.uclouvain.orthanc.RestOutput; | |
28 import be.uclouvain.orthanc.HttpMethod; | |
29 import org.springframework.mock.web.MockHttpServletRequest; | |
30 import org.springframework.mock.web.MockHttpServletResponse; | |
31 | |
32 import javax.servlet.ServletException; | |
33 import java.io.IOException; | |
34 import java.util.Map; | |
35 | |
36 public class FhirRequestHandler implements Callbacks.OnRestRequest { | |
37 private static RestfulServer server; | |
38 | |
39 FhirRequestHandler(RestfulServer server) { | |
40 this.server = server; | |
41 } | |
42 | |
43 @Override | |
44 public void call(RestOutput output, | |
45 HttpMethod method, | |
46 String uri, | |
47 String[] regularExpressionGroups, | |
48 Map<String, String> headers, | |
49 Map<String, String> getParameters, byte[] body) { | |
50 MockHttpServletRequest request = new MockHttpServletRequest(); | |
51 | |
52 if (regularExpressionGroups.length == 0) { | |
53 request.setRequestURI("/"); | |
54 } else { | |
55 request.setRequestURI(regularExpressionGroups[0]); | |
56 } | |
57 request.setContent(body); | |
58 | |
59 switch (method) { | |
60 case GET: | |
61 request.setMethod("GET"); | |
62 break; | |
63 case POST: | |
64 request.setMethod("POST"); | |
65 break; | |
66 case PUT: | |
67 request.setMethod("PUT"); | |
68 break; | |
69 case DELETE: | |
70 request.setMethod("DELETE"); | |
71 break; | |
72 default: | |
73 throw new IllegalArgumentException("Unknown HTTP method"); | |
74 } | |
75 | |
76 for (Map.Entry<String, String> entry : headers.entrySet()) { | |
77 request.addHeader(entry.getKey(), entry.getValue()); | |
78 } | |
79 | |
80 for (Map.Entry<String, String> entry : getParameters.entrySet()) { | |
81 request.setParameter(entry.getKey(), entry.getValue()); | |
82 } | |
83 | |
84 try { | |
85 MockHttpServletResponse response = new MockHttpServletResponse(); | |
86 server.service(request, response); | |
87 | |
88 for (String header : response.getHeaderNames()) { | |
89 if (header != "Content-Type") { | |
90 output.setHttpHeader(header, response.getHeader(header)); | |
91 } | |
92 } | |
93 | |
94 output.answerBuffer(response.getContentAsByteArray(), response.getContentType()); | |
95 } catch (IOException e) { | |
96 output.sendHttpStatusCode((short) 500); | |
97 } catch (ServletException e) { | |
98 output.sendHttpStatusCode((short) 500); | |
99 } | |
100 } | |
101 } |