Commit 7efd1f5fbbc1c587e418d1bacd83dedcf5618555
- Diff rendering mode:
- inline
- side by side
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 | */ | ||
| 16 | package com.springsource.greenhouse.connect.providers; | ||
| 17 | |||
| 18 | import java.io.IOException; | ||
| 19 | |||
| 20 | import javax.inject.Inject; | ||
| 21 | import javax.servlet.http.HttpServletRequest; | ||
| 22 | |||
| 23 | import org.springframework.security.core.Authentication; | ||
| 24 | import org.springframework.social.connect.ServiceProvider; | ||
| 25 | import org.springframework.social.facebook.FacebookAccessToken; | ||
| 26 | import org.springframework.social.facebook.FacebookLink; | ||
| 27 | import org.springframework.social.facebook.FacebookOperations; | ||
| 28 | import org.springframework.social.facebook.FacebookUserId; | ||
| 29 | import org.springframework.stereotype.Controller; | ||
| 30 | import org.springframework.ui.Model; | ||
| 31 | import org.springframework.web.bind.annotation.RequestMapping; | ||
| 32 | import org.springframework.web.bind.annotation.RequestMethod; | ||
| 33 | import org.springframework.web.bind.annotation.RequestParam; | ||
| 34 | import org.springframework.web.flash.FlashMap; | ||
| 35 | |||
| 36 | import com.springsource.greenhouse.account.Account; | ||
| 37 | import 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 | ||
| 44 | public 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)
|   | |||
| 31 | 31 | </list> | |
| 32 | 32 | </property> | |
| 33 | 33 | </bean> | |
| 34 | <bean class="com.springsource.greenhouse.connect.providers.FacebookConnectController" /> | ||
| 34 | <bean class="org.springframework.social.connect.web.FacebookConnectController" autowire="constructor" /> | ||
| 35 | 35 | ||
| 36 | 36 | <!-- Configurable properties TODO: this is also loaded in the root-context.xml; can we load just once instead? See GREENHOUSE-308 --> | |
| 37 | 37 | <import resource="../properties.xml" /> |
Comments
Add your comment
Please log in to comment



Add a new comment:
Login or create an account to post a comment