Fix packaging for shiro bundle
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / realm / util / http / SimpleHttpRequest.java
1 /*
2  * Copyright (c) 2017 Ericsson Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.aaa.shiro.realm.util.http;
10
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;
15 import java.net.URI;
16 import java.util.HashMap;
17 import java.util.Map;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
21
22
23 /**
24  * Basic utility to do an HTTP request. See {@link
25  * SimpleHttpClient#requestBuilder(Class)} on how to obtain a request builder.
26  *
27  * @param <T> the return type of the request.
28  */
29 public class SimpleHttpRequest<T> {
30     private final Client client;
31     private final Class<T> outputType;
32     private URI uri;
33     private String path;
34     private String method;
35     private MediaType mediaType;
36     private Object entity;
37     private Map<String, String> queryParams = new HashMap<>();
38
39     private SimpleHttpRequest(final Client client, final Class<T> outputType) {
40         this.client = client;
41         this.outputType = outputType;
42     }
43
44     /**
45      * Executes the http request.
46      *
47      * @return the result of the http request.
48      */
49     public T execute() {
50         WebResource webResource = client.resource(uri).path(path);
51
52         // add the query params
53         queryParams.entrySet().forEach(queryParamEntry ->
54                 webResource.queryParam(queryParamEntry.getKey(), queryParamEntry.getValue()));
55
56         try {
57             if (outputType == Response.class) {
58                 ClientResponse output = webResource.type(mediaType).method(method, ClientResponse.class, entity);
59                 return outputType.cast(clientResponseToResponse(output));
60             } else {
61                 return webResource.type(mediaType).method(method, outputType, entity);
62             }
63         } catch (UniformInterfaceException theException) {
64             throw new WebApplicationException(theException, clientResponseToResponse(theException.getResponse()));
65         }
66     }
67
68     /**
69      * Obtain a builder for SimpleHttpRequest.
70      *
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.
74      *
75      * @return the builder.
76      */
77     static <T> Builder<T> builder(Client client, Class<T> outputType) {
78         return new Builder<>(client, outputType);
79     }
80
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());
85         return rb.build();
86     }
87
88     public static class Builder<T> {
89         private SimpleHttpRequest<T> request;
90
91         Builder(Client client, Class<T> outputType) {
92             request = new SimpleHttpRequest<>(client, outputType);
93         }
94
95         /**
96          * Sets the URI the request is made to.
97          *
98          * @param uri the URI.
99          * @return self, the request builder.
100          */
101         public Builder<T> uri(URI uri) {
102             request.uri = uri;
103             return this;
104         }
105
106         /**
107          * Sets the relative path the request is made to.
108          *
109          * @param path the path.
110          * @return self, the request builder.
111          */
112         public Builder<T> path(String path) {
113             request.path = path;
114             return this;
115         }
116
117         /**
118          * Sets the method invoked in the request.
119          *
120          * @param method the method.
121          * @return self, the request builder.
122          */
123         public Builder<T> method(String method) {
124             request.method = method;
125             return this;
126         }
127
128         /**
129          * Sets the media type of the request payload.
130          *
131          * @param mediaType the media type.
132          * @return self, the request builder.
133          */
134         public Builder<T> mediaType(MediaType mediaType) {
135             request.mediaType = mediaType;
136             return this;
137         }
138
139         /**
140          * Sets the input payload of the request.
141          *
142          * @param input the input payload.
143          * @return self, the request builder.
144          */
145         public Builder<T> entity(Object input) {
146             request.entity = input;
147             return this;
148         }
149
150         /**
151          * Add query parameters to the request. Can be called multiple times,
152          * to add multiple query parameters. Values are overwritten when assigned
153          * the same keys.
154          *
155          * @param theQueryParam the parameter name
156          * @param theParamValue the parameter value
157          * @return  self, the request builder
158          */
159         public Builder<T> queryParam(String theQueryParam, String theParamValue) {
160             request.queryParams.put(theQueryParam, theParamValue);
161             return this;
162         }
163
164         /**
165          * Build the request.
166          *
167          * @return the request.
168          */
169         public SimpleHttpRequest<T> build() {
170             return request;
171         }
172     }
173
174 }