001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Funsho David 018 */ 019 020package org.nuxeo.ecm.core.bulk; 021 022import java.io.Serializable; 023import java.util.List; 024import java.util.Map; 025import java.util.Set; 026 027import org.nuxeo.ecm.core.bulk.message.BulkCommand; 028import org.nuxeo.ecm.core.schema.SchemaManager; 029import org.nuxeo.runtime.api.Framework; 030 031/** 032 * @since 10.10 033 */ 034public abstract class AbstractBulkActionValidation implements BulkActionValidation { 035 036 @Override 037 public void validate(BulkCommand command) throws IllegalArgumentException { 038 039 // Check for unknown parameter 040 Set<String> params = command.getParams().keySet(); 041 List<String> actionParams = getParametersToValidate(); 042 for (String param : params) { 043 if (!actionParams.contains(param)) { 044 throw new IllegalArgumentException(unknownParameterMessage(param, command)); 045 } 046 } 047 validateCommand(command); 048 } 049 050 protected String invalidParameterMessage(String parameter, BulkCommand command) { 051 return "Invalid " + parameter + " in command: " + command; 052 } 053 054 protected String unknownParameterMessage(String parameter, BulkCommand command) { 055 return "Unknown parameter " + parameter + " in command: " + command; 056 } 057 058 protected void validateBoolean(String param, BulkCommand command) { 059 Serializable value = command.getParam(param); 060 if (value != null && !Boolean.TRUE.equals(value) && !Boolean.FALSE.equals(value)) { 061 throw new IllegalArgumentException(invalidParameterMessage(param, command)); 062 } 063 } 064 065 protected void validateString(String param, BulkCommand command) { 066 Serializable value = command.getParam(param); 067 validateStringValue(param, value, command); 068 } 069 070 protected void validateStringValue(String param, Serializable value, BulkCommand command) { 071 if (value != null && !(value instanceof String)) { 072 throw new IllegalArgumentException(invalidParameterMessage(param, command)); 073 } 074 } 075 076 protected void validateMap(String param, BulkCommand command) { 077 Serializable value = command.getParam(param); 078 if (value != null && !(value instanceof Map)) { 079 throw new IllegalArgumentException(invalidParameterMessage(param, command)); 080 } 081 } 082 083 protected void validateList(String param, BulkCommand command) { 084 Serializable value = command.getParam(param); 085 if (value != null && !(value instanceof List<?>)) { 086 throw new IllegalArgumentException(invalidParameterMessage(param, command)); 087 } 088 } 089 090 protected void validateSchema(String param, Serializable value, BulkCommand command) { 091 validateStringValue(param, value, command); 092 String schema = (String) value; 093 if (Framework.getService(SchemaManager.class).getSchema(schema) == null) { 094 throw new IllegalArgumentException("Unknown schema " + schema + " in command: " + command); 095 } 096 } 097 098 protected void validateXpath(String param, Serializable value, BulkCommand command) { 099 validateStringValue(param, value, command); 100 String xpath = (String) value; 101 if (Framework.getService(SchemaManager.class).getField(xpath) == null) { 102 throw new IllegalArgumentException("Unknown xpath " + xpath + " in command: " + command); 103 } 104 } 105 106 /** 107 * Returns the list of parameters to validate. 108 */ 109 protected abstract List<String> getParametersToValidate(); 110 111 /** 112 * Validates the command. 113 */ 114 protected abstract void validateCommand(BulkCommand command) throws IllegalArgumentException; 115 116}