/*
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. 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.wso2.carbon.identity.oidc.session.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.http.HttpService;
import org.wso2.carbon.identity.oidc.session.OIDCSessionConstants;
import org.wso2.carbon.identity.oidc.session.servlet.OIDCLogoutServlet;
import org.wso2.carbon.identity.oidc.session.servlet.OIDCSessionIFrameServlet;

import javax.servlet.Servlet;

/**
 * @scr.component name="identity.oidc.session.component" immediate="true"
 * @scr.reference name="osgi.httpservice" interface="org.osgi.service.http.HttpService"
 * cardinality="1..1" policy="dynamic" bind="setHttpService"
 * unbind="unsetHttpService"
 */
public class OIDCSessionManagementComponent {
    private static final Log log = LogFactory.getLog(OIDCSessionManagementComponent.class);

    protected void activate(ComponentContext context) {

        HttpService httpService = OIDCSessionManagementComponentServiceHolder.getHttpService();

        // Register Session IFrame Servlet
        Servlet sessionIFrameServlet = new ContextPathServletAdaptor(new OIDCSessionIFrameServlet(),
                                                                     OIDCSessionConstants.OIDCEndpoints.OIDC_SESSION_IFRAME_ENDPOINT);
        try {
            httpService.registerServlet(OIDCSessionConstants.OIDCEndpoints.OIDC_SESSION_IFRAME_ENDPOINT,
                                        sessionIFrameServlet, null, null);
        } catch (Exception e) {
            String msg = "Error when registering OIDC Session IFrame Servlet via the HttpService.";
            log.error(msg, e);
            throw new RuntimeException(msg, e);
        }

        Servlet logoutServlet = new ContextPathServletAdaptor(new OIDCLogoutServlet(),
                                                              OIDCSessionConstants.OIDCEndpoints.OIDC_LOGOUT_ENDPOINT);
        try {
            httpService.registerServlet(OIDCSessionConstants.OIDCEndpoints.OIDC_LOGOUT_ENDPOINT, logoutServlet, null,
                                        null);
        } catch (Exception e) {
            String msg = "Error when registering OIDC Logout Servlet via the HttpService.";
            log.error(msg, e);
            throw new RuntimeException(msg, e);
        }
        if (log.isDebugEnabled()) {
            log.info("OIDC Session Management bundle is activated");
        }
    }

    protected void deactivate(ComponentContext context) {

        if (log.isDebugEnabled()) {
            log.info("OIDC Session Management bundle is deactivated");
        }
    }

    protected void setHttpService(HttpService httpService) {

        if (log.isDebugEnabled()) {
            log.info("Setting the HTTP Service in OIDC Session Management bundle");
        }
        OIDCSessionManagementComponentServiceHolder.setHttpService(httpService);
    }

    protected void unsetHttpService(HttpService httpService) {

        if (log.isDebugEnabled()) {
            log.info("Unsetting the HTTP Service in OIDC Session Management bundle");
        }
        OIDCSessionManagementComponentServiceHolder.setHttpService(null);
    }
}

@@ -1191,16 +1191,71 @@
 Servlet;
+%0Aimport org.wso2.carbon.user.core.service.RealmService;
 %0A%0Aimport
@@ -1535,16 +1535,218 @@
 ervice%22%0A
+ * @scr.reference name=%22user.realmservice.default%22%0A * interface=%22org.wso2.carbon.user.core.service.RealmService%22 cardinality=%221..1%22%0A * policy=%22dynamic%22 bind=%22setRealmService%22 unbind=%22unsetRealmService%22%0A
  */%0Apubl
@@ -4189,10 +4189,511 @@
 ;%0A    %7D%0A
+    protected void setRealmService(RealmService realmService) %7B%0A%0A        if (log.isDebugEnabled()) %7B%0A            log.debug(%22Setting the Realm Service%22);%0A        %7D%0A        OIDCSessionManagementComponentServiceHolder.setRealmService(realmService);%0A    %7D%0A%0A    protected void unsetRealmService(RealmService realmService) %7B%0A%0A        if (log.isDebugEnabled()) %7B%0A            log.debug(%22Unsetting the Realm Service%22);%0A        %7D%0A        OIDCSessionManagementComponentServiceHolder.setRealmService(null);%0A    %7D%0A
 %7D%0A

Add reference to realm service