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 javax.servlet.http.HttpServletRequest;
25  import org.apache.amber.oauth2.client.OAuthClient;
26  import org.apache.amber.oauth2.client.URLConnectionClient;
27  import org.apache.amber.oauth2.client.demo.Utils;
28  import org.apache.amber.oauth2.client.demo.model.OAuthParams;
29  import org.apache.amber.oauth2.client.request.OAuthBearerClientRequest;
30  import org.apache.amber.oauth2.client.request.OAuthClientRequest;
31  import org.apache.amber.oauth2.client.response.OAuthResourceResponse;
32  import org.apache.amber.oauth2.common.exception.OAuthProblemException;
33  import org.apache.amber.oauth2.common.exception.OAuthSystemException;
34  import org.springframework.stereotype.Controller;
35  import org.springframework.web.bind.annotation.ModelAttribute;
36  import org.springframework.web.bind.annotation.RequestMapping;
37  import org.springframework.web.servlet.ModelAndView;
38  
39  /**
40   *
41   *
42   *
43   */
44  @Controller
45  @RequestMapping("/get_resource")
46  public class ResourceController {
47  
48      @RequestMapping
49      public ModelAndView authorize(@ModelAttribute("oauthParams") OAuthParams oauthParams,
50                                    HttpServletRequest req) {
51  
52          try {
53  			OAuthClientRequest request=null; 
54  			
55  			if (Utils.REQUEST_TYPE_QUERY.equals(oauthParams.getRequestType())){
56  				request= new OAuthBearerClientRequest(oauthParams.getResourceUrl()).setAccessToken(oauthParams.getAccessToken()).buildQueryMessage();
57  			}else if (Utils.REQUEST_TYPE_HEADER.equals(oauthParams.getRequestType())){
58  				request= new OAuthBearerClientRequest(oauthParams.getResourceUrl()).setAccessToken(oauthParams.getAccessToken()).buildHeaderMessage();
59  			}else if (Utils.REQUEST_TYPE_BODY.equals(oauthParams.getRequestType())){
60  				request= new OAuthBearerClientRequest(oauthParams.getResourceUrl()).setAccessToken(oauthParams.getAccessToken()).buildBodyMessage();
61  			}			
62  			
63  			OAuthClient client = new OAuthClient(new URLConnectionClient());
64  			OAuthResourceResponse resourceResponse= client.resource(request, oauthParams.getRequestMethod(), OAuthResourceResponse.class);
65  			
66  			if (resourceResponse.getResponseCode()==200){			
67  				oauthParams.setResource(resourceResponse.getBody());
68  			}else{
69  				oauthParams.setErrorMessage(
70  	                    "Could not access resource: " + resourceResponse.getResponseCode() + " " + resourceResponse.getBody());
71  			}
72  		} catch (OAuthSystemException e) {
73  			 oauthParams.setErrorMessage(e.getMessage());
74  		} catch (OAuthProblemException e) {
75  			 oauthParams.setErrorMessage(e.getMessage());
76  		}
77    
78          return new ModelAndView("resource");
79  
80  
81      }
82  }