use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.
the class QueryServlet method performQuery.
private void performQuery(final HttpServletRequest request, final HttpServletResponse response, final ConfiguredObject<?> managedObject) throws IOException, ServletException {
String categoryName;
X parent = getParent(request, managedObject);
if (parent != null && ((categoryName = getRequestedCategory(request, managedObject)) != null)) {
Model model = parent.getModel();
Class<? extends ConfiguredObject> category = getSupportedCategory(categoryName, model);
if (category != null) {
List<ConfiguredObject<?>> objects = getAllObjects(parent, category, request);
try {
ConfiguredObjectQuery query = new ConfiguredObjectQuery(objects, request.getParameter("select"), request.getParameter("where"), request.getParameter("orderBy"), request.getParameter("limit"), request.getParameter("offset"));
String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
if (attachmentFilename != null) {
setContentDispositionHeaderIfNecessary(response, attachmentFilename);
}
if ("csv".equalsIgnoreCase(request.getParameter("format"))) {
sendCsvResponse(query, response);
} else {
Map<String, Object> resultsObject = new LinkedHashMap<>();
resultsObject.put("headers", query.getHeaders());
resultsObject.put("results", query.getResults());
resultsObject.put("total", query.getTotalNumberOfRows());
sendJsonResponse(resultsObject, request, response);
}
} catch (SelectorParsingException e) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} catch (EvaluationException e) {
sendJsonErrorResponse(request, response, SC_UNPROCESSABLE_ENTITY, e.getMessage());
}
} else {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Unknown object type " + categoryName);
}
} else {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Invalid path");
}
}
use of org.apache.qpid.server.model.ConfiguredObject 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.ConfiguredObject in project qpid-broker-j by apache.
the class RestServlet method doGetUserPreferences.
private void doGetUserPreferences(final ConfiguredObject<?> managedObject, final Class<? extends ConfiguredObject> configuredClass, final ConfiguredObjectFinder finder, final RequestInfo requestInfo, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
Collection<ConfiguredObject<?>> allObjects = getTargetObjects(configuredClass, finder, requestInfo, Collections.<Predicate<ConfiguredObject<?>>>emptyList());
if (allObjects == null || (allObjects.isEmpty() && isSingleObjectRequest(requestInfo, finder.getHierarchy(configuredClass)))) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Not Found");
return;
}
final Object responseObject;
if (requestInfo.hasWildcard()) {
responseObject = new ArrayList<>(allObjects.size());
for (ConfiguredObject<?> target : allObjects) {
final UserPreferences userPreferences = target.getUserPreferences();
try {
final Object preferences = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
if (preferences == null || (preferences instanceof Collection && ((Collection) preferences).isEmpty()) || (preferences instanceof Map && ((Map) preferences).isEmpty())) {
continue;
}
((List<Object>) responseObject).add(preferences);
} catch (NotFoundException e) {
// The case where the preference's type and name is provided, but this particular object does not
// have a matching preference.
}
}
} else {
ConfiguredObject<?> target = allObjects.iterator().next();
final UserPreferences userPreferences = target.getUserPreferences();
responseObject = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
}
sendJsonResponse(responseObject, request, response);
}
use of org.apache.qpid.server.model.ConfiguredObject 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.ConfiguredObject in project qpid-broker-j by apache.
the class RestServlet method doPostOrPutUserPreference.
private void doPostOrPutUserPreference(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);
final Object providedObject = getRequestProvidedObject(request, requestInfo, Object.class);
if ("POST".equals(request.getMethod())) {
_userPreferenceHandler.handlePOST(target, requestInfo, providedObject);
} else if ("PUT".equals(request.getMethod())) {
_userPreferenceHandler.handlePUT(target, requestInfo, providedObject);
} else {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "unexpected http method");
}
}
Aggregations