/*
* Copyright (C) 2015 tarent AG
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.osiam.addons.self_administration.plugin.impl;

import org.osiam.addons.self_administration.plugin_api.CallbackException;
import org.osiam.addons.self_administration.plugin_api.CallbackPlugin;
import org.osiam.client.OsiamConnector;
import org.osiam.client.oauth.AccessToken;
import org.osiam.client.oauth.Scope;
import org.osiam.client.query.Query;
import org.osiam.client.query.QueryBuilder;
import org.osiam.resources.scim.Email;
import org.osiam.resources.scim.Group;
import org.osiam.resources.scim.SCIMSearchResult;
import org.osiam.resources.scim.UpdateGroup;
import org.osiam.resources.scim.User;

/**
 * Simple Plugin implementation.
 */
public class PluginImpl implements CallbackPlugin {

    private static final String TEST_GROUP_NAME = "Test";

    private OsiamConnector connector;
    private AccessToken accessToken;

    public void performPreRegistrationActions(User user) throws CallbackException {
        if (user.getEmails() != null) for (Email email : user.getEmails()) {
            if (!email.getValue().endsWith(".org")) {
                throw new CallbackException("The given Email '" + email.getValue() + "' must end with .org!");
            }
        }
    }

    @Override
    public void performPostRegistrationActions(User user) throws CallbackException {
        OsiamConnector connector = getOsiamConnector();

        Group testGroup = getTestGroup(connector);
        UpdateGroup uGroup = new UpdateGroup.Builder().addMember(user.getId()).build();

        connector.updateGroup(testGroup.getId(), uGroup, getAccessToken());
    }

    private Group getTestGroup(OsiamConnector connector) {
        Query groupQuery = new QueryBuilder().filter("displayName eq \"" + TEST_GROUP_NAME + "\"").build();
        SCIMSearchResult<Group> result = connector.searchGroups(groupQuery, getAccessToken());

        if(result.getTotalResults() > 0){
            Group testGroup = result.getResources().get(0);
            return testGroup;
        }

        return createTestGroup(connector);
    }

    private Group createTestGroup(OsiamConnector connector) {
        Group testGroup = new Group.Builder(TEST_GROUP_NAME).build();

        return connector.createGroup(testGroup, getAccessToken());
    }

    private AccessToken getAccessToken() {
        if(accessToken == null){
            accessToken = getOsiamConnector().retrieveAccessToken(getUserName(), getUserPassword(), Scope.ADMIN);
        }

        return accessToken;
    }

    private OsiamConnector getOsiamConnector() {
        if(connector == null){
            connector = new OsiamConnector.Builder()
                .withEndpoint(getOsiamEndpoint())
                .setClientId(getClientId())
                .setClientSecret(getClientSecret())
                .build();
        }

        return connector;
    }

    private String getProperty(String key, String defaultValue) {
        return System.getProperty(key, defaultValue);
    }

    private String getUserName() {
        return getProperty("osiam.addon-self-administration.plugin.user.name", "admin");
    }

    private String getUserPassword() {
        return getProperty("osiam.addon-self-administration.plugin.user.password", "koala");
    }

    private String getOsiamEndpoint() {
        return getProperty("osiam.addon-self-administration.plugin.osiam.endpoint", "http://localhost:8080/osiam");
    }

    private String getClientId() {
        return getProperty("osiam.addon-self-administration.plugin.osiam.client.id", "example-client");
    }

    private String getClientSecret() {
        return getProperty("osiam.addon-self-administration.plugin.osiam.client.secret", "secret");
    }
}

@@ -3663,25 +3663,30 @@
         
-c
+OsiamC
 onnector
  = new O
@@ -3677,16 +3677,32 @@
 onnector
+.Builder builder
  = new O
@@ -3737,50 +3737,8 @@
     
-        .withEndpoint(getOsiamEndpoint())%0A
     
@@ -3737,36 +3737,32 @@
                 
-    
 .setClientId(get
@@ -3769,24 +3769,28 @@
 ClientId())%0A
+    
             
@@ -3828,16 +3828,18 @@
 ecret())
+;%0A
 %0A       
@@ -3847,19 +3847,103 @@
     
-    .build(
+if (getOsiamVersion().startsWith(%222%22)) %7B%0A                builder.setEndpoint(getOsiamEndpoint()
 );%0A 
@@ -3953,162 +3953,170 @@
     
+ 
-%7D%0A%0A        return connector;%0A    %7D%0A%0A    private String getProperty(String key, String defaultValue) %7B%0A        return System.getProperty(key, defaultValue)
+   %7D else %7B%0A                builder.withEndpoint(getOsiamEndpoint());%0A            %7D%0A%0A            connector = builder.build();%0A        %7D%0A%0A        return connector
 ;%0A  
@@ -4162,32 +4162,39 @@
 %0A        return 
+System.
 getProperty(%22osi
@@ -4304,32 +4304,39 @@
 %0A        return 
+System.
 getProperty(%22osi
@@ -4451,32 +4451,39 @@
 %0A        return 
+System.
 getProperty(%22osi
@@ -4566,32 +4566,174 @@
 osiam%22);%0A    %7D%0A%0A
+    private String getOsiamVersion() %7B%0A        return System.getProperty(%22osiam.addon-self-administration.plugin.osiam.version%22, %223%22);%0A    %7D%0A%0A
     private Stri
@@ -4758,32 +4758,39 @@
 %0A        return 
+System.
 getProperty(%22osi
@@ -4923,16 +4923,23 @@
  return 
+System.
 getPrope

Make OSIAM version configurable