Skip to content

Commit

Permalink
Added options for REST getObject/searchObjects.
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Sep 8, 2016
1 parent 9d54a2d commit 57a0a11
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
Expand Up @@ -18,6 +18,7 @@
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.GetOperationOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import org.apache.commons.collections.CollectionUtils;

import javax.xml.namespace.QName;

Expand Down Expand Up @@ -133,6 +134,10 @@ public static GetOperationOptions createRetrieve() {
return createRetrieve(RetrieveOption.INCLUDE);
}

public static GetOperationOptions createDontRetrieve() {
return createRetrieve(RetrieveOption.EXCLUDE);
}

public static GetOperationOptions createRetrieve(RelationalValueSearchQuery query) {
GetOperationOptions options = new GetOperationOptions();
options.retrieve = RetrieveOption.INCLUDE;
Expand Down Expand Up @@ -487,6 +492,24 @@ private void appendVal(StringBuilder sb, String name, Object val) {
}
}

public static Collection<SelectorOptions<GetOperationOptions>> fromRestOptions(List<String> options, List<String> include, List<String> exclude) {
if (CollectionUtils.isEmpty(options) && CollectionUtils.isEmpty(include) && CollectionUtils.isEmpty(exclude)) {
return null;
}
Collection<SelectorOptions<GetOperationOptions>> rv = new ArrayList<>();
GetOperationOptions rootOptions = fromRestOptions(options);
if (rootOptions != null) {
rv.add(SelectorOptions.create(rootOptions));
}
for (ItemPath includePath : ItemPath.fromStringList(include)) {
rv.add(SelectorOptions.create(includePath, GetOperationOptions.createRetrieve()));
}
for (ItemPath excludePath : ItemPath.fromStringList(exclude)) {
rv.add(SelectorOptions.create(excludePath, GetOperationOptions.createDontRetrieve()));
}
return rv;
}

