use of org.apache.qpid.server.management.plugin.ResponseType in project qpid-broker-j by apache.
the class LatestManagementController method invoke.
@Override
@SuppressWarnings("unchecked")
public ManagementResponse invoke(final ConfiguredObject<?> root, final String category, final List<String> names, final String operationName, final Map<String, Object> operationArguments, final boolean isPost, final boolean isSecureOrAllowedOnInsecureChannel) throws ManagementException {
ResponseType responseType = ResponseType.DATA;
Object returnValue;
try {
final ConfiguredObject<?> target = getTarget(root, category, names);
final Map<String, ConfiguredObjectOperation<?>> availableOperations = root.getModel().getTypeRegistry().getOperations(target.getClass());
final ConfiguredObjectOperation operation = availableOperations.get(operationName);
if (operation == null) {
throw createNotFoundManagementException(String.format("No such operation '%s' in '%s'", operationName, category));
}
final Map<String, Object> arguments;
if (isPost) {
arguments = operationArguments;
} else {
final Set<String> supported = ((List<OperationParameter>) operation.getParameters()).stream().map(OperationParameter::getName).collect(Collectors.toSet());
arguments = operationArguments.entrySet().stream().filter(e -> !RESERVED_PARAMS.contains(e.getKey()) || supported.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
if (operation.isSecure(target, arguments) && !isSecureOrAllowedOnInsecureChannel) {
throw createForbiddenManagementException(String.format("Operation '%s' can only be performed over a secure (HTTPS) connection", operationName));
}
if (!isPost && !operation.isNonModifying()) {
throw createNotAllowedManagementException(String.format("Operation '%s' modifies the object so you must use POST.", operationName), Collections.singletonMap("Allow", "POST"));
}
returnValue = operation.perform(target, arguments);
if (ConfiguredObject.class.isAssignableFrom(operation.getReturnType()) || returnsCollectionOfConfiguredObjects(operation)) {
responseType = ResponseType.MODEL_OBJECT;
}
} catch (RuntimeException e) {
throw ManagementException.toManagementException(e, getCategoryMapping(category), names);
} catch (Error e) {
throw ManagementException.handleError(e);
}
return new ControllerManagementResponse(responseType, returnValue);
}
use of org.apache.qpid.server.management.plugin.ResponseType in project qpid-broker-j by apache.
the class AbstractManagementController method handlePostOrPut.
private ManagementResponse handlePostOrPut(final ManagementRequest request) throws ManagementException {
final RequestType type = getRequestType(request);
final Collection<String> hierarchy = getCategoryHierarchy(request.getRoot(), request.getCategory());
switch(type) {
case OPERATION:
{
final List<String> operationPath = request.getPath().subList(0, hierarchy.size());
final String operationName = request.getPath().get(hierarchy.size());
@SuppressWarnings("unchecked") final Map<String, Object> arguments = request.getBody(LinkedHashMap.class);
return invoke(request.getRoot(), request.getCategory(), operationPath, operationName, arguments, true, request.isSecure() || request.isConfidentialOperationAllowedOnInsecureChannel());
}
case MODEL_OBJECT:
{
@SuppressWarnings("unchecked") final Map<String, Object> attributes = request.getBody(LinkedHashMap.class);
final Object response = createOrUpdate(request.getRoot(), request.getCategory(), request.getPath(), attributes, "POST".equalsIgnoreCase(request.getMethod()));
int responseCode = HttpServletResponse.SC_OK;
ResponseType responseType = ResponseType.EMPTY;
Map<String, String> headers = Collections.emptyMap();
if (response != null) {
responseCode = HttpServletResponse.SC_CREATED;
final StringBuilder requestURL = new StringBuilder(request.getRequestURL());
if (hierarchy.size() != request.getPath().size()) {
Object name = attributes.get("name");
requestURL.append("/").append(encode(String.valueOf(name)));
}
headers = Collections.singletonMap("Location", requestURL.toString());
responseType = ResponseType.MODEL_OBJECT;
}
return new ControllerManagementResponse(responseType, response, responseCode, headers);
}
case VISIBLE_PREFERENCES:
case USER_PREFERENCES:
{
setPreferences(request.getRoot(), request.getCategory(), request.getPath(), request.getBody(Object.class), request.getParameters(), "POST".equalsIgnoreCase(request.getMethod()));
return new ControllerManagementResponse(ResponseType.EMPTY, null);
}
default:
{
throw createBadRequestManagementException(String.format("Unexpected request type '%s' for path '%s'", type, getCategoryMapping(request.getCategory())));
}
}
}
Aggregations