/
RestUtil2.java
143 lines (123 loc) · 6.31 KB
/
RestUtil2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.admingui.common.util;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import jakarta.ws.rs.core.Cookie;
import jakarta.ws.rs.core.MediaType;
import jakarta.faces.context.FacesContext;
import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Response;
/**
* This class is intended to replace, eventually, RestUtil. Whether or not that happens depends in large part on how the
* new console is built. In the meantime, though, there is a need for REST-related utilities that behave in a manner not
* supported by the current methods in RestUtil, so these need to be written. Rather than place these new methods beside
* the existing ones, we will, for now, separate the two to maintain ease in identifying old vs new methods, as well as
* helping to ensure that these methods remain purely REST-focused, free from the encroachment of view-related libraries
* (see the pervasiveness of JSFTemplating types in RestUtil).
*
* @author jdlee
*/
public class RestUtil2 {
@Handler(id = "rest.list",
input = {
@HandlerInput(name="endpoint", type=String.class, required=true),
@HandlerInput(name="attrs", type=Map.class, required=false)},
output = {
@HandlerOutput(name="result", type=List.class)})
public static void sendRequestToCollectionResource(HandlerContext handlerCtx) {
//Map<String, Object> attrs = (Map<String, Object>) handlerCtx.getInputValue("attrs");
String endpoint = fixEndpoint((String) handlerCtx.getInputValue("endpoint"));
Response resp = RestUtil.getJerseyClient().target(endpoint)
.request(RestUtil.RESPONSE_TYPE)
.cookie(new Cookie(RestUtil.REST_TOKEN_COOKIE, RestUtil.getRestToken()))
.get(Response.class);
if (!isSuccess(resp.getStatus())) {
throw new RuntimeException(resp.readEntity(String.class));
}
List list = resp.readEntity(List.class);
handlerCtx.setOutputValue("result", list);
}
@Handler(id = "rest.get",
input = {
@HandlerInput(name="endpoint", type=String.class, required=true),
@HandlerInput(name="attrs", type=Map.class, required=false)},
output = {
@HandlerOutput(name="result", type=Map.class)})
public static void sendGetRequestToItemResource(HandlerContext handlerCtx) {
Map<String, Object> attrs = (Map<String, Object>) handlerCtx.getInputValue("attrs");
String endpoint = fixEndpoint((String) handlerCtx.getInputValue("endpoint"));
Response resp = RestUtil.targetWithQueryParams(RestUtil.getJerseyClient().target(endpoint),
RestUtil.buildMultivalueMap(attrs))
.request(RestUtil.RESPONSE_TYPE)
.cookie(new Cookie(RestUtil.REST_TOKEN_COOKIE, RestUtil.getRestToken()))
.get(Response.class);
// Response resp = makeRequest(endpoint, attrs).get(Response.class);
Map map = resp.readEntity(Map.class);
handlerCtx.setOutputValue("result", map);
}
@Handler(id = "rest.post",
input = {
@HandlerInput(name="endpoint", type=String.class, required=true),
@HandlerInput(name="attrs", type=Map.class, required=false)},
output = {
@HandlerOutput(name="result", type=String.class)})
public static void sendPostRequest(HandlerContext handlerCtx) {
Map<String, Object> attrs = (Map<String, Object>) handlerCtx.getInputValue("attrs");
String endpoint = fixEndpoint((String) handlerCtx.getInputValue("endpoint"));
// Response resp = makeRequest(endpoint, null).post(Response.class, attrs);
Response resp = RestUtil.getJerseyClient().target(endpoint)
.request(RestUtil.RESPONSE_TYPE)
.cookie(new Cookie(RestUtil.REST_TOKEN_COOKIE, RestUtil.getRestToken()))
.post(Entity.entity(attrs, MediaType.APPLICATION_JSON_TYPE), Response.class);
if (!isSuccess(resp.getStatus())) {
GuiUtil.getLogger().log(
Level.SEVERE,
GuiUtil.getCommonMessage("LOG_UPDATE_ENTITY_FAILED", new Object[]{endpoint, attrs}));
GuiUtil.handleError(handlerCtx, GuiUtil.getMessage("msg.error.checkLog"));
return;
}
handlerCtx.setOutputValue("result", endpoint);
}
private static String fixEndpoint(String endpoint) {
if (endpoint.startsWith("/")) {
endpoint = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("REST_URL") + endpoint;
}
return endpoint;
}
// private static Builder makeRequest(String endpoint, Map<String, Object> queryParams) {
// Target resource = RestUtil.getJerseyClient().target(endpoint);
// if (queryParams != null) {
// resource = resource.queryParams(RestUtil.buildMultivalueMap(queryParams));
// }
// return resource
// .type(MediaType.APPLICATION_JSON)
// .cookie(new Cookie(RestUtil.REST_TOKEN_COOKIE, RestUtil.getRestToken()))
// .accept(RestUtil.RESPONSE_TYPE);
// }
protected static boolean isSuccess(int status) {
return (status >= 200) && (status <= 299);
}
}