| 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.client; |
| 42 |
|
| 43 |
import java.net.MalformedURLException; |
| 44 |
import java.net.URI; |
| 45 |
import java.util.ArrayList; |
| 46 |
import java.util.List; |
| 47 |
import java.util.Set; |
| 48 |
import java.util.logging.Level; |
| 49 |
import java.util.logging.Logger; |
| 50 |
import javax.ws.rs.core.MultivaluedMap; |
| 51 |
import javax.ws.rs.ext.Providers; |
| 52 |
import com.sun.jersey.api.client.ClientRequest; |
| 53 |
import com.sun.jersey.core.util.MultivaluedMapImpl; |
| 54 |
import com.sun.jersey.oauth.signature.OAuthRequest; |
| 55 |
import java.net.URL; |
| 56 |
|
| 57 |
/** |
| 58 |
* Implements the OAuth signature library Request interface, wrapping a Jersey |
| 59 |
* client request object. |
| 60 |
* |
| 61 |
* @author Paul C. Bryan <[email protected]> |
| 62 |
*/ |
| 63 |
class RequestWrapper implements OAuthRequest { |
| 64 |
|
| 65 |
/** The wrapped Jersey client request. */ |
| 66 |
private final ClientRequest clientRequest; |
| 67 |
|
| 68 |
/** The registered providers. */ |
| 69 |
private final Providers providers; |
| 70 |
|
| 71 |
/** Form and query parameters from the request (lazily initialized). */ |
| 72 |
private MultivaluedMap<String, String> parameters = null; |
| 73 |
|
| 74 |
private void setParameters() { |
| 75 |
parameters = new MultivaluedMapImpl(); |
| 76 |
parameters.putAll(RequestUtil.getQueryParameters(clientRequest)); |
| 77 |
parameters.putAll(RequestUtil.getEntityParameters(clientRequest, providers)); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Constructs a new OAuth client request wrapper around the specified |
| 82 |
* Jersey client request object. |
| 83 |
* |
| 84 |
* @param request the Jersey client request object to be wrapped. |
| 85 |
*/ |
| 86 |
public RequestWrapper(final ClientRequest clientRequest, final Providers providers) { |
| 87 |
this.clientRequest = clientRequest; |
| 88 |
this.providers = providers; |
| 89 |
setParameters(); // stored because parsing query/entity parameters too much work for each value-get |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public String getRequestMethod() { |
| 94 |
return clientRequest.getMethod(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public URL getRequestURL() { |
| 99 |
try { |
| 100 |
final URI uri = clientRequest.getURI(); |
| 101 |
return uri.toURL(); |
| 102 |
} catch (MalformedURLException ex) { |
| 103 |
Logger.getLogger(RequestWrapper.class.getName()).log(Level.SEVERE, null, ex); |
| 104 |
return null; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public Set<String> getParameterNames() { |
| 110 |
return parameters.keySet(); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public List<String> getParameterValues(final String name) { |
| 115 |
return parameters.get(name); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public List<String> getHeaderValues(final String name) { |
| 120 |
|
| 121 |
ArrayList<String> list = new ArrayList(); |
| 122 |
|
| 123 |
for (Object header : clientRequest.getHeaders().get(name)) { |
| 124 |
list.add(ClientRequest.getHeaderValue(header)); |
| 125 |
} |
| 126 |
|
| 127 |
return list; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void addHeaderValue(final String name, final String value) { |
| 132 |
clientRequest.getHeaders().add(name, value); |
| 133 |
} |
| 134 |
} |
| 135 |
|