use of org.apache.qpid.server.model.Content in project qpid-broker-j by apache.
the class RestServlet method doOperation.
private void doOperation(final RequestInfo requestInfo, final ConfiguredObject<?> managedObject, final Class<? extends ConfiguredObject> configuredClass, final ConfiguredObjectFinder finder, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
ConfiguredObject<?> target = getTarget(requestInfo, managedObject, configuredClass, finder);
if (target == null) {
return;
}
String operationName = requestInfo.getOperationName();
final Map<String, ConfiguredObjectOperation<?>> availableOperations = managedObject.getModel().getTypeRegistry().getOperations(target.getClass());
ConfiguredObjectOperation operation = availableOperations.get(operationName);
Map<String, Object> operationArguments;
String requestMethod = request.getMethod();
if (operation == null) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "No such operation: " + operationName);
return;
} else {
switch(requestMethod) {
case "GET":
if (operation.isNonModifying()) {
operationArguments = getOperationArgumentsAsMap(request);
operationArguments.keySet().removeAll(Arrays.asList(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM));
} else {
response.addHeader("Allow", "POST");
sendJsonErrorResponse(request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Operation " + operationName + " modifies the object so you must use POST.");
return;
}
break;
case "POST":
operationArguments = getRequestProvidedObject(request, requestInfo);
break;
default:
response.addHeader("Allow", (operation.isNonModifying() ? "POST, GET" : "POST"));
sendJsonErrorResponse(request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Operation " + operationName + " does not support the " + requestMethod + " requestMethod.");
return;
}
}
if (operation.isSecure(target, operationArguments) && !(request.isSecure() || HttpManagementUtil.getPort(request).isAllowConfidentialOperationsOnInsecureChannels())) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_FORBIDDEN, "Operation '" + operationName + "' can only be performed over a secure (HTTPS) connection");
return;
}
Object returnVal = operation.perform(target, operationArguments);
String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
if (attachmentFilename != null) {
setContentDispositionHeaderIfNecessary(response, attachmentFilename);
}
if (returnVal instanceof Content) {
Content content = (Content) returnVal;
try {
writeTypedContent(content, request, response);
} finally {
content.release();
}
} else {
final ConfiguredObjectToMapConverter.ConverterOptions converterOptions = new ConfiguredObjectToMapConverter.ConverterOptions(DEFAULT_DEPTH, false, DEFAULT_OVERSIZE, request.isSecure(), true);
if (ConfiguredObject.class.isAssignableFrom(operation.getReturnType())) {
returnVal = _objectConverter.convertObjectToMap((ConfiguredObject<?>) returnVal, operation.getReturnType(), converterOptions);
} else if (returnsCollectionOfConfiguredObjects(operation)) {
List<Map<String, Object>> output = new ArrayList<>();
for (Object configuredObject : (Collection) returnVal) {
output.add(_objectConverter.convertObjectToMap((ConfiguredObject<?>) configuredObject, getCollectionMemberType((ParameterizedType) operation.getGenericReturnType()), converterOptions));
}
returnVal = output;
}
sendJsonResponse(returnVal, request, response);
}
}
Aggregations