MCPcopy Create free account
hub / github.com/BaseXdb/basex / send

Method send

basex-core/src/main/java/org/basex/util/http/Client.java:127–181  ·  view source on GitHub ↗

Returns an HTTP response for the specified request. @param uri target URI @param request request @return HTTP connection @throws IOException I/O Exception @throws MalformedURLException incorrect url

(final URI uri, final Request request)

Source from the content-addressed store, hash-verified

125 * @throws MalformedURLException incorrect url
126 */
127 private static HttpResponse<InputStream> send(final URI uri, final Request request)
128 throws IOException {
129
130 final HttpRequest.Builder rb;
131 try {
132 rb = HttpRequest.newBuilder(uri);
133
134 // set timeout
135 final String timeout = request.attribute(TIMEOUT);
136 if(timeout != null) rb.timeout(Duration.ofSeconds(Strings.toInt(timeout)));
137
138 // set method, attach payload
139 final String method = request.attribute(METHOD);
140 if(method != null) {
141 final BodyPublisher publisher;
142 if(request.payload.isEmpty() && request.parts.isEmpty()) {
143 publisher = HttpRequest.BodyPublishers.noBody();
144 } else {
145 setContentType(rb, request);
146 publisher = HttpRequest.BodyPublishers.ofByteArray(payload(request));
147 }
148 rb.method(method, publisher);
149 }
150
151 // assign headers to request; ensure that Accept header is sent; catch illegal header names
152 request.headers.forEach(rb::header);
153 if(((Checks<String>) name -> !name.equalsIgnoreCase(ACCEPT)).all(request.headers.keySet())) {
154 rb.header(ACCEPT, MediaType.ALL_ALL.toString());
155 }
156 } catch(final IllegalArgumentException ex) {
157 Util.debug(ex);
158 throw new IOException(ex.getMessage());
159 }
160
161 final String fw = request.attribute(FOLLOW_REDIRECT);
162 final HttpClient client = IOUrl.client(fw == null || Strings.isTrue(fw));
163 final BodyHandler<InputStream> handler = HttpResponse.BodyHandlers.ofInputStream();
164
165 // send request (with optional authorization)
166 try {
167 final UserInfo ui = new UserInfo(uri, request);
168 final boolean sa = Strings.isTrue(request.attribute(SEND_AUTHORIZATION));
169 if(sa && request.authMethod == AuthMethod.BASIC) {
170 ui.basic(rb);
171 } else {
172 final HttpResponse<InputStream> response = client.send(rb.build(), handler);
173 if(!ui.assign(rb, response)) return response;
174 }
175 return client.send(rb.build(), handler);
176 } catch(final InterruptedException | IllegalArgumentException ex) {
177 // illegal argument exception may be caused by wrongly encoded redirect URL
178 Util.debug(ex);
179 throw new IOException(ex.getMessage());
180 }
181 }
182
183 /**
184 * Sets the content type of the HTTP request.

Callers 3

sendRequestMethod · 0.95
execMethod · 0.45
responseMethod · 0.45

Calls 15

toIntMethod · 0.95
setContentTypeMethod · 0.95
payloadMethod · 0.95
debugMethod · 0.95
clientMethod · 0.95
isTrueMethod · 0.95
basicMethod · 0.95
assignMethod · 0.95
timeoutMethod · 0.80
allMethod · 0.65
toStringMethod · 0.65
attributeMethod · 0.45

Tested by 1

execMethod · 0.36