use of com.yahoo.vespa.clustercontroller.utils.communication.http.JsonHttpResult in project vespa by vespa-engine.
the class RestApiHandler method handleRequest.
@Override
public HttpResult handleRequest(HttpRequest request) {
try {
final String[] unitPath = createUnitPath(request);
if (request.getHttpOperation().equals(HttpRequest.HttpOp.GET)) {
final int recursiveLevel = getRecursiveLevel(request);
UnitResponse data = restApi.getState(new UnitStateRequest() {
@Override
public int getRecursiveLevels() {
return recursiveLevel;
}
@Override
public String[] getUnitPath() {
return unitPath;
}
});
return new JsonHttpResult().setJson(jsonWriter.createJson(data));
} else {
final JsonReader.SetRequestData setRequestData = jsonReader.getStateRequestData(request);
SetResponse setResponse = restApi.setUnitState(new SetUnitStateRequest() {
@Override
public Map<String, UnitState> getNewState() {
return setRequestData.stateMap;
}
@Override
public String[] getUnitPath() {
return unitPath;
}
@Override
public Condition getCondition() {
return setRequestData.condition;
}
@Override
public ResponseWait getResponseWait() {
return setRequestData.responseWait;
}
});
return new JsonHttpResult().setJson(jsonWriter.createJson(setResponse));
}
} catch (OtherMasterException exception) {
logRequestException(request, exception, LogLevel.DEBUG);
JsonHttpResult result = new JsonHttpResult();
result.setHttpCode(307, "Temporary Redirect");
result.addHeader("Location", getMasterLocationUrl(request, exception.getHost(), exception.getPort()));
result.setJson(jsonWriter.createErrorJson(exception.getMessage()));
return result;
} catch (UnknownMasterException exception) {
logRequestException(request, exception, Level.WARNING);
JsonHttpResult result = new JsonHttpResult();
result.setHttpCode(503, "Service Unavailable");
result.setJson(jsonWriter.createErrorJson(exception.getMessage()));
return result;
} catch (DeadlineExceededException exception) {
logRequestException(request, exception, Level.WARNING);
JsonHttpResult result = new JsonHttpResult();
result.setHttpCode(504, "Gateway Timeout");
result.setJson(jsonWriter.createErrorJson(exception.getMessage()));
return result;
} catch (StateRestApiException exception) {
logRequestException(request, exception, Level.WARNING);
JsonHttpResult result = new JsonHttpResult();
result.setHttpCode(500, "Failed to process request");
if (exception.getStatus() != null)
result.setHttpCode(result.getHttpReturnCode(), exception.getStatus());
if (exception.getCode() != null)
result.setHttpCode(exception.getCode(), result.getHttpReturnCodeDescription());
result.setJson(jsonWriter.createErrorJson(exception.getMessage()));
return result;
} catch (Exception exception) {
logRequestException(request, exception, LogLevel.ERROR);
JsonHttpResult result = new JsonHttpResult();
result.setHttpCode(500, "Failed to process request");
result.setJson(jsonWriter.createErrorJson(exception.getClass().getName() + ": " + exception.getMessage()));
return result;
}
}
Aggregations