mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
#67 [CLI,DAEMON] Return proper return codes / status codes
This commit is contained in:
parent
fa7faf9961
commit
f2223552ad
21 changed files with 424 additions and 248 deletions
|
|
@ -15,15 +15,20 @@ abstract class BaseHandler implements HttpHandler {
|
|||
|
||||
protected static final String APPLICATION_XML = "application/xml";
|
||||
|
||||
static final int OK = 200;
|
||||
|
||||
protected static void write(final HttpExchange exchange, final byte[] content, final String contentType) throws IOException {
|
||||
write(exchange, contentType, os -> os.write(content));
|
||||
write(exchange, content, contentType, HttpStatus.SC_OK);
|
||||
}
|
||||
|
||||
protected static void write(final HttpExchange exchange, final String contentType, final Write write) throws IOException {
|
||||
protected static void write(final HttpExchange exchange, final byte[] content, final String contentType, final int statusCode)
|
||||
throws IOException {
|
||||
write(exchange, contentType, os -> os.write(content), statusCode);
|
||||
}
|
||||
|
||||
protected static void write(final HttpExchange exchange, final String contentType, final Write write, final int statusCode)
|
||||
throws IOException {
|
||||
exchange.getResponseHeaders().add("Content-Type", contentType);
|
||||
exchange.sendResponseHeaders(OK, 0);
|
||||
exchange.sendResponseHeaders(statusCode, 0);
|
||||
final OutputStream os = exchange.getResponseBody();
|
||||
write.write(os);
|
||||
os.close();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package de.kosit.validationtool.daemon;
|
|||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
|
@ -49,21 +50,36 @@ class CheckHandler extends BaseHandler {
|
|||
final InputStream inputStream = httpExchange.getRequestBody();
|
||||
if (inputStream.available() > 0) {
|
||||
final SourceInput serverInput = (SourceInput) InputFactory.read(inputStream,
|
||||
"supplied_instance_" + counter.incrementAndGet());
|
||||
resolveInputName(httpExchange.getRequestURI()));
|
||||
final Result result = this.implemenation.checkInput(serverInput);
|
||||
write(httpExchange, serialize(result), APPLICATION_XML);
|
||||
write(httpExchange, serialize(result), APPLICATION_XML, resolveStatus(result));
|
||||
} else {
|
||||
error(httpExchange, 400, "No content supplied");
|
||||
error(httpExchange, HttpStatus.SC_BAD_REQUEST, "No content supplied");
|
||||
}
|
||||
|
||||
} else {
|
||||
error(httpExchange, 405, "Method not supported");
|
||||
error(httpExchange, HttpStatus.SC_METHOD_NOT_ALLOWED, "Method not supported");
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
error(httpExchange, 500, "Internal error: " + e.getMessage());
|
||||
error(httpExchange, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String resolveInputName(final URI requestURI) {
|
||||
final String path = requestURI.getPath();
|
||||
if (path.equalsIgnoreCase("/")) {
|
||||
return "supplied_instance_" + counter.incrementAndGet();
|
||||
}
|
||||
return path.substring((path.lastIndexOf('/') + 1));
|
||||
}
|
||||
|
||||
private static int resolveStatus(final Result result) {
|
||||
if (result.isProcessingSuccessful()) {
|
||||
return result.isAcceptable() ? HttpStatus.SC_OK : HttpStatus.SC_NOT_ACCEPTABLE;
|
||||
}
|
||||
return HttpStatus.SC_UNPROCESSABLE_ENTITY;
|
||||
}
|
||||
|
||||
private byte[] serialize(final Result result) {
|
||||
try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
|
||||
final Serializer serializer = this.processor.newSerializer(out);
|
||||
|
|
|
|||
32
src/main/java/de/kosit/validationtool/daemon/HttpStatus.java
Normal file
32
src/main/java/de/kosit/validationtool/daemon/HttpStatus.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package de.kosit.validationtool.daemon;
|
||||
|
||||
/**
|
||||
* Status codes for the HTTP daemon.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
public interface HttpStatus {
|
||||
|
||||
// --- 2xx Success ---
|
||||
|
||||
/** {@code 200 OK} (HTTP/1.0 - RFC 1945) */
|
||||
int SC_OK = 200;
|
||||
|
||||
// --- 4xx Client Error ---
|
||||
|
||||
/** {@code 400 Bad Request} (HTTP/1.1 - RFC 2616) */
|
||||
int SC_BAD_REQUEST = 400;
|
||||
|
||||
/** {@code 405 Method Not Allowed} (HTTP/1.1 - RFC 2616) */
|
||||
int SC_METHOD_NOT_ALLOWED = 405;
|
||||
|
||||
/** {@code 406 Not Acceptable} (HTTP/1.1 - RFC 2616) */
|
||||
int SC_NOT_ACCEPTABLE = 406;
|
||||
|
||||
/** {@code 422 Unprocessable Entity} (WebDAV - RFC 2518) */
|
||||
public static final int SC_UNPROCESSABLE_ENTITY = 422;
|
||||
|
||||
/** {@code 500 Server Error} (HTTP/1.0 - RFC 1945) */
|
||||
int SC_INTERNAL_SERVER_ERROR = 500;
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue