Skip to content

Commit

Permalink
[HWKMETRICS-801] return a proper JSON object as expected by Heapster …
Browse files Browse the repository at this point in the history
…if namespace doesn't exist (#994)
  • Loading branch information
jmartisk authored and rubenvp8510 committed Nov 14, 2018
1 parent c6eec05 commit afac7f0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Red Hat, Inc. and/or its affiliates
* Copyright 2014-2018 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,8 +17,10 @@

package org.hawkular.openshift.auth;

import io.undertow.io.Sender;
import io.undertow.server.HttpServerExchange;


/**
* @author Thomas Segismont
*/
Expand All @@ -34,7 +36,7 @@ public class Utils {
* @see HttpServerExchange#endExchange()
*/
public static void endExchange(HttpServerExchange exchange, int statusCode) {
endExchange(exchange, statusCode, null);
endExchange(exchange, statusCode, null, null);
}

/**
Expand All @@ -49,10 +51,37 @@ public static void endExchange(HttpServerExchange exchange, int statusCode) {
* @see HttpServerExchange#endExchange()
*/
public static void endExchange(HttpServerExchange exchange, int statusCode, String reasonPhrase) {
endExchange(exchange, statusCode, reasonPhrase, null);
}

/**
* Changes the status code of the response, sets the HTTP reason phrase and the response body, and ends the exchange.
*
* @param exchange the HTTP server request/response exchange
* @param statusCode the HTTP status code
* @param reasonPhrase the HTTP status message
* @param body the body of the response
*
* @see HttpServerExchange#setStatusCode(int)
* @see HttpServerExchange#setReasonPhrase(String)
* @see HttpServerExchange#endExchange()
*/
public static void endExchange(HttpServerExchange exchange, int statusCode, String reasonPhrase, String body) {
exchange.setStatusCode(statusCode);
if (reasonPhrase != null) {
exchange.setReasonPhrase(reasonPhrase);
}
if(body != null) {
Sender sender = null;
try {
sender = exchange.getResponseSender();
sender.send(body);
} finally {
if (sender != null) {
sender.close();
}
}
}
exchange.endExchange();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* Copyright 2014-2018 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -103,7 +103,9 @@ public void handleRequest(HttpServerExchange serverExchange) throws Exception {

} else {
log.debug("Could not determine a namespace id for namespace. Cannot process request. Returning NOT_FOUND.");
Utils.endExchange(serverExchange, NOT_FOUND, "Could not determine a namespace id for namespace " + namespaceID);
Utils.endExchange(serverExchange, NOT_FOUND,
"Could not determine a namespace id for namespace " + namespace,
"{\"ErrorMsg\": \"Could not determine a namespace id for namespace " + namespace+ "\"}");
return;
}
}
Expand Down

0 comments on commit afac7f0

Please sign in to comment.