| 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.io.ByteArrayOutputStream; |
| 44 |
import java.io.ByteArrayInputStream; |
| 45 |
import java.io.IOException; |
| 46 |
import java.lang.annotation.Annotation; |
| 47 |
import java.lang.reflect.Type; |
| 48 |
import java.net.URI; |
| 49 |
import javax.ws.rs.WebApplicationException; |
| 50 |
import javax.ws.rs.core.GenericEntity; |
| 51 |
import javax.ws.rs.core.MediaType; |
| 52 |
import javax.ws.rs.core.MultivaluedMap; |
| 53 |
import javax.ws.rs.ext.MessageBodyReader; |
| 54 |
import javax.ws.rs.ext.MessageBodyWriter; |
| 55 |
import javax.ws.rs.ext.Providers; |
| 56 |
import com.sun.jersey.api.client.ClientRequest; |
| 57 |
import com.sun.jersey.api.uri.UriComponent; |
| 58 |
import com.sun.jersey.core.util.MultivaluedMapImpl; |
| 59 |
|
| 60 |
/** |
| 61 |
* Utility class for processing client requests. This class somehow wants to be |
| 62 |
* more than just a utility class for this one filter. |
| 63 |
* |
| 64 |
* @author Paul C. Bryan <[email protected]> |
| 65 |
*/ |
| 66 |
class RequestUtil { |
| 67 |
|
| 68 |
private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0]; |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the media type of the request. |
| 72 |
* |
| 73 |
* @param request the client request to retrieve content type from. |
| 74 |
* @return a {@link MediaType} object representing the media type of the request. |
| 75 |
*/ |
| 76 |
public static MediaType getMediaType(ClientRequest request) { |
| 77 |
|
| 78 |
final Object header = request.getMetadata().getFirst("Content-Type"); |
| 79 |
|
| 80 |
if (header == null) { |
| 81 |
return null; |
| 82 |
} |
| 83 |
|
| 84 |
if (header instanceof MediaType) { |
| 85 |
return (MediaType)header; |
| 86 |
} |
| 87 |
|
| 88 |
return MediaType.valueOf(header.toString()); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns the query parameters of a request as a multi-valued map. |
| 93 |
* |
| 94 |
* @param request the client request to retrieve query parameters from. |
| 95 |
* @return a {@link MultivaluedMap} containing the entity query parameters. |
| 96 |
*/ |
| 97 |
public static MultivaluedMap<String, String> getQueryParameters(ClientRequest request) { |
| 98 |
|
| 99 |
URI uri = request.getURI(); |
| 100 |
|
| 101 |
if (uri == null) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
return UriComponent.decodeQuery(uri, true); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns the form parameters from a request entity as a multi-valued map. |
| 110 |
* If the request does not have a POST method, or the media type is not |
| 111 |
* x-www-form-urlencoded, then null is returned. |
| 112 |
* |
| 113 |
* @param request the client request containing the entity to extract parameters from. |
| 114 |
* @param providers the registered service providers. |
| 115 |
* @return a {@link MultivaluedMap} containing the entity form parameters. |
| 116 |
*/ |
| 117 |
public static MultivaluedMap<String, String> |
| 118 |
getEntityParameters(ClientRequest request, Providers providers) { |
| 119 |
|
| 120 |
Object entity = request.getEntity(); |
| 121 |
String method = request.getMethod(); |
| 122 |
MediaType mediaType = getMediaType(request); |
| 123 |
|
| 124 |
// no entity, not a post or not x-www-form-urlencoded: return empty map |
| 125 |
if (entity == null || method == null || !method.equalsIgnoreCase("POST") || |
| 126 |
mediaType == null || !mediaType.equals(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) { |
| 127 |
return new MultivaluedMapImpl(); |
| 128 |
} |
| 129 |
|
| 130 |
// it's ready to go if already expressed as a multi-valued map |
| 131 |
if (entity instanceof MultivaluedMap) { |
| 132 |
return (MultivaluedMap)entity; |
| 133 |
} |
| 134 |
|
| 135 |
Type entityType = entity.getClass(); |
| 136 |
|
| 137 |
// if the entity is generic, get specific type and class |
| 138 |
if (entity instanceof GenericEntity) { |
| 139 |
final GenericEntity generic = (GenericEntity)entity; |
| 140 |
entityType = generic.getType(); // overwrite |
| 141 |
entity = generic.getEntity(); |
| 142 |
} |
| 143 |
|
| 144 |
final Class entityClass = entity.getClass(); |
| 145 |
|
| 146 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 147 |
|
| 148 |
MessageBodyWriter writer = providers.getMessageBodyWriter(entityClass, |
| 149 |
entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE); |
| 150 |
|
| 151 |
try { |
| 152 |
writer.writeTo(entity, entityClass, entityType, |
| 153 |
EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, out); |
| 154 |
} |
| 155 |
catch (WebApplicationException wae) { |
| 156 |
throw new IllegalStateException(wae); |
| 157 |
} |
| 158 |
catch (IOException ioe) { |
| 159 |
throw new IllegalStateException(ioe); |
| 160 |
} |
| 161 |
|
| 162 |
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); |
| 163 |
|
| 164 |
MessageBodyReader reader = providers.getMessageBodyReader(MultivaluedMap.class, |
| 165 |
MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE); |
| 166 |
|
| 167 |
try { |
| 168 |
return (MultivaluedMap<String, String>)reader.readFrom(MultivaluedMap.class, |
| 169 |
MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, in); |
| 170 |
} |
| 171 |
catch (IOException ioe) { |
| 172 |
throw new IllegalStateException(ioe); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|