// Path: src/main/java/com/lixiaocong/cms/exception/ModuleDisabledException.java
// public class ModuleDisabledException extends Exception {
//     public ModuleDisabledException(String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/com/lixiaocong/cms/service/IConfigService.java
// public interface IConfigService {
//     String getApplicationUrl();
// 
//     void setApplicationUrl(String applicationUrl);
// 
//     boolean isBlogEnabled();
// 
//     void setBlogEnabled(boolean isEnabled);
// 
//     boolean isQQEnabled();
// 
//     void setQQEnabled(boolean isEnabled);
// 
//     String getQQId();
// 
//     void setQQId(String qqId);
// 
//     String getQQSecret();
// 
//     void setQQSecret(String qqSecret);
// 
//     boolean isWeixinEnabled();
// 
//     void setWeixinEnabled(boolean isEnabled);
// 
//     String getWeixinId();
// 
//     void setWeixinId(String weixinId);
// 
//     String getWeixinSecret();
// 
//     void setWeixinSecret(String weixinSecret);
// 
//     String getWeixinToken();
// 
//     void setWeixinToken(String weixinToken);
// 
//     String getWeixinKey();
// 
//     void setWeixinKey(String weixinKey);
// 
//     boolean isDownloaderEnabled();
// 
//     void setDownloaderEnabled(boolean isEnabled);
// 
//     String getDownloaderAria2cUrl();
// 
//     void setDownloaderAria2cUrl(String aria2cUrl);
// 
//     String getDownloaderAria2cPassword();
// 
//     void setDownloaderAria2cPassword(String aria2cPassword);
// 
//     String getDownloaderTransmissionUrl();
// 
//     void setDownloaderTransmissionUrl(String transmissionUrl);
// 
//     String getDownloaderTransmissionUsername();
// 
//     void setDownloaderTransmissionUsername(String transmissionUsername);
// 
//     String getDownloaderTransmissionPassword();
// 
//     void setDownloaderTransmissionPassword(String transmissionPassword);
// 
//     String getStorageDir();
// 
//     void setStorageDir(String storageDir);
// 
//     void setValue(String key, String value);
// }


import com.lixiaocong.cms.exception.ModuleDisabledException;
import com.lixiaocong.cms.service.IConfigService;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.web.ProviderSignInController;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
  BSD 3-Clause License

  Copyright (c) 2016, lixiaocong(lxccs@iCloud.com)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.lixiaocong.cms.interceptor;



public class QQInterceptor extends HandlerInterceptorAdapter {

    private final IConfigService configService;
    private final ConnectController connectController;
    private final ProviderSignInController signInController;
    private String applicationUrl;


    public QQInterceptor(IConfigService configService, ConnectController connectController, ProviderSignInController signInController) {
        this.configService = configService;
        this.connectController = connectController;
        this.signInController = signInController;
        this.applicationUrl = configService.getApplicationUrl();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (this.configService.isQQEnabled()) {
            String newApplicationUrl = this.configService.getApplicationUrl();
            if (!newApplicationUrl.equals(this.applicationUrl)) {
                this.applicationUrl = newApplicationUrl;
                connectController.setApplicationUrl(newApplicationUrl);
                connectController.afterPropertiesSet();
                signInController.setApplicationUrl(newApplicationUrl);
                signInController.afterPropertiesSet();
            }
            return true;
        }
// Path: src/main/java/com/lixiaocong/cms/exception/ModuleDisabledException.java
// public class ModuleDisabledException extends Exception {
//     public ModuleDisabledException(String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/com/lixiaocong/cms/service/IConfigService.java
// public interface IConfigService {
//     String getApplicationUrl();
// 
//     void setApplicationUrl(String applicationUrl);
// 
//     boolean isBlogEnabled();
// 
//     void setBlogEnabled(boolean isEnabled);
// 
//     boolean isQQEnabled();
// 
//     void setQQEnabled(boolean isEnabled);
// 
//     String getQQId();
// 
//     void setQQId(String qqId);
// 
//     String getQQSecret();
// 
//     void setQQSecret(String qqSecret);
// 
//     boolean isWeixinEnabled();
// 
//     void setWeixinEnabled(boolean isEnabled);
// 
//     String getWeixinId();
// 
//     void setWeixinId(String weixinId);
// 
//     String getWeixinSecret();
// 
//     void setWeixinSecret(String weixinSecret);
// 
//     String getWeixinToken();
// 
//     void setWeixinToken(String weixinToken);
// 
//     String getWeixinKey();
// 
//     void setWeixinKey(String weixinKey);
// 
//     boolean isDownloaderEnabled();
// 
//     void setDownloaderEnabled(boolean isEnabled);
// 
//     String getDownloaderAria2cUrl();
// 
//     void setDownloaderAria2cUrl(String aria2cUrl);
// 
//     String getDownloaderAria2cPassword();
// 
//     void setDownloaderAria2cPassword(String aria2cPassword);
// 
//     String getDownloaderTransmissionUrl();
// 
//     void setDownloaderTransmissionUrl(String transmissionUrl);
// 
//     String getDownloaderTransmissionUsername();
// 
//     void setDownloaderTransmissionUsername(String transmissionUsername);
// 
//     String getDownloaderTransmissionPassword();
// 
//     void setDownloaderTransmissionPassword(String transmissionPassword);
// 
//     String getStorageDir();
// 
//     void setStorageDir(String storageDir);
// 
//     void setValue(String key, String value);
// }


// Path: src/main/java/com/lixiaocong/cms/interceptor/QQInterceptor.java
import com.lixiaocong.cms.exception.ModuleDisabledException;
import com.lixiaocong.cms.service.IConfigService;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.web.ProviderSignInController;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
  BSD 3-Clause License

  Copyright (c) 2016, lixiaocong(lxccs@iCloud.com)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.lixiaocong.cms.interceptor;



public class QQInterceptor extends HandlerInterceptorAdapter {

    private final IConfigService configService;
    private final ConnectController connectController;
    private final ProviderSignInController signInController;
    private String applicationUrl;


    public QQInterceptor(IConfigService configService, ConnectController connectController, ProviderSignInController signInController) {
        this.configService = configService;
        this.connectController = connectController;
        this.signInController = signInController;
        this.applicationUrl = configService.getApplicationUrl();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (this.configService.isQQEnabled()) {
            String newApplicationUrl = this.configService.getApplicationUrl();
            if (!newApplicationUrl.equals(this.applicationUrl)) {
                this.applicationUrl = newApplicationUrl;
                connectController.setApplicationUrl(newApplicationUrl);
                connectController.afterPropertiesSet();
                signInController.setApplicationUrl(newApplicationUrl);
                signInController.afterPropertiesSet();
            }
            return true;
        }
        throw new ModuleDisabledException("QQ module is disabled");