Package org.hoteia.qalingo.core.web.mvc.controller.oauth

Source Code of org.hoteia.qalingo.core.web.mvc.controller.oauth.ConnectWindowsLiveController

/**
* Most of the code in the Qalingo project is copyrighted Hoteia and licensed
* under the Apache License Version 2.0 (release version 0.8.0)
*         http://www.apache.org/licenses/LICENSE-2.0
*
*                   Copyright (c) Hoteia, 2012-2014
* http://www.hoteia.com - http://twitter.com/hoteia - contact@hoteia.com
*
*/
package org.hoteia.qalingo.core.web.mvc.controller.oauth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hoteia.qalingo.core.domain.EngineSetting;
import org.hoteia.qalingo.core.domain.EngineSettingValue;
import org.hoteia.qalingo.core.domain.enumtype.FoUrls;
import org.hoteia.qalingo.core.domain.enumtype.OAuthType;
import org.hoteia.qalingo.core.pojo.RequestData;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.LiveApi;
import org.scribe.oauth.OAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
*
*/
@Controller("connectWindowsLiveController")
public class ConnectWindowsLiveController extends AbstractOAuthFrontofficeController {

  protected final Logger logger = LoggerFactory.getLogger(getClass());

  @RequestMapping("/connect-oauth-windows-live.html*")
  public ModelAndView connectWindowsLive(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final RequestData requestData = requestUtil.getRequestData(request);
   
    // SANITY CHECK
    if(!requestUtil.hasKnownCustomerLogged(request)){
      try {
          // CLIENT ID
          EngineSetting clientIdEngineSetting = engineSettingService.getSettingOAuthAppKeyOrId();
          EngineSettingValue clientIdEngineSettingValue = clientIdEngineSetting.getEngineSettingValue(OAuthType.WINDOWS_LIVE.name());
         
          // CLIENT SECRET
          EngineSetting clientSecretEngineSetting = engineSettingService.getSettingOAuthAppSecret();
          EngineSettingValue clientSecretEngineSettingValue = clientSecretEngineSetting.getEngineSettingValue(OAuthType.WINDOWS_LIVE.name());
         
          // CLIENT PERMISSIONS
          EngineSetting permissionsEngineSetting = engineSettingService.getSettingOAuthAppPermissions();
          EngineSettingValue permissionsEngineSettingValue = permissionsEngineSetting.getEngineSettingValue(OAuthType.WINDOWS_LIVE.name());
         
          if(clientIdEngineSettingValue != null
              && clientSecretEngineSetting != null
              && permissionsEngineSettingValue != null){
            final String contextValue = requestUtil.getCurrentContextNameValue(request);
          final String clientId = clientIdEngineSettingValue.getValue();
          final String clientSecret = clientSecretEngineSettingValue.getValue();
          final String permissions = permissionsEngineSettingValue.getValue();
         
          final String windowsLiveCallBackURL = urlService.buildAbsoluteUrl(requestData, urlService.buildOAuthCallBackUrl(requestData, OAuthType.WINDOWS_LIVE.getPropertyKey().toLowerCase()));

            OAuthService service = new ServiceBuilder()
                    .provider(LiveApi.class)
                    .apiKey(clientId)
                    .apiSecret(clientSecret)
                    .scope(permissions)
                    .callback(windowsLiveCallBackURL)
                    .build();
         
          // Obtain the Authorization URL
          String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);

          response.sendRedirect(authorizationUrl);
          }

      } catch (Exception e) {
        logger.error("Connect With " + OAuthType.WINDOWS_LIVE.name() + " failed!");
      }
    }

    // DEFAULT FALLBACK VALUE
    if(!response.isCommitted()){
      response.sendRedirect(urlService.generateUrl(FoUrls.LOGIN, requestData));
    }
   
    return null;
  }
 
}
TOP

Related Classes of org.hoteia.qalingo.core.web.mvc.controller.oauth.ConnectWindowsLiveController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.