refactoring of the policy stack , SYNCOPE-158
Project: http://git-wip-us.apache.org/repos/asf/syncope/repo Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/49313303 Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/49313303 Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/49313303 Branch: refs/heads/master Commit: 493133037c867b335f1e5f046bba8fa78ad40f55 Parents: 79d0316 Author: massi <massimiliano.perr...@tirasa.net> Authored: Thu Oct 22 15:45:18 2015 +0200 Committer: massi <massimiliano.perr...@tirasa.net> Committed: Thu Oct 22 15:45:18 2015 +0200 ---------------------------------------------------------------------- .../commands/policy/AbstractPolicyCommand.java | 30 +++++ .../cli/commands/policy/PolicyDelete.java | 59 +++++++++ .../client/cli/commands/policy/PolicyList.java | 59 +++++++++ .../client/cli/commands/policy/PolicyRead.java | 58 +++++++++ .../commands/policy/PolicyResultManager.java | 120 +++++++++++++++++++ 5 files changed, 326 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/syncope/blob/49313303/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/AbstractPolicyCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/AbstractPolicyCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/AbstractPolicyCommand.java new file mode 100644 index 0000000..0bab4e5 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/AbstractPolicyCommand.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.client.cli.commands.policy; + +import org.apache.syncope.client.cli.SyncopeServices; +import org.apache.syncope.common.rest.api.service.PolicyService; + +public abstract class AbstractPolicyCommand { + + protected final PolicyService policyService = SyncopeServices.get(PolicyService.class); + + protected final PolicyResultManager policyResultManager = new PolicyResultManager(); + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/49313303/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyDelete.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyDelete.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyDelete.java new file mode 100644 index 0000000..15ffa8d --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyDelete.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.client.cli.commands.policy; + +import javax.xml.ws.WebServiceException; +import org.apache.syncope.client.cli.Input; +import org.apache.syncope.common.lib.SyncopeClientException; + +public class PolicyDelete extends AbstractPolicyCommand { + + private static final String DELETE_HELP_MESSAGE = "policy --delete {POLICY-ID} {POLICY-ID} [...]"; + + private final Input input; + + public PolicyDelete(final Input input) { + this.input = input; + } + + public void delete() { + if (input.parameterNumber() >= 1) { + for (final String parameter : input.getParameters()) { + try { + policyService.delete(Long.valueOf(parameter)); + policyResultManager.deletedMessage("Policy", parameter); + } catch (final WebServiceException | SyncopeClientException ex) { + System.out.println("Error:"); + if (ex.getMessage().startsWith("NotFound")) { + policyResultManager.notFoundError("Policy", parameter); + } else if (ex.getMessage().startsWith("DataIntegrityViolation")) { + policyResultManager.generic("You cannot delete policy " + parameter); + } else { + policyResultManager.generic(ex.getMessage()); + } + } catch (final NumberFormatException ex) { + policyResultManager.notBooleanDeletedError("policy", parameter); + } + } + } else { + policyResultManager.commandOptionError(DELETE_HELP_MESSAGE); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/49313303/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyList.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyList.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyList.java new file mode 100644 index 0000000..c05acf7 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyList.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.client.cli.commands.policy; + +import java.util.LinkedList; +import org.apache.syncope.client.cli.Input; +import org.apache.syncope.client.cli.util.CommandUtils; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.policy.AbstractPolicyTO; +import org.apache.syncope.common.lib.types.PolicyType; + +public class PolicyList extends AbstractPolicyCommand { + + private static final String LIST_HELP_MESSAGE = "policy --list-policy {POLICY-TYPE}\n" + + " Policy type: ACCOUNT / PASSWORD / SYNC / PUSH"; + + private final Input input; + + public PolicyList(final Input input) { + this.input = input; + } + + public void list() { + + if (input.parameterNumber() == 1) { + try { + final PolicyType policyType = PolicyType.valueOf(input.firstParameter()); + final LinkedList<AbstractPolicyTO> policyTOs = new LinkedList<>(); + for (final AbstractPolicyTO policyTO : policyService.list(policyType)) { + policyTOs.add(policyTO); + } + policyResultManager.fromList(policyType, policyTOs); + } catch (final SyncopeClientException ex) { + policyResultManager.generic(ex.getMessage()); + } catch (final IllegalArgumentException ex) { + policyResultManager.typeNotValidError( + "policy", input.firstParameter(), CommandUtils.fromEnumToArray(PolicyType.class)); + } + } else { + policyResultManager.commandOptionError(LIST_HELP_MESSAGE); + } + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/49313303/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyRead.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyRead.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyRead.java new file mode 100644 index 0000000..f20d41e --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyRead.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.client.cli.commands.policy; + +import java.util.LinkedList; +import javax.xml.ws.WebServiceException; +import org.apache.syncope.client.cli.Input; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.policy.AbstractPolicyTO; + +public class PolicyRead extends AbstractPolicyCommand { + + private static final String READ_HELP_MESSAGE = "policy --read {POLICY-ID} {POLICY-ID} [...]"; + + private final Input input; + + public PolicyRead(final Input input) { + this.input = input; + } + + public void read() { + if (input.parameterNumber() >= 1) { + final LinkedList<AbstractPolicyTO> policyTOs = new LinkedList<>(); + for (final String parameter : input.getParameters()) { + try { + policyTOs.add(policyService.read(Long.valueOf(parameter))); + policyResultManager.fromRead(policyTOs); + } catch (final NumberFormatException ex) { + policyResultManager.notBooleanDeletedError("policy", parameter); + } catch (final WebServiceException | SyncopeClientException ex) { + if (ex.getMessage().startsWith("NotFound")) { + policyResultManager.notFoundError("Policy", parameter); + } else { + policyResultManager.generic(ex.getMessage()); + } + } + } + } else { + policyResultManager.commandOptionError(READ_HELP_MESSAGE); + } + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/49313303/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java new file mode 100644 index 0000000..88e40a0 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.client.cli.commands.policy; + +import java.util.LinkedList; +import org.apache.syncope.client.cli.commands.CommonsResultManager; +import org.apache.syncope.common.lib.policy.AbstractPolicyTO; +import org.apache.syncope.common.lib.policy.AccountPolicyTO; +import org.apache.syncope.common.lib.policy.PasswordPolicyTO; +import org.apache.syncope.common.lib.policy.SyncPolicyTO; +import org.apache.syncope.common.lib.types.PolicyType; + +public class PolicyResultManager extends CommonsResultManager { + + public void fromRead(final LinkedList<AbstractPolicyTO> policyTOs) { + for (AbstractPolicyTO policyTO : policyTOs) { + if (!policyTOs.isEmpty()) { + final PolicyType policyType = policyTO.getType(); + switch (policyType) { + case ACCOUNT: + printAccountPolicy((AccountPolicyTO) policyTO); + break; + case PASSWORD: + printPasswordPolicy((PasswordPolicyTO) policyTO); + break; + case PUSH: + System.out.println(policyTO); + break; + case SYNC: + printSyncPolicy((SyncPolicyTO) policyTO); + break; + default: + break; + } + } + } + + } + + public void fromList(final PolicyType policyType, final LinkedList<AbstractPolicyTO> policyTOs) { + switch (policyType) { + case ACCOUNT: + for (final AbstractPolicyTO policyTO : policyTOs) { + printAccountPolicy((AccountPolicyTO) policyTO); + } + break; + case PASSWORD: + for (final AbstractPolicyTO policyTO : policyTOs) { + printPasswordPolicy((PasswordPolicyTO) policyTO); + } + break; + case PUSH: + for (final AbstractPolicyTO policyTO : policyTOs) { + System.out.println(policyTO); + } + break; + case SYNC: + for (final AbstractPolicyTO policyTO : policyTOs) { + printSyncPolicy((SyncPolicyTO) policyTO); + } + break; + default: + break; + } + } + + public void printAccountPolicy(final AccountPolicyTO policyTO) { + System.out.println(" > KEY: " + String.valueOf(policyTO.getKey())); + System.out.println(" type: " + policyTO.getType().name()); + System.out.println(" description: " + policyTO.getDescription()); + System.out.println(" resources : " + policyTO.getUsedByResources().toString()); + System.out.println(" realms : " + policyTO.getUsedByRealms().toString()); + System.out.println(" max authentication attempts : " + policyTO.getMaxAuthenticationAttempts()); + System.out.println(" propagation suspension : " + policyTO.isPropagateSuspension()); + System.out.println(" RULES : "); + System.out.println(" > class : " + policyTO.getRuleConfs()); + } + + public void printPasswordPolicy(final PasswordPolicyTO policyTO) { + System.out.println(" > KEY: " + String.valueOf(policyTO.getKey())); + System.out.println(" type: " + policyTO.getType().name()); + System.out.println(" description: " + policyTO.getDescription()); + System.out.println(" resources : " + policyTO.getUsedByResources().toString()); + System.out.println(" realms : " + policyTO.getUsedByRealms().toString()); + System.out.println(" history lenght : " + policyTO.getHistoryLength()); + System.out.println(" allow null password : " + policyTO.isAllowNullPassword()); + System.out.println(" RULES : "); + System.out.println(" > class : " + ((PasswordPolicyTO) policyTO).getRuleConfs()); + } + + public void printSyncPolicy(final SyncPolicyTO policyTO) { + System.out.println(" > KEY: " + String.valueOf(policyTO.getKey())); + System.out.println(" type: " + policyTO.getType().name()); + System.out.println(" description: " + policyTO.getDescription()); + System.out.println(" resources : " + policyTO.getUsedByResources().toString()); + System.out.println(" realms : " + policyTO.getUsedByRealms().toString()); + if (policyTO.getSpecification() != null) { + System.out.println(" conflict resolution action: " + + policyTO.getSpecification().getConflictResolutionAction().name()); + System.out.println(" correlation rule : " + + policyTO.getSpecification().getCorrelationRules().toString()); + } + } +}