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.HttpServletRequest;
26  
27  import org.apache.amber.oauth2.client.URLConnectionClient;
28  import org.apache.amber.oauth2.client.demo.Utils;
29  import org.apache.amber.oauth2.client.demo.model.OAuthParams;
30  import org.apache.amber.oauth2.client.demo.model.OAuthRegParams;
31  import org.apache.amber.oauth2.client.request.OAuthClientRequest;
32  import org.apache.amber.oauth2.common.exception.OAuthProblemException;
33  import org.apache.amber.oauth2.common.exception.OAuthSystemException;
34  import org.apache.amber.oauth2.ext.dynamicreg.client.OAuthRegistrationClient;
35  import org.apache.amber.oauth2.ext.dynamicreg.client.request.OAuthClientRegistrationRequest;
36  import org.apache.amber.oauth2.ext.dynamicreg.common.OAuthRegistration;
37  import org.apache.amber.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse;
38  import org.springframework.stereotype.Controller;
39  import org.springframework.web.bind.annotation.ModelAttribute;
40  import org.springframework.web.bind.annotation.RequestMapping;
41  import org.springframework.web.servlet.ModelAndView;
42  
43  import org.apache.amber.oauth2.client.demo.exception.ApplicationException;
44  
45  /**
46   *
47   *
48   *
49   */
50  @Controller
51  @RequestMapping("/")
52  public class RegistrationController {
53  
54      @RequestMapping(value = "/register")
55      public ModelAndView authorize(@ModelAttribute("oauthRegParams") OAuthRegParams oauthRegParams,
56                                    @ModelAttribute("oauthParams") OAuthParams oauthParams,
57                                    HttpServletRequest req) throws OAuthSystemException, IOException {
58  
59  
60          try {
61  
62              Utils.validateRegistrationParams(oauthRegParams);
63  
64              OAuthClientRequest request = null;
65              if (Utils.REG_TYPE_PULL.equals(oauthRegParams.getRegistrationType())) {
66                  request = OAuthClientRegistrationRequest
67                      .location(oauthRegParams.getRegistrationEndpoint(), OAuthRegistration.Type.PULL)
68                      .setUrl(oauthRegParams.getUrl())
69                      .buildBodyMessage();
70              } else {
71                  request = OAuthClientRegistrationRequest
72                      .location(oauthRegParams.getRegistrationEndpoint(), OAuthRegistration.Type.PUSH)
73                      .setName(oauthRegParams.getName())
74                      .setUrl(oauthRegParams.getUrl())
75                      .setDescription(oauthRegParams.getDescription())
76                      .setRedirectURL(oauthRegParams.getRedirectUri())
77                      .setIcon(oauthRegParams.getIcon())
78                      .buildBodyMessage();
79              }
80  
81  
82              OAuthRegistrationClient client = new OAuthRegistrationClient(new URLConnectionClient());
83              OAuthClientRegistrationResponse response = client.clientInfo(request);
84  
85              oauthParams.setClientId(response.getClientId());
86              oauthParams.setClientSecret(response.getClientSecret());
87              oauthParams.setAuthzEndpoint(oauthRegParams.getAuthzEndpoint());
88              oauthParams.setTokenEndpoint(oauthRegParams.getTokenEndpoint());
89              oauthParams.setApplication(oauthRegParams.getApplication());
90              oauthParams.setRedirectUri(oauthRegParams.getRedirectUri());
91  
92              return new ModelAndView("get_authz");
93  
94  
95          } catch (ApplicationException e) {
96              oauthRegParams.setErrorMessage(e.getMessage());
97              return new ModelAndView("register");
98          } catch (OAuthProblemException e) {
99              oauthRegParams.setErrorMessage(e.getMessage());
100             return new ModelAndView("register");
101         }
102     }
103 
104 }