use of org.apache.qpid.server.model.ConfiguredObjectFinder in project qpid-broker-j by apache.
the class AbstractServlet method getConfiguredObjectFinder.
protected final ConfiguredObjectFinder getConfiguredObjectFinder(final ConfiguredObject<?> root) {
ConfiguredObjectFinder finder = _configuredObjectFinders.get(root);
if (finder == null) {
finder = new ConfiguredObjectFinder(root);
final ConfiguredObjectFinder existingValue = _configuredObjectFinders.putIfAbsent(root, finder);
if (existingValue != null) {
finder = existingValue;
} else {
final AbstractConfigurationChangeListener deletionListener = new AbstractConfigurationChangeListener() {
@Override
public void stateChanged(final ConfiguredObject<?> object, final State oldState, final State newState) {
if (newState == State.DELETED) {
_configuredObjectFinders.remove(root);
}
}
};
root.addChangeListener(deletionListener);
if (root.getState() == State.DELETED) {
_configuredObjectFinders.remove(root);
root.removeChangeListener(deletionListener);
}
}
}
return finder;
}
use of org.apache.qpid.server.model.ConfiguredObjectFinder in project qpid-broker-j by apache.
the class ApiDocsServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response, final ConfiguredObject<?> managedObject) throws ServletException, IOException {
ConfiguredObjectFinder finder = getConfiguredObjectFinder(managedObject);
final Class<? extends ConfiguredObject> configuredClass;
final Class<? extends ConfiguredObject>[] hierarchy;
final String[] servletPathParts = request.getServletPath().split("/");
final Model model = managedObject.getModel();
if (servletPathParts.length < 4) {
configuredClass = null;
hierarchy = null;
} else {
configuredClass = getConfiguredClass(request, managedObject);
if (configuredClass == null) {
sendError(response, HttpServletResponse.SC_NOT_FOUND);
return;
}
hierarchy = finder.getHierarchy(configuredClass);
if (hierarchy == null) {
sendError(response, HttpServletResponse.SC_NOT_FOUND);
return;
}
if (!_typeSpecialisations.containsKey(configuredClass)) {
List<Class<? extends ConfiguredObject>> types = new ArrayList<>(model.getTypeRegistry().getTypeSpecialisations(configuredClass));
_typeSpecialisations.putIfAbsent(configuredClass, types);
}
}
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter writer = response.getWriter();
writePreamble(writer);
writeHead(writer, hierarchy, configuredClass);
if (hierarchy == null) {
writer.println("<table class=\"api\">");
writer.println("<thead>");
writer.println("<tr>");
writer.println("<th class=\"type\">Type</th>");
writer.println("<th class=\"path\">Path</th>");
writer.println("<th class=\"description\">Description</th>");
writer.println("</tr>");
writer.println("</thead>");
writer.println("<tbody>");
SortedSet<Class<? extends ConfiguredObject>> managedCategories = new TreeSet<>(CLASS_COMPARATOR);
managedCategories.addAll(finder.getManagedCategories());
String pathStem = "/" + servletPathParts[1] + "/" + (servletPathParts.length == 2 ? "latest" : servletPathParts[2]) + "/";
for (Class<? extends ConfiguredObject> category : managedCategories) {
Class<? extends ConfiguredObject> objClass = category;
String path = pathStem + category.getSimpleName().toLowerCase();
writer.println("<tr>");
writer.println("<td class=\"type\"><a href=" + ((servletPathParts.length == 2) ? "\"latest/" : "") + objClass.getSimpleName().toLowerCase() + "\">" + objClass.getSimpleName() + "</a></td>");
writer.println("<td class=\"path\">" + path + "</td>");
writer.println("<td class=\"description\">" + objClass.getAnnotation(ManagedObject.class).description() + "</td>");
writer.println("</tr>");
}
writer.println("</tbody>");
writer.println("</table>");
} else {
final List<Class<? extends ConfiguredObject>> types = _typeSpecialisations.get(configuredClass);
writeCategoryDescription(writer, configuredClass);
writeUsage(writer, request, hierarchy, configuredClass);
writeTypes(writer, model, types);
writeAttributes(writer, configuredClass, model, types);
writeStatistics(writer, configuredClass, model, types);
writeOperations(writer, configuredClass, model, types);
writeContext(writer, configuredClass, model, types);
}
writeFoot(writer);
}
use of org.apache.qpid.server.model.ConfiguredObjectFinder in project qpid-broker-j by apache.
the class RestServlet method performCreateOrUpdate.
private void performCreateOrUpdate(HttpServletRequest request, HttpServletResponse response, final ConfiguredObject<?> managedObject) throws IOException, ServletException {
ConfiguredObjectFinder finder = getConfiguredObjectFinder(managedObject);
final Class<? extends ConfiguredObject> configuredClass = getConfiguredClass(request, managedObject);
final Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(configuredClass);
RequestInfoParser requestInfoParser = new RequestInfoParser(hierarchy);
response.setContentType("application/json");
RequestInfo requestInfo = requestInfoParser.parse(request);
switch(requestInfo.getType()) {
case MODEL_OBJECT:
{
List<String> names = requestInfo.getModelParts();
boolean isFullObjectURL = names.size() == hierarchy.length;
Map<String, Object> providedObject = getRequestProvidedObject(request, requestInfo);
if (names.isEmpty() && hierarchy.length == 0) {
managedObject.setAttributes(providedObject);
response.setStatus(HttpServletResponse.SC_OK);
return;
}
ConfiguredObject theParent = managedObject;
Class<? extends ConfiguredObject> objClass = configuredClass;
if (hierarchy.length > 1) {
theParent = finder.findObjectParentsFromPath(names, hierarchy, configuredClass);
}
if (isFullObjectURL) {
String name = names.get(names.size() - 1);
ConfiguredObject<?> configuredObject = theParent.getChildByName(objClass, name);
if (configuredObject != null) {
configuredObject.setAttributes(providedObject);
response.setStatus(HttpServletResponse.SC_OK);
return;
} else if ("POST".equalsIgnoreCase(request.getMethod())) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, String.format("%s '%s' not found", configuredClass.getSimpleName(), name));
return;
} else {
providedObject.put(ConfiguredObject.NAME, name);
}
}
ConfiguredObject<?> configuredObject = theParent.createChild(objClass, providedObject);
StringBuffer requestURL = request.getRequestURL();
if (!isFullObjectURL) {
requestURL.append("/").append(configuredObject.getName());
}
response.setHeader("Location", requestURL.toString());
response.setStatus(HttpServletResponse.SC_CREATED);
break;
}
case OPERATION:
{
doOperation(requestInfo, managedObject, configuredClass, finder, request, response);
break;
}
case USER_PREFERENCES:
{
doPostOrPutUserPreference(requestInfo, managedObject, configuredClass, finder, request, response);
break;
}
default:
{
throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'", requestInfo.getType(), request.getPathInfo()));
}
}
}
use of org.apache.qpid.server.model.ConfiguredObjectFinder in project qpid-broker-j by apache.
the class RestServlet method doDelete.
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response, final ConfiguredObject<?> managedObject) throws ServletException, IOException {
ConfiguredObjectFinder finder = getConfiguredObjectFinder(managedObject);
Class<? extends ConfiguredObject> configuredClass = getConfiguredClass(request, managedObject);
final Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(configuredClass);
RequestInfoParser requestInfoParser = new RequestInfoParser(hierarchy);
RequestInfo requestInfo = requestInfoParser.parse(request);
Collection<ConfiguredObject<?>> allObjects = getTargetObjects(configuredClass, finder, requestInfo, buildFilterPredicates(request));
if (allObjects == null) {
throw new NotFoundException("Not Found");
}
switch(requestInfo.getType()) {
case MODEL_OBJECT:
{
for (ConfiguredObject o : allObjects) {
o.delete();
}
sendCachingHeadersOnResponse(response);
response.setStatus(HttpServletResponse.SC_OK);
break;
}
case USER_PREFERENCES:
{
// TODO: define format how to report the results for bulk delete, i.e. how to report individual errors and success statuses
if (allObjects.size() > 1) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, "Deletion of user preferences using wildcards is unsupported");
return;
}
for (ConfiguredObject o : allObjects) {
_userPreferenceHandler.handleDELETE(o.getUserPreferences(), requestInfo);
}
break;
}
default:
{
sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, "Unsupported delete call");
}
}
}
use of org.apache.qpid.server.model.ConfiguredObjectFinder in project qpid-broker-j by apache.
the class RestServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response, final ConfiguredObject<?> managedObject) throws ServletException, IOException {
ConfiguredObjectFinder finder = getConfiguredObjectFinder(managedObject);
Class<? extends ConfiguredObject> configuredClass = getConfiguredClass(request, managedObject);
if (configuredClass == null) {
sendError(response, HttpServletResponse.SC_NOT_FOUND);
return;
}
final Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(configuredClass);
if (hierarchy == null) {
sendError(response, HttpServletResponse.SC_NOT_FOUND);
return;
}
RequestInfoParser requestInfoParser = new RequestInfoParser(hierarchy);
RequestInfo requestInfo = requestInfoParser.parse(request);
switch(requestInfo.getType()) {
case OPERATION:
{
doOperation(requestInfo, managedObject, configuredClass, finder, request, response);
break;
}
case MODEL_OBJECT:
{
// TODO - sort special params, everything else should act as a filter
String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
if (attachmentFilename != null) {
setContentDispositionHeaderIfNecessary(response, attachmentFilename);
}
List<Predicate<ConfiguredObject<?>>> filterPredicateList = buildFilterPredicates(request);
Collection<ConfiguredObject<?>> allObjects = getTargetObjects(configuredClass, finder, requestInfo, filterPredicateList);
boolean singleObjectRequest = requestInfo.isSingletonRequest() && filterPredicateList.isEmpty();
if (allObjects == null || (allObjects.isEmpty() && singleObjectRequest)) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Not Found");
return;
}
int depth;
boolean actuals;
int oversizeThreshold;
boolean excludeInheritedContext;
depth = getIntParameterFromRequest(request, DEPTH_PARAM, DEFAULT_DEPTH);
oversizeThreshold = getIntParameterFromRequest(request, OVERSIZE_PARAM, DEFAULT_OVERSIZE);
actuals = getBooleanParameterFromRequest(request, ACTUALS_PARAM);
String excludeInheritedContextParameter = request.getParameter(EXCLUDE_INHERITED_CONTEXT_PARAM);
excludeInheritedContext = excludeInheritedContextParameter == null || Boolean.parseBoolean(excludeInheritedContextParameter);
boolean responseAsList = Boolean.parseBoolean(request.getParameter(SINGLETON_MODEL_OBJECT_RESPONSE_AS_LIST));
final Object responseObject;
if (!responseAsList && singleObjectRequest) {
if (allObjects.size() != 1) {
throw new IllegalStateException(String.format("Unexpected number of objects found [%d] for singleton request URI '%s'", allObjects.size(), request.getRequestURI()));
}
ConfiguredObject<?> singletonObject = allObjects.iterator().next();
responseObject = _objectConverter.convertObjectToMap(singletonObject, configuredClass, new ConfiguredObjectToMapConverter.ConverterOptions(depth, actuals, oversizeThreshold, request.isSecure(), excludeInheritedContext));
} else {
final List<Object> outputList = new ArrayList<>();
for (ConfiguredObject configuredObject : allObjects) {
outputList.add(_objectConverter.convertObjectToMap(configuredObject, configuredClass, new ConfiguredObjectToMapConverter.ConverterOptions(depth, actuals, oversizeThreshold, request.isSecure(), excludeInheritedContext)));
}
responseObject = outputList;
}
boolean sendCachingHeaders = attachmentFilename == null;
sendJsonResponse(responseObject, request, response, HttpServletResponse.SC_OK, sendCachingHeaders);
break;
}
case VISIBLE_PREFERENCES:
case USER_PREFERENCES:
{
doGetUserPreferences(managedObject, configuredClass, finder, requestInfo, request, response);
break;
}
default:
{
throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'", requestInfo.getType(), request.getPathInfo()));
}
}
}
Aggregations