public static GetOperationOptions fromRestOptions(List<String> options){
if (options == null || options.isEmpty()){
return null;
Expand Down
Expand Up @@ -136,7 +136,7 @@ public ModelRestService() {
@GET
@Path("/users/{id}/policy")
public Response getValuePolicyForUser(@PathParam("id") String oid, @Context MessageContext mc) {
LOGGER.info("getValuePolicyForUser start");
LOGGER.debug("getValuePolicyForUser start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_GET);
Expand All @@ -159,25 +159,31 @@ public Response getValuePolicyForUser(@PathParam("id") String oid, @Context Mess
parentResult.computeStatus();
finishRequest(task);

LOGGER.info("getValuePolicyForUser finish");
LOGGER.debug("getValuePolicyForUser finish");

return response;
}

@GET
@Path("/{type}/{id}")
public <T extends ObjectType> Response getObject(@PathParam("type") String type, @PathParam("id") String id,
@QueryParam("options") List<String> options,
@QueryParam("include") List<String> include,
@QueryParam("exclude") List<String> exclude,
@Context MessageContext mc){
LOGGER.info("model rest service for get operation start");
LOGGER.debug("model rest service for get operation start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_GET);

Class<T> clazz = ObjectTypes.getClassFromRestType(type);
Collection<SelectorOptions<GetOperationOptions>> getOptions = GetOperationOptions.fromRestOptions(options, include, exclude);
Response response;

try {
PrismObject<T> object = model.getObject(clazz, id, null, task, parentResult);
PrismObject<T> object = model.getObject(clazz, id, getOptions, task, parentResult);
removeExcludes(object, exclude); // temporary measure until fixed in repo

ResponseBuilder builder = Response.ok();
builder.entity(object);
response = builder.build();
Expand All @@ -198,7 +204,7 @@ public <T extends ObjectType> Response getObject(@PathParam("type") String type,
public <T extends ObjectType> Response addObject(@PathParam("type") String type, PrismObject<T> object,
@QueryParam("options") List<String> options,
@Context UriInfo uriInfo, @Context MessageContext mc) {
LOGGER.info("model rest service for add operation start");
LOGGER.debug("model rest service for add operation start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_ADD_OBJECT);
Expand All @@ -217,7 +223,7 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
Response response;
try {
oid = model.addObject(object, modelExecuteOptions, task, parentResult);
LOGGER.info("returned oid : {}", oid );
LOGGER.debug("returned oid : {}", oid );

ResponseBuilder builder;

Expand Down Expand Up @@ -247,7 +253,7 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
PrismObject<T> object, @QueryParam("options") List<String> options, @Context UriInfo uriInfo,
@Context Request request, @Context MessageContext mc){

LOGGER.info("model rest service for add operation start");
LOGGER.debug("model rest service for add operation start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_ADD_OBJECT);
Expand All @@ -271,7 +277,7 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
Response response;
try {
oid = model.addObject(object, modelExecuteOptions, task, parentResult);
LOGGER.info("returned oid : {}", oid );
LOGGER.debug("returned oid : {}", oid );

URI resourceURI = uriInfo.getAbsolutePathBuilder().path(oid).build(oid);
ResponseBuilder builder = clazz.isAssignableFrom(TaskType.class) ?
Expand All @@ -295,7 +301,7 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
public Response deleteObject(@PathParam("type") String type, @PathParam("id") String id,
@QueryParam("options") List<String> options, @Context MessageContext mc){

LOGGER.info("model rest service for delete operation start");
LOGGER.debug("model rest service for delete operation start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_DELETE_OBJECT);
Expand Down Expand Up @@ -341,7 +347,7 @@ public <T extends ObjectType> Response modifyObjectPost(@PathParam("type") Strin
public <T extends ObjectType> Response modifyObjectPatch(@PathParam("type") String type, @PathParam("oid") String oid,
ObjectModificationType modificationType, @QueryParam("options") List<String> options, @Context MessageContext mc) {

LOGGER.info("model rest service for modify operation start");
LOGGER.debug("model rest service for modify operation start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_MODIFY_OBJECT);
Expand All @@ -366,7 +372,7 @@ public <T extends ObjectType> Response modifyObjectPatch(@PathParam("type") Stri
@Path("/notifyChange")
public Response notifyChange(ResourceObjectShadowChangeDescriptionType changeDescription,
@Context UriInfo uriInfo, @Context MessageContext mc) {
LOGGER.info("model rest service for notify change operation start");
LOGGER.debug("model rest service for notify change operation start");
Validate.notNull(changeDescription, "Chnage description must not be null");

Task task = RestServiceUtil.initRequest(mc);
Expand Down Expand Up @@ -421,7 +427,11 @@ public Response findShadowOwner(@PathParam("oid") String shadowOid, @Context Mes
@POST
@Path("/{type}/search")
// @Produces({"text/html", "application/xml"})
public Response searchObjects(@PathParam("type") String type, QueryType queryType, @Context MessageContext mc){
public Response searchObjects(@PathParam("type") String type, QueryType queryType,
@QueryParam("options") List<String> options,
@QueryParam("include") List<String> include,
@QueryParam("exclude") List<String> exclude,
@Context MessageContext mc){

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_SEARCH_OBJECTS);
Expand All @@ -430,11 +440,12 @@ public Response searchObjects(@PathParam("type") String type, QueryType queryTyp
Response response;
try {
ObjectQuery query = QueryJaxbConvertor.createObjectQuery(clazz, queryType, prismContext);

List<PrismObject<? extends ShadowType>> objects = model.searchObjects(clazz, query, null, task, parentResult);
Collection<SelectorOptions<GetOperationOptions>> searchOptions = GetOperationOptions.fromRestOptions(options, include, exclude);
List<PrismObject<? extends ShadowType>> objects = model.searchObjects(clazz, query, searchOptions, task, parentResult);

ObjectListType listType = new ObjectListType();
for (PrismObject<? extends ObjectType> o : objects) {
removeExcludes(o, exclude); // temporary measure until fixed in repo
listType.getObject().add(o.asObjectable());
}

Expand All @@ -448,12 +459,21 @@ public Response searchObjects(@PathParam("type") String type, QueryType queryTyp
return response;
}

private void removeExcludes(PrismObject<? extends ObjectType> object, List<String> exclude) {
for (ItemPath path : ItemPath.fromStringList(exclude)) {
Item item = object.findItem(path); // reduce to "removeItem" after fixing that method implementation
if (item != null) {
object.removeItem(item.getPath(), Item.class);
}
}
}

@POST
@Path("/resources/{resourceOid}/import/{objectClass}")
// @Produces({"text/html", "application/xml"})
public Response importFromResource(@PathParam("resourceOid") String resourceOid, @PathParam("objectClass") String objectClass,
@Context MessageContext mc, @Context UriInfo uriInfo) {
LOGGER.info("model rest service for import from resource operation start");
LOGGER.debug("model rest service for import from resource operation start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_IMPORT_FROM_RESOURCE);
Expand All @@ -477,7 +497,7 @@ public Response importFromResource(@PathParam("resourceOid") String resourceOid,
@Path("/resources/{resourceOid}/test")
// @Produces({"text/html", "application/xml"})
public Response testResource(@PathParam("resourceOid") String resourceOid, @Context MessageContext mc) {
LOGGER.info("model rest service for test resource operation start");
LOGGER.debug("model rest service for test resource operation start");

Task task = RestServiceUtil.initRequest(mc);
OperationResult parentResult = task.getResult().createSubresult(OPERATION_TEST_RESOURCE);
Expand Down
Expand Up @@ -1749,7 +1749,10 @@ private <T extends ObjectType> void removeIgnoredItems(PrismObject<T> object, Li
return;
}
for (ItemPath path : ignoreItems) {
object.removeItem(path, Item.class);
Item item = object.findItem(path); // reduce to "removeItem" after fixing that method implementation
if (item != null) {
object.removeItem(item.getPath(), Item.class);
}
}
}

Expand Down

0 comments on commit 57a0a11

Please sign in to comment.