001/* 002 * Copyright 2011-2016 UnboundID Corp. 003 * 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of the GNU General Public License (GPLv2 only) 006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 007 * as published by the Free Software Foundation. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, see <http://www.gnu.org/licenses>. 016 */ 017 018package org.apache.wink.client; 019 020import javax.ws.rs.client.Client; 021import javax.ws.rs.client.Entity; 022import javax.ws.rs.client.Invocation; 023import javax.ws.rs.client.WebTarget; 024import javax.ws.rs.core.Cookie; 025import javax.ws.rs.core.MediaType; 026import javax.ws.rs.core.MultivaluedHashMap; 027import javax.ws.rs.core.MultivaluedMap; 028import javax.ws.rs.core.Response; 029import java.net.URI; 030 031/** 032 * Wink compatibility layer class - see Wink docs. 033 */ 034public class Resource 035{ 036 private WebTarget webTarget; 037 private MediaType contentType; 038 private MediaType acceptType; 039 private MultivaluedMap<String, Object> headers; 040 private Cookie cookie; 041 042 /** 043 * Wink compatibility layer class - see Wink docs. 044 * @param restClient Wink compatibility layer class - see Wink docs. 045 * @param uri Wink compatibility layer class - see Wink docs. 046 */ 047 Resource(final RestClient restClient, final URI uri) 048 { 049 Client client = restClient.getClient(); 050 webTarget = client.target(uri); 051 headers = new MultivaluedHashMap<String, Object>(); 052 contentType = MediaType.APPLICATION_JSON_TYPE; 053 } 054 055 /** 056 * Wink compatibility layer class - see Wink docs. 057 * @param acceptType Wink compatibility layer class - see Wink docs. 058 * @return Wink compatibility layer class - see Wink docs. 059 */ 060 public Resource accept(final MediaType acceptType) 061 { 062 this.acceptType = acceptType; 063 return this; 064 } 065 066 /** 067 * Wink compatibility layer class - see Wink docs. 068 * @param acceptTypeString Wink compatibility layer class - see Wink docs. 069 * @return Wink compatibility layer class - see Wink docs. 070 */ 071 public Resource accept(final String acceptTypeString) 072 { 073 this.acceptType = MediaType.valueOf(acceptTypeString); 074 return this; 075 } 076 077 /** 078 * Wink compatibility layer class - see Wink docs. 079 * @param contentType Wink compatibility layer class - see Wink docs. 080 * @return Wink compatibility layer class - see Wink docs. 081 */ 082 public Resource contentType(final MediaType contentType) 083 { 084 this.contentType = contentType; 085 return this; 086 } 087 088 /** 089 * Wink compatibility layer class - see Wink docs. 090 * @param cookie Wink compatibility layer class - see Wink docs. 091 * @return Wink compatibility layer class - see Wink docs. 092 */ 093 public Resource cookie(final String cookie) 094 { 095 this.cookie = Cookie.valueOf(cookie); 096 return this; 097 } 098 099 /** 100 * Wink compatibility layer class - see Wink docs. 101 * @param cookie Wink compatibility layer class - see Wink docs. 102 * @return Wink compatibility layer class - see Wink docs. 103 */ 104 public Resource cookie(final Cookie cookie) 105 { 106 this.cookie = cookie; 107 return this; 108 } 109 110 /** 111 * Wink compatibility layer class - see Wink docs. 112 * @param contentTypeString Wink compatibility layer class - see Wink docs. 113 * @return Wink compatibility layer class - see Wink docs. 114 */ 115 public Resource contentType(final String contentTypeString) 116 { 117 this.contentType = MediaType.valueOf(contentTypeString); 118 return this; 119 } 120 121 /** 122 * Wink compatibility layer class - see Wink docs. 123 * @param name Wink compatibility layer class - see Wink docs. 124 * @param values Wink compatibility layer class - see Wink docs. 125 * @return Wink compatibility layer class - see Wink docs. 126 */ 127 public Resource header(final String name, final String ... values) 128 { 129 this.headers.addAll(name, values); 130 return this; 131 } 132 133 /** 134 * Wink compatibility layer class - see Wink docs. 135 * @param name Wink compatibility layer class - see Wink docs. 136 * @param values Wink compatibility layer class - see Wink docs. 137 * @return Wink compatibility layer class - see Wink docs. 138 */ 139 public Resource queryParam(final String name, final Object ... values) 140 { 141 webTarget = webTarget.queryParam(name, values); 142 return this; 143 } 144 145 /** 146 * Wink compatibility layer class - see Wink docs. 147 * @return Wink compatibility layer class - see Wink docs. 148 */ 149 public ClientResponse options() 150 { 151 Invocation.Builder builder = webTarget.request(); 152 builder.headers(headers); 153 if(acceptType != null) 154 { 155 builder.accept(acceptType); 156 } 157 if(cookie != null) 158 { 159 builder.cookie(cookie); 160 } 161 Response response = builder.options(); 162 163 ClientResponse clientResponse = new ClientResponse(response); 164 return clientResponse; 165 } 166 167 /** 168 * Wink compatibility layer class - see Wink docs. 169 * @return Wink compatibility layer class - see Wink docs. 170 */ 171 public ClientResponse get() 172 { 173 Invocation.Builder builder = webTarget.request(); 174 builder.headers(headers); 175 if(acceptType != null) 176 { 177 builder.accept(acceptType); 178 } 179 if(cookie != null) 180 { 181 builder.cookie(cookie); 182 } 183 Response response = builder.get(); 184 185 ClientResponse clientResponse = new ClientResponse(response); 186 return clientResponse; 187 } 188 189 /** 190 * Wink compatibility layer class - see Wink docs. 191 * @param entity Wink compatibility layer class - see Wink docs. 192 * @return Wink compatibility layer class - see Wink docs. 193 * @throws ClientWebException Wink compatibility layer class - see Wink docs. 194 */ 195 public ClientResponse post(final Object entity) throws ClientWebException 196 { 197 Invocation.Builder builder = webTarget.request(); 198 builder.headers(headers); 199 if(acceptType != null) 200 { 201 builder.accept(acceptType); 202 } 203 if(cookie != null) 204 { 205 builder.cookie(cookie); 206 } 207 Response response = builder.post(Entity.entity(entity, contentType)); 208 209 ClientResponse clientResponse = new ClientResponse(response); 210 return clientResponse; 211 } 212 213 /** 214 * Wink compatibility layer class - see Wink docs. 215 * @param entity Wink compatibility layer class - see Wink docs. 216 * @param <T> Wink compatibility layer class - see Wink docs. 217 * @return Wink compatibility layer class - see Wink docs. 218 */ 219 public <T> ClientResponse put(final T entity) 220 { 221 Invocation.Builder builder = webTarget.request(); 222 builder.headers(headers); 223 224 if(acceptType != null) 225 { 226 builder.accept(acceptType); 227 } 228 if(cookie != null) 229 { 230 builder.cookie(cookie); 231 } 232 233 Response response = builder.put(Entity.entity(entity, contentType)); 234 235 ClientResponse clientResponse = new ClientResponse(response); 236 return clientResponse; 237 } 238 239 /** 240 * Wink compatibility layer class - see Wink docs. 241 * @return Wink compatibility layer class - see Wink docs. 242 */ 243 public ClientResponse delete() 244 { 245 Invocation.Builder builder = webTarget.request(); 246 builder.headers(headers); 247 if(acceptType != null) 248 { 249 builder.accept(acceptType); 250 } 251 if(cookie != null) 252 { 253 builder.cookie(cookie); 254 } 255 256 Response response = builder.delete(); 257 258 ClientResponse clientResponse = new ClientResponse(response); 259 return clientResponse; 260 } 261 262 /** 263 * Wink compatibility layer class - see Wink docs. 264 * @param operation Wink compatibility layer class - see Wink docs. 265 * @param responseClass Wink compatibility layer class - see Wink docs. 266 * @param entity Wink compatibility layer class - see Wink docs. 267 * @param <T> Wink compatibility layer class - see Wink docs. 268 * @return Wink compatibility layer class - see Wink docs. 269 * @throws ClientWebException Wink compatibility layer class - see Wink docs. 270 */ 271 public <T> ClientResponse invoke(final String operation, 272 final Class<ClientResponse> responseClass, final T entity) 273 throws ClientWebException 274 { 275 Invocation.Builder builder = webTarget.request(); 276 builder.headers(headers); 277 if(acceptType != null) 278 { 279 builder.accept(acceptType); 280 } 281 if(cookie != null) 282 { 283 builder.cookie(cookie); 284 } 285 286 Response response = builder.method(operation, 287 Entity.entity(entity, contentType)); 288 ClientResponse clientResponse = new ClientResponse(response); 289 return clientResponse; 290 } 291}