comparison JavaSDK/be/uclouvain/orthanc/RestOutput.java @ 0:3ecef5782f2c

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Oct 2023 17:59:44 +0200
parents
children 26c08ff926a3
comparison
equal deleted inserted replaced
-1:000000000000 0:3ecef5782f2c
1 package be.uclouvain.orthanc;
2
3 /**
4 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8 /**
9 * Java plugin for Orthanc
10 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium
11 *
12 * This program is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see http://www.gnu.org/licenses/.
24 **/
25
26
27 /**
28 * Output for a call to the REST API of Orthanc
29 **/
30 public class RestOutput {
31 private long self;
32
33 /**
34 * Construct a Java object wrapping a C object that is managed by Orthanc.
35 * @param self Pointer to the C object.
36 **/
37 protected RestOutput(long self) {
38 if (self == 0) {
39 throw new IllegalArgumentException("Null pointer");
40 } else {
41 this.self = self;
42 }
43 }
44
45 /**
46 * Return the C object that is associated with this Java wrapper.
47 * @return Pointer to the C object.
48 **/
49 protected long getSelf() {
50 return self;
51 }
52
53
54
55 /**
56 * Answer to a REST request.
57 *
58 * This function answers to a REST request with the content of a memory buffer.
59 *
60 * @param answer Pointer to the memory buffer containing the answer.
61 * @param mimeType The MIME type of the answer.
62 **/
63 public void answerBuffer(
64 byte[] answer,
65 String mimeType) {
66 NativeSDK.OrthancPluginAnswerBuffer(self, answer, mimeType);
67 }
68
69 /**
70 * Answer to a REST request with a PNG image.
71 *
72 * This function answers to a REST request with a PNG image. The parameters of this
73 * function describe a memory buffer that contains an uncompressed image. The image
74 * will be automatically compressed as a PNG image by the core system of Orthanc.
75 *
76 * @param format The memory layout of the uncompressed image.
77 * @param width The width of the image.
78 * @param height The height of the image.
79 * @param pitch The pitch of the image (i.e. the number of bytes between 2
80 * successive lines of the image in the memory buffer).
81 * @param buffer The memory buffer containing the uncompressed image.
82 **/
83 public void compressAndAnswerPngImage(
84 PixelFormat format,
85 int width,
86 int height,
87 int pitch,
88 byte[] buffer) {
89 NativeSDK.OrthancPluginCompressAndAnswerPngImage(self, format.getValue(), width, height, pitch, buffer);
90 }
91
92 /**
93 * Redirect a REST request.
94 *
95 * This function answers to a REST request by redirecting the user to another URI
96 * using HTTP status 301.
97 *
98 * @param redirection Where to redirect.
99 **/
100 public void redirect(
101 String redirection) {
102 NativeSDK.OrthancPluginRedirect(self, redirection);
103 }
104
105 /**
106 * Send a HTTP status code.
107 *
108 * This function answers to a REST request by sending a HTTP status code (such as
109 * "400 - Bad Request"). Note that: - Successful requests (status 200) must use
110 * ::OrthancPluginAnswerBuffer(). - Redirections (status 301) must use
111 * ::OrthancPluginRedirect(). - Unauthorized access (status 401) must use
112 * ::OrthancPluginSendUnauthorized(). - Methods not allowed (status 405) must use
113 * ::OrthancPluginSendMethodNotAllowed().
114 *
115 * @param status The HTTP status code to be sent.
116 **/
117 public void sendHttpStatusCode(
118 short status) {
119 NativeSDK.OrthancPluginSendHttpStatusCode(self, status);
120 }
121
122 /**
123 * Signal that a REST request is not authorized.
124 *
125 * This function answers to a REST request by signaling that it is not authorized.
126 *
127 * @param realm The realm for the authorization process.
128 **/
129 public void sendUnauthorized(
130 String realm) {
131 NativeSDK.OrthancPluginSendUnauthorized(self, realm);
132 }
133
134 /**
135 * Signal that this URI does not support this HTTP method.
136 *
137 * This function answers to a REST request by signaling that the queried URI does
138 * not support this method.
139 *
140 * @param allowedMethods The allowed methods for this URI (e.g. "GET,POST" after a
141 * PUT or a POST request).
142 **/
143 public void sendMethodNotAllowed(
144 String allowedMethods) {
145 NativeSDK.OrthancPluginSendMethodNotAllowed(self, allowedMethods);
146 }
147
148 /**
149 * Set a cookie.
150 *
151 * This function sets a cookie in the HTTP client.
152 *
153 * @param cookie The cookie to be set.
154 * @param value The value of the cookie.
155 **/
156 public void setCookie(
157 String cookie,
158 String value) {
159 NativeSDK.OrthancPluginSetCookie(self, cookie, value);
160 }
161
162 /**
163 * Set some HTTP header.
164 *
165 * This function sets a HTTP header in the HTTP answer.
166 *
167 * @param key The HTTP header to be set.
168 * @param value The value of the HTTP header.
169 **/
170 public void setHttpHeader(
171 String key,
172 String value) {
173 NativeSDK.OrthancPluginSetHttpHeader(self, key, value);
174 }
175
176 /**
177 * Start an HTTP multipart answer.
178 *
179 * Initiates a HTTP multipart answer, as the result of a REST request.
180 *
181 * @param subType The sub-type of the multipart answer ("mixed" or "related").
182 * @param contentType The MIME type of the items in the multipart answer.
183 **/
184 public void startMultipartAnswer(
185 String subType,
186 String contentType) {
187 NativeSDK.OrthancPluginStartMultipartAnswer(self, subType, contentType);
188 }
189
190 /**
191 * Send an item as a part of some HTTP multipart answer.
192 *
193 * This function sends an item as a part of some HTTP multipart answer that was
194 * initiated by OrthancPluginStartMultipartAnswer().
195 *
196 * @param answer Pointer to the memory buffer containing the item.
197 **/
198 public void sendMultipartItem(
199 byte[] answer) {
200 NativeSDK.OrthancPluginSendMultipartItem(self, answer);
201 }
202
203 /**
204 * Send a HTTP status, with a custom body.
205 *
206 * This function answers to a HTTP request by sending a HTTP status code (such as
207 * "400 - Bad Request"), together with a body describing the error. The body will
208 * only be returned if the configuration option "HttpDescribeErrors" of Orthanc is
209 * set to "true".
210 *
211 * Note that: - Successful requests (status 200) must use
212 * ::OrthancPluginAnswerBuffer(). - Redirections (status 301) must use
213 * ::OrthancPluginRedirect(). - Unauthorized access (status 401) must use
214 * ::OrthancPluginSendUnauthorized(). - Methods not allowed (status 405) must use
215 * ::OrthancPluginSendMethodNotAllowed().
216 *
217 * @param status The HTTP status code to be sent.
218 * @param body The body of the answer.
219 **/
220 public void sendHttpStatus(
221 short status,
222 byte[] body) {
223 NativeSDK.OrthancPluginSendHttpStatus(self, status, body);
224 }
225
226 /**
227 * Answer to a REST request with a JPEG image.
228 *
229 * This function answers to a REST request with a JPEG image. The parameters of
230 * this function describe a memory buffer that contains an uncompressed image. The
231 * image will be automatically compressed as a JPEG image by the core system of
232 * Orthanc.
233 *
234 * @param format The memory layout of the uncompressed image.
235 * @param width The width of the image.
236 * @param height The height of the image.
237 * @param pitch The pitch of the image (i.e. the number of bytes between 2
238 * successive lines of the image in the memory buffer).
239 * @param buffer The memory buffer containing the uncompressed image.
240 * @param quality The quality of the JPEG encoding, between 1 (worst quality, best
241 * compression) and 100 (best quality, worst compression).
242 **/
243 public void compressAndAnswerJpegImage(
244 PixelFormat format,
245 int width,
246 int height,
247 int pitch,
248 byte[] buffer,
249 byte quality) {
250 NativeSDK.OrthancPluginCompressAndAnswerJpegImage(self, format.getValue(), width, height, pitch, buffer, quality);
251 }
252
253 /**
254 * Provide a detailed description for an HTTP error.
255 *
256 * This function sets the detailed description associated with an HTTP error. This
257 * description will be displayed in the "Details" field of the JSON body of the
258 * HTTP answer. It is only taken into consideration if the REST callback returns an
259 * error code that is different from "OrthancPluginErrorCode_Success", and if the
260 * "HttpDescribeErrors" configuration option of Orthanc is set to "true".
261 *
262 * @param details The details of the error message.
263 * @param log Whether to also write the detailed error to the Orthanc logs.
264 **/
265 public void setHttpErrorDetails(
266 String details,
267 byte log) {
268 NativeSDK.OrthancPluginSetHttpErrorDetails(self, details, log);
269 }
270
271 }