// Path: social/src/main/java/org/meruvian/yama/social/core/SocialService.java
// public interface SocialService<T> {
// 	Connection<T> createConnection(String authorizationCode, MultiValueMap<String, String> additionalParameters);;
// 	
// 	User createUser(Connection<?> connection);
// 	
// 	String getAuthorizeUrl();
// 	
// 	boolean isAuthorized(Connection<?> connection);
// 	
// 	OAuth2Parameters getParameters();
// 	
// 	OAuth2ConnectionFactory<T> getConnectionFactory();
// }
// 
// Path: core/src/main/java/org/meruvian/yama/web/CredentialsService.java
// public interface CredentialsService {
// 	void registerAuthentication(String userId);
// 	
// 	void registerAuthentication(String userId, HttpServletRequest request);
// }


import org.springframework.stereotype.Service;
import java.net.URI;
import java.util.List;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.meruvian.yama.social.core.SocialService;
import org.meruvian.yama.social.core.SocialServiceLocator;
import org.meruvian.yama.web.CredentialsService;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.UsersConnectionRepository;
/**
 * Copyright 2014 Meruvian
 *
 * Licensed 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.meruvian.yama.webapi.service;




/**
 * @author Dian Aditya
 *
 */
@Service
public class RestSocialSignInService implements SocialSignInService {
	public static final String OAUTH_AUTH_URL = "/oauth/authorize";
	
	@Inject
	private SocialServiceLocator socialServiceLocator;
	
	@Inject
	private UsersConnectionRepository connectionRepository;
	
	@Inject
	private CredentialsService credentialsService;
	
	@Context
	private HttpServletRequest request;
	
	@Override
	public Response socialSignIn(String provider) {
		String redirectUri = socialServiceLocator.getSocialService(provider).getAuthorizeUrl();
		
		return Response.seeOther(URI.create(redirectUri)).build();
	}

	@Override
	public Response socialSignInCallback(String provider, String code) {
// Path: social/src/main/java/org/meruvian/yama/social/core/SocialService.java
// public interface SocialService<T> {
// 	Connection<T> createConnection(String authorizationCode, MultiValueMap<String, String> additionalParameters);;
// 	
// 	User createUser(Connection<?> connection);
// 	
// 	String getAuthorizeUrl();
// 	
// 	boolean isAuthorized(Connection<?> connection);
// 	
// 	OAuth2Parameters getParameters();
// 	
// 	OAuth2ConnectionFactory<T> getConnectionFactory();
// }
// 
// Path: core/src/main/java/org/meruvian/yama/web/CredentialsService.java
// public interface CredentialsService {
// 	void registerAuthentication(String userId);
// 	
// 	void registerAuthentication(String userId, HttpServletRequest request);
// }


// Path: webapi/src/main/java/org/meruvian/yama/webapi/service/RestSocialSignInService.java
import org.springframework.stereotype.Service;
import java.net.URI;
import java.util.List;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.meruvian.yama.social.core.SocialService;
import org.meruvian.yama.social.core.SocialServiceLocator;
import org.meruvian.yama.web.CredentialsService;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.UsersConnectionRepository;

/**
 * Copyright 2014 Meruvian
 *
 * Licensed 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.meruvian.yama.webapi.service;




/**
 * @author Dian Aditya
 *
 */
@Service
public class RestSocialSignInService implements SocialSignInService {
	public static final String OAUTH_AUTH_URL = "/oauth/authorize";
	
	@Inject
	private SocialServiceLocator socialServiceLocator;
	
	@Inject
	private UsersConnectionRepository connectionRepository;
	
	@Inject
	private CredentialsService credentialsService;
	
	@Context
	private HttpServletRequest request;
	
	@Override
	public Response socialSignIn(String provider) {
		String redirectUri = socialServiceLocator.getSocialService(provider).getAuthorizeUrl();
		
		return Response.seeOther(URI.create(redirectUri)).build();
	}

	@Override
	public Response socialSignInCallback(String provider, String code) {
		SocialService<?> socialService = socialServiceLocator.getSocialService(provider);