http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeAdm.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeAdm.java b/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeAdm.java new file mode 100644 index 0000000..2bb802f --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeAdm.java @@ -0,0 +1,123 @@ +/* + * 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; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.ParameterException; +import org.apache.syncope.client.cli.commands.ConfigurationCommand; +import org.apache.syncope.client.cli.commands.EntitlementCommand; +import org.apache.syncope.client.cli.commands.LoggerCommand; +import org.apache.syncope.client.cli.commands.NotificationCommand; +import org.apache.syncope.client.cli.commands.PolicyCommand; +import org.apache.syncope.client.cli.commands.ReportCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncopeAdm { + + private static final Logger LOG = LoggerFactory.getLogger(SyncopeAdm.class); + + private static final String helpMessage = "Usage: Main [options]\n" + + " Options:\n" + + " logger --help \n" + + " config --help \n" + + " notification --help \n" + + " report --help \n" + + " policy --help \n" + + " entitlement --help \n"; + + private static final JCommander jcommander = new JCommander(); + + private static LoggerCommand loggerCommand; + + private static ConfigurationCommand configurationCommand; + + private static NotificationCommand notificationCommand; + + private static ReportCommand reportCommand; + + private static PolicyCommand policyCommand; + + private static EntitlementCommand entitlementCommand; + + public static void main(final String[] args) { + LOG.debug("Starting with args \n"); + + for (final String arg : args) { + LOG.debug("Arg: {}", arg); + } + + instantiateCommands(); + + if (args.length == 0) { + System.out.println(helpMessage); + } else { + try { + jcommander.parse(args); + } catch (final ParameterException ioe) { + System.out.println(helpMessage); + LOG.error("Parameter exception", ioe); + } + executeCommand(); + } + + } + + private static void instantiateCommands() { + LOG.debug("Init JCommander"); + loggerCommand = new LoggerCommand(); + jcommander.addCommand(loggerCommand); + LOG.debug("Added LoggerCommand"); + configurationCommand = new ConfigurationCommand(); + jcommander.addCommand(configurationCommand); + LOG.debug("Added ConfigurationCommand"); + notificationCommand = new NotificationCommand(); + jcommander.addCommand(notificationCommand); + LOG.debug("Added NotificationCommand"); + reportCommand = new ReportCommand(); + jcommander.addCommand(reportCommand); + LOG.debug("Added ReportCommand"); + policyCommand = new PolicyCommand(); + jcommander.addCommand(policyCommand); + LOG.debug("Added PolicyCommand"); + entitlementCommand = new EntitlementCommand(); + jcommander.addCommand(entitlementCommand); + LOG.debug("Added EntitlementCommand"); + } + + private static void executeCommand() { + final String command = jcommander.getParsedCommand(); + + LOG.debug("Called command {}", command); + + if ("logger".equalsIgnoreCase(command)) { + loggerCommand.execute(); + } else if ("config".equalsIgnoreCase(command)) { + configurationCommand.execute(); + } else if ("notification".equalsIgnoreCase(command)) { + notificationCommand.execute(); + } else if ("report".equalsIgnoreCase(command)) { + reportCommand.execute(); + } else if ("policy".equalsIgnoreCase(command)) { + policyCommand.execute(); + } else if ("entitlement".equalsIgnoreCase(command)) { + entitlementCommand.execute(); + } + } +}
http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeServices.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeServices.java b/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeServices.java new file mode 100644 index 0000000..26ce276 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/SyncopeServices.java @@ -0,0 +1,45 @@ +/* + * 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; + +import java.util.ResourceBundle; +import org.apache.syncope.client.lib.SyncopeClient; +import org.apache.syncope.client.lib.SyncopeClientFactoryBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class SyncopeServices { + + private static final Logger LOG = LoggerFactory.getLogger(SyncopeServices.class); + + private final static ResourceBundle SYNCOPE_PROPS = ResourceBundle.getBundle("syncope"); + + private static final SyncopeClient CLIENT = new SyncopeClientFactoryBean() + .setAddress(SYNCOPE_PROPS.getString("syncope.rest.services")) + .create(SYNCOPE_PROPS.getString("syncope.user"), SYNCOPE_PROPS.getString("syncope.password")); + + public static <T> T get(final Class<T> claz) { + LOG.debug("Creting service for {}", claz.getName()); + return CLIENT.getService(claz); + } + + private SyncopeServices() { + // private constructor for static utility class + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/commands/AbstractCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/AbstractCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/AbstractCommand.java new file mode 100644 index 0000000..a6753bf --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/AbstractCommand.java @@ -0,0 +1,32 @@ +/* + * 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; + +import com.beust.jcommander.Parameter; + +public abstract class AbstractCommand { + + @Parameter(names = {"-h", "--help"}) + protected boolean help = false; + + @Parameter(names = {"-l", "--list"}) + protected boolean list = false; + + protected abstract void execute(); +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ConfigurationCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ConfigurationCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ConfigurationCommand.java new file mode 100644 index 0000000..4ca94d0 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ConfigurationCommand.java @@ -0,0 +1,209 @@ +/* + * 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; + +import com.beust.jcommander.DynamicParameter; +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import java.io.IOException; +import java.io.SequenceInputStream; +import java.util.HashMap; +import java.util.Map; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import org.apache.commons.lang3.StringUtils; +import org.apache.syncope.client.cli.SyncopeServices; +import org.apache.syncope.client.cli.util.XmlUtils; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.to.AttrTO; +import org.apache.syncope.common.lib.to.ConfTO; +import org.apache.syncope.common.rest.api.service.ConfigurationService; +import org.apache.syncope.common.rest.api.service.SyncopeService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +@Parameters( + commandNames = "config", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope configuration service") +public class ConfigurationCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(ConfigurationCommand.class); + + private static final String EXPORT_FILE_NAME = "/content.xml"; + + private final String helpMessage = "Usage: config [options]\n" + + " Options:\n" + + " -h, --help \n" + + " -l, --list \n" + + " -r, --read \n" + + " Syntax: -r={CONF-NAME} \n" + + " -u, --update \n" + + " Syntax: {CONF-NAME}={CONF-VALUE} \n" + + " -c, --create \n" + + " Syntax: {CONF-NAME}={CONF-VALUE} \n" + + " -d, --delete \n" + + " Syntax: -d={CONF-NAME}" + + " -v, --validators \n" + + " -mt, --mail-templates \n" + + " -e, --export \n" + + " Syntax: -e={WHERE-DIR} \n"; + + @Parameter(names = { "-r", "--read" }) + public String confNameToRead; + + @DynamicParameter(names = { "-u", "--update" }) + private final Map<String, String> updateConf = new HashMap<String, String>(); + + @DynamicParameter(names = { "-c", "--create" }) + private final Map<String, String> createConf = new HashMap<String, String>(); + + @Parameter(names = { "-d", "--delete" }) + public String confNameToDelete; + + @Parameter(names = { "-v", "--validators" }) + public boolean validators = false; + + @Parameter(names = { "-mt", "--mail-templates" }) + public boolean mailTemplates = false; + + @Parameter(names = { "-e", "--export" }) + public String export; + + @Override + public void execute() { + final SyncopeService syncopeService = SyncopeServices.get(SyncopeService.class); + final ConfigurationService configurationService = SyncopeServices.get(ConfigurationService.class); + + LOG.debug("Logger service successfully created"); + + if (help) { + LOG.debug("- configuration help command"); + System.out.println(helpMessage); + } else if (list) { + LOG.debug("- configuration list command"); + try { + final ConfTO confTO = configurationService.list(); + for (final AttrTO attrTO : confTO.getPlainAttrMap().values()) { + System.out.println(" - Conf " + attrTO.getSchema() + " has value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(confNameToRead)) { + LOG.debug("- configuration read {} command", confNameToRead); + try { + final AttrTO attrTO = configurationService.read(confNameToRead); + System.out.println(" - Conf " + attrTO.getSchema() + " has value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (!updateConf.isEmpty()) { + LOG.debug("- configuration update command with params {}", updateConf); + try { + for (final Map.Entry<String, String> entrySet : updateConf.entrySet()) { + final AttrTO attrTO = configurationService.read(entrySet.getKey()); + attrTO.getValues().clear(); + attrTO.getValues().add(entrySet.getValue()); + configurationService.set(entrySet.getKey(), attrTO); + System.out.println(" - Conf " + attrTO.getSchema() + " has value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (!createConf.isEmpty()) { + LOG.debug("- configuration create command with params {}", createConf); + try { + for (final Map.Entry<String, String> entrySet : createConf.entrySet()) { + final AttrTO attrTO = new AttrTO(); + attrTO.setSchema(entrySet.getKey()); + attrTO.getValues().add(entrySet.getValue()); + configurationService.set(entrySet.getKey(), attrTO); + System.out.println(" - Conf " + attrTO.getSchema() + " created with value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(confNameToDelete)) { + try { + LOG.debug("- configuration delete {} command", confNameToDelete); + configurationService.delete(confNameToDelete); + System.out.println(" - Conf " + confNameToDelete + " deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (validators) { + LOG.debug("- configuration validators command"); + try { + System.out.println("Conf validator class: "); + for (final String validator : syncopeService.info().getValidators()) { + System.out.println(" *** " + validator); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (mailTemplates) { + LOG.debug("- configuration mailTemplates command"); + try { + System.out.println("Conf mail template for:"); + for (final String mailTemplate : syncopeService.info().getMailTemplates()) { + System.out.println(" *** " + mailTemplate); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(export)) { + LOG.debug("- configuration export command, directory where xml will be export: {}", export); + + try { + XmlUtils.createXMLFile((SequenceInputStream) configurationService.export().getEntity(), export + + EXPORT_FILE_NAME); + System.out.println(" - " + export + EXPORT_FILE_NAME + " successfully created"); + } catch (final IOException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final ParserConfigurationException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final SAXException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final TransformerConfigurationException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final TransformerException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final SyncopeClientException ex) { + LOG.error("Error calling configuration service", ex); + System.out.println(" - Error calling configuration service " + ex.getMessage()); + } + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/commands/EntitlementCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/EntitlementCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/EntitlementCommand.java new file mode 100644 index 0000000..e0058ab --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/EntitlementCommand.java @@ -0,0 +1,70 @@ +/* + * 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; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import org.apache.syncope.client.cli.SyncopeServices; +import org.apache.syncope.common.lib.wrap.EntitlementTO; +import org.apache.syncope.common.rest.api.service.EntitlementService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Parameters( + commandNames = "entitlement", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope entitlement service") +public class EntitlementCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(EntitlementCommand.class); + + private final String helpMessage = "Usage: entitlement [options]\n" + + " Options:\n" + + " -h, --help \n" + + " -l, --list \n" + + " -lo, --list-own \n"; + + @Parameter(names = { "-lo", "--list-own" }) + public boolean listOwn = false; + + @Override + public void execute() { + final EntitlementService entitlementService = SyncopeServices.get(EntitlementService.class); + LOG.debug("Entitlement service successfully created"); + + if (help) { + LOG.debug("- entitlement help command"); + System.out.println(helpMessage); + } else if (list) { + System.out.println("All entitlement:"); + for (final EntitlementTO entitlementTO : entitlementService.getAllEntitlements()) { + System.out.println(" *** " + entitlementTO.getElement()); + } + } else if (listOwn) { + System.out.println("All own entitlement:"); + for (final EntitlementTO entitlementTO : entitlementService.getOwnEntitlements()) { + System.out.println(" *** " + entitlementTO.getElement()); + } + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/commands/LoggerCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/LoggerCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/LoggerCommand.java new file mode 100644 index 0000000..e5115d9 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/LoggerCommand.java @@ -0,0 +1,168 @@ +/* + * 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; + +import com.beust.jcommander.DynamicParameter; +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.apache.syncope.client.cli.SyncopeServices; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.to.LoggerTO; +import org.apache.syncope.common.lib.types.LoggerLevel; +import org.apache.syncope.common.lib.types.LoggerType; +import org.apache.syncope.common.rest.api.service.LoggerService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Parameters( + commandNames = "logger", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope logger service") +public class LoggerCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(LoggerCommand.class); + + private final String helpMessage = "Usage: logger [options]\n" + + " Options:\n" + + " -h, --help \n" + + " -l, --list \n" + + " -r, --read \n" + + " Syntax: -r={LOG-NAME} \n" + + " -u, --update \n" + + " Syntax: {LOG-NAME}={LOG-LEVEL} \n" + + " -ua, --update-all \n" + + " Syntax: -ua={LOG-LEVEL} \n" + + " -c, --create \n" + + " Syntax: {LOG-NAME}={LOG-LEVEL} \n" + + " -d, --delete \n" + + " Syntax: -d={LOG-NAME}"; + + @Parameter(names = { "-r", "--read" }) + public String logNameToRead; + + @DynamicParameter(names = { "-u", "--update" }) + private final Map<String, String> updateLogs = new HashMap<String, String>(); + + @Parameter(names = { "-ua", "--update-all" }) + public String logLevel; + + @DynamicParameter(names = { "-c", "--create" }) + private final Map<String, String> createLogs = new HashMap<String, String>(); + + @Parameter(names = { "-d", "--delete" }) + public String logNameToDelete; + + @Override + public void execute() { + final LoggerService loggerService = SyncopeServices.get(LoggerService.class); + + LOG.debug("Logger service successfully created"); + + if (help) { + LOG.debug("- logger help command"); + System.out.println(helpMessage); + } else if (list) { + LOG.debug("- logger list command"); + try { + for (final LoggerTO loggerTO : loggerService.list(LoggerType.LOG)) { + System.out.println(" - " + loggerTO.getKey() + " -> " + loggerTO.getLevel()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(logNameToRead)) { + LOG.debug("- logger read {} command", logNameToRead); + try { + final LoggerTO loggerTO = loggerService.read(LoggerType.LOG, logNameToRead); + System.out.println(" - Logger " + loggerTO.getKey() + " with level -> " + loggerTO.getLevel()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (!updateLogs.isEmpty()) { + LOG.debug("- logger update command with params {}", updateLogs); + + for (final Map.Entry<String, String> log : updateLogs.entrySet()) { + final LoggerTO loggerTO = loggerService.read(LoggerType.LOG, log.getKey()); + try { + loggerTO.setLevel(LoggerLevel.valueOf(log.getValue())); + loggerService.update(LoggerType.LOG, loggerTO.getKey(), loggerTO); + System.out.println(" - Logger " + loggerTO.getKey() + " new level -> " + loggerTO.getLevel()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } catch (final IllegalArgumentException ex) { + System.out.println(" - Error: " + log.getValue() + " isn't a valid logger level, try with:"); + for (final LoggerLevel level : LoggerLevel.values()) { + System.out.println(" *** " + level.name()); + } + } + } + } else if (StringUtils.isNotBlank(logLevel)) { + LOG.debug("- logger update all command with level {}", logLevel); + for (final LoggerTO loggerTO : loggerService.list(LoggerType.LOG)) { + try { + loggerTO.setLevel(LoggerLevel.valueOf(logLevel)); + loggerService.update(LoggerType.LOG, loggerTO.getKey(), loggerTO); + System.out.println(" - Logger " + loggerTO.getKey() + " new level -> " + loggerTO.getLevel()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } catch (final IllegalArgumentException ex) { + System.out.println(" - Error: " + loggerTO.getLevel() + " isn't a valid logger level, try with:"); + for (final LoggerLevel level : LoggerLevel.values()) { + System.out.println(" *** " + level.name()); + } + } + } + } else if (!createLogs.isEmpty()) { + LOG.debug("- logger create command with params {}", createLogs); + + for (final Map.Entry<String, String> entrySet : createLogs.entrySet()) { + final LoggerTO loggerTO = new LoggerTO(); + try { + loggerTO.setKey(entrySet.getKey()); + loggerTO.setLevel(LoggerLevel.valueOf(entrySet.getValue())); + loggerService.update(LoggerType.LOG, loggerTO.getKey(), loggerTO); + System.out.println(" - Logger " + loggerTO.getKey() + " created with level -> " + loggerTO. + getLevel()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } catch (final IllegalArgumentException ex) { + System.out.println(" - Error: " + loggerTO.getLevel() + " isn't a valid logger level, try with:"); + for (final LoggerLevel level : LoggerLevel.values()) { + System.out.println(" *** " + level.name()); + } + } + } + } else if (StringUtils.isNotBlank(logNameToDelete)) { + try { + LOG.debug("- logger delete {} command", logNameToDelete); + loggerService.delete(LoggerType.LOG, logNameToDelete); + System.out.println(" - Logger " + logNameToDelete + " deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/commands/NotificationCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/NotificationCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/NotificationCommand.java new file mode 100644 index 0000000..4e18996 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/NotificationCommand.java @@ -0,0 +1,92 @@ +/* + * 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; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import org.apache.syncope.client.cli.SyncopeServices; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.to.NotificationTO; +import org.apache.syncope.common.rest.api.service.NotificationService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Parameters( + commandNames = "notification", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope notification service") +public class NotificationCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(NotificationCommand.class); + + private final String helpMessage = "Usage: notification [options]\n" + + " Options:\n" + + " -h, --help \n" + + " -l, --list \n" + + " -r, --read \n" + + " Syntax: -r={NOTIFICATION-ID} \n" + + " -d, --delete \n" + + " Syntax: -d={NOTIFICATION-ID}"; + + @Parameter(names = { "-r", "--read" }) + public Long notificationIdToRead = -1L; + + @Parameter(names = { "-d", "--delete" }) + public Long notificationIdToDelete = -1L; + + @Override + public void execute() { + final NotificationService notificationService = SyncopeServices.get(NotificationService.class); + + LOG.debug("Notification service successfully created"); + + if (help) { + LOG.debug("- notification help command"); + System.out.println(helpMessage); + } else if (list) { + LOG.debug("- notification list command"); + try { + for (final NotificationTO notificationTO : notificationService.list()) { + System.out.println(notificationTO); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (notificationIdToRead > -1L) { + LOG.debug("- notification read {} command", notificationIdToRead); + try { + System.out.println(notificationService.read(notificationIdToRead)); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (notificationIdToDelete > -1L) { + try { + LOG.debug("- notification delete {} command", notificationIdToDelete); + notificationService.delete(notificationIdToDelete); + System.out.println(" - Notification " + notificationIdToDelete + " deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java new file mode 100644 index 0000000..c2bcb5f --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java @@ -0,0 +1,105 @@ +/* + * 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; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import org.apache.commons.lang3.StringUtils; +import org.apache.syncope.client.cli.SyncopeServices; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.to.AbstractPolicyTO; +import org.apache.syncope.common.lib.types.PolicyType; +import org.apache.syncope.common.rest.api.service.PolicyService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Parameters( + commandNames = "policy", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope policy service") +public class PolicyCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(PolicyCommand.class); + + private final String helpMessage = "Usage: policy [options]\n" + + " Options:\n" + + " -h, --help \n" + + " -l, --list \n" + + " -ll, --list-policy \n" + + " Syntax: -ll={POLICY-TYPE} \n" + + " -r, --read \n" + + " Syntax: -r={POLICY-ID} \n" + + " -d, --delete \n" + + " Syntax: -d={POLICY-ID}"; + + @Parameter(names = { "-ll", "--list-policy" }) + public String policyType; + + @Parameter(names = { "-r", "--read" }) + public Long policyIdToRead = -1L; + + @Parameter(names = { "-d", "--delete" }) + public Long policyIdToDelete = -1L; + + @Override + public void execute() { + final PolicyService policyService = SyncopeServices.get(PolicyService.class); + LOG.debug("Policy service successfully created"); + + if (help) { + LOG.debug("- policy help command"); + System.out.println(helpMessage); + } else if (list) { + + } else if (StringUtils.isNotBlank(policyType)) { + LOG.debug("- policy list command for type {}", policyType); + try { + for (final AbstractPolicyTO policyTO : policyService.list(PolicyType.valueOf(policyType))) { + System.out.println(policyTO); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } catch (final IllegalArgumentException ex) { + System.out.println(" - Error: " + policyType + " isn't a valid policy type, try with:"); + for (final PolicyType type : PolicyType.values()) { + System.out.println(" *** " + type.name() + ": " + type.getDescription()); + } + } + } else if (policyIdToRead > -1L) { + LOG.debug("- policy read {} command", policyIdToRead); + try { + System.out.println(policyService.read(policyIdToRead)); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (policyIdToDelete > -1L) { + try { + LOG.debug("- policy delete {} command", policyIdToDelete); + policyService.delete(policyIdToDelete); + System.out.println(" - Report " + policyIdToDelete + " deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ReportCommand.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ReportCommand.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ReportCommand.java new file mode 100644 index 0000000..17f0e14 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/ReportCommand.java @@ -0,0 +1,193 @@ +/* + * 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; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import java.io.IOException; +import java.io.SequenceInputStream; +import java.util.List; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import org.apache.syncope.client.cli.SyncopeServices; +import org.apache.syncope.client.cli.util.XmlUtils; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.to.ReportExecTO; +import org.apache.syncope.common.lib.to.ReportTO; +import org.apache.syncope.common.lib.types.ReportExecExportFormat; +import org.apache.syncope.common.lib.wrap.ReportletConfClass; +import org.apache.syncope.common.rest.api.service.ReportService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +@Parameters( + commandNames = "report", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope report service") +public class ReportCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(ReportCommand.class); + + private final String helpMessage = "Usage: report [options]\n" + + " Options:\n" + + " -h, --help \n" + + " -l, --list \n" + + " -r, --read \n" + + " Syntax: -r={POLICY-ID} \n" + + " -d, --delete \n" + + " Syntax: -d={POLICY-ID} \n" + + " -e, --execute \n" + + " Syntax: -e={POLICY-ID} \n" + + " -re, --read-executecution \n" + + " Syntax: -re={EXECUTION-ID} \n" + + " -de, --delete-executecution \n" + + " Syntax: -de={EXECUTION-ID} \n" + + " -eer, --export-executecution-result \n" + + " Syntax: -eer={EXECUTION-ID} \n" + + " -rc, --reportlet-class"; + + @Parameter(names = { "-r", "--read" }) + public Long reportIdToRead = -1L; + + @Parameter(names = { "-d", "--delete" }) + public Long reportIdToDelete = -1L; + + @Parameter(names = { "-e", "--execute" }) + public Long reportIdToExecute = -1L; + + @Parameter(names = { "-re", "--read-execution" }) + public Long executionIdToRead = -1L; + + @Parameter(names = { "-de", "--delete-execution" }) + public Long executionIdToDelete = -1L; + + @Parameter(names = { "-eer", "--export-execution-result" }) + public Long exportId = -1L; + + @Parameter(names = { "-rc", "--reportlet-class" }) + public boolean reportletClass = false; + + @Override + public void execute() { + final ReportService reportService = SyncopeServices.get(ReportService.class); + LOG.debug("Report service successfully created"); + + if (help) { + LOG.debug("- report help command"); + System.out.println(helpMessage); + } else if (list) { + LOG.debug("- report list command"); + try { + for (final ReportTO reportTO : reportService.list().getResult()) { + System.out.println(reportTO); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (reportIdToRead > -1L) { + LOG.debug("- report read {} command", reportIdToRead); + try { + System.out.println(reportService.read(reportIdToRead)); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (reportIdToDelete > -1L) { + try { + LOG.debug("- report delete {} command", reportIdToDelete); + reportService.delete(reportIdToDelete); + System.out.println(" - Report " + reportIdToDelete + " deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (reportIdToExecute > -1L) { + try { + LOG.debug("- report execute {} command", reportIdToExecute); + reportService.execute(reportIdToExecute); + final List<ReportExecTO> executionList = reportService.read(reportIdToExecute).getExecutions(); + final ReportExecTO lastExecution = executionList.get(executionList.size() - 1); + System.out.println(" - Report execution id: " + lastExecution.getKey()); + System.out.println(" - Report execution status: " + lastExecution.getStatus()); + System.out.println(" - Report execution start date: " + lastExecution.getStartDate()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (executionIdToRead > -1L) { + try { + LOG.debug("- report execution read {} command", executionIdToRead); + ReportExecTO reportExecTO = reportService.readExecution(executionIdToRead); + System.out.println(" - Report execution id: " + reportExecTO.getKey()); + System.out.println(" - Report execution status: " + reportExecTO.getStatus()); + System.out.println(" - Report execution start date: " + reportExecTO.getStartDate()); + System.out.println(" - Report execution end date: " + reportExecTO.getEndDate()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (executionIdToDelete > -1L) { + try { + LOG.debug("- report execution delete {} command", executionIdToDelete); + reportService.deleteExecution(executionIdToDelete); + System.out.println(" - Report execution " + executionIdToDelete + "successfyllt deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (exportId > -1L) { + LOG.debug("- report export command for report: {}", exportId); + + try { + XmlUtils.createXMLFile((SequenceInputStream) reportService.exportExecutionResult(exportId, + ReportExecExportFormat.XML).getEntity(), "export_" + exportId + ".xml"); + System.out.println(" - " + "export_" + exportId + " successfully created"); + } catch (final IOException ex) { + LOG.error("Error creating xml file", ex); + System.out.println(" - Error creating " + "export_" + exportId + " " + ex.getMessage()); + } catch (final ParserConfigurationException ex) { + LOG.error("Error creating xml file", ex); + System.out.println(" - Error creating " + "export_" + exportId + " " + ex.getMessage()); + } catch (final SAXException ex) { + LOG.error("Error creating xml file", ex); + System.out.println(" - Error creating " + "export_" + exportId + " " + ex.getMessage()); + } catch (final TransformerConfigurationException ex) { + LOG.error("Error creating xml file", ex); + System.out.println(" - Error creating " + "export_" + exportId + " " + ex.getMessage()); + } catch (final TransformerException ex) { + LOG.error("Error creating xml file", ex); + System.out.println(" - Error creating export_" + exportId + " " + ex.getMessage()); + } catch (final SyncopeClientException ex) { + LOG.error("Error calling configuration service", ex); + System.out.println(" - Error calling configuration service " + ex.getMessage()); + } + } else if (reportletClass) { + try { + LOG.debug("- reportlet configuration class list command"); + System.out.println("Reportlet conf classes"); + for (final ReportletConfClass reportletConfClass : reportService.getReportletConfClasses()) { + System.out.println(" *** " + reportletConfClass.getElement()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/util/XmlUtils.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/util/XmlUtils.java b/client/cli/src/main/java/org/apache/syncope/client/cli/util/XmlUtils.java new file mode 100644 index 0000000..fa228d5 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/util/XmlUtils.java @@ -0,0 +1,47 @@ +/* + * 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.util; + +import java.io.File; +import java.io.IOException; +import java.io.SequenceInputStream; +import java.io.StringReader; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.apache.cxf.helpers.IOUtils; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class XmlUtils { + + public static void createXMLFile(final SequenceInputStream sis, final String filePath) + throws TransformerConfigurationException, TransformerException, SAXException, IOException, + ParserConfigurationException { + + TransformerFactory.newInstance().newTransformer() + .transform(new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( + new InputSource(new StringReader(IOUtils.toString(sis))))), + new StreamResult(new File(filePath))); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/java/org/apache/syncope/client/cli/validators/DebugLevelValidator.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/validators/DebugLevelValidator.java b/client/cli/src/main/java/org/apache/syncope/client/cli/validators/DebugLevelValidator.java new file mode 100644 index 0000000..58c12f3 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/validators/DebugLevelValidator.java @@ -0,0 +1,61 @@ +/* + * 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.validators; + +import com.beust.jcommander.IParameterValidator; +import com.beust.jcommander.ParameterException; + +public class DebugLevelValidator implements IParameterValidator { + + @Override + public void validate(final String name, final String value) throws ParameterException { + if (!Levels.contains(value)) { + final StringBuilder exceptionMessage = new StringBuilder(); + exceptionMessage.append("Parameter ") + .append(name) + .append(" should be :\n"); + for (final Levels l : Levels.values()) { + exceptionMessage.append(l).append("\n"); + } + System.out.println(">>>> " + exceptionMessage.toString()); + } + } + + private enum Levels { + + OFF, + FATAL, + ERROR, + WARN, + INFO, + DEBUG, + TRACE, + ALL; + + public static boolean contains(final String name) { + for (final Levels c : Levels.values()) { + if (c.name().equals(name)) { + return true; + } + } + return false; + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/resources/log4j2.xml ---------------------------------------------------------------------- diff --git a/client/cli/src/main/resources/log4j2.xml b/client/cli/src/main/resources/log4j2.xml new file mode 100644 index 0000000..0688f6b --- /dev/null +++ b/client/cli/src/main/resources/log4j2.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<configuration status="WARN"> + + <appenders> + + <RollingRandomAccessFile name="main" fileName="${log.directory}/cli.log" + filePattern="${log.directory}/cli-%d{yyyy-MM-dd}.log.gz" + immediateFlush="false" append="true"> + <PatternLayout> + <pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern> + </PatternLayout> + <Policies> + <TimeBasedTriggeringPolicy/> + <SizeBasedTriggeringPolicy size="250 MB"/> + </Policies> + </RollingRandomAccessFile> + + </appenders> + + <loggers> + + <asyncLogger name="com.beust" additivity="false" level="DEBUG"> + <appender-ref ref="main"/> + </asyncLogger> + + <asyncLogger name="org.apache.syncope.cli" additivity="false" level="DEBUG"> + <appender-ref ref="main"/> + </asyncLogger> + + <asyncLogger name="org.apache.syncope.client" additivity="false" level="OFF"> + <appender-ref ref="main"/> + </asyncLogger> + + <root level="DEBUG"> + <appender-ref ref="main"/> + </root> + + </loggers> + +</configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/cli/src/main/resources/syncope.properties ---------------------------------------------------------------------- diff --git a/client/cli/src/main/resources/syncope.properties b/client/cli/src/main/resources/syncope.properties new file mode 100644 index 0000000..9f84a72 --- /dev/null +++ b/client/cli/src/main/resources/syncope.properties @@ -0,0 +1,19 @@ +# 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. +syncope.rest.services=http://localhost:9080/syncope/rest/ +syncope.user=admin +syncope.password=password http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/console/pom.xml ---------------------------------------------------------------------- diff --git a/client/console/pom.xml b/client/console/pom.xml new file mode 100644 index 0000000..35185f1 --- /dev/null +++ b/client/console/pom.xml @@ -0,0 +1,112 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.syncope</groupId> + <artifactId>syncope-client</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + + <name>Apache Syncope Client Console</name> + <description>Apache Syncope Client Console</description> + <groupId>org.apache.syncope.client</groupId> + <artifactId>syncope-client-console</artifactId> + <packaging>jar</packaging> + + <properties> + <rootpom.basedir>${basedir}/../..</rootpom.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket</artifactId> + <type>pom</type> + </dependency> + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-extensions</artifactId> + </dependency> + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-datetime</artifactId> + </dependency> + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-spring</artifactId> + <!-- exclude spring framework that wicket pulls in --> + <exclusions> + <exclusion> + <groupId>org.springframework</groupId> + <artifactId>spring</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-auth-roles</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.syncope.client</groupId> + <artifactId>syncope-client-lib</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + </plugin> + </plugins> + + <resources> + <resource> + <directory>src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> + </build> +</project> http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/console/src/main/java/org/apache/syncope/client/console/BinaryPreview.java ---------------------------------------------------------------------- diff --git a/client/console/src/main/java/org/apache/syncope/client/console/BinaryPreview.java b/client/console/src/main/java/org/apache/syncope/client/console/BinaryPreview.java new file mode 100644 index 0000000..c499d60 --- /dev/null +++ b/client/console/src/main/java/org/apache/syncope/client/console/BinaryPreview.java @@ -0,0 +1,34 @@ +/* + * 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.console; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface BinaryPreview { + + public String[] mimeTypes() default {}; + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/console/src/main/java/org/apache/syncope/client/console/ExtensionPanel.java ---------------------------------------------------------------------- diff --git a/client/console/src/main/java/org/apache/syncope/client/console/ExtensionPanel.java b/client/console/src/main/java/org/apache/syncope/client/console/ExtensionPanel.java new file mode 100644 index 0000000..5574926 --- /dev/null +++ b/client/console/src/main/java/org/apache/syncope/client/console/ExtensionPanel.java @@ -0,0 +1,34 @@ +/* + * 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.console; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface ExtensionPanel { + + public String value(); + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/console/src/main/java/org/apache/syncope/client/console/SyncopeApplication.java ---------------------------------------------------------------------- diff --git a/client/console/src/main/java/org/apache/syncope/client/console/SyncopeApplication.java b/client/console/src/main/java/org/apache/syncope/client/console/SyncopeApplication.java new file mode 100644 index 0000000..ac44d43 --- /dev/null +++ b/client/console/src/main/java/org/apache/syncope/client/console/SyncopeApplication.java @@ -0,0 +1,306 @@ +/* + * 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.console; + +import java.io.Serializable; +import org.apache.commons.lang3.StringUtils; +import org.apache.syncope.client.console.commons.Constants; +import org.apache.syncope.client.console.commons.XMLRolesReader; +import org.apache.syncope.client.console.pages.Configuration; +import org.apache.syncope.client.console.pages.InfoModalPage; +import org.apache.syncope.client.console.pages.Login; +import org.apache.syncope.client.console.pages.Logout; +import org.apache.syncope.client.console.pages.Reports; +import org.apache.syncope.client.console.pages.Resources; +import org.apache.syncope.client.console.pages.Roles; +import org.apache.syncope.client.console.pages.Schema; +import org.apache.syncope.client.console.pages.Tasks; +import org.apache.syncope.client.console.pages.Todo; +import org.apache.syncope.client.console.pages.UserSelfModalPage; +import org.apache.syncope.client.console.pages.Users; +import org.apache.syncope.client.console.pages.WelcomePage; +import org.apache.syncope.client.console.resources.FilesystemResource; +import org.apache.syncope.client.console.resources.WorkflowDefGETResource; +import org.apache.syncope.client.console.resources.WorkflowDefPUTResource; +import org.apache.syncope.client.console.rest.UserSelfRestClient; +import org.apache.syncope.common.lib.to.UserTO; +import org.apache.wicket.Component; +import org.apache.wicket.Page; +import org.apache.wicket.RestartResponseAtInterceptPageException; +import org.apache.wicket.Session; +import org.apache.wicket.WicketRuntimeException; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.AjaxLink; +import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener; +import org.apache.wicket.authorization.UnauthorizedInstantiationException; +import org.apache.wicket.authroles.authorization.strategies.role.IRoleCheckingStrategy; +import org.apache.wicket.authroles.authorization.strategies.role.RoleAuthorizationStrategy; +import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy; +import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.image.Image; +import org.apache.wicket.markup.html.link.BookmarkablePageLink; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.request.Request; +import org.apache.wicket.request.Response; +import org.apache.wicket.request.resource.ContextRelativeResource; +import org.apache.wicket.request.resource.IResource; +import org.apache.wicket.request.resource.ResourceReference; +import org.apache.wicket.spring.injection.annot.SpringComponentInjector; +import org.springframework.web.context.support.WebApplicationContextUtils; + +/** + * SyncopeApplication class. + */ +public class SyncopeApplication + extends WebApplication + implements IUnauthorizedComponentInstantiationListener, IRoleCheckingStrategy, Serializable { + + private static final long serialVersionUID = -2920378752291913495L; + + public static final String IMG_PREFIX = "/img/menu/"; + + public static final String IMG_NOTSEL = "notsel/"; + + private static final String ACTIVITI_MODELER_CONTEXT = "activiti-modeler"; + + private static final int EDIT_PROFILE_WIN_HEIGHT = 550; + + private static final int EDIT_PROFILE_WIN_WIDTH = 800; + + @Override + protected void init() { + super.init(); + + getComponentInstantiationListeners().add(new SpringComponentInjector(this)); + + getResourceSettings().setThrowExceptionOnMissingResource(true); + + getSecuritySettings().setAuthorizationStrategy(new RoleAuthorizationStrategy(this)); + getSecuritySettings().setUnauthorizedComponentInstantiationListener(this); + + getMarkupSettings().setStripWicketTags(true); + getMarkupSettings().setCompressWhitespace(true); + + getRequestCycleListeners().add(new SyncopeRequestCycleListener()); + + final String activitiModelerDirectory = WebApplicationContextUtils.getWebApplicationContext( + WebApplication.get().getServletContext()).getBean("activitiModelerDirectory", String.class); + mountResource("/" + ACTIVITI_MODELER_CONTEXT, new ResourceReference(ACTIVITI_MODELER_CONTEXT) { + + private static final long serialVersionUID = -128426276529456602L; + + @Override + public IResource getResource() { + return new FilesystemResource(ACTIVITI_MODELER_CONTEXT, activitiModelerDirectory); + } + + }); + mountResource("/workflowDefGET", new ResourceReference("workflowDefGET") { + + private static final long serialVersionUID = -128426276529456602L; + + @Override + public IResource getResource() { + return new WorkflowDefGETResource(); + } + }); + mountResource("/workflowDefPUT", new ResourceReference("workflowDefPUT") { + + private static final long serialVersionUID = -128426276529456602L; + + @Override + public IResource getResource() { + return new WorkflowDefPUTResource(); + } + }); + } + + public void setupNavigationPanel(final WebPage page, final XMLRolesReader xmlRolesReader, final boolean notsel) { + final ModalWindow infoModal = new ModalWindow("infoModal"); + page.add(infoModal); + infoModal.setInitialWidth(350); + infoModal.setInitialHeight(300); + infoModal.setCssClassName(ModalWindow.CSS_CLASS_GRAY); + infoModal.setCookieName("infoModal"); + infoModal.setPageCreator(new ModalWindow.PageCreator() { + + private static final long serialVersionUID = -7834632442532690940L; + + @Override + public Page createPage() { + return new InfoModalPage(); + } + }); + + final AjaxLink<Page> infoLink = new AjaxLink<Page>("infoLink") { + + private static final long serialVersionUID = -7978723352517770644L; + + @Override + public void onClick(final AjaxRequestTarget target) { + infoModal.show(target); + } + }; + page.add(infoLink); + + BookmarkablePageLink<Page> schemaLink = new BookmarkablePageLink<>("schema", Schema.class); + MetaDataRoleAuthorizationStrategy.authorize( + schemaLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Schema", "list")); + page.add(schemaLink); + schemaLink.add(new Image("schemaIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "schema" + Constants.PNG_EXT))); + + BookmarkablePageLink<Page> usersLink = new BookmarkablePageLink<>("users", Users.class); + MetaDataRoleAuthorizationStrategy.authorize( + usersLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Users", "list")); + page.add(usersLink); + usersLink.add(new Image("usersIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "users" + Constants.PNG_EXT))); + + BookmarkablePageLink<Page> rolesLink = new BookmarkablePageLink<>("roles", Roles.class); + MetaDataRoleAuthorizationStrategy.authorize( + rolesLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Roles", "list")); + page.add(rolesLink); + rolesLink.add(new Image("rolesIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "roles" + Constants.PNG_EXT))); + + BookmarkablePageLink<Page> resourcesLink = new BookmarkablePageLink<>("resources", Resources.class); + MetaDataRoleAuthorizationStrategy.authorize( + resourcesLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Resources", "list")); + page.add(resourcesLink); + resourcesLink.add(new Image("resourcesIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "resources" + Constants.PNG_EXT))); + + BookmarkablePageLink<Page> todoLink = new BookmarkablePageLink<>("todo", Todo.class); + MetaDataRoleAuthorizationStrategy.authorize( + todoLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Approval", "list")); + page.add(todoLink); + todoLink.add(new Image("todoIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "todo" + Constants.PNG_EXT))); + + BookmarkablePageLink<Page> reportLink = new BookmarkablePageLink<>("reports", Reports.class); + MetaDataRoleAuthorizationStrategy.authorize( + reportLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Reports", "list")); + page.add(reportLink); + reportLink.add(new Image("reportsIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "reports" + Constants.PNG_EXT))); + + BookmarkablePageLink<Page> configurationLink = new BookmarkablePageLink<>("configuration", + Configuration.class); + MetaDataRoleAuthorizationStrategy.authorize( + configurationLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Configuration", "list")); + page.add(configurationLink); + configurationLink.add(new Image("configurationIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "configuration" + Constants.PNG_EXT))); + + BookmarkablePageLink<Page> taskLink = new BookmarkablePageLink<>("tasks", Tasks.class); + MetaDataRoleAuthorizationStrategy.authorize( + taskLink, WebPage.ENABLE, xmlRolesReader.getEntitlement("Tasks", "list")); + page.add(taskLink); + taskLink.add(new Image("tasksIcon", new ContextRelativeResource(IMG_PREFIX + (notsel + ? IMG_NOTSEL + : StringUtils.EMPTY) + "tasks" + Constants.PNG_EXT))); + + page.add(new BookmarkablePageLink<Page>("logout", Logout.class)); + } + + public void setupEditProfileModal(final WebPage page, final UserSelfRestClient userSelfRestClient) { + // Modal window for editing user profile + final ModalWindow editProfileModalWin = new ModalWindow("editProfileModal"); + editProfileModalWin.setCssClassName(ModalWindow.CSS_CLASS_GRAY); + editProfileModalWin.setInitialHeight(EDIT_PROFILE_WIN_HEIGHT); + editProfileModalWin.setInitialWidth(EDIT_PROFILE_WIN_WIDTH); + editProfileModalWin.setCookieName("edit-profile-modal"); + page.add(editProfileModalWin); + + final AjaxLink<Page> editProfileLink = new AjaxLink<Page>("editProfileLink") { + + private static final long serialVersionUID = -7978723352517770644L; + + @Override + public void onClick(final AjaxRequestTarget target) { + final UserTO userTO; + if (SyncopeSession.get().isAuthenticated()) { + try { + userTO = userSelfRestClient.read(); + } catch (Exception e) { + throw new WicketRuntimeException(e); + } + } else { + userTO = new UserTO(); + } + + editProfileModalWin.setPageCreator(new ModalWindow.PageCreator() { + + private static final long serialVersionUID = -7834632442532690940L; + + @Override + public Page createPage() { + return new UserSelfModalPage(page.getPageReference(), editProfileModalWin, userTO); + } + }); + + editProfileModalWin.show(target); + } + }; + + editProfileLink.add(new Label("username", SyncopeSession.get().getUsername())); + + if ("admin".equals(SyncopeSession.get().getUsername())) { + editProfileLink.setEnabled(false); + } + + page.add(editProfileLink); + } + + @Override + public Session newSession(final Request request, final Response response) { + return new SyncopeSession(request); + } + + @Override + public Class<? extends Page> getHomePage() { + return SyncopeSession.get().isAuthenticated() ? WelcomePage.class : Login.class; + } + + @Override + public void onUnauthorizedInstantiation(final Component component) { + SyncopeSession.get().invalidate(); + + if (component instanceof Page) { + throw new UnauthorizedInstantiationException(component.getClass()); + } + + throw new RestartResponseAtInterceptPageException(Login.class); + } + + @Override + public boolean hasAnyRole(final org.apache.wicket.authroles.authorization.strategies.role.Roles roles) { + return SyncopeSession.get().hasAnyRole(roles); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/2d194636/client/console/src/main/java/org/apache/syncope/client/console/SyncopeRequestCycleListener.java ---------------------------------------------------------------------- diff --git a/client/console/src/main/java/org/apache/syncope/client/console/SyncopeRequestCycleListener.java b/client/console/src/main/java/org/apache/syncope/client/console/SyncopeRequestCycleListener.java new file mode 100644 index 0000000..8b27260 --- /dev/null +++ b/client/console/src/main/java/org/apache/syncope/client/console/SyncopeRequestCycleListener.java @@ -0,0 +1,84 @@ +/* + * 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.console; + +import java.security.AccessControlException; +import javax.ws.rs.BadRequestException; +import javax.xml.ws.WebServiceException; +import org.apache.syncope.client.console.pages.ErrorPage; +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.wicket.Page; +import org.apache.wicket.authorization.UnauthorizedInstantiationException; +import org.apache.wicket.core.request.handler.PageProvider; +import org.apache.wicket.core.request.handler.RenderPageRequestHandler; +import org.apache.wicket.markup.html.pages.ExceptionErrorPage; +import org.apache.wicket.model.StringResourceModel; +import org.apache.wicket.protocol.http.PageExpiredException; +import org.apache.wicket.request.IRequestHandler; +import org.apache.wicket.request.cycle.AbstractRequestCycleListener; +import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncopeRequestCycleListener extends AbstractRequestCycleListener { + + /** + * Logger. + */ + private static final Logger LOG = LoggerFactory.getLogger(SyncopeRequestCycleListener.class); + + /** + * {@inheritDoc} + */ + @Override + public IRequestHandler onException(final RequestCycle cycle, final Exception e) { + LOG.error("Exception found", e); + + PageParameters errorParameters = new PageParameters(); + errorParameters.add("errorTitle", new StringResourceModel("alert", null).getString()); + + final Page errorPage; + if (e instanceof UnauthorizedInstantiationException) { + errorParameters.add("errorMessage", + new StringResourceModel("unauthorizedInstantiationException", null).getString()); + + errorPage = new ErrorPage(errorParameters); + } else if (e.getCause() instanceof AccessControlException) { + errorParameters.add("errorMessage", new StringResourceModel("accessControlException", null).getString()); + + errorPage = new ErrorPage(errorParameters); + } else if (e instanceof PageExpiredException || !(SyncopeSession.get()).isAuthenticated()) { + errorParameters.add("errorMessage", new StringResourceModel("pageExpiredException", null).getString()); + + errorPage = new ErrorPage(errorParameters); + } else if (e.getCause() instanceof BadRequestException || e.getCause() instanceof WebServiceException + || e.getCause() instanceof SyncopeClientException) { + + errorParameters.add("errorMessage", new StringResourceModel("restClientException", null).getString()); + + errorPage = new ErrorPage(errorParameters); + } else { + // redirect to default Wicket error page + errorPage = new ExceptionErrorPage(e, null); + } + + return new RenderPageRequestHandler(new PageProvider(errorPage)); + } +}
