View Javadoc

1   /**
2    *       Copyright 2010 Newcastle University
3    *
4    *          http://research.ncl.ac.uk/smart/
5    *
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package org.apache.amber.oauth2.client.demo.controller;
23  
24  import java.io.IOException;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.amber.oauth2.client.demo.Utils;
28  import org.apache.amber.oauth2.client.demo.model.OAuthRegParams;
29  import org.apache.amber.oauth2.common.exception.OAuthSystemException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.apache.amber.oauth2.client.demo.model.OAuthParams;
33  import org.springframework.stereotype.Controller;
34  import org.springframework.web.bind.annotation.ModelAttribute;
35  import org.springframework.web.bind.annotation.PathVariable;
36  import org.springframework.web.bind.annotation.RequestMapping;
37  import org.springframework.web.servlet.ModelAndView;
38  
39  
40  /**
41   *
42   *
43   *
44   */
45  @Controller
46  @RequestMapping("/")
47  public class MainController {
48  
49      private Logger logger = LoggerFactory.getLogger(MainController.class);
50  
51      @RequestMapping("/index")
52      public ModelAndView authorize(@ModelAttribute("oauthParams") OAuthParams oauthParams)
53          throws OAuthSystemException, IOException {
54          return new ModelAndView("index");
55      }
56  
57      @RequestMapping("/main/{app}")
58      public ModelAndView authorize(@ModelAttribute("oauthParams") OAuthParams oauthParams,
59                                    @ModelAttribute("oauthRegParams") OAuthRegParams oauthRegParams,
60                                    @PathVariable("app") String app,
61                                    HttpServletResponse res)
62          throws OAuthSystemException, IOException {
63  
64          boolean selected = false;
65          if (Utils.GENERIC.equals(app)) {
66              selected = true;
67          }else if (Utils.GITHUB.equals(app)) {
68              selected = true;
69              oauthParams.setAuthzEndpoint(Utils.GITHUB_AUTHZ);
70              oauthParams.setTokenEndpoint(Utils.GITHUB_TOKEN);
71  
72          } else if (Utils.FACEBOOK.equals(app)) {
73              selected = true;
74              oauthParams.setAuthzEndpoint(Utils.FACEBOOK_AUTHZ);
75              oauthParams.setTokenEndpoint(Utils.FACEBOOK_TOKEN);
76          }else if (Utils.GOOGLE.equals(app)) {
77                  selected = true;
78                  oauthParams.setAuthzEndpoint(Utils.GOOGLE_AUTHZ);
79                  oauthParams.setTokenEndpoint(Utils.GOOGLE_TOKEN);
80          } else if (Utils.GOWALLA.equals(app)) {
81              selected = true;
82              oauthParams.setAuthzEndpoint(Utils.GOWALLA_AUTHZ);
83              oauthParams.setTokenEndpoint(Utils.GOWALLA_TOKEN);
84          } else if (Utils.SMART_GALLERY.equals(app)) {
85              selected = true;
86              oauthRegParams.setAuthzEndpoint(Utils.SMART_GALLERY_AUTHZ);
87              oauthRegParams.setTokenEndpoint(Utils.SMART_GALLERY_TOKEN);
88              oauthRegParams.setRegistrationEndpoint(Utils.SMART_GALLERY_REGISTER);
89              oauthRegParams.setApplication(app);
90              oauthRegParams.setRedirectUri(Utils.REDIRECT_URI);
91              return new ModelAndView("register");
92          }
93          if (selected) {
94              oauthParams.setApplication(app);
95              oauthParams.setRedirectUri(Utils.REDIRECT_URI);
96              return new ModelAndView("get_authz");
97          }
98  
99          return new ModelAndView("index");
100     }
101 }