From: remillet Date: Mon, 27 Mar 2017 21:52:02 +0000 (-0700) Subject: Merge branch 'botgarden_profile' into botgarden_profile-2 X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=fa8ddc441694c22f7244fffc3be77ab230e159ee;p=tmp%2Fjakarta-migration.git Merge branch 'botgarden_profile' into botgarden_profile-2 Conflicts: services/batch/service/src/main/java/org/collectionspace/services/batch/AbstractBatchInvocable.java services/batch/service/src/main/java/org/collectionspace/services/batch/BatchResource.java services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/CreateAndLinkLoanOutBatchJob.java --- fa8ddc441694c22f7244fffc3be77ab230e159ee diff --cc services/batch/service/src/main/java/org/collectionspace/services/batch/AbstractBatchInvocable.java index c487489a7,d612fc149..a61886197 --- a/services/batch/service/src/main/java/org/collectionspace/services/batch/AbstractBatchInvocable.java +++ b/services/batch/service/src/main/java/org/collectionspace/services/batch/AbstractBatchInvocable.java @@@ -37,15 -37,17 +38,15 @@@ public abstract class AbstractBatchInvo public final int INT_ERROR_STATUS = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); protected final String CSID_VALUES_NOT_PROVIDED_IN_INVOCATION_CONTEXT = "Could not find required CSID values in the invocation context for this batch job."; + + private ServiceContext ctx; private List invocationModes; - private ResourceMap resourceMap; + private ResourceMap resourceMap; - private InvocationContext invocationCtx; - private ServiceContext ctx; - private String tenantId; - - private int completionStatus; - private InvocationResults results; - private InvocationError errorInfo; - final Logger logger = LoggerFactory.getLogger(AbstractBatchInvocable.class); + protected InvocationContext invocationCtx; + protected int completionStatus; + protected InvocationResults results; + protected InvocationError errorInfo; public AbstractBatchInvocable() { init(); diff --cc services/batch/service/src/main/java/org/collectionspace/services/batch/BatchResource.java index aa38ece22,5a5dd2d49..96deb1784 --- a/services/batch/service/src/main/java/org/collectionspace/services/batch/BatchResource.java +++ b/services/batch/service/src/main/java/org/collectionspace/services/batch/BatchResource.java @@@ -73,9 -74,53 +75,49 @@@ public class BatchResource extends Nuxe @Override //public Class getCommonPartClass() { public Class getCommonPartClass() { - try { - return Class.forName("org.collectionspace.services.batch.BatchCommon");//.class; - } catch (ClassNotFoundException e){ - return null; - } + return BatchCommon.class; } - // FIXME: Resource classes should not be invoking handlers directly. This resource method should follow the conventions used by ++ /** + // other resource methods and use the getRepositoryClient() methods. + @Override + protected AbstractCommonList getCommonList(UriInfo ui) { + try { + ServiceContext ctx = createServiceContext(ui); + MultivaluedMap queryParams = ctx.getQueryParams(); + DocumentHandler handler = createDocumentHandler(ctx); + String docType = queryParams.getFirst(IQueryManager.SEARCH_TYPE_DOCTYPE); + String mode = queryParams.getFirst(IQueryManager.SEARCH_TYPE_INVOCATION_MODE); + String whereClause = null; + DocumentFilter documentFilter = null; + String common_part = ctx.getCommonPartLabel(); + + if (docType != null && !docType.isEmpty()) { + whereClause = QueryManager.createWhereClauseForInvocableByDocType( + common_part, docType); + documentFilter = handler.getDocumentFilter(); + documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND); + } + + if (mode != null && !mode.isEmpty()) { + whereClause = QueryManager.createWhereClauseForInvocableByMode( + common_part, mode); + documentFilter = handler.getDocumentFilter(); + documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND); + } + + if (whereClause !=null && logger.isDebugEnabled()) { + logger.debug("The WHERE clause is: " + documentFilter.getWhereClause()); + } + + getRepositoryClient(ctx).getFiltered(ctx, handler); + AbstractCommonList list = (AbstractCommonList) handler.getCommonPartList(); + return list; + } catch (Exception e) { + throw bigReThrow(e, ServiceMessages.LIST_FAILED); + } + } + /** * Gets the authorityItem list for the specified authority * If partialPerm is specified, keywords will be ignored. diff --cc services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java index 52f75556d,4529bcf69..86caddda4 --- a/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java +++ b/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java @@@ -66,164 -56,31 +66,163 @@@ public class BatchDocumentModelHandler private final Logger logger = LoggerFactory.getLogger(BatchDocumentModelHandler.class); protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode(); - - public InvocationResults invokeBatchJob(ServiceContext ctx, String csid, - ResourceMap resourceMap, InvocationContext invocationCtx) throws Exception { - - String invocationMode = invocationCtx.getMode(); - String modeProperty = null; - boolean checkDocType = true; + + /** + * Return true if the batch job supports the requested mode. + * @param invocationCtx + * @param batchCommon + * @return + * @throws BadRequestException + */ + protected boolean supportsInvokationMode(InvocationContext invocationCtx, BatchCommon batchCommon) throws BadRequestException { + boolean result = false; + + String invocationMode = invocationCtx.getMode().toLowerCase(); if (BatchInvocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) { - modeProperty = BatchJAXBSchema.SUPPORTS_SINGLE_DOC; + result = batchCommon.isSupportsSingleDoc(); //BatchJAXBSchema.SUPPORTS_SINGLE_DOC; } else if (BatchInvocable.INVOCATION_MODE_LIST.equalsIgnoreCase(invocationMode)) { - modeProperty = BatchJAXBSchema.SUPPORTS_DOC_LIST; + result = batchCommon.isSupportsDocList(); //BatchJAXBSchema.SUPPORTS_DOC_LIST; } else if (BatchInvocable.INVOCATION_MODE_GROUP.equalsIgnoreCase(invocationMode)) { - modeProperty = BatchJAXBSchema.SUPPORTS_GROUP; + result = batchCommon.isSupportsGroup(); //BatchJAXBSchema.SUPPORTS_GROUP; } else if (Invocable.INVOCATION_MODE_NO_CONTEXT.equalsIgnoreCase(invocationMode)) { - modeProperty = InvocableJAXBSchema.SUPPORTS_NO_CONTEXT; - checkDocType = false; + result = batchCommon.isSupportsNoContext(); //InvocableJAXBSchema.SUPPORTS_NO_CONTEXT; } else { - throw new BadRequestException("BatchResource: unknown Invocation Mode: " + invocationMode); + String msg = String.format("BatchResource: Unknown invocation mode '%s' requested trying to invoke batch job '%s'.", + invocationMode, batchCommon.getName()); + throw new BadRequestException(msg); + } + + return result; + } + + /** + * Returns true if we found any required permissions. + * + * @param batchCommon + * @return + */ + private boolean hasRequiredPermissions(BatchCommon batchCommon) { + boolean result = false; + + try { + result = batchCommon.getResourceActionGroupList().getResourceActionGroup().size() > 0; + } catch (NullPointerException e) { + // ignore exception, we're just testing to see if we have any list elements + } + + return result; + } + + /** + * Returns true if we found any required roles. + * + * @param batchCommon + * @return + */ + private boolean hasRequiredRoles(BatchCommon batchCommon) { + boolean result = false; + + try { + result = batchCommon.getForRoles().getRoleDisplayName().size() > 0; + } catch (NullPointerException e) { + // ignore exception, we're just testing to see if we have any list elements + } + + return result; + } + + /** + * The current user is authorized to run the batch job if: + * 1. No permissions or roles are specified in the batch job + * 2. No roles are specified, but permissions are specified and the current user has those permissions + * 3. Roles are specified and the current user is a member of at least one of the roles. + * + * @param batchCommon + * @return + */ + protected boolean isAuthoritzed(BatchCommon batchCommon) { + boolean result = true; + + if (hasRequiredRoles(batchCommon)) { + result = isAuthorizedWithRoles(batchCommon); + } else if (hasRequiredPermissions(batchCommon)) { + result = isAuthoritzedWithPermissions(batchCommon); + } + + return result; + } + + protected boolean isAuthorizedWithRoles(BatchCommon batchCommon) { + boolean result = false; + + ForRoles forRolesList = batchCommon.getForRoles(); + if (forRolesList != null) { + AccountResource accountResource = new AccountResource(); + List roleDisplayNameList = accountResource.getAccountRoles(AuthN.get().getUserId(), AuthN.get().getCurrentTenantId()); + for (String target : forRolesList.getRoleDisplayName()) { + if (roleDisplayNameList.contains(target)) { + result = true; + break; + } + } } + + return result; + } + + /** + * Check to see if the current user is authorized to run/invoke this batch job. If the batch job + * did not specify any permissions, we assume that the current user is authorized to run the job. + * @param batchCommon + * @return + */ + protected boolean isAuthoritzedWithPermissions(BatchCommon batchCommon) { + boolean result = true; + + ResourceActionGroupList resourceActionGroupList = batchCommon.getResourceActionGroupList(); + if (resourceActionGroupList != null) { + String tenantId = AuthN.get().getCurrentTenantId(); + for (ResourceActionGroup resourceActionGroup: resourceActionGroupList.getResourceActionGroup()) { + String resourceName = resourceActionGroup.getResourceName(); + ActionGroup actionGroup = ActionGroup.creatActionGroup(resourceActionGroup.getActionGroup()); + for (ActionType actionType: actionGroup.getActions()) { + CSpaceResource res = new URIResourceImpl(tenantId, resourceName, AuthZ.getMethod(actionType)); + if (AuthZ.get().isAccessAllowed(res) == false) { + return false; + } + } + } + } + + return result; + } + + /** + * Returns a copy of the incoming list of strings all lower-cased. Also removes any duplicates. + * + * @param listOfStrings + * @return + */ + private List toLowerCase(List listOfStrings) { + List result = null; + + if (listOfStrings != null) { + Set stringSet = new HashSet(); + for (String s : listOfStrings) { + stringSet.add(s.toLowerCase()); + } + result = new ArrayList(stringSet); + } + + return result; + } - // - // Acquire a repository session handle if we don't already have one + public InvocationResults invokeBatchJob(ServiceContext ctx, String csid, + ResourceMap resourceMap, InvocationContext invocationCtx, BatchCommon batchCommon) throws Exception { + CoreSessionInterface repoSession = null; boolean releaseRepoSession = false; - CoreSessionInterface repoSession = this.getRepositorySession(); + RepositoryClientImpl repoClient = (RepositoryClientImpl) this.getRepositoryClient(ctx); - repoSession = this.getRepositorySession(); if (repoSession == null) { repoSession = repoClient.getRepositorySession(ctx); releaseRepoSession = true; diff --cc services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/CreateAndLinkLoanOutBatchJob.java index 4ac26db5b,b6053b06a..0b94859e0 --- a/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/CreateAndLinkLoanOutBatchJob.java +++ b/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/CreateAndLinkLoanOutBatchJob.java @@@ -15,14 -16,56 +15,14 @@@ import org.collectionspace.services.cli public class CreateAndLinkLoanOutBatchJob extends AbstractBatchInvocable { - private static ArrayList invocationModes = null; - private InvocationContext invocationCtx; - private int completionStatus; - private InvocationResults results; - private InvocationError errorInfo; private final String RELATION_TYPE = "affects"; - private final String LOAN_DOCTYPE = "LoanOut"; + private final String LOAN_DOCTYPE = "LoanOut"; private final String RELATION_PREDICATE_DISP = "affects"; - protected final int CREATED_STATUS = Response.Status.CREATED.getStatusCode(); - protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode(); - protected final int INT_ERROR_STATUS = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); public CreateAndLinkLoanOutBatchJob() { - CreateAndLinkLoanOutBatchJob.setupClassStatics(); - invocationCtx = null; - completionStatus = STATUS_UNSTARTED; - results = new InvocationResults(); - errorInfo = null; - } - - private static void setupClassStatics() { - if(invocationModes == null ) { - invocationModes = new ArrayList(1); - invocationModes.add(INVOCATION_MODE_SINGLE); - invocationModes.add(INVOCATION_MODE_LIST); - } - } - - /** - * @return a set of modes that this plugin can support on invocation. Must be non-empty. - */ - public List getSupportedInvocationModes() { - return CreateAndLinkLoanOutBatchJob.invocationModes; + setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST)); } - - @Override - public InvocationContext getInvocationContext() { - return invocationCtx; - } - - /** - * Sets the invocation context for the batch job. Called before run(). - * @param context an instance of InvocationContext. - */ - @Override - public void setInvocationContext(InvocationContext context) { - this.invocationCtx = context; - } - /** * The main work logic of the batch job. Will be called after setContext. */