1 /* 2 * Copyright 2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.resteasy.hypermedia.renderer; 17 18 import java.lang.annotation.Annotation; 19 import java.lang.reflect.Type; 20 21 import javax.servlet.http.HttpServletRequest; 22 import javax.servlet.http.HttpServletResponse; 23 import javax.ws.rs.core.MediaType; 24 import javax.ws.rs.core.MultivaluedMap; 25 import javax.ws.rs.core.UriInfo; 26 27 import de.smartics.resteasy.hypermedia.resources.Resources; 28 import de.smartics.util.lang.Arg; 29 30 /** 31 * Provides the context information for representation renderers. 32 */ 33 public final class ResourceContext 34 { 35 // ********************************* Fields ********************************* 36 37 // --- constants ------------------------------------------------------------ 38 39 // --- members -------------------------------------------------------------- 40 41 /** 42 * The access to the HTTP request. 43 */ 44 private final HttpServletRequest request; 45 46 /** 47 * The access to the HTTP response. 48 */ 49 private final HttpServletResponse response; 50 51 /** 52 * The helper to construct URIs. 53 */ 54 private final UriInfo uriInfo; 55 56 /** 57 * The helper to access localized information. 58 */ 59 private final LocalizationProvider l7nProvider; 60 61 /** 62 * The repository of resource link exits. 63 */ 64 private final Resources discovery; 65 66 /** 67 * The class of instance that is to be written. 68 */ 69 private final Class<?> type; 70 71 /** 72 * The type of instance to be written. {@link javax.ws.rs.core.GenericEntity} 73 * provides a way to specify this information at runtime. 74 */ 75 private final Type genericType; 76 77 /** 78 * The array of the annotations attached to the message entity instance. 79 */ 80 private final Annotation[] annotations; 81 82 /** 83 * The media type of the HTTP entity. 84 */ 85 private final MediaType mediaType; 86 87 /** 88 * The mutable map of the HTTP message headers. 89 */ 90 private final MultivaluedMap<String, Object> httpHeaders; 91 92 // ****************************** Initializer ******************************* 93 94 // ****************************** Constructors ****************************** 95 96 private ResourceContext(final Builder builder) 97 { 98 this.request = builder.request; 99 this.response = builder.response; 100 this.uriInfo = builder.uriInfo; 101 this.l7nProvider = builder.l7nProvider; 102 this.discovery = builder.discovery; 103 104 this.type = builder.type; 105 this.genericType = builder.genericType; 106 this.annotations = builder.annotations; 107 this.mediaType = builder.mediaType; 108 this.httpHeaders = builder.httpHeaders; 109 } 110 111 // ****************************** Inner Classes ***************************** 112 113 /** 114 * Builds instances of {@link ResourceContext}. 115 */ 116 public static final class Builder 117 { 118 // ******************************** Fields ******************************** 119 120 // --- constants ---------------------------------------------------------- 121 122 // --- members ------------------------------------------------------------ 123 124 /** 125 * The access to the HTTP request. 126 */ 127 private HttpServletRequest request; 128 129 /** 130 * The access to the HTTP response. 131 */ 132 private HttpServletResponse response; 133 134 /** 135 * The helper to construct URIs. 136 */ 137 private UriInfo uriInfo; 138 139 /** 140 * The helper to access localized information. 141 */ 142 private LocalizationProvider l7nProvider; 143 144 /** 145 * The repository of resource link exits. 146 */ 147 private Resources discovery; 148 149 /** 150 * The class of instance that is to be written. 151 */ 152 private Class<?> type; 153 154 /** 155 * The type of instance to be written. 156 * {@link javax.ws.rs.core.GenericEntity} provides a way to specify this 157 * information at runtime. 158 */ 159 private Type genericType; 160 161 /** 162 * The array of the annotations attached to the message entity instance. 163 */ 164 private Annotation[] annotations; 165 166 /** 167 * The media type of the HTTP entity. 168 */ 169 private MediaType mediaType; 170 171 /** 172 * The mutable map of the HTTP message headers. 173 */ 174 private MultivaluedMap<String, Object> httpHeaders; 175 176 // ***************************** Initializer ****************************** 177 178 // ***************************** Constructors ***************************** 179 180 // ***************************** Inner Classes **************************** 181 182 // ******************************** Methods ******************************* 183 184 // --- init --------------------------------------------------------------- 185 186 // --- get&set ------------------------------------------------------------ 187 188 /** 189 * Sets the access to the HTTP request. 190 * 191 * @param request the access to the HTTP request. 192 * @return a reference to this builder instance. 193 */ 194 public Builder with(final HttpServletRequest request) 195 { 196 this.request = request; 197 return this; 198 } 199 200 /** 201 * Sets the access to the HTTP response. 202 * 203 * @param response the access to the HTTP response. 204 * @return a reference to this builder instance. 205 */ 206 public Builder with(final HttpServletResponse response) 207 { 208 this.response = response; 209 return this; 210 } 211 212 /** 213 * Sets the helper to construct URIs. 214 * 215 * @param uriInfo the helper to construct URIs. 216 * @return a reference to this builder instance. 217 */ 218 public Builder with(final UriInfo uriInfo) 219 { 220 this.uriInfo = uriInfo; 221 return this; 222 } 223 224 /** 225 * Sets the helper to access localized information. 226 * 227 * @param l7nProvider the helper to access localized information. 228 * @return a reference to this builder instance. 229 */ 230 public Builder with(final LocalizationProvider l7nProvider) 231 { 232 this.l7nProvider = l7nProvider; 233 return this; 234 } 235 236 /** 237 * Sets the repository of resource link exits. 238 * 239 * @param discovery the repository of resource link exits. 240 * @return a reference to this builder instance. 241 */ 242 public Builder with(final Resources discovery) 243 { 244 this.discovery = discovery; 245 return this; 246 } 247 248 /** 249 * Sets the class of instance that is to be written. 250 * 251 * @param type the class of instance that is to be written. 252 * @return a reference to this builder instance. 253 */ 254 public Builder withType(final Class<?> type) 255 { 256 this.type = type; 257 return this; 258 } 259 260 /** 261 * Sets the type of instance to be written. 262 * {@link javax.ws.rs.core.GenericEntity}provides a way to specify this 263 * information at runtime. 264 * 265 * @param genericType the type of instance to be written. 266 * @return a reference to this builder instance. 267 */ 268 public Builder withGenericType(final Type genericType) 269 { 270 this.genericType = genericType; 271 return this; 272 } 273 274 /** 275 * Sets the array of the annotations attached to the message entity 276 * instance. 277 * 278 * @param annotations the array of the annotations attached to the message 279 * entity instance. 280 * @return a reference to this builder instance. 281 */ 282 public Builder with(final Annotation[] annotations) 283 { 284 this.annotations = annotations; 285 return this; 286 } 287 288 /** 289 * Sets the media type of the HTTP entity. 290 * 291 * @param mediaType the media type of the HTTP entity. 292 * @return a reference to this builder instance. 293 */ 294 public Builder with(final MediaType mediaType) 295 { 296 this.mediaType = mediaType; 297 return this; 298 } 299 300 /** 301 * Sets the mutable map of the HTTP message headers. 302 * 303 * @param httpHeaders the mutable map of the HTTP message headers. 304 * @return a reference to this builder instance. 305 */ 306 public Builder withHttpHeaders( 307 final MultivaluedMap<String, Object> httpHeaders) 308 { 309 this.httpHeaders = httpHeaders; 310 return this; 311 } 312 313 // --- business ----------------------------------------------------------- 314 315 /** 316 * Creates the instance. 317 * 318 * @return the instance. 319 */ 320 public ResourceContext build() 321 { 322 Arg.checkNotNull("request", request); 323 Arg.checkNotNull("response", response); 324 return new ResourceContext(this); 325 } 326 327 // --- object basics ------------------------------------------------------ 328 } 329 330 // ********************************* Methods ******************************** 331 332 // --- init ----------------------------------------------------------------- 333 334 // --- get&set -------------------------------------------------------------- 335 336 /** 337 * Returns the access to the HTTP request. 338 * 339 * @return the access to the HTTP request. 340 */ 341 public HttpServletRequest getRequest() 342 { 343 return request; 344 } 345 346 /** 347 * Returns the access to the HTTP response. 348 * 349 * @return the access to the HTTP response. 350 */ 351 public HttpServletResponse getResponse() 352 { 353 return response; 354 } 355 356 /** 357 * Returns the helper to construct URIs. 358 * 359 * @return the helper to construct URIs. 360 */ 361 public UriInfo getUriInfo() 362 { 363 return uriInfo; 364 } 365 366 /** 367 * Returns the helper to access localized information. 368 * 369 * @return the helper to access localized information. 370 */ 371 public LocalizationProvider getL7nProvider() 372 { 373 return l7nProvider; 374 } 375 376 /** 377 * Returns the repository of resource link exits. 378 * 379 * @return the repository of resource link exits. 380 */ 381 public Resources getDiscovery() 382 { 383 return discovery; 384 } 385 386 /** 387 * Returns the class of instance that is to be written. 388 * 389 * @return the class of instance that is to be written. 390 */ 391 public Class<?> getType() 392 { 393 return type; 394 } 395 396 /** 397 * Returns the type of instance to be written. 398 * {@link javax.ws.rs.core.GenericEntity}provides a way to specify this 399 * information at runtime. 400 * 401 * @return the type of instance to be written. 402 */ 403 public Type getGenericType() 404 { 405 return genericType; 406 } 407 408 /** 409 * Returns the array of the annotations attached to the message entity 410 * instance. 411 * 412 * @return the array of the annotations attached to the message entity 413 * instance. 414 */ 415 public Annotation[] getAnnotations() 416 { 417 return annotations; 418 } 419 420 /** 421 * Returns the media type of the HTTP entity. 422 * 423 * @return the media type of the HTTP entity. 424 */ 425 public MediaType getMediaType() 426 { 427 return mediaType; 428 } 429 430 /** 431 * Returns the mutable map of the HTTP message headers. 432 * 433 * @return the mutable map of the HTTP message headers. 434 */ 435 public MultivaluedMap<String, Object> getHttpHeaders() 436 { 437 return httpHeaders; 438 } 439 440 /** 441 * Checks if the current user has the given role. 442 * 443 * @param role the role to check. 444 * @return <code>true</code> if the current user is in the given role, 445 * <code>false</code> if not. 446 */ 447 public boolean isUserInRole(final String role) 448 { 449 final boolean inRole = request.isUserInRole(role); 450 return inRole; 451 } 452 453 // --- business ------------------------------------------------------------- 454 455 // --- object basics -------------------------------------------------------- 456 457 }