Debian Codegraph @

contribs/jersey-oauth/oauth-server/src/main/java/com/sun/jersey/oauth/server/OAuthServerRequest.java

raw
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12 * or packager/legal/LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package com.sun.jersey.oauth.server;
42
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import java.util.ArrayList;
46 import java.util.HashSet;
47 import java.util.List;
48 import java.util.Set;
49 import java.util.logging.Level;
50 import java.util.logging.Logger;
51 import javax.ws.rs.core.MultivaluedMap;
52 import com.sun.jersey.api.core.HttpRequestContext;
53 import com.sun.jersey.oauth.signature.OAuthRequest;
54
55 /**
56 * Wraps a Jersey {@link HttpRequestContext} object, implementing the
57 * OAuth signature library {@link OAuthRequest} interface.
58 *
59 * @author Hubert A. Le Van Gong <hubert.levangong at Sun.COM>
60 * @author Paul C. Bryan <[email protected]>
61 */
62 public class OAuthServerRequest implements OAuthRequest {
63
64 private HttpRequestContext context;
65
66 private static HashSet<String> EMPTY_SET = new HashSet<String>();
67
68 private static ArrayList<String> EMPTY_LIST = new ArrayList<String>();
69
70 public OAuthServerRequest(HttpRequestContext context) {
71 this.context = context;
72 }
73
74 @Override
75 public String getRequestMethod() {
76 return context.getMethod();
77 }
78
79 @Override
80 public URL getRequestURL() {
81 try {
82 return context.getRequestUri().toURL();
83 } catch (MalformedURLException ex) {
84 Logger.getLogger(OAuthServerRequest.class.getName()).log(Level.SEVERE, null, ex);
85 return null;
86 }
87 }
88
89 private static Set<String> keys(MultivaluedMap<String, String> mvm) {
90 if (mvm == null) {
91 return EMPTY_SET;
92 }
93 Set<String> s = mvm.keySet();
94 if (s == null) {
95 return EMPTY_SET;
96 }
97 return s;
98 }
99
100 private static List<String> values(MultivaluedMap<String, String> mvm, String key) {
101 if (mvm == null) {
102 return EMPTY_LIST;
103 }
104 List<String> v = mvm.get(key);
105 if (v == null) {
106 return EMPTY_LIST;
107 }
108 return v;
109 }
110
111 @Override
112 public Set<String> getParameterNames() {
113 HashSet<String> n = new HashSet<String>();
114 n.addAll(keys(context.getQueryParameters()));
115 n.addAll(keys(context.getFormParameters()));
116 return n;
117 }
118
119 @Override
120 public List<String> getParameterValues(String name) {
121 ArrayList<String> v = new ArrayList<String>();
122 v.addAll(values(context.getQueryParameters(), name));
123 v.addAll(values(context.getFormParameters(), name));
124 return v;
125 }
126
127 @Override
128 public List<String> getHeaderValues(String name) {
129 return context.getRequestHeader(name);
130 }
131
132 @Override
133 public void addHeaderValue(String name, String value) throws IllegalStateException {
134 throw new IllegalStateException("Modifying OAuthServerRequest unsupported");
135 }
136
137 }