2 * Copyright (c) 2017 Ericsson Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.shiro.realm.util.http;
11 import com.sun.jersey.api.client.Client;
12 import com.sun.jersey.api.client.ClientResponse;
13 import com.sun.jersey.api.client.UniformInterfaceException;
14 import com.sun.jersey.api.client.WebResource;
16 import java.util.HashMap;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
24 * Basic utility to do an HTTP request. See {@link
25 * SimpleHttpClient#requestBuilder(Class)} on how to obtain a request builder.
27 * @param <T> the return type of the request.
29 public class SimpleHttpRequest<T> {
30 private final Client client;
31 private final Class<T> outputType;
34 private String method;
35 private MediaType mediaType;
36 private Object entity;
37 private Map<String, String> queryParams = new HashMap<>();
39 private SimpleHttpRequest(final Client client, final Class<T> outputType) {
41 this.outputType = outputType;
45 * Executes the http request.
47 * @return the result of the http request.
50 WebResource webResource = client.resource(uri).path(path);
52 // add the query params
53 queryParams.entrySet().forEach(queryParamEntry ->
54 webResource.queryParam(queryParamEntry.getKey(), queryParamEntry.getValue()));
57 if (outputType == Response.class) {
58 ClientResponse output = webResource.type(mediaType).method(method, ClientResponse.class, entity);
59 return outputType.cast(clientResponseToResponse(output));
61 return webResource.type(mediaType).method(method, outputType, entity);
63 } catch (UniformInterfaceException theException) {
64 throw new WebApplicationException(theException, clientResponseToResponse(theException.getResponse()));
69 * Obtain a builder for SimpleHttpRequest.
71 * @param client the client used when executing the request.
72 * @param outputType the return type of the request.
73 * @param <T> the return type of the request.
75 * @return the builder.
77 static <T> Builder<T> builder(Client client, Class<T> outputType) {
78 return new Builder<>(client, outputType);
81 private static Response clientResponseToResponse(final ClientResponse clientResponse) {
82 Response.ResponseBuilder rb = Response.status(clientResponse.getStatus());
83 clientResponse.getHeaders().forEach((header, values) -> values.forEach(value -> rb.header(header, value)));
84 rb.entity(clientResponse.getEntityInputStream());
88 public static class Builder<T> {
89 private SimpleHttpRequest<T> request;
91 Builder(Client client, Class<T> outputType) {
92 request = new SimpleHttpRequest<>(client, outputType);
96 * Sets the URI the request is made to.
99 * @return self, the request builder.
101 public Builder<T> uri(URI uri) {
107 * Sets the relative path the request is made to.
109 * @param path the path.
110 * @return self, the request builder.
112 public Builder<T> path(String path) {
118 * Sets the method invoked in the request.
120 * @param method the method.
121 * @return self, the request builder.
123 public Builder<T> method(String method) {
124 request.method = method;
129 * Sets the media type of the request payload.
131 * @param mediaType the media type.
132 * @return self, the request builder.
134 public Builder<T> mediaType(MediaType mediaType) {
135 request.mediaType = mediaType;
140 * Sets the input payload of the request.
142 * @param input the input payload.
143 * @return self, the request builder.
145 public Builder<T> entity(Object input) {
146 request.entity = input;
151 * Add query parameters to the request. Can be called multiple times,
152 * to add multiple query parameters. Values are overwritten when assigned
155 * @param theQueryParam the parameter name
156 * @param theParamValue the parameter value
157 * @return self, the request builder
159 public Builder<T> queryParam(String theQueryParam, String theParamValue) {
160 request.queryParams.put(theQueryParam, theParamValue);
167 * @return the request.
169 public SimpleHttpRequest<T> build() {