Commit 7efd1f5fbbc1c587e418d1bacd83dedcf5618555

  • Tree SHA1: f753c8c
  • Parent SHA1: 3080d7b (Adjusted Greenhouse to use ConnectController and ConnectInterceptor from Spring Social. SOCIAL-78 and SOCIAL-81)
  • raw diff | raw patch
Adapted Greenhouse to use Spring Social's FacebookConnectController instead of its own.
src/main/java/com/springsource/greenhouse/connect/providers/FacebookConnectController.java
(0 / 105)
  
1/*
2 * Copyright 2010 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.springsource.greenhouse.connect.providers;
17
18import java.io.IOException;
19
20import javax.inject.Inject;
21import javax.servlet.http.HttpServletRequest;
22
23import org.springframework.security.core.Authentication;
24import org.springframework.social.connect.ServiceProvider;
25import org.springframework.social.facebook.FacebookAccessToken;
26import org.springframework.social.facebook.FacebookLink;
27import org.springframework.social.facebook.FacebookOperations;
28import org.springframework.social.facebook.FacebookUserId;
29import org.springframework.stereotype.Controller;
30import org.springframework.ui.Model;
31import org.springframework.web.bind.annotation.RequestMapping;
32import org.springframework.web.bind.annotation.RequestMethod;
33import org.springframework.web.bind.annotation.RequestParam;
34import org.springframework.web.flash.FlashMap;
35
36import com.springsource.greenhouse.account.Account;
37import com.springsource.greenhouse.members.ProfilePictureService;
38
39/**
40 * Facebook-specific UI Controller for connecting member Accounts with Facebook.
41 * @author Keith Donald
42 */
43@Controller
44public class FacebookConnectController {
45
46 private final ServiceProvider<FacebookOperations> facebookProvider;
47
48 private final ProfilePictureService profilePictureService;
49
50 @Inject
51 public FacebookConnectController(ServiceProvider<FacebookOperations> facebookProvider, ProfilePictureService profilePictureService) {
52 this.facebookProvider = facebookProvider;
53 this.profilePictureService = profilePictureService;
54 }
55
56 @RequestMapping(value="/connect/facebook", method=RequestMethod.GET)
57 public String connectView(@FacebookUserId String facebookUserId, Model model) {
58 if (facebookProvider.isConnected()) {
59 model.addAttribute("facebookUserId", facebookUserId);
60 return "connect/facebookConnected";
61 } else {
62 return "connect/facebookConnect";
63 }
64 }
65
66 @RequestMapping(value="/connect/facebook", method=RequestMethod.POST)
67 public String connectAccountToFacebook(Account account, @FacebookAccessToken String accessToken, @FacebookUserId String facebookUserId,
68 @RequestParam(required=false, defaultValue="false") boolean postToWall, @RequestParam(required=false, defaultValue="false") boolean useProfilePicture) {
69 if (facebookUserId != null && accessToken != null) {
70 facebookProvider.addConnection(accessToken, facebookUserId);
71 FacebookOperations api = facebookProvider.getServiceOperations();
72 if (postToWall) {
73 postToWall(api, account);
74 }
75 if (useProfilePicture) {
76 useFacebookProfilePicture(api, account, facebookUserId);
77 }
78 FlashMap.setSuccessMessage("Your Greenhouse account is now connected to your Facebook account!");
79 }
80 return "redirect:/connect/facebook";
81 }
82
83 @RequestMapping(value="/connect/facebook", method=RequestMethod.DELETE)
84 public String disconnectFacebook(Account account, HttpServletRequest request, Authentication authentication) {
85 facebookProvider.disconnect();
86 return "redirect:/connect/facebook";
87 }
88
89 // internal helpers
90
91 private void postToWall(FacebookOperations api, Account account) {
92 api.updateStatus("Join me at the Greenhouse!", new FacebookLink(account.getProfileUrl(), "Greenhouse", "Where Spring developers hang out.",
93 "We help you connect with fellow application developers and take advantage of everything the Spring community has to offer."));
94 }
95
96 private void useFacebookProfilePicture(FacebookOperations api, Account account, String facebookUserId) {
97 try {
98 byte[] imageBytes = api.getProfilePicture(facebookUserId);
99 profilePictureService.saveProfilePicture(account.getId(), imageBytes);
100 } catch (IOException e) {
101 FlashMap.setWarningMessage("Greenhouse was unable to use your Facebook profile picture.");
102 }
103 }
104
105}
src/main/webapp/WEB-INF/spring/appServlet/controllers.xml
(1 / 1)
  
3131 </list>
3232 </property>
3333 </bean>
34 <bean class="com.springsource.greenhouse.connect.providers.FacebookConnectController" />
34 <bean class="org.springframework.social.connect.web.FacebookConnectController" autowire="constructor" />
3535
3636 <!-- Configurable properties TODO: this is also loaded in the root-context.xml; can we load just once instead? See GREENHOUSE-308 -->
3737 <import resource="../properties.xml" />

Comments

Add a new comment:

Login or create an account to post a comment

Add your comment