public WebServiceHttpHandler extends Handler.Abstract { private WebService ws; @Override public final boolean handle(Request request, Response response, Callback callback) throws Exception { boolean handled = true; var exchange = new HttpExchange(request, response, callback); try { handled = handle(exchange); if (handled) exchange.succeeded(); } catch (Exception exc) { exchange.failed(exc); } if (handled) httpStatistics.registerExchange(exchange); return handled; } protected boolean handle(HttpExchange exchange) throws IOException { HttpMethod method = exchange.getHttpMethod(); if (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method)) handleGetRequest(exchange); else if (HttpMethod.POST.equals(method)) handlePostRequest(exchange); else exchange.showAllowedMethods(HttpMethod.OPTIONS.equals(method), HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST, HttpMethod.OPTIONS); return true; } protected void handlePostRequest(HttpExchange exchange) throws IOException { int statusNr; Logger logger = ws.getLogger(); MimeHeaders headers = new MimeHeaders(); for (HttpField field : exchange.getRequestHeaders()) headers.addHeader(field.getName(), field.getValue()); SOAPMessage responseMsg = null; StdByteArrayOutputStream baos = null; boolean clientError = true; MessageFactory factory = null; ExecutionContext execCtx = exchange.getExecutionContext(); try { factory = MessageFactory.newInstance(); SOAPMessage msg; try (InputStream is = exchange.getInputStream()) { msg = factory.createMessage(headers, is); } clientError = false; WebServiceRequest wsRequest = new WebServiceRequest(execCtx, exchange, msg); String operationName = wsRequest.getOperationName(); WebServiceHandler handler = ws.getHandler(operationName); if (handler != null) { WebServiceResponse wsResponse = new WebServiceResponse(operationName, ws.getTargetNamespace()); handler.handle(wsRequest, wsResponse); if (wsResponse.hasMessage()) { responseMsg = wsResponse.getMessage(); responseMsg.saveChanges(); baos = new StdByteArrayOutputStream(1024); responseMsg.writeTo(baos); statusNr = HttpStatus.OK_200; } else statusNr = HttpStatus.NO_CONTENT_204; } else statusNr = HttpStatus.NOT_FOUND_404; } catch (Exception exc) { statusNr = clientError ? HttpStatus.BAD_REQUEST_400 : HttpStatus.INTERNAL_SERVER_ERROR_500; responseMsg = null; baos = null; if (factory != null) { try { responseMsg = factory.createMessage(); SOAPBody body = responseMsg.getSOAPBody(); if (exc instanceof SOAPFaultDataException fde) fde.addFault(body); else { logger.logp(Debug.ERROR, CLS_NAME, "handlePostRequest", "exception while invoking handler", exc); Locale locale = execCtx.getLocale(); String errMsg = Exceptions.getDescriptions(exc, locale, 0); if (exc instanceof DataException de) { String name = de.getName(locale); if (Strings.containsData(name)) { String title = Exceptions.getExceptionTitle(name, locale); errMsg = Strings.concat(title, ":\n", errMsg); } } SOAPFaultDataException.addFault(body, clientError, errMsg); } responseMsg.saveChanges(); baos = new StdByteArrayOutputStream(512); responseMsg.writeTo(baos); } catch (Exception exc2) { responseMsg = null; baos = null; } } } exchange.setStatus(statusNr); if (responseMsg != null) { responseMsg.getMimeHeaders().getAllHeaders() .forEachRemaining(h -> exchange.setResponseHeader(h.getName(), h.getValue())); try (OutputStream os = exchange.getOutputStream()) { baos.writeTo(os); } } } }